Skip to content
Snippets Groups Projects
Commit ba60ceb0 authored by Hashim Sharif's avatar Hashim Sharif
Browse files

InsertApproxInfo - successfully adding operand bundles with l-norm metrics

parent be0872ab
No related branches found
No related tags found
No related merge requests found
......@@ -78,16 +78,23 @@ private:
void loadTrainedApproxMetrics(std::string dir_path);
void loadMetricsFromFile(std::string dir_path, std::string file_path);
void readApproxValues(const std::string line, ApproxMetrics* approx_metrics);
void initIntrinsicNames();
void initGlobalStrings();
// private data
std::unordered_map<std::string, std::string> intrinsics_map;
std::unordered_map<std::string, std::vector<ApproxMetrics*>> operation_metrics;
GlobalVariable* mean_l1_str;
GlobalVariable* mean_l2_str;
GlobalVariable* mean_linf_str;
// Tracks the id of the tensor op processed
unsigned int currentID;
public:
// Constructor
InsertApproxInfo(Module &_M, BuildDFG &_DFG) :
CodeGenTraversal(_M, _DFG){
}
InsertApproxInfo(Module &_M, BuildDFG &_DFG);
//void run(Module &M, BuildDFG &DFG);
void run(std::string dir_path);
......@@ -95,6 +102,47 @@ public:
void InsertApproxInfo::initIntrinsicNames(){
intrinsics_map["llvm.visc.tensor.convolution"] = "tensorConv";
intrinsics_map["llvm.visc.tensor.mul"] = "tensorGemm";
intrinsics_map["llvm.visc.tensor.add"] = "tensorAdd";
intrinsics_map["llvm.visc.tensor.pool.max"] = "tensorPooling";
intrinsics_map["llvm.visc.tensor.tanh"] = "tensorTanh";
}
void InsertApproxInfo::initGlobalStrings(){
/**** Creating global constant strings for each approximation metric type *******/
std::string metric_string = "mean_l1";
Constant* stringConst = ConstantDataArray::getString(M.getContext(), StringRef(metric_string.c_str()), true);
mean_l1_str = new GlobalVariable(M, stringConst->getType(), true,
GlobalValue::ExternalLinkage, stringConst, "");
metric_string = "mean_l2";
stringConst = ConstantDataArray::getString(M.getContext(), StringRef(metric_string.c_str()), true);
mean_l2_str = new GlobalVariable(M, stringConst->getType(), true,
GlobalValue::ExternalLinkage, stringConst, "");
metric_string = "mean_linf";
stringConst = ConstantDataArray::getString(M.getContext(), StringRef(metric_string.c_str()), true);
mean_linf_str = new GlobalVariable(M, stringConst->getType(), true,
GlobalValue::ExternalLinkage, stringConst, "");
}
InsertApproxInfo::InsertApproxInfo(Module &_M, BuildDFG &_DFG) :
CodeGenTraversal(_M, _DFG){
currentID = 1;
initIntrinsicNames();
initGlobalStrings();
}
void InsertApproxInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<BuildDFG>();
AU.addPreserved<BuildDFG>();
......@@ -250,25 +298,60 @@ void InsertApproxInfo::codeGen(DFLeafNode* N) {
}
Function *F = N->getFuncPointer();
Module* M = F->getParent();
std::vector<IntrinsicInst *> IItoRemove;
/**** Adding operand bundles for each tensor operation in the HPVM DFG Leaf Node ****/
for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
Instruction *I = &(*i);
errs()<<*I<<"\n";
std::vector<Value*> bundle_vals;
OperandBundleDef op_bundle("test_bundle", bundle_vals);
std::vector<OperandBundleDef> op_bundles;
op_bundles.push_back(op_bundle);
ArrayRef<OperandBundleDef> bundle_arr(op_bundles);
if (BuildDFG::isViscIntrinsic(I)) {
IntrinsicInst* II = dyn_cast<IntrinsicInst>(I);
assert((II->getCalledFunction()->getName()).startswith("llvm.visc.tensor")
&& "Only HPVM tensor intrinsics allowed in ApproxHPVM leaf nodes\n");
//StringRef f_name = II->getCalledFunction()->getName();
std::string intrinsic_id = std::string(II->getCalledFunction()->getName().data());
std::string runtime_func_name = intrinsics_map[intrinsic_id];
std::string unique_name = runtime_func_name + std::to_string(currentID);
printf("\n ---- unique_name = %s \n ", unique_name.c_str());
std::vector<ApproxMetrics*> approx_metrics;
if(operation_metrics.find(unique_name) != operation_metrics.end()){
approx_metrics = operation_metrics[unique_name];
}
else{
errs()<<"Intrinsic Name NOT found in the map - Unexpected Error. Aborting ... \n\n";
abort();
}
unsigned int num_configs = approx_metrics.size();
std::vector<OperandBundleDef> conf_bundles;
for(unsigned int i = 0; i < num_configs; i++){
std::vector<Value*> norm_vals;
norm_vals.push_back(mean_l1_str);
Constant* constFPVal = ConstantFP::get(Type::getDoubleTy(F->getContext()), approx_metrics[i]->mean_l1);
norm_vals.push_back(constFPVal);
norm_vals.push_back(mean_l2_str);
constFPVal = ConstantFP::get(Type::getDoubleTy(F->getContext()), approx_metrics[i]->mean_l2);
norm_vals.push_back(constFPVal);
norm_vals.push_back(mean_linf_str);
constFPVal = ConstantFP::get(Type::getDoubleTy(F->getContext()), approx_metrics[i]->mean_linf);
norm_vals.push_back(constFPVal);
std::string config_name = "config_" + std::to_string(i+1);
OperandBundleDef norm_bundle(config_name, norm_vals);
conf_bundles.push_back(norm_bundle);
}
ArrayRef<OperandBundleDef> bundle_arr(conf_bundles);
/*** Creating new Intrinsic call with Operand Bundles attached **/
Function* calledFunction = II->getCalledFunction();
unsigned num_args = II->getNumArgOperands();
std::vector<Value*> args;
......@@ -276,15 +359,18 @@ void InsertApproxInfo::codeGen(DFLeafNode* N) {
Value* argValue = II->getArgOperand(i);
args.push_back(argValue);
}
CallInst* CI = CallInst::Create(calledFunction,
args, bundle_arr, "", II);
errs()<<"NOTE: NEW CALLINST = "<<*CI<<"\n";
errs()<<"NOTE: New CallInst = "<<*CI<<"\n";
II->replaceAllUsesWith(CI);
// Mark to remove at the end
IItoRemove.push_back(II);
// Increment counter of op processed
currentID++;
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment