Skip to content

[codegen][gpu] Add the iree-rocdl-use-buffer-instructions pass - #21335

Merged
fabianmcg merged 2 commits into
iree-org:mainfrom
fabianmcg:pr-buffer-ops
Jul 15, 2025
Merged

fabianmcg merged 2 commits into
iree-org:mainfrom
fabianmcg:pr-buffer-ops

Conversation

@fabianmcg

@fabianmcg fabianmcg commented Jul 10, 2025 •

Copy link
Copy Markdown
Contributor

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:

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 andiree-rocdl-configure-buffer-instructions into a single pass. Currently, they are mutually exclusive.

@fabianmcg
fabianmcg force-pushed the pr-buffer-ops branch 3 times, most recently from 0eaa2d3 to 9285c33 Compare July 10, 2025 20:23
@fabianmcg
fabianmcg marked this pull request as ready for review July 10, 2025 20:42
@Groverkss
Groverkss requested a review from krzysz00 July 11, 2025 12:11

@krzysz00 krzysz00 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.)

@krzysz00

Copy link
Copy Markdown
Contributor

... 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

@fabianmcg

Copy link
Copy Markdown
Contributor Author

@krzysz00 sure, I can merge them. I was waiting for your opinion as the original author of ROCDLConfigureBufferInstructions before merging in case I was missing something, that's why they are currently mutually exclusive.

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?

@Groverkss

Groverkss commented Jul 15, 2025 •

Copy link
Copy Markdown
Contributor

@krzysz00 sure, I can merge them. I was waiting for your opinion as the original author of ROCDLConfigureBufferInstructions before merging in case I was missing something, that's why they are currently mutually exclusive.

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>() ||

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pure operations that are functions of uniform arguments are uniform?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, point. Yeah, let's leave this as as

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

// Always speculatable operation that does not touch memory.  These operations
// are always legal to hoist or sink.
def Pure : TraitList<[AlwaysSpeculatable, NoMemoryEffect]>;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

... 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)));
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

High-level note: can you factor thread uniformity analysis into a different file? There might be other contexts where it's useful

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this be using ValueBounds instead of IntegerRangeAnalysis?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sanity check: does this work for stores too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This just casts the tensor and after bufferization it becomes a raw buffer cast op.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@qedawkins for how we can make workgroup tile slices when we're storing to result tensors and how we can do that

@fabianmcg fabianmcg left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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>() ||

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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)));
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This just casts the tensor and after bufferization it becomes a raw buffer cast op.

@krzysz00
krzysz00 self-requested a review July 15, 2025 15:47

@krzysz00 krzysz00 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

@fabianmcg
fabianmcg merged commit a7561f9 into iree-org:main Jul 15, 2025
IanWood1 pushed a commit to IanWood1/iree that referenced this pull request Jul 18, 2025
…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]>
MaheshRavishankar pushed a commit to MaheshRavishankar/iree that referenced this pull request Aug 14, 2025
…ass (iree-org#21335)"

Revert due to iree-org#21693

The cause is not isolated yet.

This reverts commit a7561f9.
MaheshRavishankar pushed a commit to MaheshRavishankar/iree that referenced this pull request Aug 14, 2025
…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]>
fabianmcg pushed a commit that referenced this pull request Aug 15, 2025
…ass (#21335)" (#21695)

Revert due to #21693

The cause is not isolated yet.

This reverts commit a7561f9.

Signed-off-by: MaheshRavishankar <[email protected]>
@fabianmcg
fabianmcg deleted the pr-buffer-ops branch August 19, 2025 19:58
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
)

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]>
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
…ass (#21335)" (#21695)

Revert due to #21693

The cause is not isolated yet.

This reverts commit a7561f9.

Signed-off-by: MaheshRavishankar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants