Skip to content

Commit a9b399a

Browse files
authored
[MLIR][GPU] Fix memref.dim folding with out-of-bound index (#118890)
Fixes #118760
1 parent e60a939 commit a9b399a

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

mlir/lib/Dialect/GPU/IR/GPUDialect.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2075,7 +2075,8 @@ struct SimplifyDimOfAllocOp : public OpRewritePattern<memref::DimOp> {
20752075
return failure();
20762076

20772077
auto memrefType = llvm::dyn_cast<MemRefType>(dimOp.getSource().getType());
2078-
if (!memrefType || !memrefType.isDynamicDim(index.value()))
2078+
if (!memrefType || index.value() >= memrefType.getRank() ||
2079+
!memrefType.isDynamicDim(index.value()))
20792080
return failure();
20802081

20812082
auto alloc = dimOp.getSource().getDefiningOp<AllocOp>();

mlir/test/Dialect/GPU/canonicalize.mlir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@ func.func @gpu_dim_of_alloc(%size: index) -> index {
152152

153153
// -----
154154

155+
// CHECK-LABEL: func @out_of_bound_memref.dim
156+
// CHECK: %[[MEMREF:.[a-z0-9A-Z_]+]] = memref.dim
157+
// CHECK: return %[[MEMREF]] : index
158+
func.func @out_of_bound_memref.dim(%arg : memref<?xi8>, %size: index) -> index {
159+
%c2 = arith.constant 2 : index
160+
%1 = memref.dim %arg, %c2 : memref<?xi8>
161+
return %1 : index
162+
}
163+
164+
// -----
165+
155166
// CHECK-LABEL: func @simplify_gpu_launch
156167
func.func @simplify_gpu_launch() attributes {llvm.emit_c_interface} {
157168
%cst = arith.constant 0.000000e+00 : f32

0 commit comments

Comments
 (0)