Skip to content

Commit 829c07f

Browse files
vzakharikbluck
authored andcommitted
[flang] Use 0 for unknown function pointer result length. (llvm#97035)
1 parent bd7e992 commit 829c07f

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

flang/lib/Lower/ConvertProcedureDesignator.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,15 @@ fir::ExtendedValue Fortran::lower::convertProcedureDesignator(
8989
fir::factory::genMaxWithZero(builder, loc, rawLen);
9090
}
9191
}
92+
// The caller of the function pointer will have to allocate
93+
// the function result with the character length specified
94+
// by the boxed value. If the result length cannot be
95+
// computed statically, set it to zero (we used to use -1,
96+
// but this could cause assertions in LLVM after inlining
97+
// exposed alloca of size -1).
9298
if (!funcPtrResultLength)
9399
funcPtrResultLength = builder.createIntegerConstant(
94-
loc, builder.getCharacterLengthType(), -1);
100+
loc, builder.getCharacterLengthType(), 0);
95101
return fir::CharBoxValue{funcPtr, funcPtrResultLength};
96102
}
97103
return funcPtr;

flang/test/Lower/HLFIR/procedure-pointer.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ subroutine sub4()
128128
! CHECK: fir.store %[[VAL_2]] to %[[VAL_0]] : !fir.ref<!fir.boxproc<(!fir.ref<i32>) -> !fir.box<!fir.ptr<!fir.char<1,?>>>>>
129129
! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %[[VAL_0]] {fortran_attrs = #fir.var_attrs<pointer>, uniq_name = "_QFsub4Ep2"} : (!fir.ref<!fir.boxproc<(!fir.ref<i32>) -> !fir.box<!fir.ptr<!fir.char<1,?>>>>>) -> (!fir.ref<!fir.boxproc<(!fir.ref<i32>) -> !fir.box<!fir.ptr<!fir.char<1,?>>>>>, !fir.ref<!fir.boxproc<(!fir.ref<i32>) -> !fir.box<!fir.ptr<!fir.char<1,?>>>>>)
130130
! CHECK: %[[VAL_4:.*]] = fir.address_of(@_QPchar_func) : (!fir.ref<i32>) -> !fir.box<!fir.ptr<!fir.char<1,?>>>
131-
! CHECK: %[[VAL_12:.*]] = arith.constant -1 : index
131+
! CHECK: %[[VAL_12:.*]] = arith.constant 0 : index
132132
! CHECK: %[[VAL_5:.*]] = fir.emboxproc %[[VAL_4]] : ((!fir.ref<i32>) -> !fir.box<!fir.ptr<!fir.char<1,?>>>) -> !fir.boxproc<() -> ()>
133133
! CHECK: %[[VAL_6:.*]] = fir.convert %[[VAL_12]] : (index) -> i64
134134
! CHECK: %[[VAL_7:.*]] = fir.undefined tuple<!fir.boxproc<() -> ()>, i64>
@@ -375,7 +375,7 @@ subroutine takes_opt_proc_ptr(p)
375375

376376
! CHECK-LABEL: fir.global internal @_QFsub1Ep7 : !fir.boxproc<(!fir.ref<i32>) -> !fir.box<!fir.ptr<!fir.char<1,?>>>> {
377377
! CHECK: %[[VAL_0:.*]] = fir.address_of(@_QPchar_func) : (!fir.ref<i32>) -> !fir.box<!fir.ptr<!fir.char<1,?>>>
378-
! CHECK: %[[VAL_11:.*]] = arith.constant -1 : index
378+
! CHECK: %[[VAL_11:.*]] = arith.constant 0 : index
379379
! CHECK: %[[VAL_1:.*]] = fir.emboxproc %[[VAL_0]] : ((!fir.ref<i32>) -> !fir.box<!fir.ptr<!fir.char<1,?>>>) -> !fir.boxproc<() -> ()>
380380
! CHECK: %[[VAL_2:.*]] = fir.convert %[[VAL_11]] : (index) -> i64
381381
! CHECK: %[[VAL_3:.*]] = fir.undefined tuple<!fir.boxproc<() -> ()>, i64>

flang/test/Lower/dummy-procedure-character.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function bar4(n)
8282
end function
8383
end interface
8484
! CHECK: %[[VAL_0:.*]] = fir.address_of(@_QPbar4) : (!fir.ref<!fir.char<1,?>>, index, !fir.ref<i32>) -> !fir.boxchar<1>
85-
! CHECK: %[[VAL_1:.*]] = arith.constant -1 : index
85+
! CHECK: %[[VAL_1:.*]] = arith.constant 0 : index
8686
! CHECK: %[[VAL_2:.*]] = fir.emboxproc %[[VAL_0]] : ((!fir.ref<!fir.char<1,?>>, index, !fir.ref<i32>) -> !fir.boxchar<1>) -> !fir.boxproc<() -> ()>
8787
! CHECK: %[[VAL_3:.*]] = fir.convert %[[VAL_1]] : (index) -> i64
8888
! CHECK: %[[VAL_4:.*]] = fir.undefined tuple<!fir.boxproc<() -> ()>, i64>
@@ -97,7 +97,7 @@ subroutine cannot_compute_len_yet_2()
9797
character(*) :: bar5
9898
external :: bar5
9999
! CHECK: %[[VAL_0:.*]] = fir.address_of(@_QPbar5) : (!fir.ref<!fir.char<1,?>>, index) -> !fir.boxchar<1>
100-
! CHECK: %[[VAL_1:.*]] = arith.constant -1 : index
100+
! CHECK: %[[VAL_1:.*]] = arith.constant 0 : index
101101
! CHECK: %[[VAL_2:.*]] = fir.emboxproc %[[VAL_0]] : ((!fir.ref<!fir.char<1,?>>, index) -> !fir.boxchar<1>) -> !fir.boxproc<() -> ()>
102102
! CHECK: %[[VAL_3:.*]] = fir.convert %[[VAL_1]] : (index) -> i64
103103
! CHECK: %[[VAL_4:.*]] = fir.undefined tuple<!fir.boxproc<() -> ()>, i64>

0 commit comments

Comments
 (0)