Skip to content

Commit 2a8cbdd

Browse files
committed
[LTO] Add support for existing Config::Freestanding option.
lto::Config has a field to control whether the build is "freestanding" (no builtins) or not, but it is not hooked up to the code actually running the passes. This patch adds support for the flag to both the code that runs optimization with the new and old pass managers, by explicitly adding a TargetLibraryInfo instance. If Freestanding is true, all library functions are disabled. Reviewed By: steven_wu Differential Revision: https://reviews.llvm.org/D94630
1 parent 60cd75a commit 2a8cbdd

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

llvm/lib/LTO/LTOBackend.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
237237
CGSCCAnalysisManager CGAM(Conf.DebugPassManager);
238238
ModuleAnalysisManager MAM(Conf.DebugPassManager);
239239

240+
std::unique_ptr<TargetLibraryInfoImpl> TLII(
241+
new TargetLibraryInfoImpl(Triple(TM->getTargetTriple())));
242+
if (Conf.Freestanding)
243+
TLII->disableAllFunctions();
244+
FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
245+
240246
// Register the AA manager first so that our version is the one used.
241247
FAM.registerPass([&] { return std::move(AA); });
242248

@@ -302,6 +308,12 @@ static void runNewPMCustomPasses(const Config &Conf, Module &Mod,
302308
CGSCCAnalysisManager CGAM;
303309
ModuleAnalysisManager MAM;
304310

311+
std::unique_ptr<TargetLibraryInfoImpl> TLII(
312+
new TargetLibraryInfoImpl(Triple(TM->getTargetTriple())));
313+
if (Conf.Freestanding)
314+
TLII->disableAllFunctions();
315+
FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
316+
305317
// Register the AA manager first so that our version is the one used.
306318
FAM.registerPass([&] { return std::move(AA); });
307319

@@ -335,6 +347,8 @@ static void runOldPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
335347

336348
PassManagerBuilder PMB;
337349
PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM->getTargetTriple()));
350+
if (Conf.Freestanding)
351+
PMB.LibraryInfo->disableAllFunctions();
338352
PMB.Inliner = createFunctionInliningPass();
339353
PMB.ExportSummary = ExportSummary;
340354
PMB.ImportSummary = ImportSummary;

llvm/test/LTO/X86/tli-nobuiltin.ll

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,31 @@
1111
; RUN: llvm-nm %t.o | FileCheck %s --check-prefix=LTO-FREESTANDING
1212
; LTO-FREESTANDING: fprintf
1313

14+
; Test -lto-freestanding option for LTOBackend & legacy PM.
15+
16+
; RUN: llvm-lto2 run -r %t.bc,_fprintf,px -r %t.bc,_hello_world,px -r %t.bc,_percent_s,px -r %t.bc,_foo,px %t.bc -o %t1.o 2>&1
17+
; RUN: llvm-nm %t1.o.0 | FileCheck %s --check-prefix=LTO
18+
19+
; RUN: llvm-lto2 run -lto-freestanding -r %t.bc,_fprintf,px -r %t.bc,_hello_world,px -r %t.bc,_percent_s,px -r %t.bc,_foo,px %t.bc -o %t2.o 2>&1
20+
; RUN: llvm-nm %t2.o.0 | FileCheck %s --check-prefix=LTO-FREESTANDING
21+
22+
; Test -lto-freestanding option for LTOBackend & new PM.
23+
24+
; RUN: llvm-lto2 run -use-new-pm -r %t.bc,_fprintf,px -r %t.bc,_hello_world,px -r %t.bc,_percent_s,px -r %t.bc,_foo,px %t.bc -o %t1.o 2>&1
25+
; RUN: llvm-nm %t1.o.0 | FileCheck %s --check-prefix=LTO
26+
27+
; RUN: llvm-lto2 run -use-new-pm -lto-freestanding -r %t.bc,_fprintf,px -r %t.bc,_hello_world,px -r %t.bc,_percent_s,px -r %t.bc,_foo,px %t.bc -o %t2.o 2>&1
28+
; RUN: llvm-nm %t2.o.0 | FileCheck %s --check-prefix=LTO-FREESTANDING
29+
30+
; Test -lto-freestanding option for LTOBackend & new PM with custom pipeline.
31+
32+
; RUN: llvm-lto2 run -use-new-pm -opt-pipeline='default<O3>' -r %t.bc,_fprintf,px -r %t.bc,_hello_world,px -r %t.bc,_percent_s,px -r %t.bc,_foo,px %t.bc -o %t1.o 2>&1
33+
; RUN: llvm-nm %t1.o.0 | FileCheck %s --check-prefix=LTO
34+
35+
; RUN: llvm-lto2 run -use-new-pm -opt-pipeline='default<O3>' -lto-freestanding -r %t.bc,_fprintf,px -r %t.bc,_hello_world,px -r %t.bc,_percent_s,px -r %t.bc,_foo,px %t.bc -o %t2.o 2>&1
36+
; RUN: llvm-nm %t2.o.0 | FileCheck %s --check-prefix=LTO-FREESTANDING
37+
38+
1439
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
1540
target triple = "x86_64-apple-macosx10.11.0"
1641

llvm/tools/llvm-lto2/llvm-lto2.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ static cl::list<std::string>
158158
PassPlugins("load-pass-plugin",
159159
cl::desc("Load passes from plugin library"));
160160

161+
static cl::opt<bool> EnableFreestanding(
162+
"lto-freestanding",
163+
cl::desc("Enable Freestanding (disable builtins / TLI) during LTO"),
164+
cl::init(false), cl::Hidden);
165+
161166
static void check(Error E, std::string Msg) {
162167
if (!E)
163168
return;
@@ -269,6 +274,7 @@ static int run(int argc, char **argv) {
269274

270275
Conf.OptLevel = OptLevel - '0';
271276
Conf.UseNewPM = UseNewPM;
277+
Conf.Freestanding = EnableFreestanding;
272278
for (auto &PluginFN : PassPlugins)
273279
Conf.PassPlugins.push_back(PluginFN);
274280
switch (CGOptLevel) {

0 commit comments

Comments
 (0)