[flang-commits] [flang] [flang][lowering] propagate location info of macro expansions (PR #67446)

via flang-commits flang-commits at lists.llvm.org
Tue Sep 26 08:43:55 PDT 2023


https://github.com/jeanPerier created https://github.com/llvm/llvm-project/pull/67446

Currently flang-new -g is failing when compiling code containing a call in a macro to a function defined in the same file.

The verification added in https://reviews.llvm.org/D157447 is valid, flang lowering was failing to propagate location information in code from macro expansion because GetSourcePositionRange does not work with them (it fails to come with an end location), but we do not need a range for the MLIR location, only the start.

Use GetSourcePosition instead that works with code from macro expansion.

Note that the source location is the one of the statement where the macro appeared, if needed some FusedLocation could be later built to keep a link to the macro location in the debug info.

>From 27da6903fa35845b0385e3c3536be9299397388b Mon Sep 17 00:00:00 2001
From: Jean Perier <jperier at nvidia.com>
Date: Tue, 26 Sep 2023 08:32:56 -0700
Subject: [PATCH] [flang][lowering] propagate location info of macro expansions

Currently flang-new -g was failing when compiling code containing
a call in a macro to a function defined in the same file.
The verification added in https://reviews.llvm.org/D157447 is valid,
flang lowering was failing to propagate location information in code
from macro expansion because GetSourcePositionRange does not work with
them (it fails to come with an end location), but we do not need
a range for the MLIR location, only the start. So use GetSourcePosition
that works with code from macro expansion.

Note that the source location is the one of the statement where the
macro appeared, if needed some FusedLocation could be later built
to keep a link to the macro location in the debug info.
---
 flang/lib/Lower/Bridge.cpp                | 20 ++++++++++----------
 flang/test/Lower/macro-debug-file-loc.f90 | 13 +++++++++++++
 2 files changed, 23 insertions(+), 10 deletions(-)
 create mode 100644 flang/test/Lower/macro-debug-file-loc.f90

diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index 2fb3c2c3818d42d..ee838b3b4a546b9 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -809,16 +809,16 @@ class FirConverter : public Fortran::lower::AbstractConverter {
   genLocation(const Fortran::parser::CharBlock &block) override final {
     if (const Fortran::parser::AllCookedSources *cooked =
             bridge.getCookedSource()) {
-      if (std::optional<std::pair<Fortran::parser::SourcePosition,
-                                  Fortran::parser::SourcePosition>>
-              loc = cooked->GetSourcePositionRange(block)) {
-        // loc is a pair (begin, end); use the beginning position
-        Fortran::parser::SourcePosition &filePos = loc->first;
-        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);
+      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);
+        }
       }
     }
     return genUnknownLocation();
diff --git a/flang/test/Lower/macro-debug-file-loc.f90 b/flang/test/Lower/macro-debug-file-loc.f90
new file mode 100644
index 000000000000000..a47c9aae178530e
--- /dev/null
+++ b/flang/test/Lower/macro-debug-file-loc.f90
@@ -0,0 +1,13 @@
+! Test that the expanded macros have the location information
+! RUN: %flang_fc1 -mmlir --mlir-print-debuginfo -emit-fir -o - %s | FileCheck %s
+
+#define CMD(fname) fname()
+
+subroutine foo()
+end subroutine
+
+subroutine test()
+  ! CHECK: fir.call @_QPfoo() fastmath<contract> : () -> () loc(#[[CALL_LOC:.*]])
+  call CMD(foo)
+end subroutine
+! CHECK: #[[CALL_LOC]] = loc("{{.*}}macro-debug-file-loc.f90":11:3)



More information about the flang-commits mailing list