[Mlir-commits] [mlir] [mlir][memref] Add runtime verification for `memref.atomic_rmw` (PR #130414)

Matthias Springer llvmlistbot at llvm.org
Wed Apr 30 04:42:04 PDT 2025


https://github.com/matthias-springer updated https://github.com/llvm/llvm-project/pull/130414

>From fc034e05835dcbfa3387e20993ae2001340b1368 Mon Sep 17 00:00:00 2001
From: Matthias Springer <mspringer at nvidia.com>
Date: Sat, 8 Mar 2025 14:34:54 +0100
Subject: [PATCH 1/2] [mlir][memref] Add runtime verification for
 `memref.atomic_rmw`

---
 .../Transforms/RuntimeOpVerification.cpp      |  3 ++
 .../atomic-rmw-runtime-verification.mlir      | 45 +++++++++++++++++++
 .../MemRef/store-runtime-verification.mlir    | 45 +++++++++++++++++++
 3 files changed, 93 insertions(+)
 create mode 100644 mlir/test/Integration/Dialect/MemRef/atomic-rmw-runtime-verification.mlir
 create mode 100644 mlir/test/Integration/Dialect/MemRef/store-runtime-verification.mlir

diff --git a/mlir/lib/Dialect/MemRef/Transforms/RuntimeOpVerification.cpp b/mlir/lib/Dialect/MemRef/Transforms/RuntimeOpVerification.cpp
index cd92026562da9..2e97fafdceace 100644
--- a/mlir/lib/Dialect/MemRef/Transforms/RuntimeOpVerification.cpp
+++ b/mlir/lib/Dialect/MemRef/Transforms/RuntimeOpVerification.cpp
@@ -426,10 +426,13 @@ void mlir::memref::registerRuntimeVerifiableOpInterfaceExternalModels(
     DialectRegistry &registry) {
   registry.addExtension(+[](MLIRContext *ctx, memref::MemRefDialect *dialect) {
     AssumeAlignmentOp::attachInterface<AssumeAlignmentOpInterface>(*ctx);
+    AtomicRMWOp::attachInterface<LoadStoreOpInterface<AtomicRMWOp>>(*ctx);
     CastOp::attachInterface<CastOpInterface>(*ctx);
     CopyOp::attachInterface<CopyOpInterface>(*ctx);
     DimOp::attachInterface<DimOpInterface>(*ctx);
     ExpandShapeOp::attachInterface<ExpandShapeOpInterface>(*ctx);
+    GenericAtomicRMWOp::attachInterface<
+        LoadStoreOpInterface<GenericAtomicRMWOp>>(*ctx);
     LoadOp::attachInterface<LoadStoreOpInterface<LoadOp>>(*ctx);
     ReinterpretCastOp::attachInterface<ReinterpretCastOpInterface>(*ctx);
     StoreOp::attachInterface<LoadStoreOpInterface<StoreOp>>(*ctx);
diff --git a/mlir/test/Integration/Dialect/MemRef/atomic-rmw-runtime-verification.mlir b/mlir/test/Integration/Dialect/MemRef/atomic-rmw-runtime-verification.mlir
new file mode 100644
index 0000000000000..9f70c5ca66f65
--- /dev/null
+++ b/mlir/test/Integration/Dialect/MemRef/atomic-rmw-runtime-verification.mlir
@@ -0,0 +1,45 @@
+// RUN: mlir-opt %s -generate-runtime-verification \
+// RUN:     -test-cf-assert \
+// RUN:     -expand-strided-metadata \
+// RUN:     -lower-affine \
+// RUN:     -convert-to-llvm | \
+// RUN: mlir-runner -e main -entry-point-result=void \
+// RUN:     -shared-libs=%mlir_runner_utils 2>&1 | \
+// RUN: FileCheck %s
+
+func.func @store_dynamic(%memref: memref<?xf32>, %index: index) {
+  %cst = arith.constant 1.0 : f32
+  memref.atomic_rmw addf %cst, %memref[%index] : (f32, memref<?xf32>) -> f32
+  return
+}
+
+func.func @main() {
+  // Allocate a memref<10xf32>, but disguise it as a memref<5xf32>. This is
+  // necessary because "-test-cf-assert" does not abort the program and we do
+  // not want to segfault when running the test case.
+  %alloc = memref.alloca() : memref<10xf32>
+  %ptr = memref.extract_aligned_pointer_as_index %alloc : memref<10xf32> -> index
+  %ptr_i64 = arith.index_cast %ptr : index to i64
+  %ptr_llvm = llvm.inttoptr %ptr_i64 : i64 to !llvm.ptr
+  %c0 = llvm.mlir.constant(0 : index) : i64
+  %c1 = llvm.mlir.constant(1 : index) : i64
+  %c5 = llvm.mlir.constant(5 : index) : i64
+  %4 = llvm.mlir.poison : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
+  %5 = llvm.insertvalue %ptr_llvm, %4[0] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
+  %6 = llvm.insertvalue %ptr_llvm, %5[1] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
+  %8 = llvm.insertvalue %c0, %6[2] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
+  %9 = llvm.insertvalue %c5, %8[3, 0] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
+  %10 = llvm.insertvalue %c1, %9[4, 0] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
+  %buffer = builtin.unrealized_conversion_cast %10 : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)> to memref<5xf32>
+  %cast = memref.cast %buffer : memref<5xf32> to memref<?xf32>
+
+  //      CHECK: ERROR: Runtime op verification failed
+  // CHECK-NEXT: "memref.atomic_rmw"(%{{.*}}, %{{.*}}, %{{.*}}) <{kind = 0 : i64}> : (f32, memref<?xf32>, index) -> f32
+  // CHECK-NEXT: ^ out-of-bounds access
+  // CHECK-NEXT: Location: loc({{.*}})
+  %c9 = arith.constant 9 : index
+  func.call @store_dynamic(%cast, %c9) : (memref<?xf32>, index) -> ()
+
+  return
+}
+
diff --git a/mlir/test/Integration/Dialect/MemRef/store-runtime-verification.mlir b/mlir/test/Integration/Dialect/MemRef/store-runtime-verification.mlir
new file mode 100644
index 0000000000000..58961ba31d93a
--- /dev/null
+++ b/mlir/test/Integration/Dialect/MemRef/store-runtime-verification.mlir
@@ -0,0 +1,45 @@
+// RUN: mlir-opt %s -generate-runtime-verification \
+// RUN:     -test-cf-assert \
+// RUN:     -expand-strided-metadata \
+// RUN:     -lower-affine \
+// RUN:     -convert-to-llvm | \
+// RUN: mlir-runner -e main -entry-point-result=void \
+// RUN:     -shared-libs=%mlir_runner_utils 2>&1 | \
+// RUN: FileCheck %s
+
+func.func @store_dynamic(%memref: memref<?xf32>, %index: index) {
+  %cst = arith.constant 1.0 : f32
+  memref.store %cst, %memref[%index] :  memref<?xf32>
+  return
+}
+
+func.func @main() {
+  // Allocate a memref<10xf32>, but disguise it as a memref<5xf32>. This is
+  // necessary because "-test-cf-assert" does not abort the program and we do
+  // not want to segfault when running the test case.
+  %alloc = memref.alloca() : memref<10xf32>
+  %ptr = memref.extract_aligned_pointer_as_index %alloc : memref<10xf32> -> index
+  %ptr_i64 = arith.index_cast %ptr : index to i64
+  %ptr_llvm = llvm.inttoptr %ptr_i64 : i64 to !llvm.ptr
+  %c0 = llvm.mlir.constant(0 : index) : i64
+  %c1 = llvm.mlir.constant(1 : index) : i64
+  %c5 = llvm.mlir.constant(5 : index) : i64
+  %4 = llvm.mlir.poison : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
+  %5 = llvm.insertvalue %ptr_llvm, %4[0] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
+  %6 = llvm.insertvalue %ptr_llvm, %5[1] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
+  %8 = llvm.insertvalue %c0, %6[2] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
+  %9 = llvm.insertvalue %c5, %8[3, 0] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
+  %10 = llvm.insertvalue %c1, %9[4, 0] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
+  %buffer = builtin.unrealized_conversion_cast %10 : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)> to memref<5xf32>
+  %cast = memref.cast %buffer : memref<5xf32> to memref<?xf32>
+
+  //      CHECK: ERROR: Runtime op verification failed
+  // CHECK-NEXT: "memref.store"(%{{.*}}, %{{.*}}, %{{.*}}) : (f32, memref<?xf32>, index) -> ()
+  // CHECK-NEXT: ^ out-of-bounds access
+  // CHECK-NEXT: Location: loc({{.*}})
+  %c9 = arith.constant 9 : index
+  func.call @store_dynamic(%cast, %c9) : (memref<?xf32>, index) -> ()
+
+  return
+}
+

>From 37749b61969497b4cc7deba5336080c8e03da275 Mon Sep 17 00:00:00 2001
From: Matthias Springer <mspringer at nvidia.com>
Date: Wed, 30 Apr 2025 13:41:45 +0200
Subject: [PATCH 2/2] address comments

---
 .../Dialect/MemRef/atomic-rmw-runtime-verification.mlir         | 2 --
 .../Integration/Dialect/MemRef/store-runtime-verification.mlir  | 2 --
 2 files changed, 4 deletions(-)

diff --git a/mlir/test/Integration/Dialect/MemRef/atomic-rmw-runtime-verification.mlir b/mlir/test/Integration/Dialect/MemRef/atomic-rmw-runtime-verification.mlir
index 9f70c5ca66f65..26c731c921356 100644
--- a/mlir/test/Integration/Dialect/MemRef/atomic-rmw-runtime-verification.mlir
+++ b/mlir/test/Integration/Dialect/MemRef/atomic-rmw-runtime-verification.mlir
@@ -1,7 +1,5 @@
 // RUN: mlir-opt %s -generate-runtime-verification \
 // RUN:     -test-cf-assert \
-// RUN:     -expand-strided-metadata \
-// RUN:     -lower-affine \
 // RUN:     -convert-to-llvm | \
 // RUN: mlir-runner -e main -entry-point-result=void \
 // RUN:     -shared-libs=%mlir_runner_utils 2>&1 | \
diff --git a/mlir/test/Integration/Dialect/MemRef/store-runtime-verification.mlir b/mlir/test/Integration/Dialect/MemRef/store-runtime-verification.mlir
index 58961ba31d93a..12253fa3b5e83 100644
--- a/mlir/test/Integration/Dialect/MemRef/store-runtime-verification.mlir
+++ b/mlir/test/Integration/Dialect/MemRef/store-runtime-verification.mlir
@@ -1,7 +1,5 @@
 // RUN: mlir-opt %s -generate-runtime-verification \
 // RUN:     -test-cf-assert \
-// RUN:     -expand-strided-metadata \
-// RUN:     -lower-affine \
 // RUN:     -convert-to-llvm | \
 // RUN: mlir-runner -e main -entry-point-result=void \
 // RUN:     -shared-libs=%mlir_runner_utils 2>&1 | \



More information about the Mlir-commits mailing list