|
| 1 | +//===- llvm/unittest/CodeGen/CodeGenPassBuilderTest.cpp -------------------===// |
| 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 | +#include "llvm/Passes/CodeGenPassBuilder.h" |
| 10 | +#include "llvm/CodeGen/MachinePassManager.h" |
| 11 | +#include "llvm/CodeGen/TargetInstrInfo.h" |
| 12 | +#include "llvm/CodeGen/TargetLowering.h" |
| 13 | +#include "llvm/MC/TargetRegistry.h" |
| 14 | +#include "llvm/Passes/PassBuilder.h" |
| 15 | +#include "llvm/Target/TargetMachine.h" |
| 16 | + |
| 17 | +#include "gtest/gtest.h" |
| 18 | + |
| 19 | +using namespace llvm; |
| 20 | + |
| 21 | +namespace { |
| 22 | + |
| 23 | +class TestTargetMachine : public LLVMTargetMachine { |
| 24 | +public: |
| 25 | + TestTargetMachine() |
| 26 | + : LLVMTargetMachine(Target(), "", Triple(""), "", "", TargetOptions(), |
| 27 | + Reloc::Static, CodeModel::Small, |
| 28 | + CodeGenOptLevel::Default) {} |
| 29 | +}; |
| 30 | + |
| 31 | +TestTargetMachine &createTargetMachine() { |
| 32 | + static TestTargetMachine TM; |
| 33 | + return TM; |
| 34 | +} |
| 35 | + |
| 36 | +struct DisabledMachineFunctionPass |
| 37 | + : public PassInfoMixin<DisabledMachineFunctionPass> { |
| 38 | + PreservedAnalyses run(MachineFunction &, MachineFunctionAnalysisManager &) { |
| 39 | + return PreservedAnalyses::all(); |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +struct ReplacedMachineFunctionPass |
| 44 | + : public PassInfoMixin<ReplacedMachineFunctionPass> { |
| 45 | + PreservedAnalyses run(MachineFunction &, MachineFunctionAnalysisManager &) { |
| 46 | + return PreservedAnalyses::all(); |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +class TestCodeGenPassBuilder |
| 51 | + : public CodeGenPassBuilder<TestCodeGenPassBuilder, TestTargetMachine> { |
| 52 | + |
| 53 | +public: |
| 54 | + explicit TestCodeGenPassBuilder(PassBuilder &PB) |
| 55 | + : CodeGenPassBuilder(createTargetMachine(), CGPassBuilderOption(), PB) { |
| 56 | + // Declare disabled passes in constructor. |
| 57 | + disablePass<NoOpModulePass>(3); // Disable the third NoOpModulePass. |
| 58 | + disablePass<DisabledMachineFunctionPass>(); |
| 59 | + } |
| 60 | + |
| 61 | + // Override substitutePass is also OK. |
| 62 | + // template <typename PassT> auto substitutePass() { |
| 63 | + // if constexpr (std::is_same_v<PassT, ReplacedMachineFunctionPass>) |
| 64 | + // return NoOpMachineFunctionPass(); |
| 65 | + // else |
| 66 | + // return; |
| 67 | + // } |
| 68 | + |
| 69 | + void buildTestPipeline(ModulePassManager &MPM) { |
| 70 | + addModulePass<NoOpModulePass, NoOpModulePass>(); |
| 71 | + addFunctionPass<NoOpFunctionPass>(); |
| 72 | + addModulePass<NoOpModulePass>(); |
| 73 | + addMachineFunctionPass<DisabledMachineFunctionPass>(); |
| 74 | + addFunctionPass(NoOpFunctionPass()); |
| 75 | + addMachineFunctionPass<NoOpMachineFunctionPass, |
| 76 | + ReplacedMachineFunctionPass>(); |
| 77 | + mergePassManager(); |
| 78 | + MPM.addPass(std::move(getMPM())); |
| 79 | + getMPM() = ModulePassManager(); |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +class CodeGenPassBuilderTest : public testing::Test { |
| 84 | +public: |
| 85 | + CodeGenPassBuilderTest() : PB(&createTargetMachine()) { |
| 86 | + PIC.addClassToPassName(NoOpModulePass::name(), "no-op-module"); |
| 87 | + PIC.addClassToPassName(NoOpFunctionPass::name(), "no-op-function"); |
| 88 | + PIC.addClassToPassName(NoOpMachineFunctionPass::name(), |
| 89 | + "no-op-machine-function"); |
| 90 | + PIC.addClassToPassName(DisabledMachineFunctionPass::name(), "disabled"); |
| 91 | + PIC.addClassToPassName(ReplacedMachineFunctionPass::name(), "replaced"); |
| 92 | + } |
| 93 | + |
| 94 | + void buildPipeline(ModulePassManager &MPM) { |
| 95 | + TestCodeGenPassBuilder CGPB(PB); |
| 96 | + CGPB.buildTestPipeline(MPM); |
| 97 | + } |
| 98 | + |
| 99 | + std::string getPipelineText(ModulePassManager &MPM) { |
| 100 | + std::string PipelineText; |
| 101 | + raw_string_ostream OS(PipelineText); |
| 102 | + MPM.printPipeline( |
| 103 | + OS, [&](StringRef S) { return PIC.getPassNameForClassName(S); }); |
| 104 | + return PipelineText; |
| 105 | + } |
| 106 | + PassInstrumentationCallbacks PIC; |
| 107 | + PassBuilder PB; |
| 108 | +}; |
| 109 | + |
| 110 | +} // namespace |
| 111 | + |
| 112 | +using PassBuilderBase = |
| 113 | + CodeGenPassBuilder<TestCodeGenPassBuilder, TestTargetMachine>; |
| 114 | + |
| 115 | +// Add a specialization to substitute a pass. |
| 116 | +template <> |
| 117 | +template <> |
| 118 | +auto PassBuilderBase::substitutePass<ReplacedMachineFunctionPass>() { |
| 119 | + return NoOpMachineFunctionPass(); |
| 120 | +} |
| 121 | + |
| 122 | +TEST_F(CodeGenPassBuilderTest, Basic) { |
| 123 | + ModulePassManager MPM; |
| 124 | + buildPipeline(MPM); |
| 125 | + EXPECT_EQ(getPipelineText(MPM), |
| 126 | + "no-op-module,no-op-module,function(no-op-function,no-op-function," |
| 127 | + "machine-function(no-op-machine-function,no-op-machine-function))"); |
| 128 | +} |
0 commit comments