Skip to content

Commit 57e38bc

Browse files
committed
[flang][hlfir] Fixed lowering for optional dummy.
We have to keep it as a box, since taking box_addr of the optional box may be invalid. Reviewed By: jeanPerier Differential Revision: https://reviews.llvm.org/D149505
1 parent 6d667d4 commit 57e38bc

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

flang/include/flang/Optimizer/Builder/HLFIRTools.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ class Entity : public mlir::Value {
181181
return base.getDefiningOp<fir::FortranVariableOpInterface>();
182182
}
183183

184+
bool isOptional() const {
185+
auto varIface = getIfVariableInterface();
186+
return varIface ? varIface.isOptional() : false;
187+
}
188+
184189
// Get the entity as an mlir SSA value containing all the shape, type
185190
// parameters and dynamic shape information.
186191
mlir::Value getBase() const { return *this; }

flang/lib/Optimizer/Builder/HLFIRTools.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ translateVariableToExtendedValue(mlir::Location loc, fir::FirOpBuilder &builder,
791791

792792
if (firBase.getType().isa<fir::BaseBoxType>()) {
793793
if (!variable.isSimplyContiguous() || variable.isPolymorphic() ||
794-
variable.isDerivedWithLengthParameters()) {
794+
variable.isDerivedWithLengthParameters() || variable.isOptional()) {
795795
llvm::SmallVector<mlir::Value> nonDefaultLbounds =
796796
getNonDefaultLowerBounds(loc, builder, variable);
797797
return fir::BoxValue(firBase, nonDefaultLbounds,

flang/test/HLFIR/optional_dummy.f90

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
! RUN: bbc -emit-fir -hlfir %s -o - | FileCheck %s
2+
3+
! Check that the lowering does not generate fir.box_addr for
4+
! the optional box. It will cause segfault during execution.
5+
6+
! CHECK-LABEL: func.func @_QPtest(
7+
! CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "ext_buf", fir.contiguous, fir.optional}) {
8+
! CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {fortran_attrs = #fir.var_attrs<contiguous, optional>, uniq_name = "_QFtestEext_buf"} : (!fir.box<!fir.array<?xi32>>) -> (!fir.box<!fir.array<?xi32>>, !fir.box<!fir.array<?xi32>>)
9+
! CHECK: %[[VAL_2:.*]] = fir.is_present %[[VAL_1]]#1 : (!fir.box<!fir.array<?xi32>>) -> i1
10+
! CHECK: cf.cond_br %[[VAL_2]], ^bb1, ^bb2
11+
! CHECK: ^bb1:
12+
! CHECK: %[[VAL_3:.*]] = arith.constant 0 : i32
13+
! CHECK: %[[VAL_4:.*]] = arith.constant false
14+
! CHECK: %[[VAL_5:.*]] = arith.constant false
15+
! CHECK: %[[VAL_6:.*]] = fir.call @_FortranAStopStatement(%[[VAL_3]], %[[VAL_4]], %[[VAL_5]]) fastmath<contract> : (i32, i1, i1) -> none
16+
! CHECK: fir.unreachable
17+
! CHECK: ^bb2:
18+
! CHECK: cf.br ^bb3
19+
! CHECK: ^bb3:
20+
! CHECK: return
21+
! CHECK: }
22+
subroutine test(ext_buf)
23+
integer, contiguous, optional :: ext_buf(:)
24+
if (present(ext_buf)) then
25+
stop
26+
endif
27+
return
28+
end subroutine test

0 commit comments

Comments
 (0)