Skip to content

Commit 964888d

Browse files
authored
[llvm][CFI] Ensure COFF comdat renaming applies for imported functions (#143421)
I ran into the same issue as #139962 regarding the comdat corresponding to a renamed key function but for thinlto. My last patch had not considered the thinlto case, so this applies the same fix for imported functions.
1 parent 99e53cb commit 964888d

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

llvm/lib/Transforms/IPO/LowerTypeTests.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,8 @@ class LowerTypeTestsModule {
561561
return FunctionAnnotations.contains(V);
562562
}
563563

564+
void maybeReplaceComdat(Function *F, StringRef OriginalName);
565+
564566
public:
565567
LowerTypeTestsModule(Module &M, ModuleAnalysisManager &AM,
566568
ModuleSummaryIndex *ExportSummary,
@@ -1082,6 +1084,23 @@ void LowerTypeTestsModule::importTypeTest(CallInst *CI) {
10821084
}
10831085
}
10841086

1087+
void LowerTypeTestsModule::maybeReplaceComdat(Function *F,
1088+
StringRef OriginalName) {
1089+
// For COFF we should also rename the comdat if this function also
1090+
// happens to be the key function. Even if the comdat name changes, this
1091+
// should still be fine since comdat and symbol resolution happens
1092+
// before LTO, so all symbols which would prevail have been selected.
1093+
if (F->hasComdat() && ObjectFormat == Triple::COFF &&
1094+
F->getComdat()->getName() == OriginalName) {
1095+
Comdat *OldComdat = F->getComdat();
1096+
Comdat *NewComdat = M.getOrInsertComdat(F->getName());
1097+
for (GlobalObject &GO : M.global_objects()) {
1098+
if (GO.getComdat() == OldComdat)
1099+
GO.setComdat(NewComdat);
1100+
}
1101+
}
1102+
}
1103+
10851104
// ThinLTO backend: the function F has a jump table entry; update this module
10861105
// accordingly. isJumpTableCanonical describes the type of the jump table entry.
10871106
void LowerTypeTestsModule::importFunction(
@@ -1115,6 +1134,7 @@ void LowerTypeTestsModule::importFunction(
11151134
FDecl->setVisibility(GlobalValue::HiddenVisibility);
11161135
} else {
11171136
F->setName(Name + ".cfi");
1137+
maybeReplaceComdat(F, Name);
11181138
F->setLinkage(GlobalValue::ExternalLinkage);
11191139
FDecl = Function::Create(F->getFunctionType(), GlobalValue::ExternalLinkage,
11201140
F->getAddressSpace(), Name, &M);
@@ -1734,19 +1754,7 @@ void LowerTypeTestsModule::buildBitSetsFromFunctionsNative(
17341754
FAlias->takeName(F);
17351755
if (FAlias->hasName()) {
17361756
F->setName(FAlias->getName() + ".cfi");
1737-
// For COFF we should also rename the comdat if this function also
1738-
// happens to be the key function. Even if the comdat name changes, this
1739-
// should still be fine since comdat and symbol resolution happens
1740-
// before LTO, so all symbols which would prevail have been selected.
1741-
if (F->hasComdat() && ObjectFormat == Triple::COFF &&
1742-
F->getComdat()->getName() == FAlias->getName()) {
1743-
Comdat *OldComdat = F->getComdat();
1744-
Comdat *NewComdat = M.getOrInsertComdat(F->getName());
1745-
for (GlobalObject &GO : M.global_objects()) {
1746-
if (GO.getComdat() == OldComdat)
1747-
GO.setComdat(NewComdat);
1748-
}
1749-
}
1757+
maybeReplaceComdat(F, FAlias->getName());
17501758
}
17511759
replaceCfiUses(F, FAlias, IsJumpTableCanonical);
17521760
if (!F->hasLocalLinkage())
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
CfiFunctionDefs:
3+
- f1
4+
- f2
5+
...

llvm/test/Transforms/LowerTypeTests/cfi-coff-comdat-rename.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
; REQUIRES: x86-registered-target
22
; RUN: opt -S -passes=lowertypetests %s | FileCheck %s
3+
; RUN: opt -S -passes=lowertypetests -lowertypetests-summary-action=import -lowertypetests-read-summary=%p/Inputs/import-thinlto-funcs.yaml %s | FileCheck %s
34

45
;; This is a check to assert we don't crash with:
56
;;
67
;; LLVM ERROR: Associative COMDAT symbol '...' does not exist.
78
;;
89
;; So this just needs to exit normally.
910
; RUN: opt -S -passes=lowertypetests %s | llc -asm-verbose=false
11+
; RUN: opt -S -passes=lowertypetests -lowertypetests-summary-action=import -lowertypetests-read-summary=%p/Inputs/import-thinlto-funcs.yaml %s | llc -asm-verbose=false
1012

1113
target datalayout = "e-p:64:64"
1214
target triple = "x86_64-pc-windows-msvc"

0 commit comments

Comments
 (0)