[lld] 6b56a27 - [ELF] Replace config-> with ctx.arg. in LTO.cpp

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 21 10:11:42 PDT 2024


Author: Fangrui Song
Date: 2024-09-21T10:11:37-07:00
New Revision: 6b56a273490e403957eb3fd7d772d8769423a718

URL: https://github.com/llvm/llvm-project/commit/6b56a273490e403957eb3fd7d772d8769423a718
DIFF: https://github.com/llvm/llvm-project/commit/6b56a273490e403957eb3fd7d772d8769423a718.diff

LOG: [ELF] Replace config-> with ctx.arg. in LTO.cpp

Added: 
    

Modified: 
    lld/ELF/Driver.cpp
    lld/ELF/LTO.cpp
    lld/ELF/LTO.h

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index e25db0e4951275..189e56507e16b6 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -2527,7 +2527,7 @@ template <class ELFT>
 void LinkerDriver::compileBitcodeFiles(bool skipLinkedOutput) {
   llvm::TimeTraceScope timeScope("LTO");
   // Compile bitcode files and replace bitcode symbols.
-  lto.reset(new BitcodeCompiler);
+  lto.reset(new BitcodeCompiler(ctx));
   for (BitcodeFile *file : ctx.bitcodeFiles)
     lto->add(*file);
 

diff  --git a/lld/ELF/LTO.cpp b/lld/ELF/LTO.cpp
index f339f1c2c0ec21..6f5c320779e805 100644
--- a/lld/ELF/LTO.cpp
+++ b/lld/ELF/LTO.cpp
@@ -43,42 +43,42 @@ using namespace llvm::ELF;
 using namespace lld;
 using namespace lld::elf;
 
-static std::string getThinLTOOutputFile(StringRef modulePath) {
-  return lto::getThinLTOOutputFile(modulePath, config->thinLTOPrefixReplaceOld,
-                                   config->thinLTOPrefixReplaceNew);
+static std::string getThinLTOOutputFile(Ctx &ctx, StringRef modulePath) {
+  return lto::getThinLTOOutputFile(modulePath, ctx.arg.thinLTOPrefixReplaceOld,
+                                   ctx.arg.thinLTOPrefixReplaceNew);
 }
 
-static lto::Config createConfig() {
+static lto::Config createConfig(Ctx &ctx) {
   lto::Config c;
 
   // LLD supports the new relocations and address-significance tables.
   c.Options = initTargetOptionsFromCodeGenFlags();
   c.Options.EmitAddrsig = true;
-  for (StringRef C : config->mllvmOpts)
+  for (StringRef C : ctx.arg.mllvmOpts)
     c.MllvmArgs.emplace_back(C.str());
 
   // Always emit a section per function/datum with LTO.
   c.Options.FunctionSections = true;
   c.Options.DataSections = true;
 
-  c.Options.BBAddrMap = config->ltoBBAddrMap;
+  c.Options.BBAddrMap = ctx.arg.ltoBBAddrMap;
 
   // Check if basic block sections must be used.
   // Allowed values for --lto-basic-block-sections are "all", "labels",
   // "<file name specifying basic block ids>", or none.  This is the equivalent
   // of -fbasic-block-sections= flag in clang.
-  if (!config->ltoBasicBlockSections.empty()) {
-    if (config->ltoBasicBlockSections == "all") {
+  if (!ctx.arg.ltoBasicBlockSections.empty()) {
+    if (ctx.arg.ltoBasicBlockSections == "all") {
       c.Options.BBSections = BasicBlockSection::All;
-    } else if (config->ltoBasicBlockSections == "labels") {
+    } else if (ctx.arg.ltoBasicBlockSections == "labels") {
       c.Options.BBSections = BasicBlockSection::Labels;
-    } else if (config->ltoBasicBlockSections == "none") {
+    } else if (ctx.arg.ltoBasicBlockSections == "none") {
       c.Options.BBSections = BasicBlockSection::None;
     } else {
       ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
-          MemoryBuffer::getFile(config->ltoBasicBlockSections.str());
+          MemoryBuffer::getFile(ctx.arg.ltoBasicBlockSections.str());
       if (!MBOrErr) {
-        error("cannot open " + config->ltoBasicBlockSections + ":" +
+        error("cannot open " + ctx.arg.ltoBasicBlockSections + ":" +
               MBOrErr.getError().message());
       } else {
         c.Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
@@ -88,114 +88,114 @@ static lto::Config createConfig() {
   }
 
   c.Options.UniqueBasicBlockSectionNames =
-      config->ltoUniqueBasicBlockSectionNames;
+      ctx.arg.ltoUniqueBasicBlockSectionNames;
 
   if (auto relocModel = getRelocModelFromCMModel())
     c.RelocModel = *relocModel;
-  else if (config->relocatable)
+  else if (ctx.arg.relocatable)
     c.RelocModel = std::nullopt;
-  else if (config->isPic)
+  else if (ctx.arg.isPic)
     c.RelocModel = Reloc::PIC_;
   else
     c.RelocModel = Reloc::Static;
 
   c.CodeModel = getCodeModelFromCMModel();
-  c.DisableVerify = config->disableVerify;
+  c.DisableVerify = ctx.arg.disableVerify;
   c.DiagHandler = diagnosticHandler;
-  c.OptLevel = config->ltoo;
+  c.OptLevel = ctx.arg.ltoo;
   c.CPU = getCPUStr();
   c.MAttrs = getMAttrs();
-  c.CGOptLevel = config->ltoCgo;
+  c.CGOptLevel = ctx.arg.ltoCgo;
 
   c.PTO.LoopVectorization = c.OptLevel > 1;
   c.PTO.SLPVectorization = c.OptLevel > 1;
 
   // Set up a custom pipeline if we've been asked to.
-  c.OptPipeline = std::string(config->ltoNewPmPasses);
-  c.AAPipeline = std::string(config->ltoAAPipeline);
+  c.OptPipeline = std::string(ctx.arg.ltoNewPmPasses);
+  c.AAPipeline = std::string(ctx.arg.ltoAAPipeline);
 
   // Set up optimization remarks if we've been asked to.
-  c.RemarksFilename = std::string(config->optRemarksFilename);
-  c.RemarksPasses = std::string(config->optRemarksPasses);
-  c.RemarksWithHotness = config->optRemarksWithHotness;
-  c.RemarksHotnessThreshold = config->optRemarksHotnessThreshold;
-  c.RemarksFormat = std::string(config->optRemarksFormat);
+  c.RemarksFilename = std::string(ctx.arg.optRemarksFilename);
+  c.RemarksPasses = std::string(ctx.arg.optRemarksPasses);
+  c.RemarksWithHotness = ctx.arg.optRemarksWithHotness;
+  c.RemarksHotnessThreshold = ctx.arg.optRemarksHotnessThreshold;
+  c.RemarksFormat = std::string(ctx.arg.optRemarksFormat);
 
   // Set up output file to emit statistics.
-  c.StatsFile = std::string(config->optStatsFilename);
+  c.StatsFile = std::string(ctx.arg.optStatsFilename);
 
-  c.SampleProfile = std::string(config->ltoSampleProfile);
-  for (StringRef pluginFn : config->passPlugins)
+  c.SampleProfile = std::string(ctx.arg.ltoSampleProfile);
+  for (StringRef pluginFn : ctx.arg.passPlugins)
     c.PassPlugins.push_back(std::string(pluginFn));
-  c.DebugPassManager = config->ltoDebugPassManager;
-  c.DwoDir = std::string(config->dwoDir);
+  c.DebugPassManager = ctx.arg.ltoDebugPassManager;
+  c.DwoDir = std::string(ctx.arg.dwoDir);
 
-  c.HasWholeProgramVisibility = config->ltoWholeProgramVisibility;
+  c.HasWholeProgramVisibility = ctx.arg.ltoWholeProgramVisibility;
   c.ValidateAllVtablesHaveTypeInfos =
-      config->ltoValidateAllVtablesHaveTypeInfos;
+      ctx.arg.ltoValidateAllVtablesHaveTypeInfos;
   c.AllVtablesHaveTypeInfos = ctx.ltoAllVtablesHaveTypeInfos;
-  c.AlwaysEmitRegularLTOObj = !config->ltoObjPath.empty();
+  c.AlwaysEmitRegularLTOObj = !ctx.arg.ltoObjPath.empty();
   c.KeepSymbolNameCopies = false;
 
-  for (const llvm::StringRef &name : config->thinLTOModulesToCompile)
+  for (const llvm::StringRef &name : ctx.arg.thinLTOModulesToCompile)
     c.ThinLTOModulesToCompile.emplace_back(name);
 
-  c.TimeTraceEnabled = config->timeTraceEnabled;
-  c.TimeTraceGranularity = config->timeTraceGranularity;
+  c.TimeTraceEnabled = ctx.arg.timeTraceEnabled;
+  c.TimeTraceGranularity = ctx.arg.timeTraceGranularity;
 
-  c.CSIRProfile = std::string(config->ltoCSProfileFile);
-  c.RunCSIRInstr = config->ltoCSProfileGenerate;
-  c.PGOWarnMismatch = config->ltoPGOWarnMismatch;
+  c.CSIRProfile = std::string(ctx.arg.ltoCSProfileFile);
+  c.RunCSIRInstr = ctx.arg.ltoCSProfileGenerate;
+  c.PGOWarnMismatch = ctx.arg.ltoPGOWarnMismatch;
 
-  if (config->emitLLVM) {
-    c.PreCodeGenModuleHook = [](size_t task, const Module &m) {
+  if (ctx.arg.emitLLVM) {
+    c.PreCodeGenModuleHook = [&ctx](size_t task, const Module &m) {
       if (std::unique_ptr<raw_fd_ostream> os =
-              openLTOOutputFile(config->outputFile))
+              openLTOOutputFile(ctx.arg.outputFile))
         WriteBitcodeToFile(m, *os, false);
       return false;
     };
   }
 
-  if (config->ltoEmitAsm) {
+  if (ctx.arg.ltoEmitAsm) {
     c.CGFileType = CodeGenFileType::AssemblyFile;
     c.Options.MCOptions.AsmVerbose = true;
   }
 
-  if (!config->saveTempsArgs.empty())
-    checkError(c.addSaveTemps(config->outputFile.str() + ".",
+  if (!ctx.arg.saveTempsArgs.empty())
+    checkError(c.addSaveTemps(ctx.arg.outputFile.str() + ".",
                               /*UseInputModulePath*/ true,
-                              config->saveTempsArgs));
+                              ctx.arg.saveTempsArgs));
   return c;
 }
 
-BitcodeCompiler::BitcodeCompiler() {
+BitcodeCompiler::BitcodeCompiler(Ctx &ctx) : ctx(ctx) {
   // Initialize indexFile.
-  if (!config->thinLTOIndexOnlyArg.empty())
-    indexFile = openFile(config->thinLTOIndexOnlyArg);
+  if (!ctx.arg.thinLTOIndexOnlyArg.empty())
+    indexFile = openFile(ctx.arg.thinLTOIndexOnlyArg);
 
   // Initialize ltoObj.
   lto::ThinBackend backend;
   auto onIndexWrite = [&](StringRef s) { thinIndices.erase(s); };
-  if (config->thinLTOIndexOnly) {
+  if (ctx.arg.thinLTOIndexOnly) {
     backend = lto::createWriteIndexesThinBackend(
-        std::string(config->thinLTOPrefixReplaceOld),
-        std::string(config->thinLTOPrefixReplaceNew),
-        std::string(config->thinLTOPrefixReplaceNativeObject),
-        config->thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite);
+        std::string(ctx.arg.thinLTOPrefixReplaceOld),
+        std::string(ctx.arg.thinLTOPrefixReplaceNew),
+        std::string(ctx.arg.thinLTOPrefixReplaceNativeObject),
+        ctx.arg.thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite);
   } else {
     backend = lto::createInProcessThinBackend(
-        llvm::heavyweight_hardware_concurrency(config->thinLTOJobs),
-        onIndexWrite, config->thinLTOEmitIndexFiles,
-        config->thinLTOEmitImportsFiles);
+        llvm::heavyweight_hardware_concurrency(ctx.arg.thinLTOJobs),
+        onIndexWrite, ctx.arg.thinLTOEmitIndexFiles,
+        ctx.arg.thinLTOEmitImportsFiles);
   }
 
   constexpr llvm::lto::LTO::LTOKind ltoModes[3] =
     {llvm::lto::LTO::LTOKind::LTOK_UnifiedThin,
      llvm::lto::LTO::LTOKind::LTOK_UnifiedRegular,
      llvm::lto::LTO::LTOKind::LTOK_Default};
-  ltoObj = std::make_unique<lto::LTO>(
-      createConfig(), backend, config->ltoPartitions,
-      ltoModes[config->ltoKind]);
+  ltoObj = std::make_unique<lto::LTO>(createConfig(ctx), backend,
+                                      ctx.arg.ltoPartitions,
+                                      ltoModes[ctx.arg.ltoKind]);
 
   // Initialize usedStartStop.
   if (ctx.bitcodeFiles.empty())
@@ -291,7 +291,7 @@ static void thinLTOCreateEmptyIndexFiles() {
     if (linkedBitCodeFiles.contains(f->getName()))
       continue;
     std::string path =
-        replaceThinLTOSuffix(getThinLTOOutputFile(f->obj->getName()));
+        replaceThinLTOSuffix(getThinLTOOutputFile(ctx, f->obj->getName()));
     std::unique_ptr<raw_fd_ostream> os = openFile(path + ".thinlto.bc");
     if (!os)
       continue;
@@ -316,8 +316,8 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
   // to cache native object files for ThinLTO incremental builds. If a path was
   // specified, configure LTO to use it as the cache directory.
   FileCache cache;
-  if (!config->thinLTOCacheDir.empty())
-    cache = check(localCache("ThinLTO", "Thin", config->thinLTOCacheDir,
+  if (!ctx.arg.thinLTOCacheDir.empty())
+    cache = check(localCache("ThinLTO", "Thin", ctx.arg.thinLTOCacheDir,
                              [&](size_t task, const Twine &moduleName,
                                  std::unique_ptr<MemoryBuffer> mb) {
                                files[task] = std::move(mb);
@@ -334,21 +334,21 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
         cache));
 
   // Emit empty index files for non-indexed files but not in single-module mode.
-  if (config->thinLTOModulesToCompile.empty()) {
+  if (ctx.arg.thinLTOModulesToCompile.empty()) {
     for (StringRef s : thinIndices) {
-      std::string path = getThinLTOOutputFile(s);
+      std::string path = getThinLTOOutputFile(ctx, s);
       openFile(path + ".thinlto.bc");
-      if (config->thinLTOEmitImportsFiles)
+      if (ctx.arg.thinLTOEmitImportsFiles)
         openFile(path + ".imports");
     }
   }
 
-  if (config->thinLTOEmitIndexFiles)
+  if (ctx.arg.thinLTOEmitIndexFiles)
     thinLTOCreateEmptyIndexFiles();
 
-  if (config->thinLTOIndexOnly) {
-    if (!config->ltoObjPath.empty())
-      saveBuffer(buf[0].second, config->ltoObjPath);
+  if (ctx.arg.thinLTOIndexOnly) {
+    if (!ctx.arg.ltoObjPath.empty())
+      saveBuffer(buf[0].second, ctx.arg.ltoObjPath);
 
     // ThinLTO with index only option is required to generate only the index
     // files. After that, we exit from linker and ThinLTO backend runs in a
@@ -358,18 +358,18 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
     return {};
   }
 
-  if (!config->thinLTOCacheDir.empty())
-    pruneCache(config->thinLTOCacheDir, config->thinLTOCachePolicy, files);
+  if (!ctx.arg.thinLTOCacheDir.empty())
+    pruneCache(ctx.arg.thinLTOCacheDir, ctx.arg.thinLTOCachePolicy, files);
 
-  if (!config->ltoObjPath.empty()) {
-    saveBuffer(buf[0].second, config->ltoObjPath);
+  if (!ctx.arg.ltoObjPath.empty()) {
+    saveBuffer(buf[0].second, ctx.arg.ltoObjPath);
     for (unsigned i = 1; i != maxTasks; ++i)
-      saveBuffer(buf[i].second, config->ltoObjPath + Twine(i));
+      saveBuffer(buf[i].second, ctx.arg.ltoObjPath + Twine(i));
   }
 
-  bool savePrelink = config->saveTempsArgs.contains("prelink");
+  bool savePrelink = ctx.arg.saveTempsArgs.contains("prelink");
   std::vector<InputFile *> ret;
-  const char *ext = config->ltoEmitAsm ? ".s" : ".o";
+  const char *ext = ctx.arg.ltoEmitAsm ? ".s" : ".o";
   for (unsigned i = 0; i != maxTasks; ++i) {
     StringRef bitcodeFilePath;
     StringRef objBuf;
@@ -392,7 +392,7 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
     StringRef ltoObjName;
     if (bitcodeFilePath == "ld-temp.o") {
       ltoObjName =
-          saver().save(Twine(config->outputFile) + ".lto" +
+          saver().save(Twine(ctx.arg.outputFile) + ".lto" +
                        (i == 0 ? Twine("") : Twine('.') + Twine(i)) + ext);
     } else {
       StringRef directory = sys::path::parent_path(bitcodeFilePath);
@@ -402,16 +402,16 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
       StringRef baseName = bitcodeFilePath.ends_with(")")
                                ? sys::path::filename(bitcodeFilePath)
                                : sys::path::stem(bitcodeFilePath);
-      StringRef outputFileBaseName = sys::path::filename(config->outputFile);
+      StringRef outputFileBaseName = sys::path::filename(ctx.arg.outputFile);
       SmallString<256> path;
       sys::path::append(path, directory,
                         outputFileBaseName + ".lto." + baseName + ext);
       sys::path::remove_dots(path, true);
       ltoObjName = saver().save(path.str());
     }
-    if (savePrelink || config->ltoEmitAsm)
+    if (savePrelink || ctx.arg.ltoEmitAsm)
       saveBuffer(buf[i].second, ltoObjName);
-    if (!config->ltoEmitAsm)
+    if (!ctx.arg.ltoEmitAsm)
       ret.push_back(createObjFile(MemoryBufferRef(objBuf, ltoObjName)));
   }
   return ret;

diff  --git a/lld/ELF/LTO.h b/lld/ELF/LTO.h
index 405e7f4a649e84..6d60c5b65e9b1e 100644
--- a/lld/ELF/LTO.h
+++ b/lld/ELF/LTO.h
@@ -32,19 +32,20 @@ class LTO;
 }
 
 namespace lld::elf {
-
+struct Ctx;
 class BitcodeFile;
 class InputFile;
 
 class BitcodeCompiler {
 public:
-  BitcodeCompiler();
+  BitcodeCompiler(Ctx &ctx);
   ~BitcodeCompiler();
 
   void add(BitcodeFile &f);
   std::vector<InputFile *> compile();
 
 private:
+  Ctx &ctx;
   std::unique_ptr<llvm::lto::LTO> ltoObj;
   // An array of (module name, native relocatable file content) pairs.
   SmallVector<std::pair<std::string, SmallString<0>>, 0> buf;


        


More information about the llvm-commits mailing list