Skip to content

[flang] Source code location for IF statements and constructs #90853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 3, 2024
Merged

[flang] Source code location for IF statements and constructs #90853

merged 3 commits into from
May 3, 2024

Conversation

vdonaldson
Copy link
Contributor

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.

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.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels May 2, 2024
@llvmbot
Copy link
Member

llvmbot commented May 2, 2024

@llvm/pr-subscribers-flang-fir-hlfir

Author: None (vdonaldson)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/90853.diff

1 Files Affected:

  • (modified) flang/lib/Lower/Bridge.cpp (+10-6)
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index fb01789d3f8ae6..52c92a4e249904 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -2055,17 +2055,19 @@ class FirConverter : public Fortran::lower::AbstractConverter {
   /// Generate structured or unstructured FIR for an IF construct.
   /// The initial statement may be either an IfStmt or an IfThenStmt.
   void genFIR(const Fortran::parser::IfConstruct &) {
-    mlir::Location loc = toLocation();
     Fortran::lower::pft::Evaluation &eval = getEval();
+
+    // Structured fir.if nest.
     if (eval.lowerAsStructured()) {
-      // Structured fir.if nest.
       fir::IfOp topIfOp, currentIfOp;
       for (Fortran::lower::pft::Evaluation &e : eval.getNestedEvaluations()) {
         auto genIfOp = [&](mlir::Value cond) {
-          auto ifOp = builder->create<fir::IfOp>(loc, cond, /*withElse=*/true);
+          auto ifOp =
+              builder->create<fir::IfOp>(toLocation(), cond, /*withElse=*/true);
           builder->setInsertionPointToStart(&ifOp.getThenRegion().front());
           return ifOp;
         };
+        setCurrentPosition(e.position);
         if (auto *s = e.getIf<Fortran::parser::IfThenStmt>()) {
           topIfOp = currentIfOp = genIfOp(genIfCondition(s, e.negateCondition));
         } else if (auto *s = e.getIf<Fortran::parser::IfStmt>()) {
@@ -2096,6 +2098,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
         else // non-empty block
           genConditionalBranch(cond, e.lexicalSuccessor, e.controlSuccessor);
       };
+      setCurrentPosition(e.position);
       if (auto *s = e.getIf<Fortran::parser::IfThenStmt>()) {
         maybeStartBlock(e.block);
         genIfBranch(genIfCondition(s, e.negateCondition));
@@ -2863,6 +2866,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
     Fortran::lower::StatementContext stmtCtx;
     pushActiveConstruct(eval, stmtCtx);
     for (Fortran::lower::pft::Evaluation &e : eval.getNestedEvaluations()) {
+      setCurrentPosition(e.position);
       if (auto *stmt = e.getIf<Fortran::parser::AssociateStmt>()) {
         if (eval.lowerAsUnstructured())
           maybeStartBlock(e.block);
@@ -2891,10 +2895,10 @@ class FirConverter : public Fortran::lower::AbstractConverter {
     Fortran::lower::StatementContext stmtCtx;
     pushActiveConstruct(eval, stmtCtx);
     for (Fortran::lower::pft::Evaluation &e : eval.getNestedEvaluations()) {
+      setCurrentPosition(e.position);
       if (e.getIf<Fortran::parser::BlockStmt>()) {
         if (eval.lowerAsUnstructured())
           maybeStartBlock(e.block);
-        setCurrentPosition(e.position);
         const Fortran::parser::CharBlock &endPosition =
             eval.getLastNestedEvaluation().position;
         localSymbols.pushScope();
@@ -2921,7 +2925,6 @@ class FirConverter : public Fortran::lower::AbstractConverter {
       } else if (e.getIf<Fortran::parser::EndBlockStmt>()) {
         if (eval.lowerAsUnstructured())
           maybeStartBlock(e.block);
-        setCurrentPosition(e.position);
         localSymbols.popScope();
       } else {
         genFIR(e);
@@ -2963,7 +2966,6 @@ class FirConverter : public Fortran::lower::AbstractConverter {
   }
 
   void genFIR(const Fortran::parser::SelectTypeConstruct &selectTypeConstruct) {
-    mlir::Location loc = toLocation();
     mlir::MLIRContext *context = builder->getContext();
     Fortran::lower::StatementContext stmtCtx;
     fir::ExtendedValue selector;
@@ -2989,6 +2991,8 @@ class FirConverter : public Fortran::lower::AbstractConverter {
     pushActiveConstruct(getEval(), stmtCtx);
     for (Fortran::lower::pft::Evaluation &eval :
          getEval().getNestedEvaluations()) {
+      setCurrentPosition(eval.position);
+      mlir::Location loc = toLocation();
       if (auto *selectTypeStmt =
               eval.getIf<Fortran::parser::SelectTypeStmt>()) {
         // A genFIR(SelectTypeStmt) call would have unwanted side effects.

Copy link
Contributor

@psteinfeld psteinfeld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All builds and tests correctly. But Jean or Valentin should review before you submit since I don't know the code that well.

@clementval
Copy link
Contributor

Can we add a test like this one flang/test/Lower/OpenACC/locations.f90 to make sure it will not regress?

Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Val

vdonaldson added 2 commits May 2, 2024 14:40
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.
Copy link
Contributor

@clementval clementval left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the test Val!

@vdonaldson vdonaldson merged commit cda8270 into llvm:main May 3, 2024
@vdonaldson vdonaldson deleted the vkd1 branch May 13, 2024 18:35
@vdonaldson vdonaldson restored the vkd1 branch May 13, 2024 18:35
@vdonaldson vdonaldson deleted the vkd1 branch May 13, 2024 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants