[lld] [llvm] [lld][LTO] Teach LTO to print pipeline passes (PR #101018)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 29 07:16:30 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lld

Author: None (macurtis-amd)

<details>
<summary>Changes</summary>

I found useful while debugging code generation differences between old and new offloading drivers.
No function change (intended).

---
Full diff: https://github.com/llvm/llvm-project/pull/101018.diff


7 Files Affected:

- (modified) lld/ELF/Config.h (+1) 
- (modified) lld/ELF/Driver.cpp (+1) 
- (modified) lld/ELF/LTO.cpp (+1) 
- (modified) lld/ELF/Options.td (+2) 
- (added) lld/test/ELF/lto/print-pipeline-passes.ll (+15) 
- (modified) llvm/include/llvm/LTO/Config.h (+6-2) 
- (modified) llvm/lib/LTO/LTOBackend.cpp (+10) 


``````````diff
diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index 6abd929d2343d..65216a47ecbdf 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -260,6 +260,7 @@ struct Config {
   bool ignoreFunctionAddressEquality;
   bool ltoCSProfileGenerate;
   bool ltoPGOWarnMismatch;
+  bool ltoPrintPipelinePasses;
   bool ltoDebugPassManager;
   bool ltoEmitAsm;
   bool ltoUniqueBasicBlockSectionNames;
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 7e0a5a1937c7f..814145ae41d95 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -1313,6 +1313,7 @@ static void readConfigs(opt::InputArgList &args) {
   config->ltoCSProfileFile = args.getLastArgValue(OPT_lto_cs_profile_file);
   config->ltoPGOWarnMismatch = args.hasFlag(OPT_lto_pgo_warn_mismatch,
                                             OPT_no_lto_pgo_warn_mismatch, true);
+  config->ltoPrintPipelinePasses = args.hasArg(OPT_lto_print_pipeline_passes);
   config->ltoDebugPassManager = args.hasArg(OPT_lto_debug_pass_manager);
   config->ltoEmitAsm = args.hasArg(OPT_lto_emit_asm);
   config->ltoNewPmPasses = args.getLastArgValue(OPT_lto_newpm_passes);
diff --git a/lld/ELF/LTO.cpp b/lld/ELF/LTO.cpp
index 935d0a9eab9ee..0e56ee4a86f88 100644
--- a/lld/ELF/LTO.cpp
+++ b/lld/ELF/LTO.cpp
@@ -127,6 +127,7 @@ static lto::Config createConfig() {
   c.SampleProfile = std::string(config->ltoSampleProfile);
   for (StringRef pluginFn : config->passPlugins)
     c.PassPlugins.push_back(std::string(pluginFn));
+  c.PrintPipelinePasses = config->ltoPrintPipelinePasses;
   c.DebugPassManager = config->ltoDebugPassManager;
   c.DwoDir = std::string(config->dwoDir);
 
diff --git a/lld/ELF/Options.td b/lld/ELF/Options.td
index 74733efb28ff5..49a86c316ec46 100644
--- a/lld/ELF/Options.td
+++ b/lld/ELF/Options.td
@@ -610,6 +610,8 @@ def lto: JJ<"lto=">, HelpText<"Set LTO backend">,
                  MetaVarName<"[full,thin]">;
 def lto_aa_pipeline: JJ<"lto-aa-pipeline=">,
   HelpText<"AA pipeline to run during LTO. Used in conjunction with -lto-newpm-passes">;
+def lto_print_pipeline_passes: FF<"lto-print-pipeline-passes">,
+  HelpText<"Print a '-passes' compatible string describing the LTO pipeline (best-effort only).">;
 def lto_debug_pass_manager: FF<"lto-debug-pass-manager">,
   HelpText<"Debug new pass manager">;
 def lto_emit_asm: FF<"lto-emit-asm">,
diff --git a/lld/test/ELF/lto/print-pipeline-passes.ll b/lld/test/ELF/lto/print-pipeline-passes.ll
new file mode 100644
index 0000000000000..0ff42eebba296
--- /dev/null
+++ b/lld/test/ELF/lto/print-pipeline-passes.ll
@@ -0,0 +1,15 @@
+; REQUIRES: x86
+
+; RUN: llvm-as %s -o %t.o
+; RUN: ld.lld --lto-print-pipeline-passes -o %t.out.o %t.o 2>&1 | FileCheck %s
+
+; CHECK: pipeline-passes: verify,{{.*}},verify
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+ at llvm.compiler.used = appending global [1 x ptr] [ptr @main], section "llvm.metadata"
+
+define hidden void @main() {
+  ret void
+}
diff --git a/llvm/include/llvm/LTO/Config.h b/llvm/include/llvm/LTO/Config.h
index 482b6e55a19d3..063bf5aae7639 100644
--- a/llvm/include/llvm/LTO/Config.h
+++ b/llvm/include/llvm/LTO/Config.h
@@ -165,6 +165,10 @@ struct Config {
   /// Whether to emit the pass manager debuggging informations.
   bool DebugPassManager = false;
 
+  /// Print a '-passes' compatible string describing the pipeline (best-effort
+  /// only).
+  bool PrintPipelinePasses = false;
+
   /// Statistics output file path.
   std::string StatsFile;
 
@@ -302,7 +306,7 @@ struct LTOLLVMContext : LLVMContext {
   DiagnosticHandlerFunction DiagHandler;
 };
 
-}
-}
+} // namespace lto
+} // namespace llvm
 
 #endif
diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp
index d5d642f0d25e6..949850a3c791b 100644
--- a/llvm/lib/LTO/LTOBackend.cpp
+++ b/llvm/lib/LTO/LTOBackend.cpp
@@ -335,6 +335,16 @@ static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
   if (!Conf.DisableVerify)
     MPM.addPass(VerifierPass());
 
+  if (Conf.PrintPipelinePasses) {
+    std::string PipelineStr;
+    raw_string_ostream OS(PipelineStr);
+    MPM.printPipeline(OS, [&PIC](StringRef ClassName) {
+      auto PassName = PIC.getPassNameForClassName(ClassName);
+      return PassName.empty() ? ClassName : PassName;
+    });
+    outs() << "pipeline-passes: " << PipelineStr << '\n';
+  }
+
   MPM.run(Mod, MAM);
 }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/101018


More information about the llvm-commits mailing list