From a1441ca74708a557606bf68d42fca40bb03dbd74 Mon Sep 17 00:00:00 2001 From: Tarun Prabhu Date: Thu, 29 Aug 2024 16:21:43 -0600 Subject: [PATCH] [flang][Driver] Add support for -mllvm -print-pipeline-passes The behavior deliberately mimics that of clang. Ideally, -print-pipeline-passes should be a first-class driver option. Notes to this effect have been added in the appropriate places in both flang and clang. --------- Co-authored-by: Tarun Prabhu --- clang/lib/CodeGen/BackendUtil.cpp | 2 ++ flang/lib/Frontend/FrontendActions.cpp | 18 ++++++++++++++++++ flang/test/Driver/print-pipeline-passes.f90 | 7 +++++++ 3 files changed, 27 insertions(+) create mode 100644 flang/test/Driver/print-pipeline-passes.f90 diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 026f16484c09..564efa3181d1 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -1095,6 +1095,8 @@ void EmitAssemblyHelper::RunOptimizationPipeline( TheModule->addModuleFlag(llvm::Module::Error, "UnifiedLTO", uint32_t(1)); } + // FIXME: This should eventually be replaced by a first-class driver option. + // This should be done for both clang and flang simultaneously. // Print a textual, '-passes=' compatible, representation of pipeline if // requested. if (PrintPipelinePasses) { diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp index c9991ff18d17..cda82bcb7ecc 100644 --- a/flang/lib/Frontend/FrontendActions.cpp +++ b/flang/lib/Frontend/FrontendActions.cpp @@ -79,6 +79,10 @@ #include "flang/Tools/CLOptions.inc" +namespace llvm { +extern cl::opt PrintPipelinePasses; +} // namespace llvm + using namespace Fortran::frontend; // Declare plugin extension function declarations. @@ -1016,6 +1020,20 @@ void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) { else if (action == BackendActionTy::Backend_EmitLL) mpm.addPass(llvm::PrintModulePass(os)); + // FIXME: This should eventually be replaced by a first-class driver option. + // This should be done for both flang and clang simultaneously. + // Print a textual, '-passes=' compatible, representation of pipeline if + // requested. In this case, don't run the passes. This mimics the behavior of + // clang. + if (llvm::PrintPipelinePasses) { + mpm.printPipeline(llvm::outs(), [&pic](llvm::StringRef className) { + auto passName = pic.getPassNameForClassName(className); + return passName.empty() ? className : passName; + }); + llvm::outs() << "\n"; + return; + } + // Run the passes. mpm.run(*llvmModule, mam); } diff --git a/flang/test/Driver/print-pipeline-passes.f90 b/flang/test/Driver/print-pipeline-passes.f90 new file mode 100644 index 000000000000..72c213802508 --- /dev/null +++ b/flang/test/Driver/print-pipeline-passes.f90 @@ -0,0 +1,7 @@ +! RUN: %flang_fc1 -mllvm -print-pipeline-passes -emit-llvm-bc -o /dev/null -O0 %s 2>&1 | FileCheck %s + +! Just check a few passes to ensure that something reasonable is being printed. +! CHECK: always-inline +! CHECK-SAME: annotation-remarks + +end program