Skip to content

Commit cda8270

Browse files
authored
[flang] Source code location for IF statements and constructs (#90853)
Make source code locations for IF statements and IF construct component statements more accurate. Make similar changes to ASSOCIATE, BLOCK, and SELECT TYPE construct component statements.
1 parent 9c8b0d4 commit cda8270

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

flang/lib/Lower/Bridge.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,17 +2055,19 @@ class FirConverter : public Fortran::lower::AbstractConverter {
20552055
/// Generate structured or unstructured FIR for an IF construct.
20562056
/// The initial statement may be either an IfStmt or an IfThenStmt.
20572057
void genFIR(const Fortran::parser::IfConstruct &) {
2058-
mlir::Location loc = toLocation();
20592058
Fortran::lower::pft::Evaluation &eval = getEval();
2059+
2060+
// Structured fir.if nest.
20602061
if (eval.lowerAsStructured()) {
2061-
// Structured fir.if nest.
20622062
fir::IfOp topIfOp, currentIfOp;
20632063
for (Fortran::lower::pft::Evaluation &e : eval.getNestedEvaluations()) {
20642064
auto genIfOp = [&](mlir::Value cond) {
2065-
auto ifOp = builder->create<fir::IfOp>(loc, cond, /*withElse=*/true);
2065+
auto ifOp =
2066+
builder->create<fir::IfOp>(toLocation(), cond, /*withElse=*/true);
20662067
builder->setInsertionPointToStart(&ifOp.getThenRegion().front());
20672068
return ifOp;
20682069
};
2070+
setCurrentPosition(e.position);
20692071
if (auto *s = e.getIf<Fortran::parser::IfThenStmt>()) {
20702072
topIfOp = currentIfOp = genIfOp(genIfCondition(s, e.negateCondition));
20712073
} else if (auto *s = e.getIf<Fortran::parser::IfStmt>()) {
@@ -2096,6 +2098,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
20962098
else // non-empty block
20972099
genConditionalBranch(cond, e.lexicalSuccessor, e.controlSuccessor);
20982100
};
2101+
setCurrentPosition(e.position);
20992102
if (auto *s = e.getIf<Fortran::parser::IfThenStmt>()) {
21002103
maybeStartBlock(e.block);
21012104
genIfBranch(genIfCondition(s, e.negateCondition));
@@ -2863,6 +2866,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
28632866
Fortran::lower::StatementContext stmtCtx;
28642867
pushActiveConstruct(eval, stmtCtx);
28652868
for (Fortran::lower::pft::Evaluation &e : eval.getNestedEvaluations()) {
2869+
setCurrentPosition(e.position);
28662870
if (auto *stmt = e.getIf<Fortran::parser::AssociateStmt>()) {
28672871
if (eval.lowerAsUnstructured())
28682872
maybeStartBlock(e.block);
@@ -2891,10 +2895,10 @@ class FirConverter : public Fortran::lower::AbstractConverter {
28912895
Fortran::lower::StatementContext stmtCtx;
28922896
pushActiveConstruct(eval, stmtCtx);
28932897
for (Fortran::lower::pft::Evaluation &e : eval.getNestedEvaluations()) {
2898+
setCurrentPosition(e.position);
28942899
if (e.getIf<Fortran::parser::BlockStmt>()) {
28952900
if (eval.lowerAsUnstructured())
28962901
maybeStartBlock(e.block);
2897-
setCurrentPosition(e.position);
28982902
const Fortran::parser::CharBlock &endPosition =
28992903
eval.getLastNestedEvaluation().position;
29002904
localSymbols.pushScope();
@@ -2921,7 +2925,6 @@ class FirConverter : public Fortran::lower::AbstractConverter {
29212925
} else if (e.getIf<Fortran::parser::EndBlockStmt>()) {
29222926
if (eval.lowerAsUnstructured())
29232927
maybeStartBlock(e.block);
2924-
setCurrentPosition(e.position);
29252928
localSymbols.popScope();
29262929
} else {
29272930
genFIR(e);
@@ -2963,7 +2966,6 @@ class FirConverter : public Fortran::lower::AbstractConverter {
29632966
}
29642967

29652968
void genFIR(const Fortran::parser::SelectTypeConstruct &selectTypeConstruct) {
2966-
mlir::Location loc = toLocation();
29672969
mlir::MLIRContext *context = builder->getContext();
29682970
Fortran::lower::StatementContext stmtCtx;
29692971
fir::ExtendedValue selector;
@@ -2989,6 +2991,8 @@ class FirConverter : public Fortran::lower::AbstractConverter {
29892991
pushActiveConstruct(getEval(), stmtCtx);
29902992
for (Fortran::lower::pft::Evaluation &eval :
29912993
getEval().getNestedEvaluations()) {
2994+
setCurrentPosition(eval.position);
2995+
mlir::Location loc = toLocation();
29922996
if (auto *selectTypeStmt =
29932997
eval.getIf<Fortran::parser::SelectTypeStmt>()) {
29942998
// A genFIR(SelectTypeStmt) call would have unwanted side effects.

flang/test/Lower/if-loc.f90

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
! RUN: bbc -emit-hlfir -mlir-print-debuginfo -o - %s | FileCheck %s
2+
3+
integer :: n = 0, x = 1
4+
if (x .ne. 1) goto 9
5+
n = n + 1
6+
if (x .gt. 1) goto 9
7+
n = n + 1
8+
9 print *, 'n =', n
9+
end
10+
11+
! CHECK-LABEL: c.func @_QQmain
12+
! CHECK: %[[V_0:[0-9]+]] = fir.address_of(@_QFEn) : !fir.ref<i32> loc("{{.*}}if-loc.f90":3:
13+
! CHECK: %[[V_1:[0-9]+]]:2 = hlfir.declare %[[V_0]] {uniq_name = "_QFEn"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>) loc("{{.*}}if-loc.f90":3:
14+
! CHECK: %[[V_2:[0-9]+]] = fir.address_of(@_QFEx) : !fir.ref<i32> loc("{{.*}}if-loc.f90":3:
15+
! CHECK: %[[V_3:[0-9]+]]:2 = hlfir.declare %[[V_2]] {uniq_name = "_QFEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>) loc("{{.*}}if-loc.f90":3:
16+
! CHECK: %[[V_4:[0-9]+]] = fir.load %[[V_3]]#0 : !fir.ref<i32> loc("{{.*}}if-loc.f90":4:
17+
! CHECK: %[[V_5:[0-9]+]] = arith.cmpi ne, %[[V_4]], %c1{{.*}} : i32 loc("{{.*}}if-loc.f90":4:
18+
! CHECK: %[[V_6:[0-9]+]] = arith.xori %[[V_5]], %true{{[_0-9]*}} : i1 loc("{{.*}}if-loc.f90":4:
19+
! CHECK: fir.if %[[V_6]] {
20+
! CHECK: %[[V_18:[0-9]+]] = fir.load %[[V_1]]#0 : !fir.ref<i32> loc("{{.*}}if-loc.f90":5:
21+
! CHECK: %[[V_19:[0-9]+]] = arith.addi %[[V_18]], %c1{{.*}} : i32 loc("{{.*}}if-loc.f90":5:
22+
! CHECK: hlfir.assign %[[V_19]] to %[[V_1]]#0 : i32, !fir.ref<i32> loc("{{.*}}if-loc.f90":5:
23+
! CHECK: %[[V_20:[0-9]+]] = fir.load %[[V_3]]#0 : !fir.ref<i32> loc("{{.*}}if-loc.f90":6:
24+
! CHECK: %[[V_21:[0-9]+]] = arith.cmpi sgt, %[[V_20]], %c1{{.*}} : i32 loc("{{.*}}if-loc.f90":6:
25+
! CHECK: %[[V_22:[0-9]+]] = arith.xori %[[V_21]], %true{{[_0-9]*}} : i1 loc("{{.*}}if-loc.f90":6:
26+
! CHECK: fir.if %[[V_22]] {
27+
! CHECK: %[[V_23:[0-9]+]] = fir.load %[[V_1]]#0 : !fir.ref<i32> loc("{{.*}}if-loc.f90":7:
28+
! CHECK: %[[V_24:[0-9]+]] = arith.addi %[[V_23]], %c1{{.*}} : i32 loc("{{.*}}if-loc.f90":7:
29+
! CHECK: hlfir.assign %[[V_24]] to %[[V_1]]#0 : i32, !fir.ref<i32> loc("{{.*}}if-loc.f90":7:
30+
! CHECK: }
31+
! CHECK: }
32+
! CHECK: %[[V_9:[0-9]+]] = fir.call @_FortranAioBeginExternalListOutput{{.*}} loc("{{.*}}if-loc.f90":8:
33+
! CHECK: return loc("{{.*}}if-loc.f90":9:
34+
! CHECK:}

0 commit comments

Comments
 (0)