[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
Tue Dec 9 06:27:21 PST 2025


================
@@ -3406,10 +3276,28 @@ class FirConverter : public Fortran::lower::AbstractConverter {
               attachInliningDirectiveToStmt(dir, &eval);
             },
             [&](const Fortran::parser::CompilerDirective::Prefetch &prefetch) {
-              TODO(getCurrentLocation(), "!$dir prefetch");
-            },
-            [&](const Fortran::parser::CompilerDirective::IVDep &) {
-              attachDirectiveToLoop(dir, &eval);
+              for (const auto &p : prefetch.v) {
+                Fortran::evaluate::ExpressionAnalyzer ea{
+                    bridge.getSemanticsContext()};
+                Fortran::lower::SomeExpr expr{*ea.Analyze(
+                    std::get<Fortran::parser::DataRef>(p.value().u))};
+                Fortran::lower::StatementContext stmtCtx;
+                mlir::Location loc = genLocation(dir.source);
+                mlir::Value memRef{Fortran::lower::convertExprToHLFIR(
+                                       loc, *this, expr, localSymbols, stmtCtx)
+                                       .getBase()};
+                if (mlir::isa<fir::BaseBoxType>(
+                        fir::unwrapRefType(memRef.getType()))) {
+                  memRef = fir::LoadOp::create(*builder, loc, memRef);
----------------
tblah wrote:

This load is only needed for mutable boxes (such as allocatables). In this case it isn't needed (and cases a compiler crash):
```
subroutine test(a)
  integer :: a(:)
  !dir$ prefetch a
  a = sum(a)
end subroutine
```

You can use `builder.loadIfRef(loc, memRef)` to avoid the load when it isn't needed.

https://github.com/llvm/llvm-project/pull/167272


More information about the flang-commits mailing list