This is an archive of the discontinued LLVM Phabricator instance.

[mlir][bufferize] Add bufferization::WaitForBufferizationOp
AbandonedPublic

Authored by springerm on Dec 31 2021, 7:38 AM.

Details

Summary

This op canonicalizes away when the tensor operand has been bufferized.

This op is needed for partial bufferization of ops such as linalg.tiled_loop. Such ops can yield tensor values but not memref values. After bufferizing the loop (including the terminator) but not the loop body, ops in the loop body can DCE away because they no longer have any uses.

Example before bufferization (simplified):

linalg.tiled_loop (%i) = (%c0) to (%c24) step (%c4)
                  ins(%t0 : tensor<?xf32>)
		  outs(%t1 : tensor<?xf32>) {
  ...
  %0 = tensor.insert %f into %t0[...] : tensor<?xf32>
  linalg.yield %0
}

After bufferization of linalg.tiled_loop:

linalg.tiled_loop (%i) = (%c0) to (%c24) step (%c4)
                  ins(%m0 : memref<?xf32>)
		  outs(%m1 : memref<?xf32>) {
  ...
  %t0 = bufferization.to_tensor %m0
  %0 = tensor.insert %f into %t0[...] : tensor<?xf32>
  linalg.yield
}

Now the tensor.insert op can DCE away because it has no uses. This can be avoided by inserting the new op.

  ...
  %0 = tensor.insert %f into %t0[...] : tensor<?xf32>
  bufferization.wait_for_bufferization %0 : tensor<?xf32>
  linalg.yield
}

Note: WaitForBufferizationOp is also needed for a subsequent commit that switches the custom IR traversal of Comprehensive Bufferize to RewritePatterns (not dialect conversion but regular rewrite patterns). In that case, tensor.insert_slice ops that have a matching tensor.extract_slice op could DCE away.

Note: WaitForBufferizationOp is purpusely a new op in the bufferization dialect (as opposed to an anonymous/unnamed op) because it can survive partial bufferization. When used in Comprehensive Bufferize (One-Shot) bufferize, all WaitForBufferizationOps should have disappeared by the time bufferization is done (unless allow-unknown-ops). Other cases are considered a bufferization failure.

Depends On D116446

Diff Detail

Event Timeline

springerm created this revision.Dec 31 2021, 7:38 AM
springerm requested review of this revision.Dec 31 2021, 7:38 AM
nicolasvasilache requested changes to this revision.Jan 5 2022, 12:27 AM

putting a blocker until we have a deeper discussion, this does not pass my fishiness checks atm.

This revision now requires changes to proceed.Jan 5 2022, 12:27 AM
springerm planned changes to this revision.Jan 5 2022, 11:01 AM

Putting this revision on hold. As we discussed, we likely won't need this and there's a better way to solve this issue.

springerm abandoned this revision.Jan 5 2022, 12:18 PM