alexander-shaposhnikov created this revision. alexander-shaposhnikov added reviewers: nikic, aeubanks, tejohnson. alexander-shaposhnikov created this object with visibility "All Users". Herald added subscribers: Moerafaat, zero9178, bzcheeseman, sdasgup3, wenzhicui, wrengr, ormris, cota, teijeong, rdzhabarov, tatianashp, msifontes, jurahul, Kayjukh, grosul1, Joonsoo, liufengdb, aartbik, mgester, arpith-jacob, antiagainst, shauheen, rriddle, mehdi_amini, hiraditya. Herald added a project: All. alexander-shaposhnikov requested review of this revision. Herald added subscribers: cfe-commits, stephenneuendorffer, nicolasvasilache. Herald added projects: clang, LLVM.
This diff splits out (from LLVMCore) IR printing passes into IRPrinter. This structure is similar to what we already have for IRReader and enables us to avoid circular dependencies between LLVMCore and Analysis. The legacy interface is left unchanged, once the legacy pass manager is removed (in the future) we will be able to clean it up further. The bazel build configuration has been updated as well. Test plan: 1/ Tested the following cmake configurations: static/dynamic linking * lld/gold * clang/gcc 2/ bazel build --config=generic_clang @llvm-project//... Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D138081 Files: clang/lib/CodeGen/BackendUtil.cpp clang/lib/CodeGen/CMakeLists.txt llvm/include/llvm/CodeGen/CodeGenPassBuilder.h llvm/include/llvm/IR/IRPrintingPasses.h llvm/include/llvm/IRPrinter/IRPrintingPasses.h llvm/lib/CMakeLists.txt llvm/lib/IR/IRPrintingPasses.cpp llvm/lib/IRPrinter/CMakeLists.txt llvm/lib/IRPrinter/IRPrintingPasses.cpp llvm/lib/Passes/CMakeLists.txt llvm/lib/Passes/PassBuilder.cpp llvm/lib/Target/DirectX/DirectXTargetMachine.cpp llvm/tools/llc/llc.cpp llvm/tools/opt/CMakeLists.txt llvm/tools/opt/NewPMDriver.cpp utils/bazel/llvm-project-overlay/clang/BUILD.bazel utils/bazel/llvm-project-overlay/llvm/BUILD.bazel utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
Index: utils/bazel/llvm-project-overlay/mlir/BUILD.bazel =================================================================== --- utils/bazel/llvm-project-overlay/mlir/BUILD.bazel +++ utils/bazel/llvm-project-overlay/mlir/BUILD.bazel @@ -6521,6 +6521,7 @@ ":Support", ":TranslateLib", "//llvm:Core", + "//llvm:IRPrinter", "//llvm:IRReader", "//llvm:Support", ], Index: utils/bazel/llvm-project-overlay/llvm/BUILD.bazel =================================================================== --- utils/bazel/llvm-project-overlay/llvm/BUILD.bazel +++ utils/bazel/llvm-project-overlay/llvm/BUILD.bazel @@ -915,6 +915,7 @@ ":BinaryFormat", ":BitReader", ":Core", + ":IRPrinter", ":IRReader", ":MC", ":MCParser", @@ -1375,6 +1376,22 @@ ], ) +cc_library( + name = "IRPrinter", + srcs = glob([ + "lib/IRPrinter/*.cpp", + "lib/IRPrinter/*.h", + ]), + hdrs = glob([ + "include/llvm/IRPrinter/*.h", + ]), + copts = llvm_copts, + deps = [ + ":Core", + ":Support", + ], +) + cc_library( name = "IRReader", srcs = glob([ @@ -1438,6 +1455,7 @@ ":BitWriter", ":Core", ":FrontendOpenMP", + ":IRPrinter", ":IRReader", ":InstCombine", ":Instrumentation", @@ -2260,6 +2278,7 @@ ":BitWriter", ":CodeGen", ":Core", + ":IRPrinter", ":IRReader", ":Linker", ":MC", @@ -2746,6 +2765,7 @@ ":BitReader", ":CodeGen", ":Core", + ":IRPrinter", ":IRReader", ":MC", ":Support", @@ -2788,6 +2808,7 @@ ":CodeGen", ":Core", ":ExecutionEngine", + ":IRPrinter", ":IRReader", ":Instrumentation", ":Interpreter", @@ -2886,6 +2907,7 @@ ":BitReader", ":BitWriter", ":Core", + ":IRPrinter", ":IRReader", ":Support", ], @@ -3178,6 +3200,7 @@ ":BitWriter", ":Core", ":IPO", + ":IRPrinter", ":IRReader", ":Support", ], @@ -3311,6 +3334,7 @@ ":BitWriter", ":Core", ":IPO", + ":IRPrinter", ":IRReader", ":Linker", ":Object", @@ -3371,6 +3395,7 @@ ":BitWriter", ":CodeGen", ":Core", + ":IRPrinter", ":IRReader", ":LTO", ":Support", @@ -3482,6 +3507,7 @@ deps = [ ":BitReader", ":BitWriter", + ":IRPrinter", ":IRReader", ":Support", ], @@ -4037,6 +4063,7 @@ deps = [ ":BitWriter", ":Core", + ":IRPrinter", ":IRReader", ":Support", ":TransformUtils", @@ -4162,6 +4189,7 @@ ":BitWriter", ":CodeGen", ":Core", + ":IRPrinter", ":IRReader", ":MC", ":Passes", @@ -4323,6 +4351,7 @@ ":BitWriter", ":CodeGen", ":Core", + ":IRPrinter", ":IRReader", ":Linker", ":Passes", @@ -4416,6 +4445,7 @@ deps = [ ":Core", ":Diff", + ":IRPrinter", ":IRReader", ":Support", ], @@ -4439,6 +4469,7 @@ ":CodeGen", ":Core", ":FuzzMutate", + ":IRPrinter", ":IRReader", ":Support", ":Target", @@ -4586,6 +4617,7 @@ ":BitReader", ":BitWriter", ":Core", + ":IRPrinter", ":IRReader", ":Support", ], Index: utils/bazel/llvm-project-overlay/clang/BUILD.bazel =================================================================== --- utils/bazel/llvm-project-overlay/clang/BUILD.bazel +++ utils/bazel/llvm-project-overlay/clang/BUILD.bazel @@ -1642,6 +1642,7 @@ "//llvm:FrontendHLSL", "//llvm:FrontendOpenMP", "//llvm:IPO", + "//llvm:IRPrinter", "//llvm:IRReader", "//llvm:InstCombine", "//llvm:Instrumentation", Index: llvm/tools/opt/NewPMDriver.cpp =================================================================== --- llvm/tools/opt/NewPMDriver.cpp +++ llvm/tools/opt/NewPMDriver.cpp @@ -21,7 +21,7 @@ #include "llvm/Bitcode/BitcodeWriterPass.h" #include "llvm/Config/llvm-config.h" #include "llvm/IR/Dominators.h" -#include "llvm/IR/IRPrintingPasses.h" +#include "llvm/IRPrinter/IRPrintingPasses.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/IR/PassManager.h" Index: llvm/tools/opt/CMakeLists.txt =================================================================== --- llvm/tools/opt/CMakeLists.txt +++ llvm/tools/opt/CMakeLists.txt @@ -14,6 +14,7 @@ Extensions IPO IRReader + IRPrinter InstCombine Instrumentation MC Index: llvm/tools/llc/llc.cpp =================================================================== --- llvm/tools/llc/llc.cpp +++ llvm/tools/llc/llc.cpp @@ -28,7 +28,7 @@ #include "llvm/IR/DataLayout.h" #include "llvm/IR/DiagnosticInfo.h" #include "llvm/IR/DiagnosticPrinter.h" -#include "llvm/IR/IRPrintingPasses.h" +#include "llvm/IRPrinter/IRPrintingPasses.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/LLVMRemarkStreamer.h" #include "llvm/IR/LegacyPassManager.h" Index: llvm/lib/Target/DirectX/DirectXTargetMachine.cpp =================================================================== --- llvm/lib/Target/DirectX/DirectXTargetMachine.cpp +++ llvm/lib/Target/DirectX/DirectXTargetMachine.cpp @@ -22,7 +22,7 @@ #include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/CodeGen/Passes.h" #include "llvm/CodeGen/TargetPassConfig.h" -#include "llvm/IR/IRPrintingPasses.h" +#include "llvm/IRPrinter/IRPrintingPasses.h" #include "llvm/IR/LegacyPassManager.h" #include "llvm/MC/MCSectionDXContainer.h" #include "llvm/MC/SectionKind.h" Index: llvm/lib/Passes/PassBuilder.cpp =================================================================== --- llvm/lib/Passes/PassBuilder.cpp +++ llvm/lib/Passes/PassBuilder.cpp @@ -75,7 +75,7 @@ #include "llvm/Analysis/TypeBasedAliasAnalysis.h" #include "llvm/IR/DebugInfo.h" #include "llvm/IR/Dominators.h" -#include "llvm/IR/IRPrintingPasses.h" +#include "llvm/IRPrinter/IRPrintingPasses.h" #include "llvm/IR/PassManager.h" #include "llvm/IR/PrintPasses.h" #include "llvm/IR/SafepointIRVerifier.h" Index: llvm/lib/Passes/CMakeLists.txt =================================================================== --- llvm/lib/Passes/CMakeLists.txt +++ llvm/lib/Passes/CMakeLists.txt @@ -20,6 +20,7 @@ Coroutines IPO InstCombine + IRPrinter ObjCARC Scalar Support Index: llvm/lib/IRPrinter/IRPrintingPasses.cpp =================================================================== --- /dev/null +++ llvm/lib/IRPrinter/IRPrintingPasses.cpp @@ -0,0 +1,63 @@ +//===--- IRPrintingPasses.cpp - Module and Function printing passes -------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// PrintModulePass and PrintFunctionPass implementations. +// +//===----------------------------------------------------------------------===// + +#include "llvm/IRPrinter/IRPrintingPasses.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/Module.h" +#include "llvm/IR/PrintPasses.h" +#include "llvm/Pass.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/raw_ostream.h" + +using namespace llvm; + +PrintModulePass::PrintModulePass() : OS(dbgs()) {} +PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner, + bool ShouldPreserveUseListOrder) + : OS(OS), Banner(Banner), + ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {} + +PreservedAnalyses PrintModulePass::run(Module &M, ModuleAnalysisManager &) { + if (llvm::isFunctionInPrintList("*")) { + if (!Banner.empty()) + OS << Banner << "\n"; + M.print(OS, nullptr, ShouldPreserveUseListOrder); + } else { + bool BannerPrinted = false; + for (const auto &F : M.functions()) { + if (llvm::isFunctionInPrintList(F.getName())) { + if (!BannerPrinted && !Banner.empty()) { + OS << Banner << "\n"; + BannerPrinted = true; + } + F.print(OS); + } + } + } + return PreservedAnalyses::all(); +} + +PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {} +PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner) + : OS(OS), Banner(Banner) {} + +PreservedAnalyses PrintFunctionPass::run(Function &F, + FunctionAnalysisManager &) { + if (isFunctionInPrintList(F.getName())) { + if (forcePrintModuleIR()) + OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent(); + else + OS << Banner << '\n' << static_cast<Value &>(F); + } + return PreservedAnalyses::all(); +} Index: llvm/lib/IRPrinter/CMakeLists.txt =================================================================== --- /dev/null +++ llvm/lib/IRPrinter/CMakeLists.txt @@ -0,0 +1,13 @@ +add_llvm_component_library(LLVMIRPrinter + IRPrintingPasses.cpp + + ADDITIONAL_HEADER_DIRS + ${LLVM_MAIN_INCLUDE_DIR}/llvm/IRPrinter + + DEPENDS + intrinsics_gen + + LINK_COMPONENTS + Core + Support + ) Index: llvm/lib/IR/IRPrintingPasses.cpp =================================================================== --- llvm/lib/IR/IRPrintingPasses.cpp +++ llvm/lib/IR/IRPrintingPasses.cpp @@ -22,63 +22,38 @@ using namespace llvm; -PrintModulePass::PrintModulePass() : OS(dbgs()) {} -PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner, - bool ShouldPreserveUseListOrder) - : OS(OS), Banner(Banner), - ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {} - -PreservedAnalyses PrintModulePass::run(Module &M, ModuleAnalysisManager &) { - if (llvm::isFunctionInPrintList("*")) { - if (!Banner.empty()) - OS << Banner << "\n"; - M.print(OS, nullptr, ShouldPreserveUseListOrder); - } - else { - bool BannerPrinted = false; - for(const auto &F : M.functions()) { - if (llvm::isFunctionInPrintList(F.getName())) { - if (!BannerPrinted && !Banner.empty()) { - OS << Banner << "\n"; - BannerPrinted = true; - } - F.print(OS); - } - } - } - return PreservedAnalyses::all(); -} - -PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {} -PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner) - : OS(OS), Banner(Banner) {} - -PreservedAnalyses PrintFunctionPass::run(Function &F, - FunctionAnalysisManager &) { - if (isFunctionInPrintList(F.getName())) { - if (forcePrintModuleIR()) - OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent(); - else - OS << Banner << '\n' << static_cast<Value &>(F); - } - return PreservedAnalyses::all(); -} - namespace { class PrintModulePassWrapper : public ModulePass { - PrintModulePass P; + raw_ostream &OS; + std::string Banner; + bool ShouldPreserveUseListOrder; public: static char ID; - PrintModulePassWrapper() : ModulePass(ID) {} + PrintModulePassWrapper() : ModulePass(ID), OS(dbgs()) {} PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner, bool ShouldPreserveUseListOrder) - : ModulePass(ID), P(OS, Banner, ShouldPreserveUseListOrder) {} + : ModulePass(ID), OS(OS), Banner(Banner), + ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {} bool runOnModule(Module &M) override { - ModuleAnalysisManager DummyMAM; - P.run(M, DummyMAM); + if (llvm::isFunctionInPrintList("*")) { + if (!Banner.empty()) + OS << Banner << "\n"; + M.print(OS, nullptr, ShouldPreserveUseListOrder); + } else { + bool BannerPrinted = false; + for (const auto &F : M.functions()) { + if (llvm::isFunctionInPrintList(F.getName())) { + if (!BannerPrinted && !Banner.empty()) { + OS << Banner << "\n"; + BannerPrinted = true; + } + F.print(OS); + } + } + } return false; } @@ -90,18 +65,24 @@ }; class PrintFunctionPassWrapper : public FunctionPass { - PrintFunctionPass P; + raw_ostream &OS; + std::string Banner; public: static char ID; - PrintFunctionPassWrapper() : FunctionPass(ID) {} + PrintFunctionPassWrapper() : FunctionPass(ID), OS(dbgs()) {} PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner) - : FunctionPass(ID), P(OS, Banner) {} + : FunctionPass(ID), OS(OS), Banner(Banner) {} // This pass just prints a banner followed by the function as it's processed. bool runOnFunction(Function &F) override { - FunctionAnalysisManager DummyFAM; - P.run(F, DummyFAM); + if (isFunctionInPrintList(F.getName())) { + if (forcePrintModuleIR()) + OS << Banner << " (function: " << F.getName() << ")\n" + << *F.getParent(); + else + OS << Banner << '\n' << static_cast<Value &>(F); + } return false; } @@ -112,7 +93,7 @@ StringRef getPassName() const override { return "Print Function IR"; } }; -} +} // namespace char PrintModulePassWrapper::ID = 0; INITIALIZE_PASS(PrintModulePassWrapper, "print-module", @@ -133,7 +114,7 @@ } bool llvm::isIRPrintingPass(Pass *P) { - const char *PID = (const char*)P->getPassID(); + const char *PID = (const char *)P->getPassID(); return (PID == &PrintModulePassWrapper::ID) || (PID == &PrintFunctionPassWrapper::ID); Index: llvm/lib/CMakeLists.txt =================================================================== --- llvm/lib/CMakeLists.txt +++ llvm/lib/CMakeLists.txt @@ -7,6 +7,7 @@ add_subdirectory(FuzzMutate) add_subdirectory(FileCheck) add_subdirectory(InterfaceStub) +add_subdirectory(IRPrinter) add_subdirectory(IRReader) add_subdirectory(CodeGen) add_subdirectory(BinaryFormat) Index: llvm/include/llvm/IRPrinter/IRPrintingPasses.h =================================================================== --- /dev/null +++ llvm/include/llvm/IRPrinter/IRPrintingPasses.h @@ -0,0 +1,67 @@ +//===- IRPrintingPasses.h - Passes to print out IR constructs ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +/// \file +/// +/// This file defines passes to print out IR in various granularities. The +/// PrintModulePass pass simply prints out the entire module when it is +/// executed. The PrintFunctionPass class is designed to be pipelined with +/// other FunctionPass's, and prints out the functions of the module as they +/// are processed. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_IRPRINTER_IRPRINTINGPASSES_H +#define LLVM_IRPRINTER_IRPRINTINGPASSES_H + +#include "llvm/IR/PassManager.h" +#include <string> + +namespace llvm { +class raw_ostream; +class StringRef; +class Function; +class Module; +class Pass; + +/// Pass for printing a Module as LLVM's text IR assembly. +/// +/// Note: This pass is for use with the new pass manager. Use the create...Pass +/// functions above to create passes for use with the legacy pass manager. +class PrintModulePass : public PassInfoMixin<PrintModulePass> { + raw_ostream &OS; + std::string Banner; + bool ShouldPreserveUseListOrder; + +public: + PrintModulePass(); + PrintModulePass(raw_ostream &OS, const std::string &Banner = "", + bool ShouldPreserveUseListOrder = false); + + PreservedAnalyses run(Module &M, AnalysisManager<Module> &); + static bool isRequired() { return true; } +}; + +/// Pass for printing a Function as LLVM's text IR assembly. +/// +/// Note: This pass is for use with the new pass manager. Use the create...Pass +/// functions above to create passes for use with the legacy pass manager. +class PrintFunctionPass : public PassInfoMixin<PrintFunctionPass> { + raw_ostream &OS; + std::string Banner; + +public: + PrintFunctionPass(); + PrintFunctionPass(raw_ostream &OS, const std::string &Banner = ""); + + PreservedAnalyses run(Function &F, AnalysisManager<Function> &); + static bool isRequired() { return true; } +}; + +} // namespace llvm + +#endif Index: llvm/include/llvm/IR/IRPrintingPasses.h =================================================================== --- llvm/include/llvm/IR/IRPrintingPasses.h +++ llvm/include/llvm/IR/IRPrintingPasses.h @@ -7,18 +7,14 @@ //===----------------------------------------------------------------------===// /// \file /// -/// This file defines passes to print out IR in various granularities. The -/// PrintModulePass pass simply prints out the entire module when it is -/// executed. The PrintFunctionPass class is designed to be pipelined with -/// other FunctionPass's, and prints out the functions of the module as they -/// are processed. +/// This file contains an interface for creating legacy passes to print out IR +/// in various granularities. /// //===----------------------------------------------------------------------===// #ifndef LLVM_IR_IRPRINTINGPASSES_H #define LLVM_IR_IRPRINTINGPASSES_H -#include "llvm/IR/PassManager.h" #include <string> namespace llvm { @@ -50,40 +46,6 @@ /// Return true if a pass is for IR printing. bool isIRPrintingPass(Pass *P); -/// Pass for printing a Module as LLVM's text IR assembly. -/// -/// Note: This pass is for use with the new pass manager. Use the create...Pass -/// functions above to create passes for use with the legacy pass manager. -class PrintModulePass : public PassInfoMixin<PrintModulePass> { - raw_ostream &OS; - std::string Banner; - bool ShouldPreserveUseListOrder; - -public: - PrintModulePass(); - PrintModulePass(raw_ostream &OS, const std::string &Banner = "", - bool ShouldPreserveUseListOrder = false); - - PreservedAnalyses run(Module &M, AnalysisManager<Module> &); - static bool isRequired() { return true; } -}; - -/// Pass for printing a Function as LLVM's text IR assembly. -/// -/// Note: This pass is for use with the new pass manager. Use the create...Pass -/// functions above to create passes for use with the legacy pass manager. -class PrintFunctionPass : public PassInfoMixin<PrintFunctionPass> { - raw_ostream &OS; - std::string Banner; - -public: - PrintFunctionPass(); - PrintFunctionPass(raw_ostream &OS, const std::string &Banner = ""); - - PreservedAnalyses run(Function &F, AnalysisManager<Function> &); - static bool isRequired() { return true; } -}; - } // namespace llvm #endif Index: llvm/include/llvm/CodeGen/CodeGenPassBuilder.h =================================================================== --- llvm/include/llvm/CodeGen/CodeGenPassBuilder.h +++ llvm/include/llvm/CodeGen/CodeGenPassBuilder.h @@ -29,7 +29,7 @@ #include "llvm/CodeGen/PreISelIntrinsicLowering.h" #include "llvm/CodeGen/ReplaceWithVeclib.h" #include "llvm/CodeGen/UnreachableBlockElim.h" -#include "llvm/IR/IRPrintingPasses.h" +#include "llvm/IRPrinter/IRPrintingPasses.h" #include "llvm/IR/PassManager.h" #include "llvm/IR/Verifier.h" #include "llvm/MC/MCAsmInfo.h" Index: clang/lib/CodeGen/CMakeLists.txt =================================================================== --- clang/lib/CodeGen/CMakeLists.txt +++ clang/lib/CodeGen/CMakeLists.txt @@ -10,6 +10,7 @@ FrontendHLSL FrontendOpenMP IPO + IRPrinter IRReader AggressiveInstCombine InstCombine Index: clang/lib/CodeGen/BackendUtil.cpp =================================================================== --- clang/lib/CodeGen/BackendUtil.cpp +++ clang/lib/CodeGen/BackendUtil.cpp @@ -31,7 +31,7 @@ #include "llvm/CodeGen/TargetSubtargetInfo.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/DebugInfo.h" -#include "llvm/IR/IRPrintingPasses.h" +#include "llvm/IRPrinter/IRPrintingPasses.h" #include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Module.h" #include "llvm/IR/ModuleSummaryIndex.h"
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits