Skip to content

Commit 7059a6c

Browse files
[IR] Split out IR printing passes into IRPrinter
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 (this is a preparation for https://reviews.llvm.org/D137768). 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//... Differential revision: https://reviews.llvm.org/D138081
1 parent 07015e1 commit 7059a6c

File tree

18 files changed

+222
-101
lines changed

18 files changed

+222
-101
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "llvm/CodeGen/TargetSubtargetInfo.h"
3232
#include "llvm/IR/DataLayout.h"
3333
#include "llvm/IR/DebugInfo.h"
34-
#include "llvm/IR/IRPrintingPasses.h"
34+
#include "llvm/IRPrinter/IRPrintingPasses.h"
3535
#include "llvm/IR/LegacyPassManager.h"
3636
#include "llvm/IR/Module.h"
3737
#include "llvm/IR/ModuleSummaryIndex.h"

clang/lib/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ set(LLVM_LINK_COMPONENTS
1010
FrontendHLSL
1111
FrontendOpenMP
1212
IPO
13+
IRPrinter
1314
IRReader
1415
AggressiveInstCombine
1516
InstCombine

llvm/include/llvm/CodeGen/CodeGenPassBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
3030
#include "llvm/CodeGen/ReplaceWithVeclib.h"
3131
#include "llvm/CodeGen/UnreachableBlockElim.h"
32-
#include "llvm/IR/IRPrintingPasses.h"
32+
#include "llvm/IRPrinter/IRPrintingPasses.h"
3333
#include "llvm/IR/PassManager.h"
3434
#include "llvm/IR/Verifier.h"
3535
#include "llvm/MC/MCAsmInfo.h"

llvm/include/llvm/IR/IRPrintingPasses.h

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,14 @@
77
//===----------------------------------------------------------------------===//
88
/// \file
99
///
10-
/// This file defines passes to print out IR in various granularities. The
11-
/// PrintModulePass pass simply prints out the entire module when it is
12-
/// executed. The PrintFunctionPass class is designed to be pipelined with
13-
/// other FunctionPass's, and prints out the functions of the module as they
14-
/// are processed.
10+
/// This file contains an interface for creating legacy passes to print out IR
11+
/// in various granularities.
1512
///
1613
//===----------------------------------------------------------------------===//
1714

1815
#ifndef LLVM_IR_IRPRINTINGPASSES_H
1916
#define LLVM_IR_IRPRINTINGPASSES_H
2017

21-
#include "llvm/IR/PassManager.h"
2218
#include <string>
2319

2420
namespace llvm {
@@ -50,40 +46,6 @@ void printLLVMNameWithoutPrefix(raw_ostream &OS, StringRef Name);
5046
/// Return true if a pass is for IR printing.
5147
bool isIRPrintingPass(Pass *P);
5248

53-
/// Pass for printing a Module as LLVM's text IR assembly.
54-
///
55-
/// Note: This pass is for use with the new pass manager. Use the create...Pass
56-
/// functions above to create passes for use with the legacy pass manager.
57-
class PrintModulePass : public PassInfoMixin<PrintModulePass> {
58-
raw_ostream &OS;
59-
std::string Banner;
60-
bool ShouldPreserveUseListOrder;
61-
62-
public:
63-
PrintModulePass();
64-
PrintModulePass(raw_ostream &OS, const std::string &Banner = "",
65-
bool ShouldPreserveUseListOrder = false);
66-
67-
PreservedAnalyses run(Module &M, AnalysisManager<Module> &);
68-
static bool isRequired() { return true; }
69-
};
70-
71-
/// Pass for printing a Function as LLVM's text IR assembly.
72-
///
73-
/// Note: This pass is for use with the new pass manager. Use the create...Pass
74-
/// functions above to create passes for use with the legacy pass manager.
75-
class PrintFunctionPass : public PassInfoMixin<PrintFunctionPass> {
76-
raw_ostream &OS;
77-
std::string Banner;
78-
79-
public:
80-
PrintFunctionPass();
81-
PrintFunctionPass(raw_ostream &OS, const std::string &Banner = "");
82-
83-
PreservedAnalyses run(Function &F, AnalysisManager<Function> &);
84-
static bool isRequired() { return true; }
85-
};
86-
8749
} // namespace llvm
8850

8951
#endif
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===- IRPrintingPasses.h - Passes to print out IR constructs ---*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
/// \file
9+
///
10+
/// This file defines passes to print out IR in various granularities. The
11+
/// PrintModulePass pass simply prints out the entire module when it is
12+
/// executed. The PrintFunctionPass class is designed to be pipelined with
13+
/// other FunctionPass's, and prints out the functions of the module as they
14+
/// are processed.
15+
///
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef LLVM_IRPRINTER_IRPRINTINGPASSES_H
19+
#define LLVM_IRPRINTER_IRPRINTINGPASSES_H
20+
21+
#include "llvm/IR/PassManager.h"
22+
#include <string>
23+
24+
namespace llvm {
25+
class raw_ostream;
26+
class StringRef;
27+
class Function;
28+
class Module;
29+
class Pass;
30+
31+
/// Pass (for the new pass manager) for printing a Module as
32+
/// LLVM's text IR assembly.
33+
class PrintModulePass : public PassInfoMixin<PrintModulePass> {
34+
raw_ostream &OS;
35+
std::string Banner;
36+
bool ShouldPreserveUseListOrder;
37+
38+
public:
39+
PrintModulePass();
40+
PrintModulePass(raw_ostream &OS, const std::string &Banner = "",
41+
bool ShouldPreserveUseListOrder = false);
42+
43+
PreservedAnalyses run(Module &M, AnalysisManager<Module> &);
44+
static bool isRequired() { return true; }
45+
};
46+
47+
/// Pass (for the new pass manager) for printing a Function as
48+
/// LLVM's text IR assembly.
49+
class PrintFunctionPass : public PassInfoMixin<PrintFunctionPass> {
50+
raw_ostream &OS;
51+
std::string Banner;
52+
53+
public:
54+
PrintFunctionPass();
55+
PrintFunctionPass(raw_ostream &OS, const std::string &Banner = "");
56+
57+
PreservedAnalyses run(Function &F, AnalysisManager<Function> &);
58+
static bool isRequired() { return true; }
59+
};
60+
61+
} // namespace llvm
62+
63+
#endif

llvm/lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_subdirectory(IR)
77
add_subdirectory(FuzzMutate)
88
add_subdirectory(FileCheck)
99
add_subdirectory(InterfaceStub)
10+
add_subdirectory(IRPrinter)
1011
add_subdirectory(IRReader)
1112
add_subdirectory(CodeGen)
1213
add_subdirectory(BinaryFormat)

llvm/lib/IR/IRPrintingPasses.cpp

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// PrintModulePass and PrintFunctionPass implementations.
9+
// PrintModulePass and PrintFunctionPass implementations for the legacy pass
10+
// manager.
1011
//
1112
//===----------------------------------------------------------------------===//
1213

@@ -22,63 +23,38 @@
2223

2324
using namespace llvm;
2425

25-
PrintModulePass::PrintModulePass() : OS(dbgs()) {}
26-
PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner,
27-
bool ShouldPreserveUseListOrder)
28-
: OS(OS), Banner(Banner),
29-
ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
30-
31-
PreservedAnalyses PrintModulePass::run(Module &M, ModuleAnalysisManager &) {
32-
if (llvm::isFunctionInPrintList("*")) {
33-
if (!Banner.empty())
34-
OS << Banner << "\n";
35-
M.print(OS, nullptr, ShouldPreserveUseListOrder);
36-
}
37-
else {
38-
bool BannerPrinted = false;
39-
for(const auto &F : M.functions()) {
40-
if (llvm::isFunctionInPrintList(F.getName())) {
41-
if (!BannerPrinted && !Banner.empty()) {
42-
OS << Banner << "\n";
43-
BannerPrinted = true;
44-
}
45-
F.print(OS);
46-
}
47-
}
48-
}
49-
return PreservedAnalyses::all();
50-
}
51-
52-
PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
53-
PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
54-
: OS(OS), Banner(Banner) {}
55-
56-
PreservedAnalyses PrintFunctionPass::run(Function &F,
57-
FunctionAnalysisManager &) {
58-
if (isFunctionInPrintList(F.getName())) {
59-
if (forcePrintModuleIR())
60-
OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent();
61-
else
62-
OS << Banner << '\n' << static_cast<Value &>(F);
63-
}
64-
return PreservedAnalyses::all();
65-
}
66-
6726
namespace {
6827

6928
class PrintModulePassWrapper : public ModulePass {
70-
PrintModulePass P;
29+
raw_ostream &OS;
30+
std::string Banner;
31+
bool ShouldPreserveUseListOrder;
7132

7233
public:
7334
static char ID;
74-
PrintModulePassWrapper() : ModulePass(ID) {}
35+
PrintModulePassWrapper() : ModulePass(ID), OS(dbgs()) {}
7536
PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner,
7637
bool ShouldPreserveUseListOrder)
77-
: ModulePass(ID), P(OS, Banner, ShouldPreserveUseListOrder) {}
38+
: ModulePass(ID), OS(OS), Banner(Banner),
39+
ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
7840

7941
bool runOnModule(Module &M) override {
80-
ModuleAnalysisManager DummyMAM;
81-
P.run(M, DummyMAM);
42+
if (llvm::isFunctionInPrintList("*")) {
43+
if (!Banner.empty())
44+
OS << Banner << "\n";
45+
M.print(OS, nullptr, ShouldPreserveUseListOrder);
46+
} else {
47+
bool BannerPrinted = false;
48+
for (const auto &F : M.functions()) {
49+
if (llvm::isFunctionInPrintList(F.getName())) {
50+
if (!BannerPrinted && !Banner.empty()) {
51+
OS << Banner << "\n";
52+
BannerPrinted = true;
53+
}
54+
F.print(OS);
55+
}
56+
}
57+
}
8258
return false;
8359
}
8460

@@ -90,18 +66,24 @@ class PrintModulePassWrapper : public ModulePass {
9066
};
9167

9268
class PrintFunctionPassWrapper : public FunctionPass {
93-
PrintFunctionPass P;
69+
raw_ostream &OS;
70+
std::string Banner;
9471

9572
public:
9673
static char ID;
97-
PrintFunctionPassWrapper() : FunctionPass(ID) {}
74+
PrintFunctionPassWrapper() : FunctionPass(ID), OS(dbgs()) {}
9875
PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
99-
: FunctionPass(ID), P(OS, Banner) {}
76+
: FunctionPass(ID), OS(OS), Banner(Banner) {}
10077

10178
// This pass just prints a banner followed by the function as it's processed.
10279
bool runOnFunction(Function &F) override {
103-
FunctionAnalysisManager DummyFAM;
104-
P.run(F, DummyFAM);
80+
if (isFunctionInPrintList(F.getName())) {
81+
if (forcePrintModuleIR())
82+
OS << Banner << " (function: " << F.getName() << ")\n"
83+
<< *F.getParent();
84+
else
85+
OS << Banner << '\n' << static_cast<Value &>(F);
86+
}
10587
return false;
10688
}
10789

@@ -112,7 +94,7 @@ class PrintFunctionPassWrapper : public FunctionPass {
11294
StringRef getPassName() const override { return "Print Function IR"; }
11395
};
11496

115-
}
97+
} // namespace
11698

11799
char PrintModulePassWrapper::ID = 0;
118100
INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
@@ -133,7 +115,7 @@ FunctionPass *llvm::createPrintFunctionPass(llvm::raw_ostream &OS,
133115
}
134116

135117
bool llvm::isIRPrintingPass(Pass *P) {
136-
const char *PID = (const char*)P->getPassID();
118+
const char *PID = (const char *)P->getPassID();
137119

138120
return (PID == &PrintModulePassWrapper::ID) ||
139121
(PID == &PrintFunctionPassWrapper::ID);

llvm/lib/IRPrinter/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
add_llvm_component_library(LLVMIRPrinter
2+
IRPrintingPasses.cpp
3+
4+
ADDITIONAL_HEADER_DIRS
5+
${LLVM_MAIN_INCLUDE_DIR}/llvm/IRPrinter
6+
7+
DEPENDS
8+
intrinsics_gen
9+
10+
LINK_COMPONENTS
11+
Core
12+
Support
13+
)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===--- IRPrintingPasses.cpp - Module and Function printing passes -------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// PrintModulePass and PrintFunctionPass implementations.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "llvm/IRPrinter/IRPrintingPasses.h"
14+
#include "llvm/ADT/StringRef.h"
15+
#include "llvm/IR/Function.h"
16+
#include "llvm/IR/Module.h"
17+
#include "llvm/IR/PrintPasses.h"
18+
#include "llvm/Pass.h"
19+
#include "llvm/Support/Debug.h"
20+
#include "llvm/Support/raw_ostream.h"
21+
22+
using namespace llvm;
23+
24+
PrintModulePass::PrintModulePass() : OS(dbgs()) {}
25+
PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner,
26+
bool ShouldPreserveUseListOrder)
27+
: OS(OS), Banner(Banner),
28+
ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
29+
30+
PreservedAnalyses PrintModulePass::run(Module &M, ModuleAnalysisManager &) {
31+
if (llvm::isFunctionInPrintList("*")) {
32+
if (!Banner.empty())
33+
OS << Banner << "\n";
34+
M.print(OS, nullptr, ShouldPreserveUseListOrder);
35+
} else {
36+
bool BannerPrinted = false;
37+
for (const auto &F : M.functions()) {
38+
if (llvm::isFunctionInPrintList(F.getName())) {
39+
if (!BannerPrinted && !Banner.empty()) {
40+
OS << Banner << "\n";
41+
BannerPrinted = true;
42+
}
43+
F.print(OS);
44+
}
45+
}
46+
}
47+
return PreservedAnalyses::all();
48+
}
49+
50+
PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
51+
PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
52+
: OS(OS), Banner(Banner) {}
53+
54+
PreservedAnalyses PrintFunctionPass::run(Function &F,
55+
FunctionAnalysisManager &) {
56+
if (isFunctionInPrintList(F.getName())) {
57+
if (forcePrintModuleIR())
58+
OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent();
59+
else
60+
OS << Banner << '\n' << static_cast<Value &>(F);
61+
}
62+
return PreservedAnalyses::all();
63+
}

llvm/lib/Passes/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ add_llvm_component_library(LLVMPasses
2020
Coroutines
2121
IPO
2222
InstCombine
23+
IRPrinter
2324
ObjCARC
2425
Scalar
2526
Support

0 commit comments

Comments
 (0)