Skip to content

Commit 16ab081

Browse files
ilovepitstellar
authored andcommitted
[clang][fat-lto-objects] Make module flags match non-FatLTO pipelines (llvm#83159)
In addition to being rather hard to follow, there isn't a good reason why FatLTO shouldn't just share the same code for setting module flags for (Thin)LTO. This patch simplifies the logic and makes sure we use set these flags in a consistent way, independent of FatLTO. Additionally, we now test that output in the .llvm.lto section actually matches the output from Full and Thin LTO compilation. (cherry picked from commit 7d8b50a)
1 parent 267d9b1 commit 16ab081

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,14 @@ class EmitAssemblyHelper {
186186
TargetTriple.getVendor() != llvm::Triple::Apple;
187187
}
188188

189+
/// Check whether we should emit a flag for UnifiedLTO.
190+
/// The UnifiedLTO module flag should be set when UnifiedLTO is enabled for
191+
/// ThinLTO or Full LTO with module summaries.
192+
bool shouldEmitUnifiedLTOModueFlag() const {
193+
return CodeGenOpts.UnifiedLTO &&
194+
(CodeGenOpts.PrepareForThinLTO || shouldEmitRegularLTOSummary());
195+
}
196+
189197
public:
190198
EmitAssemblyHelper(DiagnosticsEngine &_Diags,
191199
const HeaderSearchOptions &HeaderSearchOpts,
@@ -1029,7 +1037,8 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
10291037
if (!actionRequiresCodeGen(Action) && CodeGenOpts.VerifyModule)
10301038
MPM.addPass(VerifierPass());
10311039

1032-
if (Action == Backend_EmitBC || Action == Backend_EmitLL) {
1040+
if (Action == Backend_EmitBC || Action == Backend_EmitLL ||
1041+
CodeGenOpts.FatLTO) {
10331042
if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
10341043
if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
10351044
TheModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit",
@@ -1040,11 +1049,9 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
10401049
if (!ThinLinkOS)
10411050
return;
10421051
}
1043-
if (CodeGenOpts.UnifiedLTO)
1044-
TheModule->addModuleFlag(llvm::Module::Error, "UnifiedLTO", uint32_t(1));
10451052
MPM.addPass(ThinLTOBitcodeWriterPass(
10461053
*OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr));
1047-
} else {
1054+
} else if (Action == Backend_EmitLL) {
10481055
MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists,
10491056
/*EmitLTOSummary=*/true));
10501057
}
@@ -1058,24 +1065,17 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
10581065
if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
10591066
TheModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit",
10601067
uint32_t(1));
1061-
if (CodeGenOpts.UnifiedLTO)
1062-
TheModule->addModuleFlag(llvm::Module::Error, "UnifiedLTO", uint32_t(1));
10631068
}
1064-
if (Action == Backend_EmitBC)
1069+
if (Action == Backend_EmitBC) {
10651070
MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
10661071
EmitLTOSummary));
1067-
else
1072+
} else if (Action == Backend_EmitLL) {
10681073
MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists,
10691074
EmitLTOSummary));
1075+
}
10701076
}
1071-
}
1072-
if (CodeGenOpts.FatLTO) {
1073-
// Set the EnableSplitLTOUnit and UnifiedLTO module flags, since FatLTO
1074-
// uses a different action than Backend_EmitBC or Backend_EmitLL.
1075-
if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
1076-
TheModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit",
1077-
uint32_t(CodeGenOpts.EnableSplitLTOUnit));
1078-
if (CodeGenOpts.UnifiedLTO && !TheModule->getModuleFlag("UnifiedLTO"))
1077+
1078+
if (shouldEmitUnifiedLTOModueFlag())
10791079
TheModule->addModuleFlag(llvm::Module::Error, "UnifiedLTO", uint32_t(1));
10801080
}
10811081

clang/test/CodeGen/fat-lto-objects.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
// RUN: llvm-objcopy --dump-section=.llvm.lto=%t.full.split.bc %t.full.split.o
1212
// RUN: llvm-dis %t.full.split.bc -o - | FileCheck %s --check-prefixes=FULL,SPLIT,NOUNIFIED
1313

14+
/// Full LTO always sets EnableSplitLTOUnit when the summary is used.
1415
// RUN: %clang -cc1 -triple x86_64-unknown-linux-gnu -flto=full -ffat-lto-objects -emit-obj < %s -o %t.full.nosplit.o
1516
// RUN: llvm-readelf -S %t.full.nosplit.o | FileCheck %s --check-prefixes=ELF
1617
// RUN: llvm-objcopy --dump-section=.llvm.lto=%t.full.nosplit.bc %t.full.nosplit.o
17-
// RUN: llvm-dis %t.full.nosplit.bc -o - | FileCheck %s --check-prefixes=FULL,NOSPLIT,NOUNIFIED
18+
// RUN: llvm-dis %t.full.nosplit.bc -o - | FileCheck %s --check-prefixes=FULL,SPLIT,NOUNIFIED
1819

1920
// RUN: %clang -cc1 -triple x86_64-unknown-linux-gnu -flto=thin -fsplit-lto-unit -ffat-lto-objects -emit-obj < %s -o %t.thin.split.o
2021
// RUN: llvm-readelf -S %t.thin.split.o | FileCheck %s --check-prefixes=ELF
@@ -34,6 +35,21 @@
3435
// RUN: %clang -cc1 -triple x86_64-unknown-linux-gnu -flto=full -ffat-lto-objects -fsplit-lto-unit -S < %s -o - \
3536
// RUN: | FileCheck %s --check-prefixes=ASM
3637

38+
/// Make sure that FatLTO generates .llvm.lto sections that are the same as the output from normal LTO compilations
39+
// RUN: %clang -O2 --target=x86_64-unknown-linux-gnu -fPIE -flto=full -ffat-lto-objects -c %s -o %t.fatlto.full.o
40+
// RUN: llvm-objcopy --dump-section=.llvm.lto=%t.fatlto.full.bc %t.fatlto.full.o
41+
// RUN: llvm-dis < %t.fatlto.full.bc -o %t.fatlto.full.ll
42+
// RUN: %clang -O2 --target=x86_64-unknown-linux-gnu -fPIE -flto=full -c %s -o %t.nofat.full.bc
43+
// RUN: llvm-dis < %t.nofat.full.bc -o %t.nofat.full.ll
44+
// RUN: diff %t.fatlto.full.ll %t.nofat.full.ll
45+
46+
// RUN: %clang -O2 --target=x86_64-unknown-linux-gnu -fPIE -flto=thin -ffat-lto-objects -c %s -o %t.fatlto.thin.o
47+
// RUN: llvm-objcopy --dump-section=.llvm.lto=%t.fatlto.thin.bc %t.fatlto.thin.o
48+
// RUN: llvm-dis < %t.fatlto.thin.bc -o %t.fatlto.thin.ll
49+
// RUN: %clang -O2 --target=x86_64-unknown-linux-gnu -fPIE -flto=thin -c %s -o %t.nofat.thin.bc
50+
// RUN: llvm-dis < %t.nofat.thin.bc -o %t.nofat.thin.ll
51+
// RUN: diff %t.fatlto.thin.ll %t.nofat.thin.ll
52+
3753
/// Be sure we enable split LTO units correctly under -ffat-lto-objects.
3854
// SPLIT: ![[#]] = !{i32 1, !"EnableSplitLTOUnit", i32 1}
3955
// NOSPLIT: ![[#]] = !{i32 1, !"EnableSplitLTOUnit", i32 0}
@@ -51,6 +67,9 @@
5167
// ASM-NEXT: .asciz "BC
5268
// ASM-NEXT: .size .Lllvm.embedded.object
5369

70+
const char* foo = "foo";
71+
5472
int test(void) {
73+
const char* bar = "bar";
5574
return 0xabcd;
5675
}

0 commit comments

Comments
 (0)