Skip to content

Commit

Permalink
WIP: refactoring dep resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
examon committed Oct 29, 2018
1 parent 38d4c13 commit 1803ea4
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea*
.*un~
build
llvm
88 changes: 79 additions & 9 deletions apex/apex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bool APEXPass::runOnModule(Module &M) {
// Initialize apex_dg, so we have dependencies stored in neat little graph.
apexDgInit(apex_dg);
// apexDgPrint(apex_dg, true);
// apexDgPrintDataDependeniesCompact(apex_dg);
apexDgPrintDataDependeniesCompact(apex_dg);

// Use @apex_dg and make graph only out of data dependencies.
// Store everything back into the @apex_dg.
Expand All @@ -41,18 +41,88 @@ bool APEXPass::runOnModule(Module &M) {
printPath(path, ARG_SOURCE_FCN, ARG_TARGET_FCN);

// Put exit call before selected @target function call.
moduleInsertExitAfterTarget(M, path, ARG_TARGET_FCN);
// moduleInsertExitAfterTarget(M, path, ARG_TARGET_FCN);

// Resolve data dependencies for functions in @path.
// TODO: @y should be added to deps when target=@n
// updatePathAddDependencies(path, apex_dg);
// printPath(path, ARG_SOURCE_FCN, ARG_TARGET_FCN);

logPrintUnderline("Dependency resolver: START");
// Investigate each function in @path.
for (auto &fcn : path) {
logPrint("FCN: " + fcn->getGlobalIdentifier());
for (auto &BB : *fcn) {
// Go over instructions for each function in @path.
for (auto &I : BB) {
if (false == isa<CallInst>(I)) {
continue;
}
// Ok, @I is call instruction.
// If @I calls something in @path, compute dependencies and include
// new stuff to @path.
CallInst *call_inst = cast<CallInst>(&I);
Function *called_fcn = call_inst->getCalledFunction();
logPrint("- @I calling: " + called_fcn->getGlobalIdentifier());

// We care if @I is calling function that is in the @path.
bool call_to_path = false;
for (Function *path_function : path) {
if (called_fcn == path_function) {
call_to_path = true;
}
}
if (false == call_to_path) {
// Do not investigate @I if it is calling function OUTSIDE PATH.
continue;
}

// Find @I in the @apex_dg.
logPrint(" - finding @I in @apex_dg");
APEXDependencyNode *I_apex = nullptr;
for (APEXDependencyFunction apex_fcn : apex_dg.functions) {
std::string apex_fcn_name = apex_fcn.value->getName();
if (apex_fcn_name == fcn->getGlobalIdentifier()) {

for (APEXDependencyNode apex_node : apex_fcn.nodes) {
if (true == isa<CallInst>(apex_node.value)) {
CallInst *node_inst = cast<CallInst>(apex_node.value);
Function *node_called_fcn = node_inst->getCalledFunction();
if (node_called_fcn == called_fcn) {
I_apex = &apex_node;
logPrint(" - found @I in @apex_dg");
}
}
}
}
}
if (nullptr == I_apex) {
logPrint("ERROR: Could not find @I in @apex_dg!");
exit(-1);
}

// Now, check if @I has some data dependencies that are fcn calls.
std::vector<LLVMNode *> data_dependencies;
std::vector<LLVMNode *> rev_data_dependencies;
LLVMNode *I_apex_node = I_apex->node;
apexDgFindDataDependencies(apex_dg, *I_apex_node, data_dependencies,
rev_data_dependencies);
// TODO: dependencies in vectors look weird/broken.
for (auto &dd : data_dependencies) {
dd->getValue()->dump();
}
}
}

logPrint("\n---\n");
}

// Remove all functions (along with dependencies) that are not in the @path.
// Excluding functions in @PROTECTED_FCNS.
// moduleRemoveFunctionsNotInPath(M, apex_dg, path);
// moduleRemoveFunctionsNotInPath(M, apex_dg, path);

logPrintUnderline("Final Module Dump:");
M.dump();
// logPrintUnderline("Final Module Dump:");
// M.dump();
logPrintUnderline("APEXPass END");
return true;
}
Expand Down Expand Up @@ -638,13 +708,13 @@ void APEXPass::apexDgPrintGraph(APEXDependencyGraph &apex_dg) {
}
}

// TODO: Refactor this function.
/// Amazing, we have instruction that is calling some function.
/// Now check if this instruction has dependencies.
///
/// If it has dependencies and any of those is function call
/// that is not in the @path, add function that it calls to
/// the path.
// TODO: Refactor this function.
void APEXPass::apexDgNodeResolveDependencies(std::vector<Function *> &path,
APEXDependencyGraph &apex_dg,
const APEXDependencyNode &node) {
Expand Down Expand Up @@ -789,9 +859,9 @@ void APEXPass::updatePathAddDependencies(std::vector<Function *> &path,
for (Function *path_function : path) {
// We care only if @I is call instruction into some function that
// is in @path.
// if (called_fcn != path_function) {
// continue;
// }
if (called_fcn != path_function) {
continue;
}

// Ok, so @current_fcn is calling @called_fcn and @called_fcn
// is in @path.
Expand Down
29 changes: 29 additions & 0 deletions build_llvm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash
# Needs to run as a root

LLVM_VERSION=7.0.0

rm -rf llvm
mkdir llvm
cd llvm

curl -SL http://llvm.org/releases/${LLVM_VERSION}/cfe-${LLVM_VERSION}.src.tar.xz | tar xJ
#curl -SL https://github.com/viktormalik/llvm/archive/diffkemp.tar.gz | tar xz
#curl -SL https://github.com/examon/llvm/archive/diffkemp.tar.gz | tar xz
#git clone https://github.com/examon/llvm
curl -SL https://releases.llvm.org/${LLVM_VERSION}/llvm-${LLVM_VERSION}.src.tar.xz | tar xJ

mv llvm-${LLVM_VERSION}.src llvm
mv cfe-${LLVM_VERSION}.src llvm/tools/clang
cd llvm
mkdir build
cd build

cmake .. -GNinja \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DCMAKE_INSTALL_PREFIX=/usr/local \
-DLLVM_PARALLEL_LINK_JOBS=1 \
-DLLVM_TARGETS_TO_BUILD=X86 \
-DLLVM_BUILD_LLVM_DYLIB=ON
ninja -j9 && ninja install
49 changes: 26 additions & 23 deletions c-code/test_dependencies_minimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,29 @@ int x(void) {
return 10;
}

int y(int n) {
printf("y: in\n");
int tmp = n+n;
printf("y: out\n");
return tmp;
}

int z(void) {
printf("z: in\n");
int tmp = 1;
printf("z: out\n");
return tmp;
}

int n(void) {
printf("n: in\n");
int y_ret = y(10);
printf("n: out\n");
return y_ret;
int y (int n) {
return n;
}
//int y(int n) {
// printf("y: in\n");
// int tmp = n+n;
// printf("y: out\n");
// return tmp;
//}

//int z(void) {
// printf("z: in\n");
// int tmp = 1;
// printf("z: out\n");
// return tmp;
//}

//int n(void) {
// printf("n: in\n");
// int y_ret = y(10);
// printf("n: out\n");
// return y_ret;
//}

int main(void) {
printf("main: in\n");
Expand All @@ -35,11 +38,11 @@ int main(void) {
int y_ret = y(x_ret);
int y_store = y_ret;

int z_ret = z();
int z_store = z_ret;
// int z_ret = z();
// int z_store = z_ret;

int n_ret = n();
int n_store = n_ret;
// int n_ret = n();
// int n_store = n_ret;

printf("main: out\n");
return 0;
Expand Down

0 comments on commit 1803ea4

Please sign in to comment.