[flang-commits] [flang] [flang][debug] Avoid redundant module info. (PR #161542)

Abid Qadeer via flang-commits flang-commits at lists.llvm.org
Thu Oct 2 08:41:36 PDT 2025


https://github.com/abidh updated https://github.com/llvm/llvm-project/pull/161542

>From a21aa474f123f0904e7b23c4db83f87d5e2ca02a Mon Sep 17 00:00:00 2001
From: Abid Qadeer <haqadeer at amd.com>
Date: Wed, 1 Oct 2025 16:45:35 +0100
Subject: [PATCH 1/2] [flang][debug] Avoid redundant module info.

Fixes https://github.com/llvm/llvm-project/issues/160907.

When a module is just being used (imported) and not defined, we generate
it with decl=true. But if the file/line fields are valid, the module is
not merged with the original and is considered different. This patch
avoids setting file/line/scope in such cases.
---
 flang/lib/Optimizer/Transforms/AddDebugInfo.cpp |  5 +++--
 flang/test/Transforms/debug-module-3.fir        | 13 +++++++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)
 create mode 100644 flang/test/Transforms/debug-module-3.fir

diff --git a/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp b/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp
index bdf7e4a366cf1..2c3c71a595950 100644
--- a/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp
+++ b/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp
@@ -286,10 +286,11 @@ mlir::LLVM::DIModuleAttr AddDebugInfoPass::getOrCreateModuleAttr(
     modAttr = iter->getValue();
   } else {
     modAttr = mlir::LLVM::DIModuleAttr::get(
-        context, fileAttr, scope, mlir::StringAttr::get(context, name),
+        context, decl ? nullptr : fileAttr, decl ? nullptr : scope,
+        mlir::StringAttr::get(context, name),
         /* configMacros */ mlir::StringAttr(),
         /* includePath */ mlir::StringAttr(),
-        /* apinotes */ mlir::StringAttr(), line, decl);
+        /* apinotes */ mlir::StringAttr(), decl ? 0 : line, decl);
     moduleMap[name] = modAttr;
   }
   return modAttr;
diff --git a/flang/test/Transforms/debug-module-3.fir b/flang/test/Transforms/debug-module-3.fir
new file mode 100644
index 0000000000000..03cc21ea4faa4
--- /dev/null
+++ b/flang/test/Transforms/debug-module-3.fir
@@ -0,0 +1,13 @@
+// RUN: fir-opt --add-debug-info --mlir-print-debuginfo %s | FileCheck %s
+
+module {
+  func.func @_QQmain() {
+    %2 = fir.address_of(@_QMmodEvar1) : !fir.ref<i32> loc(#loc1)
+    %3 = fircg.ext_declare %2 {uniq_name = "_QMmodEvar1"} : (!fir.ref<i32>) -> !fir.ref<i32> loc(#loc1)
+    return
+  } loc(#loc1)
+  fir.global @_QMmodEvar1 : i32 loc(#loc1)
+}
+#loc1 = loc("test1.f90":1:0)
+
+// CHECK: #llvm.di_module<name = "mod", isDecl = true>

>From 669246848aac55bf0ab2e0429d11d3d37a34005f Mon Sep 17 00:00:00 2001
From: Abid Qadeer <haqadeer at amd.com>
Date: Thu, 2 Oct 2025 16:41:07 +0100
Subject: [PATCH 2/2] Handle review comments.

Add comments to explain why certain fields are not set when decl is true.
---
 flang/lib/Optimizer/Transforms/AddDebugInfo.cpp | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp b/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp
index 2c3c71a595950..e006d2e878fd8 100644
--- a/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp
+++ b/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp
@@ -285,6 +285,10 @@ mlir::LLVM::DIModuleAttr AddDebugInfoPass::getOrCreateModuleAttr(
   if (auto iter{moduleMap.find(name)}; iter != moduleMap.end()) {
     modAttr = iter->getValue();
   } else {
+    // When decl is true, it means that module is only being used in this
+    // compilation unit and it is defined elsewhere. But if the file/line/scope
+    // fields are valid, the module is not merged with its definition and is
+    // considered different. So we only set those fields when decl is false.
     modAttr = mlir::LLVM::DIModuleAttr::get(
         context, decl ? nullptr : fileAttr, decl ? nullptr : scope,
         mlir::StringAttr::get(context, name),



More information about the flang-commits mailing list