Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MLIR][XLA] Remove redundant LHLO CopyOp #36335

Merged

Conversation

dfki-ehna
Copy link
Contributor

The redundant LHLO CopyOp that copies an allocated buffer to a block argument is removed. The uses of the allocated buffer are replaced with the block argument. The associated Alloc and Dealloc are removed.

@tensorflow-bot tensorflow-bot bot added the size:M CL Change Size: Medium label Jan 30, 2020
@rthadur rthadur self-assigned this Jan 30, 2020
@rthadur rthadur added this to Assigned Reviewer in PR Queue via automation Jan 30, 2020
@rthadur
Copy link
Contributor

rthadur commented Jan 30, 2020

@dfki-ehna can you please include test cases around the new changes ?

@rthadur rthadur added the comp:xla XLA label Jan 30, 2020
@rthadur rthadur requested review from thomasjoerg and sanjoy and removed request for sherhut and pifon2a January 30, 2020 14:08
pifon2a
pifon2a previously approved these changes Jan 31, 2020
Copy link
Contributor

@pifon2a pifon2a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

PR Queue automation moved this from Assigned Reviewer to Approved by Reviewer Jan 31, 2020
@tensorflow-bot tensorflow-bot bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Jan 31, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Jan 31, 2020
@rthadur
Copy link
Contributor

rthadur commented Feb 3, 2020

@pifon2a can you please merge this change internally.

PR Queue automation moved this from Approved by Reviewer to Reviewer Requested Changes Feb 11, 2020
@tensorflow-bot tensorflow-bot bot removed the ready to pull PR ready for merge process label Feb 11, 2020
pifon2a
pifon2a previously approved these changes Feb 11, 2020
PR Queue automation moved this from Reviewer Requested Changes to Approved by Reviewer Feb 11, 2020
@tensorflow-bot tensorflow-bot bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Feb 11, 2020
PR Queue automation moved this from Approved by Reviewer to Reviewer Requested Changes Feb 11, 2020
@tensorflow-bot tensorflow-bot bot removed the ready to pull PR ready for merge process label Feb 11, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Feb 11, 2020
@pifon2a pifon2a added the ready to pull PR ready for merge process label Feb 11, 2020
tensorflow-copybara pushed a commit that referenced this pull request Feb 11, 2020
PiperOrigin-RevId: 294433205
Change-Id: I32e9bb27f9cdd49158938f05d4e676506fc30dac
@tensorflow-copybara tensorflow-copybara merged commit aa2d06d into tensorflow:master Feb 11, 2020
PR Queue automation moved this from Reviewer Requested Changes to Merged Feb 11, 2020
}
auto allocOp = operand.getDefiningOp();
if (auto deallocOp =
mlir::dyn_cast<mlir::DeallocOp>(*allocOp->getUsers().begin())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(transmitting @joker-eph): You are dereferencing the user of the allocOp while you already deleted the copyOp, which mean there might be no user left.

Here is a minimal reproducer that will show it:

func @Fusion(%arg0: memref<2x2xf32>) -> memref<2x2xf32> {
%0 = alloc() {temp = true} : memref<2x2xf32>
"xla_lhlo.copy"(%0, %arg0) : (memref<2x2xf32>, memref<2x2xf32>) -> ()
return %0 : memref<2x2xf32>
}

This comes back to my other point about testing: with dedicated testing for this pass, I would have expected this IR to be the first basic test to validate the pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the hint. You are right that this code snippet would break our current version. Although this code could exist from a theoretical point of view as an input program, the current implementation of the HLO-to-LHLO-Legalization pass ensures that there will always be a dealloc. Furthermore, it is even worse: currently, the Std.Return-to-LHLO converter expects a final dealloc operation to place a proper CopyOp. The following code snippet is taken from the hlo_legalize_to_lhlo.cc file (starting in line 425):

if (dealloc == nullptr) {
    returnOp.emitOpError()
        << "Missing dealloc for operand " << operand.index();
    return matchFailure();
}

This code snippet also fails in the following tiny test case:

func @TestFunc(%arg0: tensor<2xf32>) -> tensor<2xf32> {
   return %arg0 : tensor<2xf32>
}

@dfki-ehna
Copy link
Contributor Author

We have addressed all the comments in the new PR.

@joker-eph
Copy link
Contributor

(GitHub does not let me answer inline apparently)

Although this code could exist from a theoretical point of view as an input program, the current implementation of the HLO-to-LHLO-Legalization pass ensures that there will always be a dealloc.

I think this hits a principle of development for MLIR passes: the only invariant you can assume on your input is that the verifier is passing. It isn't allowed to crash on random vali input, we should be able to fuzz your pass safely (you can gracefully signal a pass failure and return though)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes comp:xla XLA ready to pull PR ready for merge process size:M CL Change Size: Medium
Projects
PR Queue
  
Merged
Development

Successfully merging this pull request may close these issues.

None yet

7 participants