Ignore:
Timestamp:
May 22, 2017, 12:14:19 PM (2 years ago)
Author:
nmedfort
Message:

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:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/IR_Gen/tracegen.h

    r5398 r5464  
    88#include "idisa_builder.h"
    99#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 
    1610
    1711class TraceTool {
     
    3428};
    3529
    36 using namespace llvm;
    37 
    38 TraceTool::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 
    51 unsigned 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 
    57 void 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 
    67 void 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 
    9730#endif
Note: See TracChangeset for help on using the changeset viewer.