Commit 46bc369e authored by guruhegde's avatar guruhegde

[LoopSplitter] Step 3: Add prefetch instruction

parent 6f61b616
......@@ -15,17 +15,29 @@ using namespace llvm;
namespace tas {
bool LoopSplitter::run() {
SmallVector<Value *, 4> AnnotatedVars;
detectExpPtrVars(F, AnnotatedVars);
SmallVector<const LoadInst *, 4> VarUsePoints;
auto detectExpPtrUses(SmallVectorImpl<Value *> & AnnotatedVars) {
SmallVector<LoadInst *, 4> VarUsePoints;
for_each(AnnotatedVars, [&]
(const auto & Var) {
auto FU = findFirstUseOfValueInInstType<LoadInst>(Var);
if (FU) VarUsePoints.push_back(FU);
if (!FU) return;
// TODO Assume Load instruction immeditately follows.
if (auto * Ptr = dyn_cast<LoadInst>(FU->getNextNode())) {
if (Ptr->getOperand(0) == FU)
VarUsePoints.push_back(const_cast<LoadInst *>(FU));
}
});
return VarUsePoints;
}
bool LoopSplitter::run() {
auto AnnotatedVars = detectExpPtrVars(F);
auto VarUsePoints = detectExpPtrUses(AnnotatedVars);
for_each(VarUsePoints,
[&] (auto & VarUse) { insertLLVMPrefetchIntrinsic(F, VarUse); });
stat = Stats(AnnotatedVars.size(), VarUsePoints.size());
return true;
......
......@@ -64,7 +64,8 @@ void getAnnotatedFnList(Module * M, map<Function *, string> & FnList) {
}
}
void detectExpPtrVars(Function * F, SmallVectorImpl<Value *> & ExpensivePointers) {
SmallVector<Value *, 4> detectExpPtrVars(Function * F) {
SmallVector<Value *, 4> ExpensivePointers;
auto varAnnotationIntrinsic = Function::lookupIntrinsicID("llvm.var.annotation");
// XXX Checking only entry basic block for annotated variables.
for (auto & I : F->front()) {
......@@ -86,6 +87,8 @@ void detectExpPtrVars(Function * F, SmallVectorImpl<Value *> & ExpensivePointers
cast<BitCastInst>(CI->getArgOperand(0))->getOperand(0));
}
}
return ExpensivePointers;
}
Instruction * findBatchBeginMarkerInstruction(Function * F) {
......@@ -178,14 +181,10 @@ void cloneLoopBasicBlocks(Function * F, Loop * L, ValueToValueMapTy & VMap) {
LoopTerminator->setSuccessor(1, ClonedBlocks.front());
}
// Insert Prefetch instruction BEFORE the instruction I.
void insertLLVMPrefetchIntrinsic(Function * F, Instruction * I, Value * PtrVal) {
// Set prefetch instruction insertion point.
IRBuilder<> Builder(F->getContext());
Builder.SetInsertPoint(I->getPrevNode());
// Cast pointer value to i8* type
auto CastI = Builder.CreateBitCast(PtrVal, Builder.getInt8PtrTy(), "TAS-inst1");
void insertLLVMPrefetchIntrinsic(Function * F, Instruction * PtrAllocaUse) {
IRBuilder<> Builder(PtrAllocaUse);
auto Ptr = Builder.CreateLoad(PtrAllocaUse->getOperand(0), "prefetch_load");
auto CastI = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy(), "prefetch1");
// Add llvm prefetch intrinsic call.
Type *I32 = Type::getInt32Ty(F->getContext());
......
......@@ -35,16 +35,14 @@ void setAnnotationInFunctionObject(llvm::Module * M);
void cloneLoopBasicBlocks(llvm::Function * F, llvm::Loop * L,
llvm::ValueToValueMapTy & VMap);
void insertLLVMPrefetchIntrinsic(llvm::Function * F, llvm::Instruction * I,
llvm::Value * PtrVal);
void insertLLVMPrefetchIntrinsic(llvm::Function * F, llvm::Instruction * I);
void replaceUsesWithinBB(llvm::Value * From, llvm::Value * To,
llvm::BasicBlock * BB);
unsigned getTypeSizeInBits(llvm::Type * Ty);
void detectExpPtrVars(llvm::Function * F,
llvm::SmallVectorImpl<llvm::Value *> & ExpensivePointers);
llvm::SmallVector<llvm::Value *, 4> detectExpPtrVars(llvm::Function * F);
void detectBatchParameters(llvm::Function * F,
llvm::SmallPtrSet<llvm::Value *, 4> & BatchParameters);
......
......@@ -77,8 +77,7 @@ TEST_CASE("detect expensive variables") {
{
// Function with two expensive variables
auto F = M->getFunction("test_fn6");
SmallVector<Value *, 4> ExpensiveVars;
detectExpPtrVars(F, ExpensiveVars);
auto ExpensiveVars = detectExpPtrVars(F);
REQUIRE(ExpensiveVars.size() == 2);
}
}
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