source: icGREP/icgrep-devel/icgrep/IR_Gen/tracegen.cpp @ 5732

Last change on this file since 5732 was 5464, checked in by nmedfort, 2 years ago

Restructuring work for the Driver classes. Start of work to eliminate the memory leaks with the ExecutionEngine?. Replaced custom AlignedMalloc? with backend call to std::aligned_malloc. Salvaged some work on DistributionPass? for reevaluation.

File size: 3.5 KB
Line 
1#include "tracegen.h"
2
3#include <llvm/IR/Module.h>
4#include <llvm/IR/Constants.h>
5#include <llvm/IR/Intrinsics.h>
6#include <llvm/IR/Function.h>
7
8using namespace llvm;
9
10TraceTool::TraceTool(IDISA::IDISA_Builder * b, unsigned log2TraceBufSize)
11: iBuilder(b)
12, mLog2TraceBufSize(log2TraceBufSize)
13, mTraceVarCount(0) {
14
15    llvm::Type * entryType = llvm::StructType::get(iBuilder->getInt8Ty()->getPointerTo(), iBuilder->getSizeTy(), nullptr);
16    llvm::Type * bufferType = llvm::ArrayType::get(entryType, 1 << mLog2TraceBufSize);
17    mTraceBufferPtr = iBuilder->CreateAlloca(bufferType);
18    mTraceIndexPtr = iBuilder->CreateAlloca(iBuilder->getInt32Ty());
19    iBuilder->CreateStore(llvm::ConstantInt::getNullValue(iBuilder->getInt32Ty()), mTraceIndexPtr);
20    mTraceIndexMask = llvm::ConstantInt::get(iBuilder->getInt32Ty(), (1 << mLog2TraceBufSize) - 1);
21}
22
23unsigned TraceTool::newTraceVar(std::string traceName) {
24    std::string formatString = traceName + " = %" PRIx64 "\n";
25    mTraceFormatString.push_back(iBuilder->GetString(formatString.c_str()));
26    return mTraceVarCount++;
27}
28
29void TraceTool::addTraceEntry(unsigned traceVar, llvm::Value * traceVal) {
30
31    Value * traceIndex = iBuilder->CreateLoad(mTraceIndexPtr);
32    Value * entryVarPtr = iBuilder->CreateGEP(mTraceBufferPtr, {iBuilder->getInt32(0), traceIndex, iBuilder->getInt32(0)});
33    iBuilder->CreateStore(mTraceFormatString[traceVar], entryVarPtr);
34    Value * entryValPtr = iBuilder->CreateGEP(mTraceBufferPtr, {iBuilder->getInt32(0), traceIndex, iBuilder->getInt32(1)});
35    iBuilder->CreateStore(iBuilder->CreateZExt(traceVal, iBuilder->getSizeTy()), entryValPtr);
36    iBuilder->CreateStore(iBuilder->CreateAnd(mTraceIndexMask, iBuilder->CreateAdd(traceIndex, iBuilder->getInt32(1))), mTraceIndexPtr);
37}
38
39void TraceTool::createDumpTrace() {
40
41    Constant * traceBufSize = ConstantInt::get(iBuilder->getInt32Ty(), 1<<mLog2TraceBufSize);
42    Function * printF = iBuilder->GetPrintf();
43    BasicBlock * DumpEntryBlock = iBuilder->GetInsertBlock();
44    Function * currentFn = DumpEntryBlock->getParent();
45    BasicBlock * DumpTraceLoop = BasicBlock::Create(iBuilder->getContext(), "DumpTraceLoop", currentFn, 0);
46    BasicBlock * DumpTraceExit = BasicBlock::Create(iBuilder->getContext(), "DumpTraceExit", currentFn, 0);
47
48    Value * lastTraceIndex = iBuilder->CreateLoad(mTraceIndexPtr);
49    Value * truncated = iBuilder->CreateICmpUGT(lastTraceIndex, traceBufSize);
50    Value * firstDumpIndex = iBuilder->CreateSelect(truncated, iBuilder->CreateSub(lastTraceIndex, traceBufSize), ConstantInt::getNullValue(iBuilder->getInt32Ty()));
51
52    iBuilder->CreateBr(DumpTraceLoop);
53    iBuilder->SetInsertPoint(DumpTraceLoop);
54    PHINode * loopIndex = iBuilder->CreatePHI(iBuilder->getInt32Ty(), 2);
55    loopIndex->addIncoming(firstDumpIndex, DumpEntryBlock);
56
57    Value * entryVarPtr = iBuilder->CreateGEP(mTraceBufferPtr, {iBuilder->getInt32(0), loopIndex, iBuilder->getInt32(0)});
58    Value * formatString = iBuilder->CreateLoad(entryVarPtr);
59    Value * entryValPtr = iBuilder->CreateGEP(mTraceBufferPtr, {iBuilder->getInt32(0), loopIndex, iBuilder->getInt32(1)});
60    Value * entryVal = iBuilder->CreateLoad(entryValPtr);
61    iBuilder->CreateCall(printF, {formatString, entryVal});
62
63    Value * nextIndex = iBuilder->CreateAnd(iBuilder->CreateAdd(loopIndex, iBuilder->getInt32(1)), mTraceIndexMask);
64    loopIndex->addIncoming(nextIndex, DumpTraceLoop);
65    Value * atLastTraceIndex = iBuilder->CreateICmpEQ(nextIndex, lastTraceIndex);
66    iBuilder->CreateCondBr(atLastTraceIndex, DumpTraceExit, DumpTraceLoop);
67    iBuilder->SetInsertPoint(DumpTraceExit);
68}
Note: See TracBrowser for help on using the repository browser.