Skip to content
Snippets Groups Projects
Commit 476bf32c authored by Akash Kothari's avatar Akash Kothari
Browse files

Making the implementation of some functions in VISCUtils more concise

parent 85a369cc
No related branches found
No related tags found
No related merge requests found
...@@ -111,41 +111,21 @@ void fixHintMetadata(Module &M, Function* F, Function* G) { ...@@ -111,41 +111,21 @@ void fixHintMetadata(Module &M, Function* F, Function* G) {
MDTuple* MDT_G = MDNode::get(F->getContext(), ArrayRef<Metadata*>(ValueAsMetadata::get(G))); MDTuple* MDT_G = MDNode::get(F->getContext(), ArrayRef<Metadata*>(ValueAsMetadata::get(G)));
DEBUG(errs() << "New Metadata: " << *MDT_G << "\n"); DEBUG(errs() << "New Metadata: " << *MDT_G << "\n");
NamedMDNode* HintNode = M.getOrInsertNamedMetadata("visc_hint_gpu"); auto FixHint = [&](StringRef Name) {
for(unsigned i = 0; i < HintNode->getNumOperands(); i++) { NamedMDNode* HintNode = M.getOrInsertNamedMetadata(Name);
if(HintNode->getOperand(i) == MDT_F) for(unsigned i = 0; i < HintNode->getNumOperands(); i++) {
HintNode->setOperand(i, MDT_G); if(HintNode->getOperand(i) == MDT_F)
} HintNode->setOperand(i, MDT_G);
HintNode = M.getOrInsertNamedMetadata("visc_hint_spir"); }
for(unsigned i = 0; i < HintNode->getNumOperands(); i++) { };
if(HintNode->getOperand(i) == MDT_F)
HintNode->setOperand(i, MDT_G); FixHint("visc_hint_gpu");
} FixHint("visc_hint_spir");
HintNode = M.getOrInsertNamedMetadata("visc_hint_cudnn"); FixHint("visc_hint_cudnn");
for(unsigned i = 0; i < HintNode->getNumOperands(); i++) { FixHint("visc_hint_promise");
if(HintNode->getOperand(i) == MDT_F) FixHint("visc_hint_cpu");
HintNode->setOperand(i, MDT_G); FixHint("visc_hint_cpu_gpu");
} FixHint("visc_hint_cpu_spir");
HintNode = M.getOrInsertNamedMetadata("visc_hint_promise");
for(unsigned i = 0; i < HintNode->getNumOperands(); i++) {
if(HintNode->getOperand(i) == MDT_F)
HintNode->setOperand(i, MDT_G);
}
HintNode = M.getOrInsertNamedMetadata("visc_hint_cpu");
for(unsigned i = 0; i < HintNode->getNumOperands(); i++) {
if(HintNode->getOperand(i) == MDT_F)
HintNode->setOperand(i, MDT_G);
}
HintNode = M.getOrInsertNamedMetadata("visc_hint_cpu_gpu");
for(unsigned i = 0; i < HintNode->getNumOperands(); i++) {
if(HintNode->getOperand(i) == MDT_F)
HintNode->setOperand(i, MDT_G);
}
HintNode = M.getOrInsertNamedMetadata("visc_hint_cpu_spir");
for(unsigned i = 0; i < HintNode->getNumOperands(); i++) {
if(HintNode->getOperand(i) == MDT_F)
HintNode->setOperand(i, MDT_G);
}
} }
// Assuming that the changed function is a node function, it is only used as a // Assuming that the changed function is a node function, it is only used as a
...@@ -153,13 +133,12 @@ void fixHintMetadata(Module &M, Function* F, Function* G) { ...@@ -153,13 +133,12 @@ void fixHintMetadata(Module &M, Function* F, Function* G) {
// calls in the program. // calls in the program.
void replaceNodeFunctionInIR(Module &M, Function* F, Function* G) { void replaceNodeFunctionInIR(Module &M, Function* F, Function* G) {
for (Module::iterator mi = M.begin(), me = M.end(); mi != me; ++mi) { for (auto &Func : M) {
Function* f = &*mi; DEBUG(errs() << "Function: " << Func.getName() << "\n");
DEBUG(errs() << "Function: " << f->getName() << "\n");
std::vector<Instruction*> toBeErased; std::vector<Instruction*> toBeErased;
for (inst_iterator i = inst_begin(f), e = inst_end(f); i != e ; ++i) { for (inst_iterator i = inst_begin(&Func), e = inst_end(&Func); i != e ; ++i) {
Instruction* I = &*i; // Grab pointer to Instruction Instruction* I = &*i; // Grab pointer to Instruction
if (isViscCreateNodeIntrinsic(I)) { if (isViscCreateNodeIntrinsic(I)) {
...@@ -181,7 +160,7 @@ void replaceNodeFunctionInIR(Module &M, Function* F, Function* G) { ...@@ -181,7 +160,7 @@ void replaceNodeFunctionInIR(Module &M, Function* F, Function* G) {
continue; // skip it continue; // skip it
DEBUG(errs() << "Fixing use: " << *CI << "\n"); DEBUG(errs() << "Fixing use: " << *CI << "\n");
DEBUG(errs() << "in function: " << f->getName() << "\n"); DEBUG(errs() << "in function: " << Func.getName() << "\n");
// Replace use of F with use of G // Replace use of F with use of G
CI->setArgOperand(1, G); CI->setArgOperand(1, G);
DEBUG(errs() << "Fixed use: " << *CI << "\n"); DEBUG(errs() << "Fixed use: " << *CI << "\n");
...@@ -355,42 +334,35 @@ bool tagIncludesTarget(visc::Target Tag, visc::Target T) { ...@@ -355,42 +334,35 @@ bool tagIncludesTarget(visc::Target Tag, visc::Target T) {
case visc::CPU_TARGET: case visc::CPU_TARGET:
if (T == visc::CPU_TARGET) if (T == visc::CPU_TARGET)
return true; return true;
else return false;
return false;
case visc::GPU_TARGET: case visc::GPU_TARGET:
if (T == visc::GPU_TARGET) if (T == visc::GPU_TARGET)
return true; return true;
else return false;
return false;
case visc::SPIR_TARGET: case visc::SPIR_TARGET:
if (T == visc::SPIR_TARGET) if (T == visc::SPIR_TARGET)
return true; return true;
else return false;
return false;
case visc::CUDNN_TARGET: case visc::CUDNN_TARGET:
if (T == visc::CUDNN_TARGET) if (T == visc::CUDNN_TARGET)
return true; return true;
else return false;
return false;
case visc::PROMISE_TARGET: case visc::PROMISE_TARGET:
if (T == visc::PROMISE_TARGET) if (T == visc::PROMISE_TARGET)
return true; return true;
else return false;
return false;
case visc::CPU_OR_GPU_TARGET: case visc::CPU_OR_GPU_TARGET:
if ((T == visc::CPU_TARGET) || if ((T == visc::CPU_TARGET) ||
(T == visc::GPU_TARGET) || (T == visc::GPU_TARGET) ||
(T == visc::CPU_OR_GPU_TARGET)) (T == visc::CPU_OR_GPU_TARGET))
return true; return true;
else return false;
return false;
case visc::CPU_OR_SPIR_TARGET: case visc::CPU_OR_SPIR_TARGET:
if ((T == visc::CPU_TARGET) || if ((T == visc::CPU_TARGET) ||
(T == visc::SPIR_TARGET) || (T == visc::SPIR_TARGET) ||
(T == visc::CPU_OR_SPIR_TARGET)) (T == visc::CPU_OR_SPIR_TARGET))
return true; return true;
else return false;
return false;
default: default:
assert(false && "Unknown Target\n"); assert(false && "Unknown Target\n");
} }
...@@ -566,53 +538,24 @@ void removeHint(Function* F, visc::Target T) { ...@@ -566,53 +538,24 @@ void removeHint(Function* F, visc::Target T) {
visc::Target getPreferredTarget(Function* F) { visc::Target getPreferredTarget(Function* F) {
DEBUG(errs() << "Finding preferred target for " << F->getName() << "\n"); DEBUG(errs() << "Finding preferred target for " << F->getName() << "\n");
Module* M = F->getParent(); Module* M = F->getParent();
NamedMDNode* HintNode = M->getOrInsertNamedMetadata("visc_hint_gpu");
for(unsigned i = 0; i < HintNode->getNumOperands(); i++) { auto PrefTarget = [=](StringRef Name) {
MDNode* N = HintNode->getOperand(i); NamedMDNode* HintNode = M->getOrInsertNamedMetadata(Name);
Value* FHint = dyn_cast<ValueAsMetadata>(N->getOperand(0).get())->getValue(); for(unsigned i = 0; i < HintNode->getNumOperands(); i++) {
if(F == FHint) MDNode* N = HintNode->getOperand(i);
return visc::GPU_TARGET; Value* FHint = dyn_cast<ValueAsMetadata>(N->getOperand(0).get())->getValue();
} if(F == FHint)
return true;
HintNode = M->getOrInsertNamedMetadata("visc_hint_spir"); }
for(unsigned i = 0; i < HintNode->getNumOperands(); i++) { };
MDNode* N = HintNode->getOperand(i);
Value* FHint = dyn_cast<ValueAsMetadata>(N->getOperand(0).get())->getValue(); if(PrefTarget("visc_hint_gpu")) return visc::GPU_TARGET;
if(F == FHint) if(PrefTarget("visc_hint_spir")) return visc::SPIR_TARGET;
return visc::SPIR_TARGET; if(PrefTarget("visc_hint_cudnn")) return visc::CUDNN_TARGET;
} if(PrefTarget("visc_hint_promise")) return visc::PROMISE_TARGET;
if(PrefTarget("visc_hint_cpu_gpu")) return visc::CPU_OR_GPU_TARGET;
HintNode = M->getOrInsertNamedMetadata("visc_hint_cudnn"); if(PrefTarget("visc_hint_cpu_spir")) return visc::CPU_OR_SPIR_TARGET;
for(unsigned i = 0; i < HintNode->getNumOperands(); i++) {
MDNode* N = HintNode->getOperand(i);
Value* FHint = dyn_cast<ValueAsMetadata>(N->getOperand(0).get())->getValue();
if(F == FHint)
return visc::CUDNN_TARGET;
}
HintNode = M->getOrInsertNamedMetadata("visc_hint_promise");
for(unsigned i = 0; i < HintNode->getNumOperands(); i++) {
MDNode* N = HintNode->getOperand(i);
Value* FHint = dyn_cast<ValueAsMetadata>(N->getOperand(0).get())->getValue();
if(F == FHint)
return visc::PROMISE_TARGET;
}
HintNode = M->getOrInsertNamedMetadata("visc_hint_cpu_gpu");
for(unsigned i = 0; i < HintNode->getNumOperands(); i++) {
MDNode* N = HintNode->getOperand(i);
Value* FHint = dyn_cast<ValueAsMetadata>(N->getOperand(0).get())->getValue();
if(F == FHint)
return visc::CPU_OR_GPU_TARGET;
}
HintNode = M->getOrInsertNamedMetadata("visc_hint_cpu_spir");
for(unsigned i = 0; i < HintNode->getNumOperands(); i++) {
MDNode* N = HintNode->getOperand(i);
Value* FHint = dyn_cast<ValueAsMetadata>(N->getOperand(0).get())->getValue();
if(F == FHint)
return visc::CPU_OR_SPIR_TARGET;
}
return visc::CPU_TARGET; return visc::CPU_TARGET;
} }
......
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