[Mlir-commits] [mlir] 477c0b6 - [mlir][affine][gpu] Replace DivSIOp to CeilDivSIOp when lowering to GPU launch (#73328)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Mon Nov 27 00:05:59 PST 2023
Author: Hsiangkai Wang
Date: 2023-11-27T08:05:54Z
New Revision: 477c0b67a3ab30e74f3563b3f0b9d4d53caba465
URL: https://github.com/llvm/llvm-project/commit/477c0b67a3ab30e74f3563b3f0b9d4d53caba465
DIFF: https://github.com/llvm/llvm-project/commit/477c0b67a3ab30e74f3563b3f0b9d4d53caba465.diff
LOG: [mlir][affine][gpu] Replace DivSIOp to CeilDivSIOp when lowering to GPU launch (#73328)
When converting affine.for to GPU launch operator, we have to calculate
the block dimension and thread dimension for the launch operator.
The formula of the dimension size is
(upper_bound - lower_bound) / step_size
When the difference is indivisible by step_size, we use rounding-to-zero
as the division result. However, the block dimension and thread
dimension is right-open range, i.e., [0, block_dim) and [0, thread_dim).
So, we will get the wrong result if we use DivSIOp. In this patch, we
replace it with CeilDivSIOp to get the correct block and thread
dimension values.
Added:
Modified:
mlir/lib/Conversion/SCFToGPU/SCFToGPU.cpp
mlir/test/Conversion/SCFToGPU/step_positive.mlir
Removed:
################################################################################
diff --git a/mlir/lib/Conversion/SCFToGPU/SCFToGPU.cpp b/mlir/lib/Conversion/SCFToGPU/SCFToGPU.cpp
index 11b4cbb2506705b..c2218b7656a9b62 100644
--- a/mlir/lib/Conversion/SCFToGPU/SCFToGPU.cpp
+++ b/mlir/lib/Conversion/SCFToGPU/SCFToGPU.cpp
@@ -195,7 +195,8 @@ AffineLoopToGpuConverter::collectBounds(AffineForOp forOp, unsigned numLoops) {
upperBound, lowerBound);
Value step = getOrCreateStep(currentLoop, builder);
if (getConstantIntValue(step) != static_cast<int64_t>(1))
- range = builder.create<arith::DivSIOp>(currentLoop.getLoc(), range, step);
+ range =
+ builder.create<arith::CeilDivSIOp>(currentLoop.getLoc(), range, step);
dims.push_back(range);
lbs.push_back(lowerBound);
diff --git a/mlir/test/Conversion/SCFToGPU/step_positive.mlir b/mlir/test/Conversion/SCFToGPU/step_positive.mlir
index 97fd7d598621b39..84e8454e56171de 100644
--- a/mlir/test/Conversion/SCFToGPU/step_positive.mlir
+++ b/mlir/test/Conversion/SCFToGPU/step_positive.mlir
@@ -3,8 +3,8 @@
// CHECK-LABEL: @step_var
func.func @step_var(%A : memref<?x?xf32>, %B : memref<?x?xf32>) {
// Check that we divide by step.
- // CHECK: %[[range_i:.*]] = arith.divsi {{.*}}, %{{.*}}
- // CHECK: %[[range_j:.*]] = arith.divsi {{.*}}, %{{.*}}
+ // CHECK: %[[range_i:.*]] = arith.ceildivsi {{.*}}, %{{.*}}
+ // CHECK: %[[range_j:.*]] = arith.ceildivsi {{.*}}, %{{.*}}
// CHECK: gpu.launch
// CHECK-SAME: blocks(%{{[^)]*}}, %{{[^)]*}}, %{{[^)]*}}) in (%{{[^)]*}} = %[[range_i]], %{{[^)]*}} = %{{[^)]*}}, %{{[^)]*}} = %{{[^)]*}})
More information about the Mlir-commits
mailing list