Skip to content

Conversation

AlexandreEichenberger
Copy link
Contributor

Currently memref.prefetch has no memory side effects, which are necessary for some optimizations.

This PR adds the needed side effect, as recommended in https://discourse.llvm.org/t/modeling-volatility-with-memory-effects/67946

This PR was created after a discussion on this specific topic here https://discourse.llvm.org/t/memref-prefetch-op-has-no-memory-side-effects-decoration-in-the-def-td-file/87482

@llvmbot llvmbot added mlir mlir:bufferization Bufferization infrastructure mlir:memref labels Jul 30, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 30, 2025

@llvm/pr-subscribers-mlir-bufferization

@llvm/pr-subscribers-mlir

Author: Alexandre Eichenberger (AlexandreEichenberger)

Changes

Currently memref.prefetch has no memory side effects, which are necessary for some optimizations.

This PR adds the needed side effect, as recommended in https://discourse.llvm.org/t/modeling-volatility-with-memory-effects/67946

This PR was created after a discussion on this specific topic here https://discourse.llvm.org/t/memref-prefetch-op-has-no-memory-side-effects-decoration-in-the-def-td-file/87482


Full diff: https://github.com/llvm/llvm-project/pull/151261.diff

2 Files Affected:

  • (modified) mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td (+2-1)
  • (modified) mlir/test/Dialect/Bufferization/Transforms/OwnershipBasedBufferDeallocation/misc-other.mlir (+9-1)
diff --git a/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td b/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
index 9321089ab55fa..5a46e86eca2b5 100644
--- a/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
+++ b/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
@@ -1351,7 +1351,8 @@ def MemRef_PrefetchOp : MemRef_Op<"prefetch"> {
     instruction cache.
   }];
 
-  let arguments = (ins AnyMemRef:$memref, Variadic<Index>:$indices,
+  let arguments = (ins Arg<AnyMemRef, "prefetch address", [MemWrite]> :$memref,
+                       Variadic<Index>:$indices,
                        BoolAttr:$isWrite,
                        ConfinedAttr<I32Attr, [IntMinValue<0>,
                                           IntMaxValue<3>]>:$localityHint,
diff --git a/mlir/test/Dialect/Bufferization/Transforms/OwnershipBasedBufferDeallocation/misc-other.mlir b/mlir/test/Dialect/Bufferization/Transforms/OwnershipBasedBufferDeallocation/misc-other.mlir
index c50c25ad8194f..fc137f1f2f722 100644
--- a/mlir/test/Dialect/Bufferization/Transforms/OwnershipBasedBufferDeallocation/misc-other.mlir
+++ b/mlir/test/Dialect/Bufferization/Transforms/OwnershipBasedBufferDeallocation/misc-other.mlir
@@ -17,4 +17,12 @@ func.func @func_with_assert(%arg0: index, %arg1: index) {
 func.func @func_with_assume_alignment(%arg0: memref<128xi8>) {
   %0 = memref.assume_alignment %arg0, 64 : memref<128xi8>
   return
-}
\ No newline at end of file
+}
+
+// CHECK-LABEL: func @func_with_prefetch(
+//       CHECK: memref.prefetch %arg0[%c0, %c0], read, locality<1>, data : memref<4x8xf32>
+func.func @func_with_prefetch(%arg0: memref<4x8xf32>) {
+  %c0 = arith.constant 0 : index
+  memref.prefetch %arg0[%c0, %c0], read, locality<1>, data : memref<4x8xf32>
+  return
+}

@llvmbot
Copy link
Member

llvmbot commented Jul 30, 2025

@llvm/pr-subscribers-mlir-memref

Author: Alexandre Eichenberger (AlexandreEichenberger)

Changes

Currently memref.prefetch has no memory side effects, which are necessary for some optimizations.

This PR adds the needed side effect, as recommended in https://discourse.llvm.org/t/modeling-volatility-with-memory-effects/67946

This PR was created after a discussion on this specific topic here https://discourse.llvm.org/t/memref-prefetch-op-has-no-memory-side-effects-decoration-in-the-def-td-file/87482


Full diff: https://github.com/llvm/llvm-project/pull/151261.diff

2 Files Affected:

  • (modified) mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td (+2-1)
  • (modified) mlir/test/Dialect/Bufferization/Transforms/OwnershipBasedBufferDeallocation/misc-other.mlir (+9-1)
diff --git a/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td b/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
index 9321089ab55fa..5a46e86eca2b5 100644
--- a/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
+++ b/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
@@ -1351,7 +1351,8 @@ def MemRef_PrefetchOp : MemRef_Op<"prefetch"> {
     instruction cache.
   }];
 
-  let arguments = (ins AnyMemRef:$memref, Variadic<Index>:$indices,
+  let arguments = (ins Arg<AnyMemRef, "prefetch address", [MemWrite]> :$memref,
+                       Variadic<Index>:$indices,
                        BoolAttr:$isWrite,
                        ConfinedAttr<I32Attr, [IntMinValue<0>,
                                           IntMaxValue<3>]>:$localityHint,
diff --git a/mlir/test/Dialect/Bufferization/Transforms/OwnershipBasedBufferDeallocation/misc-other.mlir b/mlir/test/Dialect/Bufferization/Transforms/OwnershipBasedBufferDeallocation/misc-other.mlir
index c50c25ad8194f..fc137f1f2f722 100644
--- a/mlir/test/Dialect/Bufferization/Transforms/OwnershipBasedBufferDeallocation/misc-other.mlir
+++ b/mlir/test/Dialect/Bufferization/Transforms/OwnershipBasedBufferDeallocation/misc-other.mlir
@@ -17,4 +17,12 @@ func.func @func_with_assert(%arg0: index, %arg1: index) {
 func.func @func_with_assume_alignment(%arg0: memref<128xi8>) {
   %0 = memref.assume_alignment %arg0, 64 : memref<128xi8>
   return
-}
\ No newline at end of file
+}
+
+// CHECK-LABEL: func @func_with_prefetch(
+//       CHECK: memref.prefetch %arg0[%c0, %c0], read, locality<1>, data : memref<4x8xf32>
+func.func @func_with_prefetch(%arg0: memref<4x8xf32>) {
+  %c0 = arith.constant 0 : index
+  memref.prefetch %arg0[%c0, %c0], read, locality<1>, data : memref<4x8xf32>
+  return
+}

}];

let arguments = (ins AnyMemRef:$memref, Variadic<Index>:$indices,
let arguments = (ins Arg<AnyMemRef, "prefetch address", [MemWrite]> :$memref,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to "write to the buffer" semantically here?
(it's worth a comment, as this may not be intuitive!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion, will add a comment. It's basically to ensure that dead-code analysis does not remove the operation. It is inline with making that operation a "volatile" so that it remains in place with respect to the other load/store of the prefetched data.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be removing the operation if it doesn't declare any side effects at all though, and conservatively assume it has all possible side effects. It feels like there's a bug somewhere

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ftynse I believe the source of the problem is that without side-effects defined, the buffer allocation will fail:

"ops with unknown memory side effects are not supported"

So we need to add side effects, and the part about

It's basically to ensure that dead-code analysis does not remove the operation

is only an answer to "why write and not just read".

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative design here, more SSA-friendly, would be for the op to be side-effect free and return a token to be consumed later by some load operation.

@AlexandreEichenberger
Copy link
Contributor Author

@joker-eph Page states that a review is not required to merge. Is that acceptable behavior? Want to follow the recommended procedures.

@joker-eph
Copy link
Collaborator

@joker-eph Page states that a review is not required to merge. Is that acceptable behavior? Want to follow the recommended procedures.

In general in MLIR we've been reserving "merging without review" to "trivial changes" (fixing the build, fixing a comment, other similar things like this).

@joker-eph joker-eph changed the title Memref prefetch side effect [MLIR] Define memory effects for memref.prefetch operation Aug 12, 2025
@AlexandreEichenberger
Copy link
Contributor Author

I responded to comments, would it be possible to merge the PR? I don't see a merge button, presumably I don't have access to that functionality. Thanks

@AlexandreEichenberger
Copy link
Contributor Author

@joker-eph can you please merge the PR. I don't seem to have permission. You approved the changes .

Thanks

@ftynse ftynse merged commit d45b427 into llvm:main Oct 8, 2025
9 checks passed
svkeerthy pushed a commit that referenced this pull request Oct 9, 2025
Currently `memref.prefetch` has no memory side effects, which are
necessary for some optimizations.

This PR adds the needed side effect, as recommended in
https://discourse.llvm.org/t/modeling-volatility-with-memory-effects/67946

This PR was created after a discussion on this specific topic here
https://discourse.llvm.org/t/memref-prefetch-op-has-no-memory-side-effects-decoration-in-the-def-td-file/87482

---------

Signed-off-by: Alexandre Eichenberger <[email protected]>
clingfei pushed a commit to clingfei/llvm-project that referenced this pull request Oct 10, 2025
…1261)

Currently `memref.prefetch` has no memory side effects, which are
necessary for some optimizations.

This PR adds the needed side effect, as recommended in
https://discourse.llvm.org/t/modeling-volatility-with-memory-effects/67946

This PR was created after a discussion on this specific topic here
https://discourse.llvm.org/t/memref-prefetch-op-has-no-memory-side-effects-decoration-in-the-def-td-file/87482

---------

Signed-off-by: Alexandre Eichenberger <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mlir:bufferization Bufferization infrastructure mlir:memref mlir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants