Skip to content

Commit

Permalink
WIP: extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
examon committed Nov 18, 2018
1 parent 9ef9074 commit ad64141
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 26 deletions.
56 changes: 45 additions & 11 deletions apex/apex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,50 @@ bool APEXPass::runOnModule(Module &M) {
logPrintUnderline("Removing functions and dependency blocks that do not "
"affect calculated path.");
removeUnneededStuff(M, path, blocks_functions_callgraph,
function_dependency_blocks, source_function_id,
target_function_id);
function_dependency_blocks);

logPrintUnderline("Inserting exit call after target instructions.");
moduleInsertExitAfterTarget(M, target_function_id);
moduleInsertExit(M, target_function_id);

// TODO: extract data
logPrintUnderline("Extracting data.");
// TODO: extract operand from target instruction and pass it as arg to dump.
{
Instruction *insertion_point =
const_cast<Instruction *>(target_instructions_.back());
std::vector<Type *> params_tmp = {Type::getInt32Ty(M.getContext())};
ArrayRef<Type *> params = makeArrayRef(params_tmp);
FunctionType *fType =
FunctionType::get(Type::getVoidTy(M.getContext()), params, false);
Constant *temp = M.getOrInsertFunction("lib_dump_int", fType);
Function *dump_fcn = cast<Function>(temp);

insertion_point->dump();
for (unsigned i = 0; i < insertion_point->getNumOperands(); ++i) {
logPrint("- op");
Value *op = insertion_point->getOperand(i);
op->dump();
}

Value *dump_arg_val =
ConstantInt::get(Type::getInt32Ty(M.getContext()), 123456);
dump_arg_val->dump();

ArrayRef<Value *> dump_params =
makeArrayRef(dump_arg_val);
CallInst *dump_call_inst = CallInst::Create(dump_fcn, dump_params, "");

if (insertion_point->isTerminator()) {
logPrintFlat("- @insertion_point is terminator, inserting " +
dump_fcn->getGlobalIdentifier() + " before: ");
insertion_point->dump();
dump_call_inst->insertBefore(insertion_point);
} else {
logPrintFlat("- @insertion_point is NOT terminator, inserting " +
dump_fcn->getGlobalIdentifier() + " after: ");
insertion_point->dump();
dump_call_inst->insertAfter(insertion_point);
}
}

logPrintUnderline("Stripping debug symbols from every function in module.");
stripAllDebugSymbols(M);
Expand Down Expand Up @@ -775,8 +812,8 @@ void APEXPass::moduleFindTargetInstructionsOrDie(Module &M,

/// Inserts lib_exit() call at the end of the @target_instructions_.
// TODO: Rename this method.
void APEXPass::moduleInsertExitAfterTarget(
Module &M, const std::string &target_function_id) {
void APEXPass::moduleInsertExit(Module &M,
const std::string &target_function_id) {

logPrint("Supplied target instructions from: " +
target_instructions_.back()->getFunction()->getGlobalIdentifier());
Expand Down Expand Up @@ -815,9 +852,8 @@ void APEXPass::moduleInsertExitAfterTarget(
logPrint("- loaded function: " + exit_fcn->getGlobalIdentifier());

// Create exit call instruction.
unsigned EXIT_CODE = 9;
Value *exit_arg_val =
ConstantInt::get(Type::getInt32Ty(M.getContext()), EXIT_CODE);
ConstantInt::get(Type::getInt32Ty(M.getContext()), APEX_DONE);
ArrayRef<Value *> exit_params = makeArrayRef(exit_arg_val);
// CallInst::Create build call instruction to @exit_fcn that has
// @exit_params. Empty string "" means that the @exit_call_inst will not
Expand Down Expand Up @@ -857,9 +893,7 @@ void APEXPass::removeUnneededStuff(
std::map<DependencyBlock, std::vector<const Function *>>
&blocks_functions_callgraph,
std::map<const Function *, std::vector<DependencyBlock>>
&function_dependency_blocks,
const std::string &source_function_id,
const std::string &target_function_id) {
&function_dependency_blocks) {

// Mapping from function to dependency blocks. Blocks belong to function.
// We are going to keep these function:blocks.
Expand Down
21 changes: 13 additions & 8 deletions apex/apex.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,22 @@ bool VERBOSE_DEBUG = false;
/// Error code when we want to crash APEXPass.
int FATAL_ERROR = -1;

/// Exit code when we want to successfully exit.
int APEX_DONE = 0;

/// Function LLVMNodes connected via the data dependencies.
using DependencyBlock = std::vector<LLVMNode *>;

/// Protected functions IDs. These will not be removed by APEXPass.
std::vector<std::string> PROTECTED_FCNS = {
"lib_test", "lib_exit", "exit", // These come from c-code/lib.c.
"printf", // Maybe remove prints?
"llvm.dbg.declare" // Introduced with debug symbols.
// These belong to the lib_exit()
"lib_exit", "exit",
// These belong to the lib_dump_int()
"lib_dump_int", "abs", "log10", "floor", "fopen", "fputs", "fclose", "exit",
"sprintf",
"printf", // Maybe remove prints?
"llvm.dbg.declare", "llvm.stackrestore",
"llvm.stacksave", // Introduced with debug symbols.
};

/// Command line arguments for opt.
Expand Down Expand Up @@ -141,8 +149,7 @@ class APEXPass : public ModulePass {
void moduleParseCmdLineArgsOrDie(void);
void moduleFindTargetInstructionsOrDie(Module &M, const std::string &file,
const std::string &line);
void moduleInsertExitAfterTarget(Module &M,
const std::string &target_function_id);
void moduleInsertExit(Module &M, const std::string &target_function_id);

void findPath(Module &M,
std::map<DependencyBlock, std::vector<const Function *>>
Expand All @@ -158,9 +165,7 @@ class APEXPass : public ModulePass {
std::map<DependencyBlock, std::vector<const Function *>>
&blocks_functions_callgraph,
std::map<const Function *, std::vector<DependencyBlock>>
&function_dependency_blocks,
const std::string &source_function_id,
const std::string &target_function_id);
&function_dependency_blocks);
void stripAllDebugSymbols(Module &M);
};

Expand Down
4 changes: 2 additions & 2 deletions build_and_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
INPUT_LIB="c-code/lib.c"
INPUT_SOURCE="c-code/test_dependencies_minimal.c"
FILE="c-code/test_dependencies_minimal.c"
LINE=47
LINE=43


# Generated bytecodes during all stages of processing.
Expand Down Expand Up @@ -70,7 +70,7 @@ clang-format -style=llvm -i apex/*.cpp apex/*.h
echo "========= Compiling input."

# compile external libraries
clang -O0 -g -c -emit-llvm ${INPUT_LIB} -o build/${BYTECODE_FROM_LIB}
clang -O0 -lm -g -c -emit-llvm ${INPUT_LIB} -o build/${BYTECODE_FROM_LIB}

# compile without anything
clang -O0 -g -c -emit-llvm ${INPUT_SOURCE} -o build/${BYTECODE_FROM_INPUT}
Expand Down
35 changes: 30 additions & 5 deletions c-code/lib.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void lib_test(void)
{
printf("lib_test() from lib.c\n");
}

/// Function used for getting out of the program, usually after calling @lib_dump_int().
void lib_exit(int exit_code) {
exit(exit_code);
}

/// Function used for dumping value @i to file @APEX_OUTPUT.
void lib_dump_int(int i) {
#define APEX_OUTPUT "apex.out"
// Figure out number of digits @i has.
// So we can allocate big enough buffer.
int i_digits = 0;
if (i == 0) {
i_digits = 1;
} else {
i_digits = floor(log10(abs(i)))+1;
}
// Store @i as string into @i_string buffer.
char i_string[i_digits+2];
sprintf(i_string, "%d\n", i);

// Save @i_string into @APEX_OUTPUT
int status = 0;
FILE *f = fopen(APEX_OUTPUT, "w+");
if (f != NULL) {
if (fputs(i_string, f) != EOF) {
status = 1;
}
fclose(f);
}
if (status != 1) {
printf("Error: Could not save to file!\n");
}
}

0 comments on commit ad64141

Please sign in to comment.