[flang-commits] [flang] [Flang] Fix symbol name clash when dummy procedure name is the same as common-block-name (PR #155508)

Carlos Seo via flang-commits flang-commits at lists.llvm.org
Wed Sep 3 10:54:53 PDT 2025


https://github.com/ceseo updated https://github.com/llvm/llvm-project/pull/155508

>From b4c91f12e8a1ff0b9324f8aaaba3a5264ed618ac Mon Sep 17 00:00:00 2001
From: Carlos Seo <carlos.seo at linaro.org>
Date: Wed, 3 Sep 2025 17:49:19 +0000
Subject: [PATCH] [Flang] Fix symbol name clash when dummy procedure name is
 the same as common-block-name

Dummy procedures in interface blocks are not external procedures that need to be linked. Do not externalize those.

Fixes #122822
---
 .../Transforms/ExternalNameConversion.cpp     | 37 +++++++++++++++++++
 .../dummy-procedure-common-block-name.f       | 12 ++++++
 2 files changed, 49 insertions(+)
 create mode 100644 flang/test/Transforms/dummy-procedure-common-block-name.f

diff --git a/flang/lib/Optimizer/Transforms/ExternalNameConversion.cpp b/flang/lib/Optimizer/Transforms/ExternalNameConversion.cpp
index 2fcff87fdc393..031a5aeb28d71 100644
--- a/flang/lib/Optimizer/Transforms/ExternalNameConversion.cpp
+++ b/flang/lib/Optimizer/Transforms/ExternalNameConversion.cpp
@@ -76,12 +76,49 @@ void ExternalNameConversionPass::runOnOperation() {
   auto *context = &getContext();
 
   llvm::DenseMap<mlir::StringAttr, mlir::FlatSymbolRefAttr> remappings;
+  mlir::SymbolTable symbolTable(op);
 
   auto processFctOrGlobal = [&](mlir::Operation &funcOrGlobal) {
     auto symName = funcOrGlobal.getAttrOfType<mlir::StringAttr>(
         mlir::SymbolTable::getSymbolAttrName());
     auto deconstructedName = fir::NameUniquer::deconstruct(symName);
     if (fir::NameUniquer::isExternalFacingUniquedName(deconstructedName)) {
+      // Check if this is a private function that would conflict with a common
+      // block and get its mangled name.
+      if (auto funcOp = llvm::dyn_cast<mlir::func::FuncOp>(funcOrGlobal)) {
+        if (funcOp.isPrivate()) {
+          std::string mangledName =
+              mangleExternalName(deconstructedName, appendUnderscoreOpt);
+          auto mod = funcOp->getParentOfType<mlir::ModuleOp>();
+          bool hasConflictingCommonBlock = false;
+
+          // Check if any existing global has the same mangled name.
+          if (symbolTable.lookup<fir::GlobalOp>(mangledName))
+            hasConflictingCommonBlock = true;
+
+          // Skip externalization if the function has a conflicting common block
+          // and is not directly called (i.e. procedure pointers or type
+          // specifications)
+          if (hasConflictingCommonBlock) {
+            bool isDirectlyCalled = false;
+            std::optional<SymbolTable::UseRange> uses =
+                funcOp.getSymbolUses(mod);
+            if (uses.has_value()) {
+              for (auto use : *uses) {
+                mlir::Operation *user = use.getUser();
+                if (mlir::isa<fir::CallOp>(user) ||
+                    mlir::isa<mlir::func::CallOp>(user)) {
+                  isDirectlyCalled = true;
+                  break;
+                }
+              }
+            }
+            if (!isDirectlyCalled)
+              return;
+          }
+        }
+      }
+
       auto newName = mangleExternalName(deconstructedName, appendUnderscoreOpt);
       auto newAttr = mlir::StringAttr::get(context, newName);
       mlir::SymbolTable::setSymbolName(&funcOrGlobal, newAttr);
diff --git a/flang/test/Transforms/dummy-procedure-common-block-name.f b/flang/test/Transforms/dummy-procedure-common-block-name.f
new file mode 100644
index 0000000000000..2c3ebb965fe49
--- /dev/null
+++ b/flang/test/Transforms/dummy-procedure-common-block-name.f
@@ -0,0 +1,12 @@
+! RUN: bbc -emit-fir %s -o - | FileCheck %s
+
+subroutine ss5()
+common /com_dummy1/ x
+! CHECK: fir.global common @com_dummy1_
+interface
+    subroutine com_dummy1()
+    end subroutine
+end interface
+! CHECK: func.func private @_QPcom_dummy1()
+print *,fun_sub(com_dummy1)
+end



More information about the flang-commits mailing list