[flang-commits] [flang] [Flang] Handle %VAL arguments correctly (PR #157186)
Tom Eccles via flang-commits
flang-commits at lists.llvm.org
Mon Sep 8 03:54:20 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());
----------------
tblah wrote:
Could you trace back `fst` to see if it comes from a FortranVairableInterface (usually hlfir.declare)? In your unit test there is already a hlfir.declare giving storage for this variable. In this case it is passing a load as the argument so it would be easy to pass the storage that was loaded instead.
LLVM can probably clean this up but it would be nice to generate cleaner IR where we can.
https://github.com/llvm/llvm-project/pull/157186
More information about the flang-commits
mailing list