Skip to content

Commit a19e5ae

Browse files
authored
[flang] load SECOND result in genSecond (#99342)
Until genSecond, all intrinsic `genXXX` returning scalar intrinsic (except NULL) were returning them as value. The code calling genIntrinsicCall is using that assumption when generation the asExprOp because hflir.expr<> of scalar are badly supported in tools (I should likely just forbid them all together), the type is meant for "non trivial" values: arrays, character, and derived type. For instance, the added tests crashed with error: `'arith.subf' op operand #0 must be floating-point-like, but got '!hlfir.expr<f32>'` Load the result in genSecond and add an assert after genIntrinsicCall to better enforce this.
1 parent 8afb395 commit a19e5ae

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

flang/lib/Lower/ConvertCall.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,6 +2005,8 @@ genIntrinsicRefCore(Fortran::lower::PreparedActualArguments &loweredActuals,
20052005
// returns a null pointer variable that should not be transformed into a value
20062006
// (what matters is the memory address).
20072007
if (resultEntity.isVariable() && intrinsicName != "null") {
2008+
assert(!fir::isa_trivial(fir::unwrapRefType(resultEntity.getType())) &&
2009+
"expect intrinsic scalar results to not be in memory");
20082010
hlfir::AsExprOp asExpr;
20092011
// Character/Derived MERGE lowering returns one of its argument address
20102012
// (this is the only intrinsic implemented in that way so far). The

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6161,7 +6161,7 @@ IntrinsicLibrary::genSecond(std::optional<mlir::Type> resultType,
61616161
genCpuTime(subroutineArgs);
61626162

61636163
if (resultType)
6164-
return result;
6164+
return builder.create<fir::LoadOp>(loc, fir::getBase(result));
61656165
return {};
61666166
}
61676167

flang/test/Lower/Intrinsics/second.f90

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,28 @@ subroutine test_function(time)
2828
! CHECK: %[[VAL_4:.*]] = fir.call @_FortranACpuTime() fastmath<contract> : () -> f64
2929
! CHECK: %[[VAL_5:.*]] = fir.convert %[[VAL_4]] : (f64) -> f32
3030
! CHECK: fir.store %[[VAL_5]] to %[[VAL_1]] : !fir.ref<f32>
31-
! CHECK: %[[VAL_6:.*]]:2 = hlfir.declare %[[VAL_1]] {uniq_name = ".tmp.intrinsic_result"} : (!fir.ref<f32>) -> (!fir.ref<f32>, !fir.ref<f32>)
32-
! CHECK: %[[VAL_7:.*]] = arith.constant false
33-
! CHECK: %[[VAL_8:.*]] = hlfir.as_expr %[[VAL_6]]#0 move %[[VAL_7]] : (!fir.ref<f32>, i1) -> !hlfir.expr<f32>
34-
! CHECK: hlfir.assign %[[VAL_8]] to %[[VAL_3]]#0 : !hlfir.expr<f32>, !fir.ref<f32>
35-
! CHECK: hlfir.destroy %[[VAL_8]] : !hlfir.expr<f32>
31+
! CHECK: %[[VAL_6:.*]] = fir.load %[[VAL_1]] : !fir.ref<f32>
32+
! CHECK: hlfir.assign %[[VAL_6]] to %[[VAL_3]]#0 : f32, !fir.ref<f32>
33+
! CHECK: return
34+
! CHECK: }
35+
36+
subroutine test_function_subexpr(t1, t2)
37+
real :: t1, t2
38+
t2 = second() - t1
39+
end subroutine
40+
! CHECK-LABEL: func.func @_QPtest_function_subexpr(
41+
! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<f32> {fir.bindc_name = "t1"},
42+
! CHECK-SAME: %[[VAL_1:.*]]: !fir.ref<f32> {fir.bindc_name = "t2"}) {
43+
! CHECK: %[[VAL_2:.*]] = fir.alloca f32
44+
! CHECK: %[[VAL_3:.*]] = fir.dummy_scope : !fir.dscope
45+
! CHECK: %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_0]] dummy_scope %[[VAL_3]] {uniq_name = "_QFtest_function_subexprEt1"} : (!fir.ref<f32>, !fir.dscope) -> (!fir.ref<f32>, !fir.ref<f32>)
46+
! CHECK: %[[VAL_5:.*]]:2 = hlfir.declare %[[VAL_1]] dummy_scope %[[VAL_3]] {uniq_name = "_QFtest_function_subexprEt2"} : (!fir.ref<f32>, !fir.dscope) -> (!fir.ref<f32>, !fir.ref<f32>)
47+
! CHECK: %[[VAL_6:.*]] = fir.call @_FortranACpuTime() fastmath<contract> : () -> f64
48+
! CHECK: %[[VAL_7:.*]] = fir.convert %[[VAL_6]] : (f64) -> f32
49+
! CHECK: fir.store %[[VAL_7]] to %[[VAL_2]] : !fir.ref<f32>
50+
! CHECK: %[[VAL_8:.*]] = fir.load %[[VAL_2]] : !fir.ref<f32>
51+
! CHECK: %[[VAL_9:.*]] = fir.load %[[VAL_4]]#0 : !fir.ref<f32>
52+
! CHECK: %[[VAL_10:.*]] = arith.subf %[[VAL_8]], %[[VAL_9]] fastmath<contract> : f32
53+
! CHECK: hlfir.assign %[[VAL_10]] to %[[VAL_5]]#0 : f32, !fir.ref<f32>
3654
! CHECK: return
3755
! CHECK: }

0 commit comments

Comments
 (0)