@@ -497,13 +497,12 @@ Fortran::lower::genCallOpAndResult(
497497
498498 // Special handling for %VAL arguments: internal procedures expect
499499 // reference parameters. When %VAL is used, the argument should be
500- // passed by value. So we need to create a temporary variable and
501- // pass its address to avoid a type conversion error.
500+ // passed by value. Pass the originally loaded value.
502501 if (fir::isa_ref_type (snd) && !fir::isa_ref_type (fst.getType ()) &&
503502 fir::dyn_cast_ptrEleTy (snd) == fst.getType ()) {
504- mlir::Value temp = builder. createTemporary (loc, fst.getType ());
505- builder. create <fir::StoreOp>(loc, fst, temp );
506- cast = temp ;
503+ auto loadOp = mlir::cast<fir::LoadOp>( fst.getDefiningOp ());
504+ mlir::Value originalStorage = loadOp. getMemref ( );
505+ cast = originalStorage ;
507506 } else if (mlir::isa<fir::BoxProcType>(snd) &&
508507 mlir::isa<mlir::FunctionType>(fst.getType ())) {
509508 mlir::FunctionType funcTy = mlir::FunctionType::get (context, {}, {});
@@ -1654,10 +1653,9 @@ void prepareUserCallArguments(
16541653 caller.placeInput (arg, value);
16551654 else if (fir::isa_ref_type (argTy) &&
16561655 fir::dyn_cast_ptrEleTy (argTy) == value.getType ()) {
1657- // We're trying to convert value to reference - create temporary
1658- mlir::Value temp = builder.createTemporary (loc, value.getType ());
1659- builder.create <fir::StoreOp>(loc, value, temp);
1660- caller.placeInput (arg, temp);
1656+ auto loadOp = mlir::cast<fir::LoadOp>(value.getDefiningOp ());
1657+ mlir::Value originalStorage = loadOp.getMemref ();
1658+ caller.placeInput (arg, originalStorage);
16611659 } else
16621660 caller.placeInput (arg, builder.createConvert (loc, argTy, value));
16631661
0 commit comments