Skip to content

Commit e91a4be

Browse files
authored
[flang][hlfir] Fix c_null_ptr lowering in structure constructors (#68321)
Lowering handles C_PTR initial values that are designators or NULL() inside structure constructors as an extension to support. This extension is used by initial values generated for runtime derived type info. But c_null_ptr wrongly fell into this extension path with HLFIR, causing the initial value to be set to some (non null) address containing c_null_ptr instead of c_null_ptr itself... This was caused by the FIR lowering relying on genExtAddrInInitializer to not place c_null_ptr inside an address. Fix this by only falling through into the extension handling code if this is an extension: i.e, the expression is some designated symbol or NULL().
1 parent ff5e2ba commit e91a4be

File tree

2 files changed

+49
-29
lines changed

2 files changed

+49
-29
lines changed

flang/lib/Lower/ConvertConstant.cpp

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -388,37 +388,38 @@ static mlir::Value genInlinedStructureCtorLitImpl(
388388

389389
// Special handling for scalar c_ptr/c_funptr constants. The array constant
390390
// must fall through to genConstantValue() below.
391-
if (Fortran::semantics::IsBuiltinCPtr(sym) && sym->Rank() == 0) {
392-
// Builtin c_ptr and c_funptr have special handling because initial
393-
// values are handled for them as an extension.
391+
if (Fortran::semantics::IsBuiltinCPtr(sym) && sym->Rank() == 0 &&
392+
(Fortran::evaluate::GetLastSymbol(expr.value()) ||
393+
Fortran::evaluate::IsNullPointer(expr.value()))) {
394+
// Builtin c_ptr and c_funptr have special handling because designators
395+
// and NULL() are handled as initial values for them as an extension
396+
// (otherwise only c_ptr_null/c_funptr_null are allowed and these are
397+
// replaced by structure constructors by semantics, so GetLastSymbol
398+
// returns nothing).
399+
400+
// The Ev::Expr is an initializer that is a pointer target (e.g., 'x' or
401+
// NULL()) that must be inserted into an intermediate cptr record value's
402+
// address field, which ought to be an intptr_t on the target.
394403
mlir::Value addr = fir::getBase(Fortran::lower::genExtAddrInInitializer(
395404
converter, loc, expr.value()));
396-
if (addr.getType() == componentTy) {
397-
// Do nothing. The Ev::Expr was returned as a value that can be
398-
// inserted directly to the component without an intermediary.
399-
} else {
400-
// The Ev::Expr returned is an initializer that is a pointer (e.g.,
401-
// null) that must be inserted into an intermediate cptr record
402-
// value's address field, which ought to be an intptr_t on the target.
403-
if (addr.getType().isa<fir::BoxProcType>())
404-
addr = builder.create<fir::BoxAddrOp>(loc, addr);
405-
assert((fir::isa_ref_type(addr.getType()) ||
406-
addr.getType().isa<mlir::FunctionType>()) &&
407-
"expect reference type for address field");
408-
assert(fir::isa_derived(componentTy) &&
409-
"expect C_PTR, C_FUNPTR to be a record");
410-
auto cPtrRecTy = componentTy.cast<fir::RecordType>();
411-
llvm::StringRef addrFieldName = Fortran::lower::builtin::cptrFieldName;
412-
mlir::Type addrFieldTy = cPtrRecTy.getType(addrFieldName);
413-
auto addrField = builder.create<fir::FieldIndexOp>(
414-
loc, fieldTy, addrFieldName, componentTy,
415-
/*typeParams=*/mlir::ValueRange{});
416-
mlir::Value castAddr = builder.createConvert(loc, addrFieldTy, addr);
417-
auto undef = builder.create<fir::UndefOp>(loc, componentTy);
418-
addr = builder.create<fir::InsertValueOp>(
419-
loc, componentTy, undef, castAddr,
420-
builder.getArrayAttr(addrField.getAttributes()));
421-
}
405+
if (addr.getType().isa<fir::BoxProcType>())
406+
addr = builder.create<fir::BoxAddrOp>(loc, addr);
407+
assert((fir::isa_ref_type(addr.getType()) ||
408+
addr.getType().isa<mlir::FunctionType>()) &&
409+
"expect reference type for address field");
410+
assert(fir::isa_derived(componentTy) &&
411+
"expect C_PTR, C_FUNPTR to be a record");
412+
auto cPtrRecTy = componentTy.cast<fir::RecordType>();
413+
llvm::StringRef addrFieldName = Fortran::lower::builtin::cptrFieldName;
414+
mlir::Type addrFieldTy = cPtrRecTy.getType(addrFieldName);
415+
auto addrField = builder.create<fir::FieldIndexOp>(
416+
loc, fieldTy, addrFieldName, componentTy,
417+
/*typeParams=*/mlir::ValueRange{});
418+
mlir::Value castAddr = builder.createConvert(loc, addrFieldTy, addr);
419+
auto undef = builder.create<fir::UndefOp>(loc, componentTy);
420+
addr = builder.create<fir::InsertValueOp>(
421+
loc, componentTy, undef, castAddr,
422+
builder.getArrayAttr(addrField.getAttributes()));
422423
res = builder.create<fir::InsertValueOp>(
423424
loc, recTy, res, addr, builder.getArrayAttr(field.getAttributes()));
424425
continue;

flang/test/HLFIR/c-null-ptr-init.f90

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
! Test lowering of C_NULL_PTR in structure constructor initial value.
2+
! RUN: bbc -emit-hlfir -o - %s | FileCheck %s
3+
subroutine test
4+
use, intrinsic :: iso_c_binding, only : c_ptr, c_null_ptr
5+
type t
6+
type(c_ptr) :: ptr
7+
end type t
8+
type(t) :: x = t(c_null_ptr)
9+
end subroutine
10+
! CHECK-LABEL: fir.global internal @_QFtestEx : !fir.type<_QFtestTt{ptr:!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>}> {
11+
! CHECK: %[[VAL_0:.*]] = fir.undefined !fir.type<_QFtestTt{ptr:!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>}>
12+
! CHECK: %[[VAL_1:.*]] = fir.field_index ptr, !fir.type<_QFtestTt{ptr:!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>}>
13+
! CHECK: %[[VAL_2:.*]] = fir.undefined !fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>
14+
! CHECK: %[[VAL_3:.*]] = fir.field_index __address, !fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>
15+
! CHECK: %[[VAL_4:.*]] = arith.constant 0 : i64
16+
! CHECK: %[[VAL_5:.*]] = fir.insert_value %[[VAL_2]], %[[VAL_4]], ["__address", !fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>] : (!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>, i64) -> !fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>
17+
! CHECK: %[[VAL_6:.*]] = fir.insert_value %[[VAL_0]], %[[VAL_5]], ["ptr", !fir.type<_QFtestTt{ptr:!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>}>] : (!fir.type<_QFtestTt{ptr:!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>}>, !fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>) -> !fir.type<_QFtestTt{ptr:!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>}>
18+
! CHECK: fir.has_value %[[VAL_6]] : !fir.type<_QFtestTt{ptr:!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>}>
19+
! CHECK: }

0 commit comments

Comments
 (0)