Skip to content

Commit e475e34

Browse files
committed
Extract top function pass
1 parent 0978e56 commit e475e34

File tree

17 files changed

+284
-5
lines changed

17 files changed

+284
-5
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
[submodule "vitis-hls"]
88
path = vitis-hls
99
url = https://github.com/Xilinx/HLS
10+
[submodule "example/polybench"]
11+
path = example/polybench
12+
url = git@github.com:MatthiasJReisinger/PolyBenchC-4.2.1.git

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
4141
# ------------------------------------------------- Dependencies
4242

4343
# ------------------------------------------------- This project
44-
set(PHISM_MAIN_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include" ) # --src-root
44+
set(PHISM_MAIN_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include" ) # --src-root
4545
set(PHISM_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/include" ) # --includedir
4646

4747
set(PHISM_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
@@ -65,3 +65,4 @@ include_directories("${PROJECT_BINARY_DIR}/include")
6565

6666
add_subdirectory(lib)
6767
add_subdirectory(test)
68+
add_subdirectory(tools)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//===- PhismTransforms.h ----------------------------------------*- C++ -*-===//
2+
//
3+
// This file declares all the registration interfaces for Phism passes.
4+
//
5+
//===----------------------------------------------------------------------===//
6+
7+
#ifndef PHISM_MLIR_TRANSFORMS_PHISMTRANSFORMS_H
8+
#define PHISM_MLIR_TRANSFORMS_PHISMTRANSFORMS_H
9+
10+
namespace phism {
11+
void registerExtractTopFuncPass();
12+
void registerAllPhismPasses();
13+
} // namespace phism
14+
15+
#endif

lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
add_subdirectory(llvm)
2+
add_subdirectory(mlir)

lib/mlir/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(Transforms)

lib/mlir/Transforms/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
add_mlir_library(PhismTransforms
2+
ExtractTopFunc.cc
3+
PhismTransforms.cc
4+
5+
ADDITIONAL_HEADER_DIRS
6+
"${PHISM_MAIN_INCLUDE_DIR}/phism/mlir/Transforms"
7+
8+
LINK_LIBS PUBLIC
9+
MLIRAffine
10+
MLIRAnalysis
11+
MLIRPass
12+
MLIRTransforms
13+
MLIRTransformUtils
14+
MLIRIR
15+
MLIRStandard
16+
MLIRSupport
17+
MLIRAffineToStandard
18+
)
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//===- ExtractTopFunc.cc ----------------------------------------*- C++ -*-===//
2+
//
3+
// This file implements a pass that extract the specified top function out of
4+
// the given module.
5+
//
6+
//===----------------------------------------------------------------------===//
7+
8+
#include "phism/mlir/Transforms/PhismTransforms.h"
9+
10+
#include "mlir/Analysis/AffineAnalysis.h"
11+
#include "mlir/Analysis/AffineStructures.h"
12+
#include "mlir/Analysis/SliceAnalysis.h"
13+
#include "mlir/Dialect/Affine/IR/AffineOps.h"
14+
#include "mlir/IR/BlockAndValueMapping.h"
15+
#include "mlir/IR/Builders.h"
16+
#include "mlir/IR/Dominance.h"
17+
#include "mlir/IR/OpImplementation.h"
18+
#include "mlir/IR/PatternMatch.h"
19+
#include "mlir/IR/Types.h"
20+
#include "mlir/IR/Value.h"
21+
#include "mlir/Pass/Pass.h"
22+
#include "mlir/Transforms/DialectConversion.h"
23+
#include "mlir/Transforms/Passes.h"
24+
#include "mlir/Transforms/RegionUtils.h"
25+
#include "mlir/Transforms/Utils.h"
26+
27+
#include "llvm/ADT/SetVector.h"
28+
29+
using namespace mlir;
30+
using namespace llvm;
31+
using namespace phism;
32+
33+
/// Union the given function and all the others it calls into 'keep'.
34+
static void unionCallee(FuncOp f, ModuleOp m,
35+
SmallPtrSetImpl<Operation *> &keep) {
36+
std::deque<Operation *> worklist;
37+
worklist.push_back(f);
38+
keep.insert(f);
39+
40+
// A breadth first search on the call graph, starting from f.
41+
while (!worklist.empty()) {
42+
Operation *g = worklist.front();
43+
worklist.pop_front();
44+
45+
g->walk([&](Operation *op) {
46+
if (CallOp callOp = dyn_cast<CallOp>(op)) {
47+
FuncOp callee =
48+
dyn_cast_or_null<FuncOp>(m.lookupSymbol(callOp.getCallee()));
49+
assert(callee && "A callee cannot be resolved in the module.");
50+
51+
if (!keep.contains(callee)) {
52+
worklist.push_back(callee);
53+
keep.insert(callee);
54+
}
55+
}
56+
});
57+
}
58+
}
59+
60+
namespace {
61+
62+
/// Extract the specified top function out of its module.
63+
struct ExtractTopFuncPass
64+
: public PassWrapper<ExtractTopFuncPass, OperationPass<ModuleOp>> {
65+
66+
ExtractTopFuncPass() = default;
67+
ExtractTopFuncPass(const ExtractTopFuncPass &pass) {}
68+
69+
Option<std::string> topFuncName{
70+
*this, "name",
71+
llvm::cl::desc("Name of the top function to be extracted.")};
72+
73+
void runOnOperation() override {
74+
ModuleOp m = getOperation();
75+
OpBuilder b(m.getContext());
76+
77+
assert(!topFuncName.empty() && "-name should be specified.");
78+
79+
FuncOp f = dyn_cast_or_null<FuncOp>(m.lookupSymbol(topFuncName));
80+
assert(f && "Given name cannot be found in the module as a FuncOp.");
81+
82+
SmallPtrSet<Operation *, 4> keep;
83+
unionCallee(f, m, keep);
84+
85+
m.walk([&](FuncOp g) {
86+
if (!keep.contains(g))
87+
g.erase();
88+
});
89+
}
90+
};
91+
92+
} // namespace
93+
94+
void phism::registerExtractTopFuncPass() {
95+
PassRegistration<ExtractTopFuncPass>(
96+
"extract-top-func", "Extract the top function out of its module.");
97+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===- PhismTransforms.cc ---------------------------------------*- C++ -*-===//
2+
//
3+
// This file implements the registration interfaces for all Phism passes.
4+
//
5+
//===----------------------------------------------------------------------===//
6+
7+
#include "phism/mlir/Transforms/PhismTransforms.h"
8+
9+
namespace phism {
10+
11+
void registerAllPhismPasses() { registerExtractTopFuncPass(); }
12+
13+
} // namespace phism

llvm

Submodule llvm updated from 71dc13c to c5d00ae

test/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ configure_lit_site_cfg(
77

88
set(PHISM_TEST_DEPENDS
99
FileCheck count not
10-
# phism-opt
10+
phism-opt
11+
# LLVM passes
12+
VhlsLLVMRewriter
1113
)
1214

1315
add_lit_testsuite(check-phism "Running the Phism regression tests"

0 commit comments

Comments
 (0)