Skip to content

Commit d291f1f

Browse files
sperytaeubanks
authored andcommitted
[LegacyPM] Port example pass SimplifyCFG to new PM
This is part of effort in removing -enable-new-pm flag. As a prat of this effort one of example passes SimplifyCFG must be ported to new PM which will allow to remove the flag calls from the tests that are using this pass. Reviewed By: aeubanks Differential Revision: https://reviews.llvm.org/D137103
1 parent 5c126e3 commit d291f1f

18 files changed

+84
-149
lines changed
Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
set(LLVM_LINK_COMPONENTS
2-
Analysis
3-
Core
4-
Support
5-
)
1+
if(LLVM_EXAMPLEIRTRANSFORMS_LINK_INTO_TOOLS)
2+
message(WARNING "Setting LLVM_EXAMPLEIRTRANSFORMS_LINK_INTO_TOOLS=ON only makes sense for testing purpose")
3+
endif()
64

7-
add_llvm_example_library(ExampleIRTransforms
8-
InitializePasses.cpp
9-
SimplifyCFG.cpp
5+
# The plugin expects to not link against the Support and Core libraries,
6+
# but expects them to exist in the process loading the plugin. This doesn't
7+
# work with DLLs on Windows (where a shared library can't have undefined
8+
# references), so just skip this example on Windows.
9+
if (NOT WIN32)
10+
add_llvm_pass_plugin(ExampleIRTransforms
11+
SimplifyCFG.cpp
12+
DEPENDS
13+
intrinsics_gen
14+
BUILDTREE_ONLY
15+
)
1016

11-
ADDITIONAL_HEADER_DIRS
12-
13-
DEPENDS
14-
intrinsics_gen
15-
)
17+
install(TARGETS ${name} RUNTIME DESTINATION "${LLVM_EXAMPLES_INSTALL_DIR}")
18+
set_target_properties(${name} PROPERTIES FOLDER "Examples")
19+
endif()

llvm/examples/IRTransforms/InitializePasses.cpp

Lines changed: 0 additions & 21 deletions
This file was deleted.

llvm/examples/IRTransforms/InitializePasses.h

Lines changed: 0 additions & 23 deletions
This file was deleted.

llvm/examples/IRTransforms/SimplifyCFG.cpp

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,18 @@
2727
// predecessor, if that block has a single successor.
2828
//
2929
// TODOs
30-
// * Hook up pass to the new pass manager.
3130
// * Preserve LoopInfo.
3231
// * Add fixed point iteration to delete all dead blocks
3332
// * Add implementation using reachability to discover dead blocks.
3433
//===----------------------------------------------------------------------===//
3534

36-
#include "SimplifyCFG.h"
37-
#include "InitializePasses.h"
3835
#include "llvm/Analysis/DomTreeUpdater.h"
3936
#include "llvm/IR/Dominators.h"
4037
#include "llvm/IR/Function.h"
4138
#include "llvm/IR/PassManager.h"
4239
#include "llvm/IR/PatternMatch.h"
43-
#include "llvm/InitializePasses.h"
40+
#include "llvm/Passes/PassBuilder.h"
41+
#include "llvm/Passes/PassPlugin.h"
4442
#include "llvm/Support/CommandLine.h"
4543

4644
using namespace llvm;
@@ -369,46 +367,48 @@ static bool doSimplify_v3(Function &F, DominatorTree &DT) {
369367
}
370368

371369
namespace {
372-
struct SimplifyCFGLegacyPass : public FunctionPass {
373-
static char ID;
374-
SimplifyCFGLegacyPass() : FunctionPass(ID) {
375-
initializeSimplifyCFGLegacyPassPass(*PassRegistry::getPassRegistry());
376-
}
377-
378-
void getAnalysisUsage(AnalysisUsage &AU) const override {
379-
AU.addRequired<DominatorTreeWrapperPass>();
380-
// Version 1 of the implementation does not preserve the dominator tree.
381-
if (Version != V1)
382-
AU.addPreserved<DominatorTreeWrapperPass>();
383-
384-
FunctionPass::getAnalysisUsage(AU);
385-
}
386-
387-
bool runOnFunction(Function &F) override {
388-
if (skipFunction(F))
389-
return false;
390-
370+
struct SimplifyCFGPass : public PassInfoMixin<SimplifyCFGPass> {
371+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM) {
391372
switch (Version) {
392373
case V1:
393-
return doSimplify_v1(F);
374+
doSimplify_v1(F);
375+
break;
394376
case V2: {
395-
auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
396-
return doSimplify_v2(F, DT);
377+
DominatorTree &DT = FAM.getResult<DominatorTreeAnalysis>(F);
378+
doSimplify_v2(F, DT);
379+
break;
397380
}
398381
case V3: {
399-
auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
400-
return doSimplify_v3(F, DT);
382+
DominatorTree &DT = FAM.getResult<DominatorTreeAnalysis>(F);
383+
doSimplify_v3(F, DT);
384+
break;
401385
}
402386
}
403387

404-
llvm_unreachable("Unsupported version");
388+
return PreservedAnalyses::none();
405389
}
406390
};
407391
} // namespace
408392

409-
char SimplifyCFGLegacyPass::ID = 0;
410-
INITIALIZE_PASS_BEGIN(SimplifyCFGLegacyPass, DEBUG_TYPE,
411-
"Tutorial CFG simplification", false, false)
412-
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
413-
INITIALIZE_PASS_END(SimplifyCFGLegacyPass, DEBUG_TYPE,
414-
"Tutorial CFG simplifications", false, false)
393+
/* New PM Registration */
394+
llvm::PassPluginLibraryInfo getExampleIRTransformsPluginInfo() {
395+
return {LLVM_PLUGIN_API_VERSION, "SimplifyCFG", LLVM_VERSION_STRING,
396+
[](PassBuilder &PB) {
397+
PB.registerPipelineParsingCallback(
398+
[](StringRef Name, llvm::FunctionPassManager &PM,
399+
ArrayRef<llvm::PassBuilder::PipelineElement>) {
400+
if (Name == "tut-simplifycfg") {
401+
PM.addPass(SimplifyCFGPass());
402+
return true;
403+
}
404+
return false;
405+
});
406+
}};
407+
}
408+
409+
#ifndef LLVM_SIMPLIFYCFG_LINK_INTO_TOOLS
410+
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo
411+
llvmGetPassPluginInfo() {
412+
return getExampleIRTransformsPluginInfo();
413+
}
414+
#endif

llvm/examples/IRTransforms/SimplifyCFG.h

Lines changed: 0 additions & 24 deletions
This file was deleted.

llvm/test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ llvm_canonicalize_cmake_booleans(
1616
LLVM_BUILD_EXAMPLES
1717
LLVM_ENABLE_PLUGINS
1818
LLVM_BYE_LINK_INTO_TOOLS
19+
LLVM_EXAMPLEIRTRANSFORMS_LINK_INTO_TOOLS
1920
LLVM_HAVE_TF_AOT
2021
LLVM_HAVE_TFLITE
2122
LLVM_INLINER_MODEL_AUTOGENERATED
@@ -190,6 +191,7 @@ if(LLVM_BUILD_EXAMPLES)
190191
if (NOT WIN32)
191192
list(APPEND LLVM_TEST_DEPENDS
192193
Bye
194+
ExampleIRTransforms
193195
)
194196
endif()
195197
endif()

llvm/test/Examples/IRTransforms/SimplifyCFG/tut-simplify-cfg-blockaddress.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2-
; RUN: opt -tut-simplifycfg -tut-simplifycfg-version=v1 -enable-new-pm=0 -S < %s | FileCheck %s
3-
; RUN: opt -tut-simplifycfg -tut-simplifycfg-version=v2 -enable-new-pm=0 -S < %s | FileCheck %s
4-
; RUN: opt -tut-simplifycfg -tut-simplifycfg-version=v3 -enable-new-pm=0 -S < %s | FileCheck %s
2+
; RUN: opt %loadexampleirtransforms -passes=tut-simplifycfg -tut-simplifycfg-version=v1 -S < %s | FileCheck %s
3+
; RUN: opt %loadexampleirtransforms -passes=tut-simplifycfg -tut-simplifycfg-version=v2 -S < %s | FileCheck %s
4+
; RUN: opt %loadexampleirtransforms -passes=tut-simplifycfg -tut-simplifycfg-version=v3 -S < %s | FileCheck %s
55

66
define ptr @simp1(i32 %x) {
77
; CHECK-LABEL: @simp1(

llvm/test/Examples/IRTransforms/SimplifyCFG/tut-simplify-cfg1.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2-
; RUN: opt -tut-simplifycfg -tut-simplifycfg-version=v1 -enable-new-pm=0 -S < %s | FileCheck %s
3-
; RUN: opt -tut-simplifycfg -tut-simplifycfg-version=v2 -enable-new-pm=0 -S < %s | FileCheck %s
4-
; RUN: opt -tut-simplifycfg -tut-simplifycfg-version=v3 -enable-new-pm=0 -S < %s | FileCheck %s
2+
; RUN: opt %loadexampleirtransforms -passes=tut-simplifycfg -tut-simplifycfg-version=v1 -S < %s | FileCheck %s
3+
; RUN: opt %loadexampleirtransforms -passes=tut-simplifycfg -tut-simplifycfg-version=v2 -S < %s | FileCheck %s
4+
; RUN: opt %loadexampleirtransforms -passes=tut-simplifycfg -tut-simplifycfg-version=v3 -S < %s | FileCheck %s
55

66
define i32 @simp1() {
77
; CHECK-LABEL: @simp1(

llvm/test/Examples/IRTransforms/SimplifyCFG/tut-simplify-cfg2-dead-block-order.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2-
; RUN: opt -tut-simplifycfg -tut-simplifycfg-version=v1 -enable-new-pm=0 -S < %s | FileCheck %s
3-
; RUN: opt -tut-simplifycfg -tut-simplifycfg-version=v2 -enable-new-pm=0 -S < %s | FileCheck %s
4-
; RUN: opt -tut-simplifycfg -tut-simplifycfg-version=v3 -enable-new-pm=0 -S < %s | FileCheck %s
2+
; RUN: opt %loadexampleirtransforms -passes=tut-simplifycfg -tut-simplifycfg-version=v1 -S < %s | FileCheck %s
3+
; RUN: opt %loadexampleirtransforms -passes=tut-simplifycfg -tut-simplifycfg-version=v2 -S < %s | FileCheck %s
4+
; RUN: opt %loadexampleirtransforms -passes=tut-simplifycfg -tut-simplifycfg-version=v3 -S < %s | FileCheck %s
55

66
define i32 @remove_dead_blocks() {
77
; CHECK-LABEL: @remove_dead_blocks(

llvm/test/Examples/IRTransforms/SimplifyCFG/tut-simplify-cfg3-phis.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2-
; RUN: opt -tut-simplifycfg -tut-simplifycfg-version=v1 -enable-new-pm=0 -S < %s | FileCheck %s
3-
; RUN: opt -tut-simplifycfg -tut-simplifycfg-version=v2 -enable-new-pm=0 -S < %s | FileCheck %s
4-
; RUN: opt -tut-simplifycfg -tut-simplifycfg-version=v3 -enable-new-pm=0 -S < %s | FileCheck %s
2+
; RUN: opt %loadexampleirtransforms -passes=tut-simplifycfg -tut-simplifycfg-version=v1 -S < %s | FileCheck %s
3+
; RUN: opt %loadexampleirtransforms -passes=tut-simplifycfg -tut-simplifycfg-version=v2 -S < %s | FileCheck %s
4+
; RUN: opt %loadexampleirtransforms -passes=tut-simplifycfg -tut-simplifycfg-version=v3 -S < %s | FileCheck %s
55

66
define i32 @phi_cond_branch_eliminated() {
77
; CHECK-LABEL: @phi_cond_branch_eliminated(

0 commit comments

Comments
 (0)