Skip to content

Commit fc034e0

Browse files
[mlir][memref] Add runtime verification for memref.atomic_rmw
1 parent 571e024 commit fc034e0

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

mlir/lib/Dialect/MemRef/Transforms/RuntimeOpVerification.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,13 @@ void mlir::memref::registerRuntimeVerifiableOpInterfaceExternalModels(
426426
DialectRegistry &registry) {
427427
registry.addExtension(+[](MLIRContext *ctx, memref::MemRefDialect *dialect) {
428428
AssumeAlignmentOp::attachInterface<AssumeAlignmentOpInterface>(*ctx);
429+
AtomicRMWOp::attachInterface<LoadStoreOpInterface<AtomicRMWOp>>(*ctx);
429430
CastOp::attachInterface<CastOpInterface>(*ctx);
430431
CopyOp::attachInterface<CopyOpInterface>(*ctx);
431432
DimOp::attachInterface<DimOpInterface>(*ctx);
432433
ExpandShapeOp::attachInterface<ExpandShapeOpInterface>(*ctx);
434+
GenericAtomicRMWOp::attachInterface<
435+
LoadStoreOpInterface<GenericAtomicRMWOp>>(*ctx);
433436
LoadOp::attachInterface<LoadStoreOpInterface<LoadOp>>(*ctx);
434437
ReinterpretCastOp::attachInterface<ReinterpretCastOpInterface>(*ctx);
435438
StoreOp::attachInterface<LoadStoreOpInterface<StoreOp>>(*ctx);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// RUN: mlir-opt %s -generate-runtime-verification \
2+
// RUN: -test-cf-assert \
3+
// RUN: -expand-strided-metadata \
4+
// RUN: -lower-affine \
5+
// RUN: -convert-to-llvm | \
6+
// RUN: mlir-runner -e main -entry-point-result=void \
7+
// RUN: -shared-libs=%mlir_runner_utils 2>&1 | \
8+
// RUN: FileCheck %s
9+
10+
func.func @store_dynamic(%memref: memref<?xf32>, %index: index) {
11+
%cst = arith.constant 1.0 : f32
12+
memref.atomic_rmw addf %cst, %memref[%index] : (f32, memref<?xf32>) -> f32
13+
return
14+
}
15+
16+
func.func @main() {
17+
// Allocate a memref<10xf32>, but disguise it as a memref<5xf32>. This is
18+
// necessary because "-test-cf-assert" does not abort the program and we do
19+
// not want to segfault when running the test case.
20+
%alloc = memref.alloca() : memref<10xf32>
21+
%ptr = memref.extract_aligned_pointer_as_index %alloc : memref<10xf32> -> index
22+
%ptr_i64 = arith.index_cast %ptr : index to i64
23+
%ptr_llvm = llvm.inttoptr %ptr_i64 : i64 to !llvm.ptr
24+
%c0 = llvm.mlir.constant(0 : index) : i64
25+
%c1 = llvm.mlir.constant(1 : index) : i64
26+
%c5 = llvm.mlir.constant(5 : index) : i64
27+
%4 = llvm.mlir.poison : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
28+
%5 = llvm.insertvalue %ptr_llvm, %4[0] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
29+
%6 = llvm.insertvalue %ptr_llvm, %5[1] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
30+
%8 = llvm.insertvalue %c0, %6[2] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
31+
%9 = llvm.insertvalue %c5, %8[3, 0] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
32+
%10 = llvm.insertvalue %c1, %9[4, 0] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
33+
%buffer = builtin.unrealized_conversion_cast %10 : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)> to memref<5xf32>
34+
%cast = memref.cast %buffer : memref<5xf32> to memref<?xf32>
35+
36+
// CHECK: ERROR: Runtime op verification failed
37+
// CHECK-NEXT: "memref.atomic_rmw"(%{{.*}}, %{{.*}}, %{{.*}}) <{kind = 0 : i64}> : (f32, memref<?xf32>, index) -> f32
38+
// CHECK-NEXT: ^ out-of-bounds access
39+
// CHECK-NEXT: Location: loc({{.*}})
40+
%c9 = arith.constant 9 : index
41+
func.call @store_dynamic(%cast, %c9) : (memref<?xf32>, index) -> ()
42+
43+
return
44+
}
45+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// RUN: mlir-opt %s -generate-runtime-verification \
2+
// RUN: -test-cf-assert \
3+
// RUN: -expand-strided-metadata \
4+
// RUN: -lower-affine \
5+
// RUN: -convert-to-llvm | \
6+
// RUN: mlir-runner -e main -entry-point-result=void \
7+
// RUN: -shared-libs=%mlir_runner_utils 2>&1 | \
8+
// RUN: FileCheck %s
9+
10+
func.func @store_dynamic(%memref: memref<?xf32>, %index: index) {
11+
%cst = arith.constant 1.0 : f32
12+
memref.store %cst, %memref[%index] : memref<?xf32>
13+
return
14+
}
15+
16+
func.func @main() {
17+
// Allocate a memref<10xf32>, but disguise it as a memref<5xf32>. This is
18+
// necessary because "-test-cf-assert" does not abort the program and we do
19+
// not want to segfault when running the test case.
20+
%alloc = memref.alloca() : memref<10xf32>
21+
%ptr = memref.extract_aligned_pointer_as_index %alloc : memref<10xf32> -> index
22+
%ptr_i64 = arith.index_cast %ptr : index to i64
23+
%ptr_llvm = llvm.inttoptr %ptr_i64 : i64 to !llvm.ptr
24+
%c0 = llvm.mlir.constant(0 : index) : i64
25+
%c1 = llvm.mlir.constant(1 : index) : i64
26+
%c5 = llvm.mlir.constant(5 : index) : i64
27+
%4 = llvm.mlir.poison : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
28+
%5 = llvm.insertvalue %ptr_llvm, %4[0] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
29+
%6 = llvm.insertvalue %ptr_llvm, %5[1] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
30+
%8 = llvm.insertvalue %c0, %6[2] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
31+
%9 = llvm.insertvalue %c5, %8[3, 0] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
32+
%10 = llvm.insertvalue %c1, %9[4, 0] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
33+
%buffer = builtin.unrealized_conversion_cast %10 : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)> to memref<5xf32>
34+
%cast = memref.cast %buffer : memref<5xf32> to memref<?xf32>
35+
36+
// CHECK: ERROR: Runtime op verification failed
37+
// CHECK-NEXT: "memref.store"(%{{.*}}, %{{.*}}, %{{.*}}) : (f32, memref<?xf32>, index) -> ()
38+
// CHECK-NEXT: ^ out-of-bounds access
39+
// CHECK-NEXT: Location: loc({{.*}})
40+
%c9 = arith.constant 9 : index
41+
func.call @store_dynamic(%cast, %c9) : (memref<?xf32>, index) -> ()
42+
43+
return
44+
}
45+

0 commit comments

Comments
 (0)