[flang-commits] [flang] [flang] Enhance location information (PR #95862)

Valentin Clement バレンタイン クレメン via flang-commits flang-commits at lists.llvm.org
Mon Jun 17 16:10:36 PDT 2024


https://github.com/clementval created https://github.com/llvm/llvm-project/pull/95862

None

>From 9a1ac702916ff7160dab6b27465490162f4f7b59 Mon Sep 17 00:00:00 2001
From: Valentin Clement <clementval at gmail.com>
Date: Mon, 17 Jun 2024 13:08:25 -0700
Subject: [PATCH] [flang] Enhance location information

---
 flang/include/flang/Parser/provenance.h |  2 ++
 flang/lib/Lower/Bridge.cpp              | 41 ++++++++++++++++++++-----
 flang/lib/Parser/provenance.cpp         | 21 +++++++++++++
 flang/test/Lower/location.f90           | 11 +++++++
 flang/test/Lower/location0.inc          |  1 +
 flang/test/Lower/location1.inc          |  1 +
 6 files changed, 70 insertions(+), 7 deletions(-)
 create mode 100644 flang/test/Lower/location.f90
 create mode 100644 flang/test/Lower/location0.inc
 create mode 100644 flang/test/Lower/location1.inc

diff --git a/flang/include/flang/Parser/provenance.h b/flang/include/flang/Parser/provenance.h
index 42c5b3de2cbe2..01ac8b060d5f7 100644
--- a/flang/include/flang/Parser/provenance.h
+++ b/flang/include/flang/Parser/provenance.h
@@ -172,6 +172,8 @@ class AllSources {
   }
   void setShowColors(bool showColors) { showColors_ = showColors; }
   bool getShowColors() const { return showColors_; }
+  std::optional<ProvenanceRange> GetInclusionInfo(
+      const std::optional<ProvenanceRange> &) const;
   void EmitMessage(llvm::raw_ostream &, const std::optional<ProvenanceRange> &,
       const std::string &message, const std::string &prefix,
       llvm::raw_ostream::Colors color, bool echoSourceLine = false) const;
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index c73d43210a260..ca8b465f2e7b9 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -844,24 +844,51 @@ class FirConverter : public Fortran::lower::AbstractConverter {
     return mlir::UnknownLoc::get(&getMLIRContext());
   }
 
+  static mlir::Location genLocation(Fortran::parser::SourcePosition pos,
+                                    mlir::MLIRContext &ctx) {
+    llvm::SmallString<256> path(*pos.path);
+    llvm::sys::fs::make_absolute(path);
+    llvm::sys::path::remove_dots(path);
+    return mlir::FileLineColLoc::get(&ctx, path.str(), pos.line, pos.column);
+  }
+
   /// Generate a `Location` from the `CharBlock`.
   mlir::Location
   genLocation(const Fortran::parser::CharBlock &block) override final {
+    mlir::Location mainLocation = genUnknownLocation();
     if (const Fortran::parser::AllCookedSources *cooked =
             bridge.getCookedSource()) {
       if (std::optional<Fortran::parser::ProvenanceRange> provenance =
               cooked->GetProvenanceRange(block)) {
         if (std::optional<Fortran::parser::SourcePosition> filePos =
-                cooked->allSources().GetSourcePosition(provenance->start())) {
-          llvm::SmallString<256> filePath(*filePos->path);
-          llvm::sys::fs::make_absolute(filePath);
-          llvm::sys::path::remove_dots(filePath);
-          return mlir::FileLineColLoc::get(&getMLIRContext(), filePath.str(),
-                                           filePos->line, filePos->column);
+                cooked->allSources().GetSourcePosition(provenance->start()))
+          mainLocation = genLocation(*filePos, getMLIRContext());
+
+        llvm::SmallVector<mlir::Location> locs;
+        locs.push_back(mainLocation);
+
+        // Gather include location information if any.
+        Fortran::parser::ProvenanceRange *prov = &*provenance;
+        while (prov) {
+          if (std::optional<Fortran::parser::ProvenanceRange> include =
+                  cooked->allSources().GetInclusionInfo(*prov)) {
+            if (std::optional<Fortran::parser::SourcePosition> incPos =
+                    cooked->allSources().GetSourcePosition(include->start()))
+              locs.push_back(genLocation(*incPos, getMLIRContext()));
+            prov = &*include;
+          } else {
+            prov = nullptr;
+          }
+        }
+        if (locs.size() > 1) {
+          auto attr = mlir::IntegerAttr::get(
+              mlir::IntegerType::get(&getMLIRContext(), 32), 1);
+          return mlir::FusedLocWith<mlir::IntegerAttr>::get(&getMLIRContext(),
+                                                            locs, attr);
         }
       }
     }
-    return genUnknownLocation();
+    return mainLocation;
   }
 
   const Fortran::semantics::Scope &getCurrentScope() override final {
diff --git a/flang/lib/Parser/provenance.cpp b/flang/lib/Parser/provenance.cpp
index 6e2e7326e2167..31ed2380eb2af 100644
--- a/flang/lib/Parser/provenance.cpp
+++ b/flang/lib/Parser/provenance.cpp
@@ -246,6 +246,27 @@ static void EmitPrefix(llvm::raw_ostream &o, llvm::raw_ostream::Colors color,
   }
 }
 
+std::optional<ProvenanceRange> AllSources::GetInclusionInfo(
+    const std::optional<ProvenanceRange> &range) const {
+  if (!range)
+    return std::nullopt;
+  const Origin &origin{MapToOrigin(range->start())};
+
+  return common::visit(
+      common::visitors{
+          [&](const Inclusion &inc) -> std::optional<ProvenanceRange> {
+            if (IsValid(origin.replaces) &&
+                range_.Contains(origin.replaces.start()))
+              return origin.replaces;
+            return std::nullopt;
+          },
+          [&](const auto &) -> std::optional<ProvenanceRange> {
+            return std::nullopt;
+          },
+      },
+      origin.u);
+}
+
 void AllSources::EmitMessage(llvm::raw_ostream &o,
     const std::optional<ProvenanceRange> &range, const std::string &message,
     const std::string &prefix, llvm::raw_ostream::Colors color,
diff --git a/flang/test/Lower/location.f90 b/flang/test/Lower/location.f90
new file mode 100644
index 0000000000000..0e9d456bd4356
--- /dev/null
+++ b/flang/test/Lower/location.f90
@@ -0,0 +1,11 @@
+! RUN: bbc -emit-hlfir --mlir-print-debuginfo %s -o - | FileCheck %s
+
+program test
+include 'location0.inc'
+
+end 
+
+! CHECK-LABEL: func.func @_QQmain() attributes {fir.bindc_name = "test"} {
+! CHECK: fir.call @_FortranAioOutputAscii(%{{.*}}, %{{.*}}, %{{.*}}) fastmath<contract> : (!fir.ref<i8>, !fir.ref<i8>, i64) -> i1 loc(fused<1 : i32>["{{.*}}flang/test/Lower/location1.inc":1:8, "{{.*}}flang/test/Lower/location0.inc":1:1, "{{.*}}flang/test/Lower/location.f90":4:1])
+! CHECK: return loc("{{.*}}flang/test/Lower/location.f90":6:1)
+! CHECK: } loc("{{.*}}flang/test/Lower/location.f90":3:1)
diff --git a/flang/test/Lower/location0.inc b/flang/test/Lower/location0.inc
new file mode 100644
index 0000000000000..d46282c73c51e
--- /dev/null
+++ b/flang/test/Lower/location0.inc
@@ -0,0 +1 @@
+include 'location1.inc'
diff --git a/flang/test/Lower/location1.inc b/flang/test/Lower/location1.inc
new file mode 100644
index 0000000000000..d55fb01230af6
--- /dev/null
+++ b/flang/test/Lower/location1.inc
@@ -0,0 +1 @@
+print*,'from location.inc'



More information about the flang-commits mailing list