llvm.org GIT mirror llvm / e4c5b69
Add support for -fno-builtin to LTO and ThinLTO to libLTO Reviewers: tejohnson, pcc Subscribers: Prazek, dexonsmith, llvm-commits Differential Revision: https://reviews.llvm.org/D30791 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@298936 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 2 years ago
7 changed file(s) with 86 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
173173 ///
174174 /// Calls \a verifyMergedModuleOnce().
175175 bool compileOptimized(ArrayRef Out);
176
177 /// Enable the Freestanding mode: indicate that the optimizer should not
178 /// assume builtins are present on the target.
179 void setFreestanding(bool Enabled) { Freestanding = Enabled; }
176180
177181 void setDiagnosticHandler(lto_diagnostic_handler_t, void *);
178182
236240 bool ShouldRestoreGlobalsLinkage = false;
237241 TargetMachine::CodeGenFileType FileType = TargetMachine::CGFT_ObjectFile;
238242 std::unique_ptr DiagnosticOutputFile;
243 bool Freestanding = false;
239244 };
240245 }
241246 #endif
204204 TMBuilder.Options = std::move(Options);
205205 }
206206
207 /// Enable the Freestanding mode: indicate that the optimizer should not
208 /// assume builtins are present on the target.
209 void setFreestanding(bool Enabled) { Freestanding = Enabled; }
210
207211 /// CodeModel
208212 void setCodePICModel(Optional Model) {
209213 TMBuilder.RelocModel = Model;
320324 /// Flag to indicate that only the CodeGen will be performed, no cross-module
321325 /// importing or optimization.
322326 bool CodeGenOnly = false;
327
328 /// Flag to indicate that the optimizer should not assume builtins are present
329 /// on the target.
330 bool Freestanding = false;
323331
324332 /// IR Optimization Level [0-3].
325333 unsigned OptLevel = 3;
554554 if (!DisableInline)
555555 PMB.Inliner = createFunctionInliningPass();
556556 PMB.LibraryInfo = new TargetLibraryInfoImpl(TargetTriple);
557 if (Freestanding)
558 PMB.LibraryInfo->disableAllFunctions();
557559 PMB.OptLevel = OptLevel;
558560 PMB.VerifyInput = !DisableVerify;
559561 PMB.VerifyOutput = !DisableVerify;
186186 }
187187
188188 static void optimizeModule(Module &TheModule, TargetMachine &TM,
189 unsigned OptLevel) {
189 unsigned OptLevel, bool Freestanding) {
190190 // Populate the PassManager
191191 PassManagerBuilder PMB;
192192 PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple());
193 if (Freestanding)
194 PMB.LibraryInfo->disableAllFunctions();
193195 PMB.Inliner = createFunctionInliningPass();
194196 // FIXME: should get it from the bitcode?
195197 PMB.OptLevel = OptLevel;
263265 const std::map &ResolvedODR,
264266 const GVSummaryMapTy &DefinedFunctions,
265267 const DenseSet &PreservedSymbols, unsigned OptLevel,
266 const TargetMachineBuilder &TMBuilder) {
268 bool Freestanding, const TargetMachineBuilder &TMBuilder) {
267269 if (CachePath.empty())
268270 return;
269271
320322 AddUnsigned(*TMBuilder.RelocModel);
321323 AddUnsigned(TMBuilder.CGOptLevel);
322324 AddUnsigned(OptLevel);
325 AddUnsigned(Freestanding);
323326
324327 Hasher.update(ArrayRef((uint8_t *)&ModHash[0], sizeof(ModHash)));
325328 for (auto F : ExportList)
403406 const GVSummaryMapTy &DefinedGlobals,
404407 const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
405408 bool DisableCodeGen, StringRef SaveTempsDir,
406 unsigned OptLevel, unsigned count) {
409 bool Freestanding, unsigned OptLevel, unsigned count) {
407410
408411 // "Benchmark"-like optimization: single-source case
409412 bool SingleModule = (ModuleMap.size() == 1);
435438 saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
436439 }
437440
438 optimizeModule(TheModule, TM, OptLevel);
441 optimizeModule(TheModule, TM, OptLevel, Freestanding);
439442
440443 saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
441444
761764 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
762765
763766 // Optimize now
764 optimizeModule(TheModule, *TMBuilder.create(), OptLevel);
767 optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding);
765768 }
766769
767770 /**
947950 ImportLists[ModuleIdentifier], ExportList,
948951 ResolvedODR[ModuleIdentifier],
949952 DefinedFunctions, GUIDPreservedSymbols,
950 OptLevel, TMBuilder);
953 OptLevel, Freestanding, TMBuilder);
951954 auto CacheEntryPath = CacheEntry.getEntryPath();
952955
953956 {
993996 *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
994997 ExportList, GUIDPreservedSymbols,
995998 ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
996 DisableCodeGen, SaveTempsDir, OptLevel, count);
999 DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count);
9971000
9981001 // Commit to the cache (if enabled)
9991002 CacheEntry.write(*OutputBuffer);
0 ; Test -lto-freestanding option for libLTO.
1 ; RUN: llvm-as < %s > %t.bc
2
3 ; Regular run: expects fprintf to be turned into fwrite
4 ; RUN: llvm-lto %t.bc -exported-symbol=_foo -o %t.o
5 ; RUN: llvm-nm %t.o | FileCheck %s --check-prefix=LTO
6 ; LTO: fwrite
7
8 ; Freestanding run: expects fprintf to NOT be turned into fwrite
9 ; RUN: llvm-lto %t.bc -lto-freestanding -exported-symbol=_foo -o %t.o
10 ; RUN: llvm-nm %t.o | FileCheck %s --check-prefix=LTO-FREESTANDING
11 ; LTO-FREESTANDING: fprintf
12
13 ; Same with ThinLTO now.
14 ; RUN: opt -module-hash -module-summary %s -o %t.bc
15
16 ; Regular run: expects fprintf to be turned into fwrite
17 ; RUN: llvm-lto -exported-symbol=_foo -thinlto-action=run %t.bc
18 ; RUN: llvm-nm %t.bc.thinlto.o | FileCheck %s --check-prefix=ThinLTO
19 ; ThinLTO: fwrite
20
21 ; Freestanding run: expects fprintf to NOT be turned into fwrite
22 ; RUN: llvm-lto -lto-freestanding -exported-symbol=_foo -thinlto-action=run %t.bc
23 ; RUN: llvm-nm %t.bc.thinlto.o | FileCheck %s --check-prefix=ThinLTO-FREESTANDING
24 ; ThinLTO-FREESTANDING: fprintf
25
26
27 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
28 target triple = "x86_64-apple-macosx10.11.0"
29
30 declare i32 @fprintf(%FILE*, i8*, ...)
31
32 %FILE = type { }
33
34 @hello_world = constant [13 x i8] c"hello world\0A\00"
35 @percent_s = constant [3 x i8] c"%s\00"
36
37 ; Check fprintf(fp, "%s", str) -> fwrite(str, fp) only when builtins are enabled
38
39 define void @foo(%FILE* %fp) {
40 %fmt = getelementptr [3 x i8], [3 x i8]* @percent_s, i32 0, i32 0
41 %str = getelementptr [13 x i8], [13 x i8]* @hello_world, i32 0, i32 0
42 call i32 (%FILE*, i8*, ...) @fprintf(%FILE* %fp, i8* %fmt, i8* %str)
43 ret void
44 }
45
6262 "disable-lto-vectorization", cl::init(false),
6363 cl::desc("Do not run loop or slp vectorization during LTO"));
6464
65 static cl::opt EnableFreestanding(
66 "lto-freestanding", cl::init(false),
67 cl::desc("Enable Freestanding (disable builtins / TLI) during LTO"));
68
6569 static cl::opt UseDiagnosticHandler(
6670 "use-diagnostic-handler", cl::init(false),
6771 cl::desc("Use a diagnostic handler to test the handler interface"));
432436 ThinGenerator.setCodePICModel(getRelocModel());
433437 ThinGenerator.setTargetOptions(Options);
434438 ThinGenerator.setCacheDir(ThinLTOCacheDir);
439 ThinGenerator.setFreestanding(EnableFreestanding);
435440
436441 // Add all the exported symbols to the table of symbols to preserve.
437442 for (unsigned i = 0; i < ExportedSymbols.size(); ++i)
808813 CodeGen.setDiagnosticHandler(handleDiagnostics, nullptr);
809814
810815 CodeGen.setCodePICModel(getRelocModel());
816 CodeGen.setFreestanding(EnableFreestanding);
811817
812818 CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
813819 CodeGen.setTargetOptions(Options);
4343 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
4444 cl::desc("Do not run the GVN load PRE pass"));
4545
46 static cl::opt
47 DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
48 cl::desc("Do not run loop or slp vectorization during LTO"));
46 static cl::opt DisableLTOVectorization(
47 "disable-lto-vectorization", cl::init(false),
48 cl::desc("Do not run loop or slp vectorization during LTO"));
49
50 static cl::opt EnableFreestanding(
51 "lto-freestanding", cl::init(false),
52 cl::desc("Enable Freestanding (disable builtins / TLI) during LTO"));
4953
5054 #ifdef NDEBUG
5155 static bool VerifyByDefault = false;
158162 if (OptLevel < '0' || OptLevel > '3')
159163 report_fatal_error("Optimization level must be between 0 and 3");
160164 CG->setOptLevel(OptLevel - '0');
165 CG->setFreestanding(EnableFreestanding);
161166 }
162167
163168 extern const char* lto_get_version() {
463468 lto_initialize();
464469 ThinLTOCodeGenerator *CodeGen = new ThinLTOCodeGenerator();
465470 CodeGen->setTargetOptions(InitTargetOptionsFromCodeGenFlags());
471 CodeGen->setFreestanding(EnableFreestanding);
466472
467473 if (OptLevel.getNumOccurrences()) {
468474 if (OptLevel < '0' || OptLevel > '3')