[llvm] r290682 - [ThinLTO] Honor -O{0, 1, 2, 4} passed through the libLTO interface for ThinLTO

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 28 11:37:16 PST 2016


Author: mehdi_amini
Date: Wed Dec 28 13:37:16 2016
New Revision: 290682

URL: http://llvm.org/viewvc/llvm-project?rev=290682&view=rev
Log:
[ThinLTO] Honor -O{0,1,2,4} passed through the libLTO interface for ThinLTO

This was hardcoded to be O3 till now, without any way to change it
without changing the code.

Modified:
    llvm/trunk/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h
    llvm/trunk/lib/LTO/ThinLTOCodeGenerator.cpp
    llvm/trunk/tools/lto/lto.cpp

Modified: llvm/trunk/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h?rev=290682&r1=290681&r2=290682&view=diff
==============================================================================
--- llvm/trunk/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h (original)
+++ llvm/trunk/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h Wed Dec 28 13:37:16 2016
@@ -38,7 +38,7 @@ struct TargetMachineBuilder {
   std::string MAttr;
   TargetOptions Options;
   Optional<Reloc::Model> RelocModel;
-  CodeGenOpt::Level CGOptLevel = CodeGenOpt::Default;
+  CodeGenOpt::Level CGOptLevel = CodeGenOpt::Aggressive;
 
   std::unique_ptr<TargetMachine> create() const;
 };
@@ -199,6 +199,11 @@ public:
     TMBuilder.CGOptLevel = CGOptLevel;
   }
 
+  /// IR optimization level: from 0 to 3.
+  void setOptLevel(unsigned NewOptLevel) {
+    OptLevel = (NewOptLevel > 3) ? 3 : NewOptLevel;
+  }
+
   /// Disable CodeGen, only run the stages till codegen and stop. The output
   /// will be bitcode.
   void disableCodeGen(bool Disable) { DisableCodeGen = Disable; }
@@ -300,6 +305,9 @@ private:
   /// Flag to indicate that only the CodeGen will be performed, no cross-module
   /// importing or optimization.
   bool CodeGenOnly = false;
+
+  /// IR Optimization Level [0-3].
+  unsigned OptLevel = 3;
 };
 }
 #endif

Modified: llvm/trunk/lib/LTO/ThinLTOCodeGenerator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/ThinLTOCodeGenerator.cpp?rev=290682&r1=290681&r2=290682&view=diff
==============================================================================
--- llvm/trunk/lib/LTO/ThinLTOCodeGenerator.cpp (original)
+++ llvm/trunk/lib/LTO/ThinLTOCodeGenerator.cpp Wed Dec 28 13:37:16 2016
@@ -200,13 +200,14 @@ crossImportIntoModule(Module &TheModule,
     report_fatal_error("importFunctions failed");
 }
 
-static void optimizeModule(Module &TheModule, TargetMachine &TM) {
+static void optimizeModule(Module &TheModule, TargetMachine &TM,
+                           unsigned OptLevel) {
   // Populate the PassManager
   PassManagerBuilder PMB;
   PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple());
   PMB.Inliner = createFunctionInliningPass();
   // FIXME: should get it from the bitcode?
-  PMB.OptLevel = 3;
+  PMB.OptLevel = OptLevel;
   PMB.LoopVectorize = true;
   PMB.SLPVectorize = true;
   PMB.VerifyInput = true;
@@ -383,7 +384,7 @@ ProcessThinLTOModule(Module &TheModule,
                      const GVSummaryMapTy &DefinedGlobals,
                      const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
                      bool DisableCodeGen, StringRef SaveTempsDir,
-                     unsigned count) {
+                     unsigned OptLevel, unsigned count) {
 
   // "Benchmark"-like optimization: single-source case
   bool SingleModule = (ModuleMap.size() == 1);
@@ -415,7 +416,7 @@ ProcessThinLTOModule(Module &TheModule,
     saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
   }
 
-  optimizeModule(TheModule, TM);
+  optimizeModule(TheModule, TM, OptLevel);
 
   saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
 
@@ -534,6 +535,7 @@ std::unique_ptr<TargetMachine> TargetMac
   SubtargetFeatures Features(MAttr);
   Features.getDefaultSubtargetFeatures(TheTriple);
   std::string FeatureStr = Features.getString();
+
   return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
       TheTriple.str(), MCpu, FeatureStr, Options, RelocModel,
       CodeModel::Default, CGOptLevel));
@@ -726,7 +728,7 @@ void ThinLTOCodeGenerator::optimize(Modu
   initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
 
   // Optimize now
-  optimizeModule(TheModule, *TMBuilder.create());
+  optimizeModule(TheModule, *TMBuilder.create(), OptLevel);
 }
 
 /**
@@ -947,7 +949,7 @@ void ThinLTOCodeGenerator::run() {
             *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
             ExportList, GUIDPreservedSymbols,
             ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
-            DisableCodeGen, SaveTempsDir, count);
+            DisableCodeGen, SaveTempsDir, OptLevel, count);
 
         // Commit to the cache (if enabled)
         CacheEntry.write(*OutputBuffer);

Modified: llvm/trunk/tools/lto/lto.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lto/lto.cpp?rev=290682&r1=290681&r2=290682&view=diff
==============================================================================
--- llvm/trunk/tools/lto/lto.cpp (original)
+++ llvm/trunk/tools/lto/lto.cpp Wed Dec 28 13:37:16 2016
@@ -465,6 +465,25 @@ thinlto_code_gen_t thinlto_create_codege
   ThinLTOCodeGenerator *CodeGen = new ThinLTOCodeGenerator();
   CodeGen->setTargetOptions(InitTargetOptionsFromCodeGenFlags());
 
+  if (OptLevel.getNumOccurrences()) {
+    if (OptLevel < '0' || OptLevel > '3')
+      report_fatal_error("Optimization level must be between 0 and 3");
+    CodeGen->setOptLevel(OptLevel - '0');
+    switch (OptLevel) {
+    case '0':
+      CodeGen->setCodeGenOptLevel(CodeGenOpt::None);
+      break;
+    case '1':
+      CodeGen->setCodeGenOptLevel(CodeGenOpt::Less);
+      break;
+    case '2':
+      CodeGen->setCodeGenOptLevel(CodeGenOpt::Default);
+      break;
+    case '3':
+      CodeGen->setCodeGenOptLevel(CodeGenOpt::Aggressive);
+      break;
+    }
+  }
   return wrap(CodeGen);
 }
 




More information about the llvm-commits mailing list