Skip to content

[MLIR][AMDGPU] Fixing word alignment check for bufferload fastpath - #135982

Merged
jerryyin merged 3 commits into
mainfrom
users/zyin/fix-delta-bytes-compute
Apr 17, 2025
Merged

jerryyin merged 3 commits into
mainfrom
users/zyin/fix-delta-bytes-compute

Conversation

@jerryyin

Copy link
Copy Markdown
Member

delta_bytes % (32 ceilDiv elementBitwidth) != 0 condition is incorrect in #135014

For example, last load is issued to load only one last element of fp16. Then delta bytes = 2, (32 ceildiv 16) = 2. In this case it will be judged as word aligned. It will send to fast path but get all zeros for the fp16 because it cross the word boundary.

In reality the equation should be just delta_bytes % 4 , since a word is 4 bytes. This PR fix the bug by amending the mod target to 4.

@llvmbot

llvmbot commented Apr 16, 2025 •

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-mlir-gpu
@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-backend-amdgpu

Author: Zhuoran Yin (jerryyin)

Changes

delta_bytes % (32 ceilDiv elementBitwidth) != 0 condition is incorrect in #135014

For example, last load is issued to load only one last element of fp16. Then delta bytes = 2, (32 ceildiv 16) = 2. In this case it will be judged as word aligned. It will send to fast path but get all zeros for the fp16 because it cross the word boundary.

In reality the equation should be just delta_bytes % 4 , since a word is 4 bytes. This PR fix the bug by amending the mod target to 4.


Full diff: https://github.com/llvm/llvm-project/pull/135982.diff

2 Files Affected:

  • (modified) mlir/lib/Dialect/AMDGPU/Transforms/TransferReadToLoad.cpp (+3-4)
  • (modified) mlir/test/Dialect/AMDGPU/transfer-read-to-load.mlir (+6-9)
diff --git a/mlir/lib/Dialect/AMDGPU/Transforms/TransferReadToLoad.cpp b/mlir/lib/Dialect/AMDGPU/Transforms/TransferReadToLoad.cpp
index f665c1794cdd4..7268bd56f43f6 100644
--- a/mlir/lib/Dialect/AMDGPU/Transforms/TransferReadToLoad.cpp
+++ b/mlir/lib/Dialect/AMDGPU/Transforms/TransferReadToLoad.cpp
@@ -225,15 +225,14 @@ struct TransferReadLowering final : OpRewritePattern<vector::TransferReadOp> {
     Value isOutofBounds = rewriter.create<arith::CmpIOp>(
         loc, arith::CmpIPredicate::ult, delta, vectorSizeOffset);
 
-    // 2) check if (detla_bytes % (32 / elementBitwidth) != 0)
+    // 2) check if (detla_bytes % bytes_per_word != 0)
     Value deltaBytes = rewriter.create<arith::MulIOp>(
         loc, delta,
         rewriter.create<arith::ConstantIndexOp>(loc, elementBitWidth / 8));
-    Value elementsPerWord = rewriter.create<arith::ConstantIndexOp>(
-        loc, llvm::divideCeil(32, elementBitWidth));
+    Value bytesPerWord = rewriter.create<arith::ConstantIndexOp>(loc, 4);
     Value isNotWordAligned = rewriter.create<arith::CmpIOp>(
         loc, arith::CmpIPredicate::ne,
-        rewriter.create<arith::RemUIOp>(loc, deltaBytes, elementsPerWord),
+        rewriter.create<arith::RemUIOp>(loc, deltaBytes, bytesPerWord),
         rewriter.create<arith::ConstantIndexOp>(loc, 0));
 
     // We take the fallback of transfer_read default lowering only it is both
diff --git a/mlir/test/Dialect/AMDGPU/transfer-read-to-load.mlir b/mlir/test/Dialect/AMDGPU/transfer-read-to-load.mlir
index d0805b6b8a973..d41adf3ad2c7f 100644
--- a/mlir/test/Dialect/AMDGPU/transfer-read-to-load.mlir
+++ b/mlir/test/Dialect/AMDGPU/transfer-read-to-load.mlir
@@ -10,8 +10,7 @@ func.func @transfer_to_maskedload_fatrawbuffer(%mem : memref<8x8xf32, #amdgpu.ad
   return %res : vector<4xf32>
 }
 
-// CHECK: %[[FALSE:.*]] = arith.constant false
-// CHECK: %[[IF:.*]] = scf.if %[[FALSE]] -> (vector<4xf32>) {
+// CHECK: %[[IF:.*]] = scf.if
 // CHECK: vector.transfer_read %[[ARG0]][%[[ARG1]], %[[ARG1]]]
 
 // CHECK: } else {
@@ -35,14 +34,14 @@ func.func @transfer_to_maskedload_fatrawbuffer_f16(%mem : memref<8x8xf16, #amdgp
 // CHECK-DAG: %[[C0:.*]] = arith.constant 0
 // CHECK-DAG: %[[SIZE:.*]] = arith.constant 64
 // CHECK-DAG: %[[BYTES:.*]] = arith.constant 2
-// CHECK-DAG: %[[VECTORSIZE:.*]] = arith.constant 4
+// CHECK-DAG: %[[C4:.*]] = arith.constant 4
 
 // CHECK: %[[LINEAR:.*]] = affine.apply #map()[%[[ARG1]], %[[ARG2]]]
 // CHECK: %[[DELTA:.*]] = arith.subi %[[SIZE]], %[[LINEAR]]
-// CHECK: %[[COND1:.*]] = arith.cmpi ult, %[[DELTA]], %[[VECTORSIZE]]
+// CHECK: %[[COND1:.*]] = arith.cmpi ult, %[[DELTA]], %[[C4]]
 
 // CHECK: %[[DELTABYTES:.*]] = arith.muli %[[DELTA]], %[[BYTES]]
-// CHECK: %[[REM:.*]] = arith.remui %[[DELTABYTES]], %[[BYTES]]
+// CHECK: %[[REM:.*]] = arith.remui %[[DELTABYTES]], %[[C4]]
 // CHECK: %[[COND2:.*]] = arith.cmpi ne, %[[REM]], %[[C0]]
 
 // CHECK: %[[COND:.*]] = arith.andi %[[COND1]], %[[COND2]]
@@ -120,8 +119,7 @@ func.func @transfer_broadcasting(%mem : memref<8x8xf32, #amdgpu.address_space<fa
   return %res : vector<4xf32>
 }
 // CHECK: %[[CST:.*]] = arith.constant dense<0.000000e+00> : vector<1xf32>
-// CHECK: %[[FALSE:.*]] = arith.constant false
-// CHECK: %[[IF:.*]] = scf.if %[[FALSE]] -> (vector<4xf32>) {
+// CHECK: %[[IF:.*]] = scf.if
 // CHECK: %[[LOAD:.*]] = vector.load %arg0[%arg1, %arg1]
 // CHECK: %[[SELECT:.*]] = arith.select %arg2, %[[LOAD]], %[[CST]]
 // CHECK: %[[BROADCAST:.*]] = vector.broadcast %[[SELECT]] : vector<1xf32> to vector<4xf32>
@@ -140,7 +138,6 @@ func.func @transfer_scalar(%mem : memref<8x8xf32, #amdgpu.address_space<fat_raw_
   return %res : vector<1xf32>
 }
 // CHECK: %[[CST:.*]] = arith.constant dense<0.000000e+00> : vector<1xf32>
-// CHECK: %[[FALSE:.*]] = arith.constant false
-// CHECK: %[[IF:.*]] = scf.if %[[FALSE]] -> (vector<1xf32>) {
+// CHECK: %[[IF:.*]] = scf.if
 // CHECK: %[[LOAD:.*]] = vector.load %[[ARG0]][%[[ARG1]], %[[ARG1]]]
 // CHECK: %[[SELECT:.*]] = arith.select %arg2, %[[LOAD]], %[[CST]]

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

Oh, yeah, if it's deltaBytes, it needs to be 4

I think the original condition is correct if you use delta and not deltaBytes - might not be a bad idea, since it saves on multiplications?

@jerryyin

Copy link
Copy Markdown
Member Author

@krzysz00 Good point, I used delta instead in the latest commit.

@jerryyin
jerryyin force-pushed the users/zyin/fix-delta-bytes-compute branch from e792ecd to 6bce0bb Compare April 16, 2025 16:44

@kuhar kuhar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM but wait for an approval from @krzysz00

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

Approved assuming tests pass

(Side note: this version now works for i4 and friends - so long as the bitwidth of the elements is a power of 2)

@llvmbot llvmbot added the bazel "Peripheral" support tier build system: utils/bazel label Apr 16, 2025
@jerryyin

Copy link
Copy Markdown
Member Author

IREE CI help me caught a few bazel build related failures. (That won't reproduce in standlone upstream build). I'll leave this PR till tomorrow in case there's any additional review feedbacks on the new commit. Thanks for everyone's review!

@jerryyin
jerryyin merged commit 47f4f39 into main Apr 17, 2025
@jerryyin
jerryyin deleted the users/zyin/fix-delta-bytes-compute branch April 17, 2025 12:50
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…lvm#135982)

`delta_bytes % (32 ceilDiv elementBitwidth) != 0` condition is incorrect
in llvm#135014

For example, last load is issued to load only one last element of fp16.
Then `delta bytes = 2`, `(32 ceildiv 16) = 2`. In this case it will be
judged as word aligned. It will send to fast path but get all zeros for
the fp16 because it cross the word boundary.

In reality the equation should be just `delta_bytes % 4` , since a word
is 4 bytes. This PR fix the bug by amending the mod target to 4.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend:AMDGPU bazel "Peripheral" support tier build system: utils/bazel mlir:amdgpu mlir:gpu mlir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants