-
Notifications
You must be signed in to change notification settings - Fork 862
[Codegen] Fix correctness issues in ConvertGatherToLDS narrow type emulation
#23763
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
Merged
lialan
merged 2 commits into
users/lialan/subbyte_gather_to_lds
from
copilot/sub-pr-23758
Mar 12, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,7 +91,9 @@ struct ConvertGatherToLDS final : OpConversionPattern<amdgpu::GatherToLDSOp> { | |
| vecType.getNumElements() * vecType.getElementTypeBitWidth(); | ||
| if (totalBits % newSrcBits != 0) { | ||
| return rewriter.notifyMatchFailure( | ||
| op, "transfer vector bit-width is not a multiple of byte width"); | ||
| op, | ||
| "transfer vector bit-width is not a multiple of the new element " | ||
| "bit width"); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -100,15 +102,17 @@ struct ConvertGatherToLDS final : OpConversionPattern<amdgpu::GatherToLDSOp> { | |
| origSrcType, origSrcBits, newSrcBits); | ||
| if (!srcIdx) { | ||
| return rewriter.notifyMatchFailure( | ||
| op, "failed to linearize source indices (dynamic strides)"); | ||
| op, "failed to linearize source indices (dynamic or mismatched " | ||
| "strides/offset, or invalid bit-width ratio)"); | ||
| } | ||
|
|
||
| // Linearize destination indices. | ||
| Value dstIdx = linearizeAndPack(rewriter, loc, op.getDstIndices(), | ||
| origDstType, origDstBits, newDstBits); | ||
| if (!dstIdx) { | ||
| return rewriter.notifyMatchFailure( | ||
| op, "failed to linearize destination indices (dynamic strides)"); | ||
| op, "failed to linearize destination indices (dynamic or mismatched " | ||
| "strides/offset, or invalid bit-width ratio)"); | ||
| } | ||
|
|
||
| // Adjust transfer type to use the new element type. | ||
|
|
@@ -129,29 +133,31 @@ struct ConvertGatherToLDS final : OpConversionPattern<amdgpu::GatherToLDSOp> { | |
| private: | ||
| // Linearizes multi-dimensional indices into a 1D index for the packed | ||
| // byte-addressable memref. | ||
| // linearIdx = sum(idx[i] * stride[i]) | ||
| // linearIdx = offset + sum(idx[i] * stride[i]) | ||
| // packedIdx = linearIdx * origBits / newBits | ||
| static Value linearizeAndPack(ConversionPatternRewriter &rewriter, | ||
| Location loc, ValueRange indices, | ||
| MemRefType origType, int origBits, | ||
| int newBits) { | ||
| if (origBits == newBits) { | ||
| // No packing needed; if also 1D, just pass through. | ||
| if (indices.size() == 1) { | ||
| return indices.front(); | ||
| } | ||
| } | ||
|
|
||
| auto [strides, offset] = origType.getStridesAndOffset(); | ||
|
|
||
| // Fail if the offset or any stride is dynamic. | ||
| if (ShapedType::isDynamic(offset)) { | ||
| return nullptr; | ||
| } | ||
| for (int64_t stride : strides) { | ||
| if (ShapedType::isDynamic(stride)) { | ||
| return nullptr; | ||
| } | ||
| } | ||
|
|
||
| // Linearize: sum(idx[i] * stride[i]). | ||
| Value linearIdx = arith::ConstantIndexOp::create(rewriter, loc, 0); | ||
| // Fail if the number of indices doesn't match the rank. | ||
| if (indices.size() != strides.size()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can this happen? |
||
| return nullptr; | ||
| } | ||
|
|
||
| // Linearize: offset + sum(idx[i] * stride[i]). | ||
| Value linearIdx = arith::ConstantIndexOp::create(rewriter, loc, offset); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't there a utility function for this these days? |
||
| for (auto [idx, stride] : llvm::zip(indices, strides)) { | ||
| Value strideVal = arith::ConstantIndexOp::create(rewriter, loc, stride); | ||
| Value product = arith::MulIOp::create(rewriter, loc, idx, strideVal); | ||
|
|
@@ -160,7 +166,9 @@ struct ConvertGatherToLDS final : OpConversionPattern<amdgpu::GatherToLDSOp> { | |
|
|
||
| // Pack: convert from origBits-element units to newBits-element units. | ||
| if (origBits != newBits) { | ||
| assert(newBits > origBits && newBits % origBits == 0); | ||
| if (newBits <= origBits || newBits % origBits != 0) { | ||
| return nullptr; | ||
| } | ||
| int64_t packRatio = newBits / origBits; | ||
| Value ratioVal = arith::ConstantIndexOp::create(rewriter, loc, packRatio); | ||
| linearIdx = arith::DivUIOp::create(rewriter, loc, linearIdx, ratioVal); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... Why is this a failure?