Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(optimizer): graph pruning #529

Merged
merged 31 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
8335b94
minor improvements
lukasrothenberger Jan 17, 2024
c3faadb
fix: extended recusrion limits
lukasrothenberger Jan 17, 2024
8fab4a7
type fixes and recursion depth fixes
lukasrothenberger Jan 17, 2024
54a3328
feat(optimizer): added argument to enable optimization step
lukasrothenberger Jan 17, 2024
9bac127
feat(optimizer): mark intermediate suggestions as non-applicable
lukasrothenberger Jan 17, 2024
deb2ccf
feat(profiler): taken branch instrumentation
lukasrothenberger Jan 19, 2024
151ff7d
Merge branch 'profiler/instrument_cu_entries' into optimizer/big_soft…
lukasrothenberger Jan 19, 2024
7e021d7
Merge branch 'optimizer/mapping_data_movement' into optimizer/big_sof…
lukasrothenberger Jan 19, 2024
3c905d6
chore(profiler): cleanup
lukasrothenberger Jan 19, 2024
904616a
Merge branch 'profiler/instrument_cu_entries' into optimizer/big_soft…
lukasrothenberger Jan 19, 2024
e174dc1
feat(optimizer): preparations for graph pruning
lukasrothenberger Jan 19, 2024
58e0661
node likelihood calculation
lukasrothenberger Jan 19, 2024
cd357b5
pruning to most likely path
lukasrothenberger Jan 19, 2024
3b83743
minor fix
lukasrothenberger Jan 19, 2024
c4fe948
minor reformatting
lukasrothenberger Jan 19, 2024
4521b9b
chore: formatting and type fixes
lukasrothenberger Jan 19, 2024
488f735
Merge remote-tracking branch 'origin/release/3.2.0' into optimizer/gr…
lukasrothenberger Jan 22, 2024
3ab9bc5
feat(optimizer, profiler): optional branch tracking
lukasrothenberger Jan 23, 2024
08e15c7
fix(profiler): enable branch tracking
lukasrothenberger Jan 23, 2024
d4ae32e
feat(optimizer): add configurable pruning levels
lukasrothenberger Jan 23, 2024
76ac11f
feat(optimizer): preparations for pruning level 2
lukasrothenberger Jan 23, 2024
9cdd77f
feat(optimizer): pruning level 2
lukasrothenberger Jan 23, 2024
47f7879
chore: cleanup
lukasrothenberger Jan 23, 2024
c0da1fa
feat(optimizer)[-p2]: formatted verbose output
lukasrothenberger Jan 23, 2024
af80e6c
doc(optimizer): cleanup help string
lukasrothenberger Jan 23, 2024
321e9d3
chore(optimizer): formatting
lukasrothenberger Jan 24, 2024
c13792b
fix(CI): profiler test
lukasrothenberger Jan 24, 2024
cbeccd4
Merge branch 'release/3.2.0' into optimizer/graph_pruning
lukasrothenberger Jan 24, 2024
0b40562
chore: formatting
lukasrothenberger Jan 24, 2024
af9a3c6
fix(optimizer)[pruning]: switched level 1 and 2 due to agressiveness
lukasrothenberger Jan 24, 2024
8eabcfc
fix(optimizer): incorrect argument name
lukasrothenberger Jan 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions .github/workflows/tests/profiler.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ TARGET_NAME=$1
PASS_NAME=$2

function test_discopopPass {
cp ${DISCOPOP_SRC}/scripts/dp-fmap .
./dp-fmap
clang++ -g -c -O0 -S -emit-llvm -fno-discard-value-names "$1" -o out.ll || return 1
opt-11 -S -load=${DISCOPOP_INSTALL}/libi/LLVMDiscoPoP.so --DiscoPoP out.ll -o out_dp.ll || return 1
clang++ out_dp.ll -o out_prof -L${DISCOPOP_INSTALL}/rtlib -lDiscoPoP_RT -lpthread || return 1
${DISCOPOP_INSTALL}/scripts/CXX_wrapper.sh "$1" -o out_prof


# cp ${DISCOPOP_SRC}/scripts/dp-fmap .
# ./dp-fmap
# clang++ -g -c -O0 -S -emit-llvm -fno-discard-value-names "$1" -o out.ll || return 1
# opt-11 -S -load=${DISCOPOP_INSTALL}/libi/LLVMDiscoPoP.so --DiscoPoP out.ll -o out_dp.ll || return 1
# clang++ out_dp.ll -o out_prof -L${DISCOPOP_INSTALL}/rtlib -lDiscoPoP_RT -lpthread || return 1
./out_prof || return 1
}

Expand Down
59 changes: 55 additions & 4 deletions DiscoPoP/DiscoPoP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
#define DP_VERBOSE false // prints warning messages
#define DP_hybrid_DEBUG false
#define DP_hybrid_SKIP false //todo add parameter to disable hybrid dependence analysis on demand.

#define DP_BRANCH_TRACKING true // toggles the creation of instrumentation calls for tracking taken branches.
// Required by the graph pruning step of the DiscoPoP optimizer.

using namespace llvm;
using namespace std;
Expand Down Expand Up @@ -105,6 +106,8 @@ void DiscoPoP::setupCallbacks() {
DpLoopExit = ThisModule->getOrInsertFunction("__dp_loop_exit",
Void,
Int32, Int32);

DpTakenBranchCounterIncr = ThisModule->getOrInsertFunction("__dp_incr_taken_branch_counter", Void, CharPtr, Int32, Int32);
}

bool DiscoPoP::doInitialization(Module &M) {
Expand Down Expand Up @@ -258,7 +261,6 @@ bool DiscoPoP::doFinalization(Module &M) {
}

// DPInstrumentationOmission end

return true;
}

Expand Down Expand Up @@ -451,6 +453,48 @@ void DiscoPoP::populateGlobalVariablesSet(Region *TopRegion,
}
}


void DiscoPoP::createTakenBranchInstrumentation(Region* TopRegion, map <string, vector<CU *>> &BBIDToCUIDsMap){
/* Create calls to count taken branches inbetween CUs during execution */
for (Region::block_iterator bb = TopRegion->block_begin();
bb != TopRegion->block_end(); ++bb) {
for (BasicBlock::iterator instruction = (*bb)->begin();
instruction != (*bb)->end(); ++instruction) {
if(isa<BranchInst>(instruction)){
BranchInst* branchInst = cast<BranchInst>(instruction);
// check for conditional branches, as unconditional ones can be ignored for counting
if(! branchInst->isUnconditional()){
// branchInst is conditional
// prepare IRBuilder to insert instrumentation
IRBuilder<> IRB(branchInst);
// get BBId and CU IDS of the source
string source_BBID = bb->getName().str();
for(auto source_cu : BBIDToCUIDsMap[source_BBID]){
// get BBIds of all targets
for(int i = 0; i < branchInst->getNumSuccessors(); i++){
string successor_BBID = branchInst->getSuccessor(i)->getName().str();
// get CUs of all targets
for(auto target_cu : BBIDToCUIDsMap[successor_BBID]){
// add instrumentation prior to the branch instruction
vector<Value*> args;
string source_and_target = source_cu->ID + ";" + target_cu->ID;
args.push_back(getOrInsertVarName_dynamic(source_and_target, IRB));
args.push_back(branchInst->getCondition());
bool counter_active_on_cmp_value = (i == 0 ? 1 : 0);
args.push_back(ConstantInt::get(Int32, counter_active_on_cmp_value));
IRB.CreateCall(DpTakenBranchCounterIncr, args);
}
}
}
}
}
}
}
}




void DiscoPoP::createCUs(Region *TopRegion, set <string> &globalVariablesSet,
vector<CU *> &CUVector,
map <string, vector<CU *>> &BBIDToCUIDsMap,
Expand Down Expand Up @@ -1889,13 +1933,17 @@ void DiscoPoP::dp_reduction_insert_functions() {
llvm::FunctionType* output_fn_type =
llvm::FunctionType::get(llvm::Type::getVoidTy(*ctx_), false);
FunctionCallee loop_counter_output_callee = module_->getOrInsertFunction("loop_counter_output", output_fn_type);
FunctionCallee cu_taken_branch_counter_output_callee = module_->getOrInsertFunction("__dp_taken_branch_counter_output", output_fn_type);
llvm::Function* main_fn = module_->getFunction("main");
if (main_fn) {
for (auto it = llvm::inst_begin(main_fn); it != llvm::inst_end(main_fn);
++it) {
if (llvm::isa<llvm::ReturnInst>(&(*it))) {
llvm::IRBuilder<> ir_builder(&(*it));
ir_builder.CreateCall(loop_counter_output_callee);
if(DP_BRANCH_TRACKING){
ir_builder.CreateCall(cu_taken_branch_counter_output_callee);
}
break;
}
}
Expand Down Expand Up @@ -2087,7 +2135,7 @@ bool DiscoPoP::runOnModule(Module &M) {

bool DiscoPoP::runOnFunction(Function &F) {
if (DP_DEBUG) {
errs() << "pass DiscoPoP: run pass on function\n";
errs() << "pass DiscoPoP: run pass on function " << F.getName().str() << "\n";
}

StringRef funcName = F.getName();
Expand Down Expand Up @@ -2173,8 +2221,11 @@ bool DiscoPoP::runOnFunction(Function &F) {

createCUs(TopRegion, globalVariablesSet, CUVector, BBIDToCUIDsMap, root, LI);

fillCUVariables(TopRegion, globalVariablesSet, CUVector, BBIDToCUIDsMap);
if(DP_BRANCH_TRACKING){
createTakenBranchInstrumentation(TopRegion, BBIDToCUIDsMap);
}

fillCUVariables(TopRegion, globalVariablesSet, CUVector, BBIDToCUIDsMap);

fillStartEndLineNumbers(root, LI);

Expand Down
3 changes: 3 additions & 0 deletions DiscoPoP/DiscoPoP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ namespace {
FunctionCallee DpCallOrInvoke;
FunctionCallee DpFuncEntry, DpFuncExit;
FunctionCallee DpLoopEntry, DpLoopExit;
FunctionCallee DpTakenBranchCounterIncr;

// Basic types
Type *Void;
Expand Down Expand Up @@ -378,6 +379,8 @@ namespace {
map <string, vector<CU *>> &BBIDToCUIDsMap, Node *root,
LoopInfo &LI);

void createTakenBranchInstrumentation(Region* TopRegion, map <string, vector<CU *>> &BBIDToCUIDsMap);

void fillCUVariables(Region *TopRegion, set <string> &globalVariablesSet,
vector<CU *> &CUVector,
map <string, vector<CU *>> &BBIDToCUIDsMap);
Expand Down
2 changes: 2 additions & 0 deletions discopop_explorer/pattern_detectors/PatternBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class PatternBase(object):
node_id: NodeID
start_line: LineID
end_line: LineID
applicable_pattern: bool

def __init__(self, node: Node):
# create a file lock to synchronize processes
Expand All @@ -41,6 +42,7 @@ def __init__(self, node: Node):
self.node_id = node.id
self.start_line = node.start_position()
self.end_line = node.end_position()
self.applicable_pattern = True

def to_json(self):
dic = self.__dict__
Expand Down
3 changes: 3 additions & 0 deletions discopop_library/PatchGenerator/from_json_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def from_json_patterns(
if suggestion_type == "optimizer_output":
from_optimizer_output(file_mapping, patterns_by_type, suggestion, arguments, patch_generator_dir)
continue
suggestion_dict = json.loads(suggestion)
if not suggestion_dict["applicable_pattern"]:
continue
if arguments.verbose:
print("Suggestion: ", suggestion)
file_id_to_modified_code: Dict[int, str] = from_json_strings(
Expand Down
Loading