[llvm] [GlobalOpt] Update debug info when changing CC to Fast (PR #144303)

Maurice Heumann via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 15 23:34:46 PDT 2025


https://github.com/momo5502 created https://github.com/llvm/llvm-project/pull/144303

Changing the CC of local functions to `fastcc` in GlobalOpt causes the PDB to misalign.
Updating the debug info is requried to reflect the change in the PDB.

This fixes #144301

>From a8a00848e117664b7c6123a0303444db829b89bb Mon Sep 17 00:00:00 2001
From: momo5502 <mauriceheumann at gmail.com>
Date: Mon, 16 Jun 2025 08:27:57 +0200
Subject: [PATCH] Update debug info when changing CC to Fast

This fixes #144301
---
 llvm/lib/Transforms/IPO/GlobalOpt.cpp | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
index 7db0586386506..b0f1dee415efd 100644
--- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -1920,6 +1920,14 @@ static void RemovePreallocated(Function *F) {
   }
 }
 
+static unsigned char GetDebugInfoFastCC(const Triple &Triple) {
+  if (Triple.isOSWindows() && Triple.isArch32Bit()) {
+    return llvm::dwarf::DW_CC_BORLAND_msfastcall;
+  }
+
+  return llvm::dwarf::DW_CC_normal;
+}
+
 static bool
 OptimizeFunctions(Module &M,
                   function_ref<TargetLibraryInfo &(Function &)> GetTLI,
@@ -1938,6 +1946,9 @@ OptimizeFunctions(Module &M,
     if (hasOnlyColdCalls(F, GetBFI, ChangeableCCCache))
       AllCallsCold.push_back(&F);
 
+  unsigned char DebugInfoFastCC =
+      GetDebugInfoFastCC(Triple(M.getTargetTriple()));
+
   // Optimize functions.
   for (Function &F : llvm::make_early_inc_range(M)) {
     // Don't perform global opt pass on naked functions; we don't want fast
@@ -2021,6 +2032,13 @@ OptimizeFunctions(Module &M,
       // Fast calling convention.
       F.setCallingConv(CallingConv::Fast);
       ChangeCalleesToFastCall(&F);
+
+      if (F.getSubprogram()) {
+        DISubprogram *SP = F.getSubprogram();
+        auto Temp = SP->getType()->cloneWithCC(DebugInfoFastCC);
+        SP->replaceType(MDNode::replaceWithPermanent(std::move(Temp)));
+      }
+
       ++NumFastCallFns;
       Changed = true;
     }



More information about the llvm-commits mailing list