Commit 39ebb921 authored by guruhegde's avatar guruhegde

Handle opaque type

* Passes local tests, not tested on server.
parent 67c02af9
......@@ -29,6 +29,27 @@ namespace tas {
// TODO Use return val array
// Identify whether BatchArg is opaque pointer type, deduce the concrete type
// by checking the bitcast op usage on that.
Type * handleOpaqueType(Value * V) {
Type * OpaqueTy = nullptr;
if (auto * PT = dyn_cast<PointerType>(V->getType()))
if (auto * IT = dyn_cast<IntegerType>(PT->getPointerElementType())) {
if (IT->getBitWidth() == 8) {
auto StoreI = findFirstUseInStoreInst(V);
auto Alloca = StoreI->getPointerOperand();
for (auto U : Alloca->users()) {
if (isa<LoadInst>(U) &&
isa<BitCastInst>(cast<Instruction>(U)->getNextNode())) {
OpaqueTy = PointerType::get(cast<LoadInst>(U)->getNextNode()->getType(), 0);
}
}
}
}
return OpaqueTy;
}
/// Old Function Prototype:
/// Ret Fn(Type1 A, Type2 B BATCH_ARG, Type3 C BATCH_ARG);
/// BatchForm Fn Prototype:
......@@ -40,8 +61,9 @@ void BatchMaker::createBatchedFormFnPrototype(vector<TASArgAttr> & BatchFuncArgL
int i = 0;
for (auto & Arg : NonBatchFunc->args()) {
bool IsBatch = ArgsToBatch.find(&Arg) != ArgsToBatch.end();
Type * OpaqueTy = IsBatch ? handleOpaqueType(&Arg) : nullptr;
BatchFuncArgList.emplace_back(TASArgAttr { IsBatch, i++, Arg.getType(), &Arg,
Arg.getName() });
Arg.getName(), OpaqueTy });
}
// Batch size parameter
......@@ -81,20 +103,24 @@ void BatchMaker::replaceOldArgUsesWithBatchArgs(vector<TASArgAttr> & BatchFuncAr
auto EntryBB = &BatchFunc->front();
Builder.SetInsertPoint(&EntryBB->front());
SmallVector<Value *, 4> BatchArgs;
for_each(BatchFuncArgList.begin(), BatchFuncArgList.end(),
[&] (TASArgAttr & Attr) {
if (Attr.IsBatch) BatchArgs.push_back(Attr.Val); });
for (auto & BatchFuncArg : BatchFuncArgList) {
if (!BatchFuncArg.IsBatch) continue;
for (auto & BatchArg : BatchArgs) {
auto BatchArgAlloca = Builder.CreateAlloca(BatchArg->getType());
auto & BatchArg = BatchFuncArg.Val;
auto * BatchTy = BatchFuncArg.ConcreteTy != nullptr ? BatchFuncArg.ConcreteTy
: BatchArg->getType();
Builder.SetInsertPoint(&EntryBB->front());
auto BatchArgAlloca = Builder.CreateAlloca(BatchTy);
auto StoreI = findFirstUseInStoreInst(BatchArg);
// Store argument in alloca variable.
AllocaInst * OldAlloca = nullptr;
if (StoreI) {
if ((OldAlloca = dyn_cast<AllocaInst>(StoreI->getPointerOperand())))
if ((OldAlloca = dyn_cast<AllocaInst>(StoreI->getPointerOperand()))) {
auto Addr = CastInst::CreatePointerCast(StoreI->getOperand(0), BatchTy, "", StoreI);
StoreI->setOperand(0, Addr);
StoreI->setOperand(1, BatchArgAlloca);
}
} else {
continue;
}
......@@ -110,7 +136,13 @@ void BatchMaker::replaceOldArgUsesWithBatchArgs(vector<TASArgAttr> & BatchFuncAr
Builder.SetInsertPoint(cast<Instruction>(U));
auto ElemPtr = Builder.CreateGEP(Builder.CreateLoad(BatchArgAlloca),
Builder.CreateLoad(IdxPtr));
U->replaceUsesOfWith(OldAlloca, ElemPtr);
if (isa<LoadInst>(U)) {
auto NewLoad = Builder.CreateLoad(ElemPtr);
U->replaceAllUsesWith(NewLoad);
cast<Instruction>(U)->eraseFromParent();
} else {
U->replaceUsesOfWith(OldAlloca, ElemPtr);
}
NumUses--;
}
}
......
......@@ -21,12 +21,14 @@ struct TASArgAttr {
llvm::Type * Ty;
llvm::Argument * Val;
std::string Name;
llvm::Type * ConcreteTy; // Non null means void type.
TASArgAttr(bool b, int p, llvm::Type * t, llvm::Argument * v, std::string n) :
TASArgAttr(bool b, int p, llvm::Type * t, llvm::Argument * v, std::string n, llvm::Type * ct = nullptr) :
Prefix("batch_"), IsBatch(b), PosIdx(p),
Ty(IsBatch ? llvm::PointerType::get(t, 0) : t),
Val(v),
Name(IsBatch ? Prefix + std::to_string(p) : n) {}
Name(IsBatch ? Prefix + std::to_string(p) : n),
ConcreteTy(ct) {}
};
/// This pass converts the single packet processing function into
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment