Skip to content

Commit 266d65c

Browse files
committed
[flang][hlfir] Fixed boxchar variables emboxing.
We have to unbox the boxchar variables in order to get to the raw address that can be used for emboxing. Reviewed By: jeanPerier Differential Revision: https://reviews.llvm.org/D149473
1 parent 57e38bc commit 266d65c

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

flang/lib/Optimizer/Builder/HLFIRTools.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ mlir::Value hlfir::genVariableRawAddress(mlir::Location loc,
270270
if (var.isMutableBox())
271271
baseAddr = builder.create<fir::LoadOp>(loc, baseAddr);
272272
// Get raw address.
273+
if (var.getType().isa<fir::BoxCharType>())
274+
baseAddr = genUnboxChar(loc, builder, var.getBase()).getAddr();
273275
if (baseAddr.getType().isa<fir::BaseBoxType>())
274276
baseAddr = builder.create<fir::BoxAddrOp>(loc, baseAddr);
275277
return baseAddr;
@@ -310,9 +312,12 @@ hlfir::Entity hlfir::genVariableBox(mlir::Location loc,
310312
var.getFortranElementType().dyn_cast<fir::CharacterType>();
311313
if (!maybeCharType || maybeCharType.hasDynamicLen())
312314
hlfir::genLengthParameters(loc, builder, var, typeParams);
315+
mlir::Value addr = var.getBase();
316+
if (var.getType().isa<fir::BoxCharType>())
317+
addr = genVariableRawAddress(loc, builder, var);
313318
mlir::Type boxType = fir::BoxType::get(var.getElementOrSequenceType());
314319
auto embox =
315-
builder.create<fir::EmboxOp>(loc, boxType, var, shape,
320+
builder.create<fir::EmboxOp>(loc, boxType, addr, shape,
316321
/*slice=*/mlir::Value{}, typeParams);
317322
return hlfir::Entity{embox.getResult()};
318323
}

flang/test/HLFIR/boxchar_emboxing.f90

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
! RUN: bbc -polymorphic-type -emit-fir -hlfir %s -o - | FileCheck %s
2+
3+
! CHECK-LABEL: func.func @_QPtest1(
4+
! CHECK-SAME: %[[VAL_0:.*]]: !fir.class<none> {fir.bindc_name = "x"}) {
5+
! CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFtest1Ex"} : (!fir.class<none>) -> (!fir.class<none>, !fir.class<none>)
6+
! CHECK: fir.select_type %[[VAL_1]]#1 : !fir.class<none> [#fir.type_is<!fir.char<1,?>>, ^bb1, unit, ^bb2]
7+
! CHECK: ^bb1:
8+
! CHECK: %[[VAL_2:.*]] = fir.box_addr %[[VAL_1]]#1 : (!fir.class<none>) -> !fir.ref<!fir.char<1,?>>
9+
! CHECK: %[[VAL_3:.*]] = fir.box_elesize %[[VAL_1]]#1 : (!fir.class<none>) -> index
10+
! CHECK: %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_2]] typeparams %[[VAL_3]] {uniq_name = "_QFtest1Ex"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
11+
! CHECK: %[[VAL_5:.*]]:2 = fir.unboxchar %[[VAL_4]]#0 : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
12+
! CHECK: %[[VAL_6:.*]] = fir.embox %[[VAL_5]]#0 typeparams %[[VAL_3]] : (!fir.ref<!fir.char<1,?>>, index) -> !fir.box<!fir.char<1,?>>
13+
! CHECK: %[[VAL_7:.*]] = fir.rebox %[[VAL_6]] : (!fir.box<!fir.char<1,?>>) -> !fir.class<none>
14+
! CHECK: fir.call @_QPprint(%[[VAL_7]]) fastmath<contract> : (!fir.class<none>) -> ()
15+
! CHECK: cf.br ^bb3
16+
! CHECK: ^bb2:
17+
! CHECK: %[[VAL_8:.*]]:2 = hlfir.declare %[[VAL_1]]#1 {uniq_name = "_QFtest1Ex"} : (!fir.class<none>) -> (!fir.class<none>, !fir.class<none>)
18+
! CHECK: %[[VAL_9:.*]] = fir.address_of(@_QQcl.4641494C) : !fir.ref<!fir.char<1,4>>
19+
! CHECK: %[[VAL_10:.*]] = arith.constant 4 : index
20+
! CHECK: %[[VAL_11:.*]]:2 = hlfir.declare %[[VAL_9]] typeparams %[[VAL_10]] {fortran_attrs = #fir.var_attrs<parameter>, uniq_name = "_QQcl.4641494C"} : (!fir.ref<!fir.char<1,4>>, index) -> (!fir.ref<!fir.char<1,4>>, !fir.ref<!fir.char<1,4>>)
21+
! CHECK: %[[VAL_12:.*]] = fir.convert %[[VAL_11]]#1 : (!fir.ref<!fir.char<1,4>>) -> !fir.ref<i8>
22+
! CHECK: %[[VAL_13:.*]] = fir.convert %[[VAL_10]] : (index) -> i64
23+
! CHECK: %[[VAL_14:.*]] = arith.constant false
24+
! CHECK: %[[VAL_15:.*]] = arith.constant false
25+
! CHECK: %[[VAL_16:.*]] = fir.call @_FortranAStopStatementText(%[[VAL_12]], %[[VAL_13]], %[[VAL_14]], %[[VAL_15]]) fastmath<contract> : (!fir.ref<i8>, i64, i1, i1) -> none
26+
! CHECK: fir.unreachable
27+
! CHECK: ^bb3:
28+
! CHECK: return
29+
! CHECK: }
30+
subroutine test1(x)
31+
interface
32+
subroutine print(x)
33+
class(*) x
34+
end subroutine print
35+
end interface
36+
class(*) x
37+
select type(x)
38+
type is (Character(*))
39+
call print(x)
40+
class Default
41+
stop 'FAIL'
42+
end select
43+
end subroutine test1
44+
45+
! CHECK-LABEL: func.func @_QPtest2(
46+
! CHECK-SAME: %[[VAL_0:.*]]: !fir.class<!fir.array<10xnone>> {fir.bindc_name = "x"}) {
47+
! CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFtest2Ex"} : (!fir.class<!fir.array<10xnone>>) -> (!fir.class<!fir.array<10xnone>>, !fir.class<!fir.array<10xnone>>)
48+
! CHECK: fir.select_type %[[VAL_1]]#1 : !fir.class<!fir.array<10xnone>> [#fir.type_is<!fir.char<1,?>>, ^bb1, unit, ^bb2]
49+
! CHECK: ^bb1:
50+
! CHECK: %[[VAL_2:.*]] = fir.convert %[[VAL_1]]#1 : (!fir.class<!fir.array<10xnone>>) -> !fir.box<!fir.array<10x!fir.char<1,?>>>
51+
! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %[[VAL_2]] {uniq_name = "_QFtest2Ex"} : (!fir.box<!fir.array<10x!fir.char<1,?>>>) -> (!fir.box<!fir.array<10x!fir.char<1,?>>>, !fir.box<!fir.array<10x!fir.char<1,?>>>)
52+
! CHECK: %[[VAL_4:.*]] = fir.box_elesize %[[VAL_3]]#1 : (!fir.box<!fir.array<10x!fir.char<1,?>>>) -> index
53+
! CHECK: %[[VAL_5:.*]] = arith.constant 1 : index
54+
! CHECK: %[[VAL_6:.*]] = hlfir.designate %[[VAL_3]]#0 (%[[VAL_5]]) typeparams %[[VAL_4]] : (!fir.box<!fir.array<10x!fir.char<1,?>>>, index, index) -> !fir.boxchar<1>
55+
! CHECK: %[[VAL_7:.*]]:2 = fir.unboxchar %[[VAL_6]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
56+
! CHECK: %[[VAL_8:.*]] = fir.embox %[[VAL_7]]#0 typeparams %[[VAL_4]] : (!fir.ref<!fir.char<1,?>>, index) -> !fir.box<!fir.char<1,?>>
57+
! CHECK: %[[VAL_9:.*]] = fir.rebox %[[VAL_8]] : (!fir.box<!fir.char<1,?>>) -> !fir.class<none>
58+
! CHECK: fir.call @_QPprint(%[[VAL_9]]) fastmath<contract> : (!fir.class<none>) -> ()
59+
! CHECK: cf.br ^bb3
60+
! CHECK: ^bb2:
61+
! CHECK: %[[VAL_10:.*]]:2 = hlfir.declare %[[VAL_1]]#1 {uniq_name = "_QFtest2Ex"} : (!fir.class<!fir.array<10xnone>>) -> (!fir.class<!fir.array<10xnone>>, !fir.class<!fir.array<10xnone>>)
62+
! CHECK: %[[VAL_11:.*]] = fir.address_of(@_QQcl.4641494C) : !fir.ref<!fir.char<1,4>>
63+
! CHECK: %[[VAL_12:.*]] = arith.constant 4 : index
64+
! CHECK: %[[VAL_13:.*]]:2 = hlfir.declare %[[VAL_11]] typeparams %[[VAL_12]] {fortran_attrs = #fir.var_attrs<parameter>, uniq_name = "_QQcl.4641494C"} : (!fir.ref<!fir.char<1,4>>, index) -> (!fir.ref<!fir.char<1,4>>, !fir.ref<!fir.char<1,4>>)
65+
! CHECK: %[[VAL_14:.*]] = fir.convert %[[VAL_13]]#1 : (!fir.ref<!fir.char<1,4>>) -> !fir.ref<i8>
66+
! CHECK: %[[VAL_15:.*]] = fir.convert %[[VAL_12]] : (index) -> i64
67+
! CHECK: %[[VAL_16:.*]] = arith.constant false
68+
! CHECK: %[[VAL_17:.*]] = arith.constant false
69+
! CHECK: %[[VAL_18:.*]] = fir.call @_FortranAStopStatementText(%[[VAL_14]], %[[VAL_15]], %[[VAL_16]], %[[VAL_17]]) fastmath<contract> : (!fir.ref<i8>, i64, i1, i1) -> none
70+
! CHECK: fir.unreachable
71+
! CHECK: ^bb3:
72+
! CHECK: return
73+
! CHECK: }
74+
subroutine test2(x)
75+
interface
76+
subroutine print(x)
77+
class(*) x
78+
end subroutine print
79+
end interface
80+
class(*) x(10)
81+
select type(x)
82+
type is (Character(*))
83+
call print(x(1))
84+
class Default
85+
stop 'FAIL'
86+
end select
87+
end subroutine test2

0 commit comments

Comments
 (0)