Skip to content

Conversation

abidh
Copy link
Contributor

@abidh abidh commented Oct 1, 2025

Fixes #160907.

When a module is just being used 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.

Fixes llvm#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.
@abidh abidh requested review from tblah, jeanPerier and kiranktp October 1, 2025 15:55
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Oct 1, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 1, 2025

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

Author: Abid Qadeer (abidh)

Changes

Fixes #160907.

When a module is just being used 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.


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

2 Files Affected:

  • (modified) flang/lib/Optimizer/Transforms/AddDebugInfo.cpp (+3-2)
  • (added) flang/test/Transforms/debug-module-3.fir (+13)
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>

Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

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

LGTM with one nit

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

Add comments to explain why certain fields are not set when decl is true.
@abidh abidh merged commit 6c40c76 into llvm:main Oct 3, 2025
9 checks passed
MixedMatched pushed a commit to MixedMatched/llvm-project that referenced this pull request Oct 3, 2025
Fixes llvm#160907.

When a module is just being used 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.
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.

[flang][debug] Duplicate module information.
4 participants