File tree Expand file tree Collapse file tree 2 files changed +20
-5
lines changed Expand file tree Collapse file tree 2 files changed +20
-5
lines changed Original file line number Diff line number Diff line change @@ -284,7 +284,10 @@ struct SimplifyAllocConst : public OpRewritePattern<AllocLikeOp> {
284
284
// Check to see if any dimensions operands are constants. If so, we can
285
285
// substitute and drop them.
286
286
if (llvm::none_of (alloc.getDynamicSizes (), [](Value operand) {
287
- return matchPattern (operand, matchConstantIndex ());
287
+ APInt constSizeArg;
288
+ if (!matchPattern (operand, m_ConstantInt (&constSizeArg)))
289
+ return false ;
290
+ return constSizeArg.isNonNegative ();
288
291
}))
289
292
return failure ();
290
293
@@ -305,11 +308,11 @@ struct SimplifyAllocConst : public OpRewritePattern<AllocLikeOp> {
305
308
continue ;
306
309
}
307
310
auto dynamicSize = alloc.getDynamicSizes ()[dynamicDimPos];
308
- auto *defOp = dynamicSize. getDefiningOp () ;
309
- if (auto constantIndexOp =
310
- dyn_cast_or_null<arith::ConstantIndexOp>(defOp )) {
311
+ APInt constSizeArg ;
312
+ if (matchPattern (dynamicSize, m_ConstantInt (&constSizeArg)) &&
313
+ constSizeArg. isNonNegative ( )) {
311
314
// Dynamic shape dimension will be folded.
312
- newShapeConstants.push_back (constantIndexOp. value ());
315
+ newShapeConstants.push_back (constSizeArg. getZExtValue ());
313
316
} else {
314
317
// Dynamic shape dimension not folded; copy dynamicSize from old memref.
315
318
newShapeConstants.push_back (ShapedType::kDynamic );
Original file line number Diff line number Diff line change @@ -928,3 +928,15 @@ func.func @fold_multiple_memory_space_cast(%arg : memref<?xf32>) -> memref<?xf32
928
928
%1 = memref.memory_space_cast %0 : memref <?xf32 , 1 > to memref <?xf32 , 2 >
929
929
return %1 : memref <?xf32 , 2 >
930
930
}
931
+
932
+ // -----
933
+
934
+ // CHECK-lABEL: func @ub_negative_alloc_size
935
+ func.func private @ub_negative_alloc_size () -> memref <?x?x?xi1 > {
936
+ %idx1 = index.constant 1
937
+ %c -2 = arith.constant -2 : index
938
+ %c15 = arith.constant 15 : index
939
+ // CHECK: %[[ALLOC:.*]] = memref.alloc(%c-2) : memref<15x?x1xi1>
940
+ %alloc = memref.alloc (%c15 , %c -2 , %idx1 ) : memref <?x?x?xi1 >
941
+ return %alloc : memref <?x?x?xi1 >
942
+ }
You can’t perform that action at this time.
0 commit comments