source: icGREP/icgrep-devel/icgrep/IR_Gen/tracegen.h @ 5398

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

Continued work on processing stdin input. Partial integration of ParabixDriver? methods into icgrep and editd. Object cache does not currently work for recursive REs.

File size: 4.3 KB
Line 
1/*
2 *  Copyright (c) 2017 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5#ifndef TRACEGEN_H
6#define TRACEGEN_H
7
8#include "idisa_builder.h"
9#include <string>
10#include <llvm/IR/Module.h>
11#include <llvm/IR/Constants.h>
12#include <llvm/IR/Intrinsics.h>
13#include <llvm/IR/Function.h>
14
15
16
17class TraceTool {
18public:
19    TraceTool(IDISA::IDISA_Builder * b, unsigned log2TraceBufSize = 16);
20   
21    unsigned declareTraceVar(std::string traceVarName);
22    unsigned newTraceVar(std::string traceName);
23    void addTraceEntry(unsigned traceVar, llvm::Value * traceVal);
24    void createDumpTrace();
25   
26private:
27    IDISA::IDISA_Builder * iBuilder;
28    unsigned mLog2TraceBufSize;
29    unsigned mTraceVarCount;
30    std::vector<llvm::Value *> mTraceFormatString;
31    llvm::Value * mTraceBufferPtr;
32    llvm::Value * mTraceIndexPtr;
33    llvm::Constant * mTraceIndexMask;
34};
35
36using namespace llvm;
37
38TraceTool::TraceTool(IDISA::IDISA_Builder * b, unsigned log2TraceBufSize) :
39    iBuilder(b),
40    mLog2TraceBufSize(log2TraceBufSize),
41    mTraceVarCount(0) {
42
43    Type * entryType = StructType::get(iBuilder->getInt8Ty()->getPointerTo(), iBuilder->getSizeTy(), nullptr);
44    Type * bufferType = ArrayType::get(entryType, 1 << mLog2TraceBufSize);
45    mTraceBufferPtr = iBuilder->CreateAlloca(bufferType);
46    mTraceIndexPtr = iBuilder->CreateAlloca(iBuilder->getInt32Ty());
47    iBuilder->CreateStore(ConstantInt::getNullValue(iBuilder->getInt32Ty()), mTraceIndexPtr);
48    mTraceIndexMask = ConstantInt::get(iBuilder->getInt32Ty(), (1 << mLog2TraceBufSize) - 1);
49}
50
51unsigned TraceTool::newTraceVar(std::string traceName) {
52    std::string formatString = traceName + " = %" PRIx64 "\n";
53    mTraceFormatString.push_back(iBuilder->GetString(formatString.c_str()));
54    return mTraceVarCount++;
55}
56
57void TraceTool::addTraceEntry(unsigned traceVar, llvm::Value * traceVal) {
58   
59    Value * traceIndex = iBuilder->CreateLoad(mTraceIndexPtr);
60    Value * entryVarPtr = iBuilder->CreateGEP(mTraceBufferPtr, {iBuilder->getInt32(0), traceIndex, iBuilder->getInt32(0)});
61    iBuilder->CreateStore(mTraceFormatString[traceVar], entryVarPtr);
62    Value * entryValPtr = iBuilder->CreateGEP(mTraceBufferPtr, {iBuilder->getInt32(0), traceIndex, iBuilder->getInt32(1)});
63    iBuilder->CreateStore(iBuilder->CreateZExt(traceVal, iBuilder->getSizeTy()), entryValPtr);
64    iBuilder->CreateStore(iBuilder->CreateAnd(mTraceIndexMask, iBuilder->CreateAdd(traceIndex, iBuilder->getInt32(1))), mTraceIndexPtr);
65}
66
67void TraceTool::createDumpTrace() {
68    Constant * traceBufSize = ConstantInt::get(iBuilder->getInt32Ty(), 1<<mLog2TraceBufSize);
69    Function * printF = iBuilder->GetPrintf();
70    BasicBlock * DumpEntryBlock = iBuilder->GetInsertBlock();
71    Function * currentFn = DumpEntryBlock->getParent();
72    BasicBlock * DumpTraceLoop = BasicBlock::Create(iBuilder->getContext(), "DumpTraceLoop", currentFn, 0);
73    BasicBlock * DumpTraceExit = BasicBlock::Create(iBuilder->getContext(), "DumpTraceExit", currentFn, 0);
74   
75    Value * lastTraceIndex = iBuilder->CreateLoad(mTraceIndexPtr);
76    Value * truncated = iBuilder->CreateICmpUGT(lastTraceIndex, traceBufSize);
77    Value * firstDumpIndex = iBuilder->CreateSelect(truncated, iBuilder->CreateSub(lastTraceIndex, traceBufSize), ConstantInt::getNullValue(iBuilder->getInt32Ty()));
78   
79    iBuilder->CreateBr(DumpTraceLoop);
80    iBuilder->SetInsertPoint(DumpTraceLoop);
81    PHINode * loopIndex = iBuilder->CreatePHI(iBuilder->getInt32Ty(), 2);
82    loopIndex->addIncoming(firstDumpIndex, DumpEntryBlock);
83   
84    Value * entryVarPtr = iBuilder->CreateGEP(mTraceBufferPtr, {iBuilder->getInt32(0), loopIndex, iBuilder->getInt32(0)});
85    Value * formatString = iBuilder->CreateLoad(entryVarPtr);
86    Value * entryValPtr = iBuilder->CreateGEP(mTraceBufferPtr, {iBuilder->getInt32(0), loopIndex, iBuilder->getInt32(1)});
87    Value * entryVal = iBuilder->CreateLoad(entryValPtr);
88    iBuilder->CreateCall(printF, {formatString, entryVal});
89   
90    Value * nextIndex = iBuilder->CreateAnd(iBuilder->CreateAdd(loopIndex, iBuilder->getInt32(1)), mTraceIndexMask);
91    loopIndex->addIncoming(nextIndex, DumpTraceLoop);
92    Value * atLastTraceIndex = iBuilder->CreateICmpEQ(nextIndex, lastTraceIndex);
93    iBuilder->CreateCondBr(atLastTraceIndex, DumpTraceExit, DumpTraceLoop);
94    iBuilder->SetInsertPoint(DumpTraceExit);
95}
96
97#endif
Note: See TracBrowser for help on using the repository browser.