Skip to content

Commit b154b05

Browse files
authored
[Flang] Make handling of %VAL consistent with gfortran (#157873)
Prevent fir.convert operation from being generated between logical and pointer types.
1 parent 5518538 commit b154b05

File tree

3 files changed

+10
-24
lines changed

3 files changed

+10
-24
lines changed

flang/lib/Lower/ConvertCall.cpp

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -516,16 +516,8 @@ Fortran::lower::genCallOpAndResult(
516516
mlir::Value cast;
517517
auto *context = builder.getContext();
518518

519-
// Special handling for %VAL arguments: internal procedures expect
520-
// reference parameters. When %VAL is used, the argument should be
521-
// passed by value. Pass the originally loaded value.
522-
if (fir::isa_ref_type(snd) && !fir::isa_ref_type(fst.getType()) &&
523-
fir::dyn_cast_ptrEleTy(snd) == fst.getType()) {
524-
auto loadOp = mlir::cast<fir::LoadOp>(fst.getDefiningOp());
525-
mlir::Value originalStorage = loadOp.getMemref();
526-
cast = originalStorage;
527-
} else if (mlir::isa<fir::BoxProcType>(snd) &&
528-
mlir::isa<mlir::FunctionType>(fst.getType())) {
519+
if (mlir::isa<fir::BoxProcType>(snd) &&
520+
mlir::isa<mlir::FunctionType>(fst.getType())) {
529521
mlir::FunctionType funcTy = mlir::FunctionType::get(context, {}, {});
530522
fir::BoxProcType boxProcTy = builder.getBoxProcType(funcTy);
531523
if (mlir::Value host = argumentHostAssocs(converter, fst)) {
@@ -1677,17 +1669,8 @@ void prepareUserCallArguments(
16771669
break;
16781670
}
16791671
// For %VAL arguments, we should pass the value directly without
1680-
// conversion to reference types. If argTy is different from value type,
1681-
// it might be due to signature mismatch with internal procedures.
1682-
if (argTy == value.getType())
1683-
caller.placeInput(arg, value);
1684-
else if (fir::isa_ref_type(argTy) &&
1685-
fir::dyn_cast_ptrEleTy(argTy) == value.getType()) {
1686-
auto loadOp = mlir::cast<fir::LoadOp>(value.getDefiningOp());
1687-
mlir::Value originalStorage = loadOp.getMemref();
1688-
caller.placeInput(arg, originalStorage);
1689-
} else
1690-
caller.placeInput(arg, builder.createConvert(loc, argTy, value));
1672+
// conversion to reference types.
1673+
caller.placeInput(arg, builder.createConvert(loc, argTy, value));
16911674

16921675
} break;
16931676
case PassBy::BaseAddressValueAttribute:

flang/lib/Optimizer/Dialect/FIROps.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,8 +1521,8 @@ bool fir::ConvertOp::canBeConverted(mlir::Type inType, mlir::Type outType) {
15211521
(isInteger(inType) && isFloatCompatible(outType)) ||
15221522
(isFloatCompatible(inType) && isInteger(outType)) ||
15231523
(isFloatCompatible(inType) && isFloatCompatible(outType)) ||
1524-
(isIntegerCompatible(inType) && isPointerCompatible(outType)) ||
1525-
(isPointerCompatible(inType) && isIntegerCompatible(outType)) ||
1524+
(isInteger(inType) && isPointerCompatible(outType)) ||
1525+
(isPointerCompatible(inType) && isInteger(outType)) ||
15261526
(mlir::isa<fir::BoxType>(inType) &&
15271527
mlir::isa<fir::BoxType>(outType)) ||
15281528
(mlir::isa<fir::BoxProcType>(inType) &&

flang/test/Lower/percent-val-actual-argument.f90

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ program main
66
call sa(%val(a1))
77
! CHECK: %[[A1_ADDR:.*]] = fir.address_of(@_QFEa1) : !fir.ref<!fir.logical<4>>
88
! CHECK: %[[A1_DECL:.*]]:2 = hlfir.declare %[[A1_ADDR]] {uniq_name = "_QFEa1"} : (!fir.ref<!fir.logical<4>>) -> (!fir.ref<!fir.logical<4>>, !fir.ref<!fir.logical<4>>)
9-
! CHECK: fir.call @_QPsa(%[[A1_DECL]]#0) fastmath<contract> : (!fir.ref<!fir.logical<4>>) -> ()
9+
! CHECK: %[[A1_LOADED:.*]] = fir.load %[[A1_DECL]]#0 : !fir.ref<!fir.logical<4>>
10+
! CHECK: %[[SA_ADDR:.*]] = fir.address_of(@_QPsa) : (!fir.ref<!fir.logical<4>>) -> ()
11+
! CHECK: %[[SA_CONVERT:.*]] = fir.convert %[[SA_ADDR]] : ((!fir.ref<!fir.logical<4>>) -> ()) -> ((!fir.logical<4>) -> ())
12+
! CHECK: fir.call %[[SA_CONVERT]](%[[A1_LOADED]]) fastmath<contract> : (!fir.logical<4>) -> ()
1013
! CHECK: func.func @_QPsa(%[[SA_ARG:.*]]: !fir.ref<!fir.logical<4>> {fir.bindc_name = "x1"}) {
1114
write(6,*) "a1 = ", a1
1215
end program main

0 commit comments

Comments
 (0)