Repository navigation
[codegen][gpu] Add the iree-rocdl-use-buffer-instructions pass - #21335
Conversation
0eaa2d3 to
9285c33
Compare
Signed-off-by: Fabian Mora <[email protected]>
Signed-off-by: Fabian Mora <[email protected]>
krzysz00
left a comment
There was a problem hiding this comment.
High-level comment ... there's a pass that already annotates dispatch tensor loads with whether they can be turned into buffer descriptors. It's ROCDLConfigureBufferInstructions.cpp.
If we're doing to do all this by dataflow analysis instead, could you remove that pass or move this code into said pass (and move the calls etc etc.)
|
... Ah, just noticed the line about this being future work I'm sort of -1 on leaving that as future work, and would prefer we pick one (presumably the analysis-based one) approach to this. rocdl-configure-buffer-instructions was a quick hack anyway |
|
@krzysz00 sure, I can merge them. I was waiting for your opinion as the original author of Now, as a side note, I think this was needed for some perf testing (right @Groverkss?), in which case how do we feel about merging this patch as it is, and then in a follow up patch merging the two? |
I'd prefer landing this and iterating. @krzysz00 It might be easier to land this and then merge the two paths. There might be more work needed as we merge them and it will be easier having it in the code base and slowly merging these paths. |
| /// This function is an overly conservative estimate ops that are safe to assume | ||
| /// to be uniform. TODO: Encode this in an interface. | ||
| static bool maybeDefinitelyWorkgroupUniform(Operation *op) { | ||
| if (op->hasTrait<OpTrait::ConstantLike>() || |
There was a problem hiding this comment.
Pure operations that are functions of uniform arguments are uniform?
There was a problem hiding this comment.
That's nuanced, not all pure are uniform, e.g. gpu.thread is pure. But let's say we exclude all such ops, then the problem are pure operations that are distributed by vector distribution, such ops are not uniform.
So, the pure operations that are functions of uniform arguments are uniform applies only after vector distribution and atm this pass is expected to run before. This can be solved in the future.
There was a problem hiding this comment.
Ah, point. Yeah, let's leave this as as
There was a problem hiding this comment.
fwiw gpu.thread probably shouldn't be pure, it should be MemoryEffects::Read (this applies to a lot of other id-like operations that implicitly read from some register behind the scenes where they seem pure but aren't actually legal to hoist)
There was a problem hiding this comment.
// Always speculatable operation that does not touch memory. These operations
// are always legal to hoist or sink.
def Pure : TraitList<[AlwaysSpeculatable, NoMemoryEffect]>;
There was a problem hiding this comment.
... I'd argue that these IDs are reading global constants that are program-invariant, and so they're morally pure more than anything else
You'll note that the LLVM intrinsics of interest - at least on AMD - are marked with the Pure traits https://github.com/llvm/llvm-project/blob/09f7cab9a34ed0837375f1677a4322d40b37ed5f/llvm/include/llvm/IR/IntrinsicsAMDGPU.td#L23
(Maybe if we had a different effect for "reads device state" we could use that)
| join(std::get<1>(it), *getLatticeElementFor(point, std::get<0>(it))); | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
High-level note: can you factor thread uniformity analysis into a different file? There might be other contexts where it's useful
There was a problem hiding this comment.
What if we leave this for the follow up patch? Right now there's no use outside of this pass.
I definitely agree that this should be outside, as it would be useful to promote values to SGPRs.
There was a problem hiding this comment.
Yeah - I'd mainly think it'd be useful for doing a reassociation of index computations
| rewriter.getIndexAttr(std::numeric_limits<int32_t>::max()); | ||
|
|
||
| // Check if all the sizes in the slice are less than 2^31. | ||
| if (!llvm::all_of(sizes, [&](OpFoldResult sz) { |
There was a problem hiding this comment.
Should this be using ValueBounds instead of IntegerRangeAnalysis?
There was a problem hiding this comment.
I'd argue yes, ValueBounds can prove things and it's more general, also AFAICT ValueBounds will defer in some circumstances to IntegerRangeAnalysis to get upper bounds.
There was a problem hiding this comment.
ValueBounds doesn't defer to IntegerRangeAnalysis, no
Also, it's not strictly more general - because ValueBounds operates on the affine realm, it doesn't reason about arith.divsi, for example (and can't do that)
| auto buffOp = rewriter.create<IREE::GPU::BufferResourceCastOp>( | ||
| slice.getLoc(), slice.getType(), slice, Value()); | ||
| rewriter.replaceAllUsesExcept(slice, buffOp, buffOp); | ||
| LDBGS("- success, added cast: " << buffOp); |
There was a problem hiding this comment.
Sanity check: does this work for stores too?
There was a problem hiding this comment.
This just casts the tensor and after bufferization it becomes a raw buffer cast op.
There was a problem hiding this comment.
@qedawkins for how we can make workgroup tile slices when we're storing to result tensors and how we can do that
fabianmcg
left a comment
There was a problem hiding this comment.
See specific comments.
| /// This function is an overly conservative estimate ops that are safe to assume | ||
| /// to be uniform. TODO: Encode this in an interface. | ||
| static bool maybeDefinitelyWorkgroupUniform(Operation *op) { | ||
| if (op->hasTrait<OpTrait::ConstantLike>() || |
There was a problem hiding this comment.
That's nuanced, not all pure are uniform, e.g. gpu.thread is pure. But let's say we exclude all such ops, then the problem are pure operations that are distributed by vector distribution, such ops are not uniform.
So, the pure operations that are functions of uniform arguments are uniform applies only after vector distribution and atm this pass is expected to run before. This can be solved in the future.
| join(std::get<1>(it), *getLatticeElementFor(point, std::get<0>(it))); | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
What if we leave this for the follow up patch? Right now there's no use outside of this pass.
I definitely agree that this should be outside, as it would be useful to promote values to SGPRs.
| rewriter.getIndexAttr(std::numeric_limits<int32_t>::max()); | ||
|
|
||
| // Check if all the sizes in the slice are less than 2^31. | ||
| if (!llvm::all_of(sizes, [&](OpFoldResult sz) { |
There was a problem hiding this comment.
I'd argue yes, ValueBounds can prove things and it's more general, also AFAICT ValueBounds will defer in some circumstances to IntegerRangeAnalysis to get upper bounds.
| auto buffOp = rewriter.create<IREE::GPU::BufferResourceCastOp>( | ||
| slice.getLoc(), slice.getType(), slice, Value()); | ||
| rewriter.replaceAllUsesExcept(slice, buffOp, buffOp); | ||
| LDBGS("- success, added cast: " << buffOp); |
There was a problem hiding this comment.
This just casts the tensor and after bufferization it becomes a raw buffer cast op.
krzysz00
left a comment
There was a problem hiding this comment.
I'd argue we should refactor the analysis into its own file, but ... it's probably fine for now if you're trying to land this.
On the assumption that we'll be doing the followup work, approved
…e-org#21335) This patch adds the `iree-rocdl-use-buffer-instructions` pass to cover some buffers not covered by: `iree-rocdl-configure-buffer-instructions`. In particular this pass will look for `tensor.extact_slices` coming from a `iree_tensor_ext.dispatch.tensor.load` and if the size is less than 2GB and the offsets are uniform, then the pass adds a `iree_gpu.buffer_resource_cast` to use raw buffer ops. This is useful when the main buffer is too big to use raw buffers, but the slices are small enough to use raw buffers. Example: ```mlir func.func @uniform_loop() { %0 = hal.interface.binding.subspan layout(#pipeline_layout) binding(0) alignment(64) : !iree_tensor_ext.dispatch.tensor<readonly:tensor<32x?xi64>> %1 = hal.interface.constant.load layout(#pipeline_layout) ordinal(0) : index %2 = util.assume.int %1<umin = 1, umax = 8589934592> : index %3 = iree_tensor_ext.dispatch.tensor.load %0, offsets = [0, 0], sizes = [32, %2], strides = [1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<32x?xi64>>{%1} -> tensor<32x?xi64> %4 = hal.interface.constant.load layout(#pipeline_layout) ordinal(1) : index %5 = util.assume.int %1<umin = 1, umax = 8192> : index %c0 = arith.constant 0 : index %c1 = arith.constant 1 : index scf.for %iv = %c0 to %5 step %c1 { %6 = tensor.extract_slice %3[%iv, 0] [32, %5] [1, 1] : tensor<32x?xi64> to tensor<32x?xi64> scf.yield } return } // Result of applying the pass. func.func @uniform_loop() { %0 = hal.interface.binding.subspan layout(#pipeline_layout) binding(0) alignment(64) : !iree_tensor_ext.dispatch.tensor<readonly:tensor<32x?xi64>> %1 = hal.interface.constant.load layout(#pipeline_layout) ordinal(0) : index %2 = util.assume.int %1<umin = 1, umax = 8589934592> : index %3 = iree_tensor_ext.dispatch.tensor.load %0, offsets = [0, 0], sizes = [32, %2], strides = [1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<32x?xi64>>{%1} -> tensor<32x?xi64> %4 = hal.interface.constant.load layout(#pipeline_layout) ordinal(1) : index %5 = util.assume.int %1<umin = 1, umax = 8192> : index %c0 = arith.constant 0 : index %c1 = arith.constant 1 : index scf.for %arg0 = %c0 to %5 step %c1 { %extracted_slice = tensor.extract_slice %3[%arg0, 0] [32, %5] [1, 1] : tensor<32x?xi64> to tensor<32x?xi64> %6 = iree_gpu.buffer_resource_cast %extracted_slice : tensor<32x?xi64> } return } ``` It's future work to merge `iree-rocdl-use-buffer-instructions` and`iree-rocdl-configure-buffer-instructions` into a single pass. Currently, they are mutually exclusive. --------- Signed-off-by: Fabian Mora <[email protected]>
…ass (iree-org#21335)" Revert due to iree-org#21693 The cause is not isolated yet. This reverts commit a7561f9.
…ass (iree-org#21335)" Revert due to iree-org#21693 The cause is not isolated yet. This reverts commit a7561f9. Signed-off-by: MaheshRavishankar <[email protected]>
…ass (#21335)" (#21695) Revert due to #21693 The cause is not isolated yet. This reverts commit a7561f9. Signed-off-by: MaheshRavishankar <[email protected]>
) This patch adds the `iree-rocdl-use-buffer-instructions` pass to cover some buffers not covered by: `iree-rocdl-configure-buffer-instructions`. In particular this pass will look for `tensor.extact_slices` coming from a `iree_tensor_ext.dispatch.tensor.load` and if the size is less than 2GB and the offsets are uniform, then the pass adds a `iree_gpu.buffer_resource_cast` to use raw buffer ops. This is useful when the main buffer is too big to use raw buffers, but the slices are small enough to use raw buffers. Example: ```mlir func.func @uniform_loop() { %0 = hal.interface.binding.subspan layout(#pipeline_layout) binding(0) alignment(64) : !iree_tensor_ext.dispatch.tensor<readonly:tensor<32x?xi64>> %1 = hal.interface.constant.load layout(#pipeline_layout) ordinal(0) : index %2 = util.assume.int %1<umin = 1, umax = 8589934592> : index %3 = iree_tensor_ext.dispatch.tensor.load %0, offsets = [0, 0], sizes = [32, %2], strides = [1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<32x?xi64>>{%1} -> tensor<32x?xi64> %4 = hal.interface.constant.load layout(#pipeline_layout) ordinal(1) : index %5 = util.assume.int %1<umin = 1, umax = 8192> : index %c0 = arith.constant 0 : index %c1 = arith.constant 1 : index scf.for %iv = %c0 to %5 step %c1 { %6 = tensor.extract_slice %3[%iv, 0] [32, %5] [1, 1] : tensor<32x?xi64> to tensor<32x?xi64> scf.yield } return } // Result of applying the pass. func.func @uniform_loop() { %0 = hal.interface.binding.subspan layout(#pipeline_layout) binding(0) alignment(64) : !iree_tensor_ext.dispatch.tensor<readonly:tensor<32x?xi64>> %1 = hal.interface.constant.load layout(#pipeline_layout) ordinal(0) : index %2 = util.assume.int %1<umin = 1, umax = 8589934592> : index %3 = iree_tensor_ext.dispatch.tensor.load %0, offsets = [0, 0], sizes = [32, %2], strides = [1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<32x?xi64>>{%1} -> tensor<32x?xi64> %4 = hal.interface.constant.load layout(#pipeline_layout) ordinal(1) : index %5 = util.assume.int %1<umin = 1, umax = 8192> : index %c0 = arith.constant 0 : index %c1 = arith.constant 1 : index scf.for %arg0 = %c0 to %5 step %c1 { %extracted_slice = tensor.extract_slice %3[%arg0, 0] [32, %5] [1, 1] : tensor<32x?xi64> to tensor<32x?xi64> %6 = iree_gpu.buffer_resource_cast %extracted_slice : tensor<32x?xi64> } return } ``` It's future work to merge `iree-rocdl-use-buffer-instructions` and`iree-rocdl-configure-buffer-instructions` into a single pass. Currently, they are mutually exclusive. --------- Signed-off-by: Fabian Mora <[email protected]>
…ass (#21335)" (#21695) Revert due to #21693 The cause is not isolated yet. This reverts commit a7561f9. Signed-off-by: MaheshRavishankar <[email protected]>
This patch adds the
iree-rocdl-use-buffer-instructionspass to cover some buffers not covered by:iree-rocdl-configure-buffer-instructions.In particular this pass will look for
tensor.extact_slicescoming from airee_tensor_ext.dispatch.tensor.loadand if the size is less than 2GB and the offsets are uniform, then the pass adds airee_gpu.buffer_resource_castto use raw buffer ops.This is useful when the main buffer is too big to use raw buffers, but the slices are small enough to use raw buffers.
Example:
It's future work to merge
iree-rocdl-use-buffer-instructionsandiree-rocdl-configure-buffer-instructionsinto a single pass. Currently, they are mutually exclusive.