[flang-commits] [flang] [Flang] Add FIR and LLVM lowering support for prefetch directive (PR #167272)
Tom Eccles via flang-commits
flang-commits at lists.llvm.org
Fri Dec 12 02:33:29 PST 2025
================
@@ -353,6 +351,39 @@ def fir_StoreOp : fir_Op<"store", [FirAliasTagOpInterface,
}];
}
+def fir_PrefetchOp : fir_Op<"prefetch", []> {
+ let summary = "prefetch a memory reference";
+
+ let description = [{
+ The prefetch is a hint to the code generator that the memory reference will
+ be used in the near future. The prefetch is not guaranteed to be executed.
+
+ ```
+ %a = ... -> !fir.ref<i32>
+ fir.prefetch %a {read, data, localityHint = 3 : i32} : !fir.ref<i32>
+ // ...
+ fir.load %a : !fir.ref<i32> // use the prefetched value
+ ```
+ }];
+
+ /// `memref' is the address to be prefetched
+ /// `rw' : rw specifier >
+ /// read is 0 (default), write is 1
+ /// `localityHint': temporal locality specifier >
+ /// value ranging from 0 - no locality to 3 - extremely local
+ /// `cacheType' : cache type specifier >
+ /// instruction cache is 0, data cache is 1 (default)
+ /// NOTE: The numerical values used here is in reference to the LLVM LangRef
+ let arguments =
+ (ins Arg<AnyReferenceLike,
+ "prefetch memory address", [MemWrite]> : $memref,
----------------
tblah wrote:
I'm told that with MemRead, fir.prefetch is removed as dead code. I guess that makes sense because prefetch isn't modelled as producing a value which is elsewhere used - I just didn't think this through.
My worry with the write effect is that it might be difficult for the compiler to prove that the write doesn't affect other values, and that could then pessimise other optimisations. But it would at least work and perhaps this wouldn't matter for real applications.
One hack would be to model this as a read (to tie it to memref) but also as an allocation as a kind of "don't touch this" note to the compiler. For fir.declare we added a memalloc on the debugging resource for this reason.
I wonder if some hybrid solution could be a memwrite to a fake resource (in addition to the read to memref) to represent that this has some unmodelled side effect (like how a printf would be a memwrite to soemthing not modelled in IR).
But if you would prefer to just MemWrite to memref I think that is okay for now. I don't want to force premature optimisation.
https://github.com/llvm/llvm-project/pull/167272
More information about the flang-commits
mailing list