[flang-commits] [flang] [Flang] Handle %VAL arguments correctly (PR #157186)

Carlos Seo via flang-commits flang-commits at lists.llvm.org
Mon Sep 8 08:13:59 PDT 2025


================
@@ -494,10 +494,20 @@ Fortran::lower::genCallOpAndResult(
     // arguments of any type and vice versa.
     mlir::Value cast;
     auto *context = builder.getContext();
-    if (mlir::isa<fir::BoxProcType>(snd) &&
-        mlir::isa<mlir::FunctionType>(fst.getType())) {
-      auto funcTy = mlir::FunctionType::get(context, {}, {});
-      auto boxProcTy = builder.getBoxProcType(funcTy);
+
+    // Special handling for %VAL arguments: internal procedures expect
+    // reference parameters. When %VAL is used, the argument should be
+    // passed by value. So we need to create a temporary variable and
+    // pass its address to avoid a type conversion error.
+    if (fir::isa_ref_type(snd) && !fir::isa_ref_type(fst.getType()) &&
+        fir::dyn_cast_ptrEleTy(snd) == fst.getType()) {
+      mlir::Value temp = builder.createTemporary(loc, fst.getType());
----------------
ceseo wrote:

Yes, you're correct. This can actually be done in both locations modified in this file. I'll fix this.

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


More information about the flang-commits mailing list