Skip to content

Commit 7d8b50a

Browse files
authored
[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.
1 parent daf3079 commit 7d8b50a

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,
@@ -1036,7 +1044,8 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
10361044
if (!actionRequiresCodeGen(Action) && CodeGenOpts.VerifyModule)
10371045
MPM.addPass(VerifierPass());
10381046

1039-
if (Action == Backend_EmitBC || Action == Backend_EmitLL) {
1047+
if (Action == Backend_EmitBC || Action == Backend_EmitLL ||
1048+
CodeGenOpts.FatLTO) {
10401049
if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
10411050
if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
10421051
TheModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit",
@@ -1047,11 +1056,9 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
10471056
if (!ThinLinkOS)
10481057
return;
10491058
}
1050-
if (CodeGenOpts.UnifiedLTO)
1051-
TheModule->addModuleFlag(llvm::Module::Error, "UnifiedLTO", uint32_t(1));
10521059
MPM.addPass(ThinLTOBitcodeWriterPass(
10531060
*OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr));
1054-
} else {
1061+
} else if (Action == Backend_EmitLL) {
10551062
MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists,
10561063
/*EmitLTOSummary=*/true));
10571064
}
@@ -1065,24 +1072,17 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
10651072
if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
10661073
TheModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit",
10671074
uint32_t(1));
1068-
if (CodeGenOpts.UnifiedLTO)
1069-
TheModule->addModuleFlag(llvm::Module::Error, "UnifiedLTO", uint32_t(1));
10701075
}
1071-
if (Action == Backend_EmitBC)
1076+
if (Action == Backend_EmitBC) {
10721077
MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
10731078
EmitLTOSummary));
1074-
else
1079+
} else if (Action == Backend_EmitLL) {
10751080
MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists,
10761081
EmitLTOSummary));
1082+
}
10771083
}
1078-
}
1079-
if (CodeGenOpts.FatLTO) {
1080-
// Set the EnableSplitLTOUnit and UnifiedLTO module flags, since FatLTO
1081-
// uses a different action than Backend_EmitBC or Backend_EmitLL.
1082-
if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
1083-
TheModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit",
1084-
uint32_t(CodeGenOpts.EnableSplitLTOUnit));
1085-
if (CodeGenOpts.UnifiedLTO && !TheModule->getModuleFlag("UnifiedLTO"))
1084+
1085+
if (shouldEmitUnifiedLTOModueFlag())
10861086
TheModule->addModuleFlag(llvm::Module::Error, "UnifiedLTO", uint32_t(1));
10871087
}
10881088

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)