-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[mlir][memref] Add runtime verification for memref.assume_alignment
#130412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
matthias-springer
merged 1 commit into
main
from
users/matthias-springer/runtime_verify_memref_assume
Mar 19, 2025
Merged
[mlir][memref] Add runtime verification for memref.assume_alignment
#130412
matthias-springer
merged 1 commit into
main
from
users/matthias-springer/runtime_verify_memref_assume
Mar 19, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
|
@llvm/pr-subscribers-mlir-memref @llvm/pr-subscribers-mlir Author: Matthias Springer (matthias-springer) ChangesImplement runtime verification for Full diff: https://github.com/llvm/llvm-project/pull/130412.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/MemRef/Transforms/RuntimeOpVerification.cpp b/mlir/lib/Dialect/MemRef/Transforms/RuntimeOpVerification.cpp
index f825d7d9d42c2..c8e7325d7ac89 100644
--- a/mlir/lib/Dialect/MemRef/Transforms/RuntimeOpVerification.cpp
+++ b/mlir/lib/Dialect/MemRef/Transforms/RuntimeOpVerification.cpp
@@ -23,7 +23,7 @@ using namespace mlir;
namespace mlir {
namespace memref {
namespace {
-/// Generate a runtime check for lb <= value < ub.
+/// Generate a runtime check for lb <= value < ub.
Value generateInBoundsCheck(OpBuilder &builder, Location loc, Value value,
Value lb, Value ub) {
Value inBounds1 = builder.createOrFold<arith::CmpIOp>(
@@ -35,6 +35,28 @@ Value generateInBoundsCheck(OpBuilder &builder, Location loc, Value value,
return inBounds;
}
+struct AssumeAlignmentOpInterface
+ : public RuntimeVerifiableOpInterface::ExternalModel<
+ AssumeAlignmentOpInterface, AssumeAlignmentOp> {
+ void generateRuntimeVerification(Operation *op, OpBuilder &builder,
+ Location loc) const {
+ auto assumeOp = cast<AssumeAlignmentOp>(op);
+ Value ptr = builder.create<ExtractAlignedPointerAsIndexOp>(
+ loc, assumeOp.getMemref());
+ Value rest = builder.create<arith::RemUIOp>(
+ loc, ptr,
+ builder.create<arith::ConstantIndexOp>(loc, assumeOp.getAlignment()));
+ Value isAligned = builder.create<arith::CmpIOp>(
+ loc, arith::CmpIPredicate::eq, rest,
+ builder.create<arith::ConstantIndexOp>(loc, 0));
+ builder.create<cf::AssertOp>(
+ loc, isAligned,
+ RuntimeVerifiableOpInterface::generateErrorMessage(
+ op, "memref is not aligned to " +
+ std::to_string(assumeOp.getAlignment())));
+ }
+};
+
struct CastOpInterface
: public RuntimeVerifiableOpInterface::ExternalModel<CastOpInterface,
CastOp> {
@@ -354,6 +376,7 @@ struct ExpandShapeOpInterface
void mlir::memref::registerRuntimeVerifiableOpInterfaceExternalModels(
DialectRegistry ®istry) {
registry.addExtension(+[](MLIRContext *ctx, memref::MemRefDialect *dialect) {
+ AssumeAlignmentOp::attachInterface<AssumeAlignmentOpInterface>(*ctx);
CastOp::attachInterface<CastOpInterface>(*ctx);
DimOp::attachInterface<DimOpInterface>(*ctx);
ExpandShapeOp::attachInterface<ExpandShapeOpInterface>(*ctx);
diff --git a/mlir/test/Integration/Dialect/MemRef/assume-alignment-runtime-verification.mlir b/mlir/test/Integration/Dialect/MemRef/assume-alignment-runtime-verification.mlir
new file mode 100644
index 0000000000000..394648d1b8bfa
--- /dev/null
+++ b/mlir/test/Integration/Dialect/MemRef/assume-alignment-runtime-verification.mlir
@@ -0,0 +1,37 @@
+// RUN: mlir-opt %s -generate-runtime-verification \
+// RUN: -expand-strided-metadata \
+// RUN: -test-cf-assert \
+// 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 @main() {
+ // This buffer is properly aligned. There should be no error.
+ // CHECK-NOT: ^ memref is not aligned to 8
+ %alloc = memref.alloca() : memref<5xf64>
+ memref.assume_alignment %alloc, 8 : memref<5xf64>
+
+ // Construct a memref descriptor with a pointer that is not aligned to 4.
+ // This cannot be done with just the memref dialect. We have to resort to
+ // the LLVM dialect.
+ %c0 = llvm.mlir.constant(0 : index) : i64
+ %c1 = llvm.mlir.constant(1 : index) : i64
+ %c3 = llvm.mlir.constant(3 : index) : i64
+ %unaligned_ptr = llvm.inttoptr %c3 : i64 to !llvm.ptr
+ %4 = llvm.mlir.poison : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
+ %5 = llvm.insertvalue %unaligned_ptr, %4[0] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
+ %6 = llvm.insertvalue %unaligned_ptr, %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 %c1, %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<1xf32>
+
+ // CHECK: ERROR: Runtime op verification failed
+ // CHECK-NEXT: "memref.assume_alignment"(%{{.*}}) <{alignment = 4 : i32}> : (memref<1xf32>) -> ()
+ // CHECK-NEXT: ^ memref is not aligned to 4
+ // CHECK-NEXT: Location: loc({{.*}})
+ memref.assume_alignment %buffer, 4 : memref<1xf32>
+
+ return
+}
|
6883972 to
a44867a
Compare
Base automatically changed from
users/matthias-springer/runtime_verify_memref_dim
to
main
March 18, 2025 08:10
7cf452f to
d6b573e
Compare
grypp
approved these changes
Mar 19, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implement runtime verification for
memref.assume_alignment.