Changeset 5429 for icGREP


Ignore:
Timestamp:
Apr 28, 2017, 2:50:59 PM (2 years ago)
Author:
nmedfort
Message:

Refactored source kernels. icgrep from stdin should now be able to handle any file size.

Location:
icGREP/icgrep-devel/icgrep
Files:
2 added
4 deleted
17 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/CMakeLists.txt

    r5425 r5429  
    5757link_directories(${Boost_LIBRARY_DIR})
    5858
    59 SET(TOOLCHAIN_SRC toolchain/toolchain.cpp toolchain/pipeline.cpp toolchain/object_cache.cpp)
     59SET(OBJECT_CACHE_SRC toolchain/object_cache.cpp)
     60
     61SET(TOOLCHAIN_SRC toolchain/toolchain.cpp toolchain/pipeline.cpp ${OBJECT_CACHE_SRC})
    6062
    6163SET(KERNEL_SRC kernels/kernel.cpp kernels/streamset.cpp kernels/interface.cpp)
    62 SET(KERNEL_SRC ${KERNEL_SRC} kernels/s2p_kernel.cpp kernels/mmap_kernel.cpp kernels/deletion.cpp kernels/swizzle.cpp kernels/p2s_kernel.cpp kernels/stdin_kernel.cpp kernels/stdout_kernel.cpp)
     64SET(KERNEL_SRC ${KERNEL_SRC} kernels/source_kernel.cpp kernels/s2p_kernel.cpp kernels/deletion.cpp kernels/swizzle.cpp kernels/p2s_kernel.cpp kernels/stdout_kernel.cpp)
    6365
    6466SET(IDISA_SRC IR_Gen/CBuilder.cpp IR_Gen/idisa_builder.cpp IR_Gen/idisa_avx_builder.cpp IR_Gen/idisa_i64_builder.cpp IR_Gen/idisa_sse_builder.cpp IR_Gen/idisa_nvptx_builder.cpp IR_Gen/idisa_target.cpp)
  • icGREP/icgrep-devel/icgrep/IR_Gen/CBuilder.cpp

    r5427 r5429  
    3030// ssize_t write(int fildes, const void *buf, size_t nbyte);
    3131Value * CBuilder::CreateWriteCall(Value * fileDescriptor, Value * buf, Value * nbyte) {
     32    PointerType * voidPtrTy = getVoidPtrTy();
    3233    Function * write = mMod->getFunction("write");
    3334    if (write == nullptr) {
    3435        IntegerType * sizeTy = getSizeTy();
    3536        IntegerType * int32Ty = getInt32Ty();
    36         PointerType * int8PtrTy = getInt8PtrTy();
    3737        write = cast<Function>(mMod->getOrInsertFunction("write",
    3838                                                        AttributeSet().addAttribute(getContext(), 2U, Attribute::NoAlias),
    39                                                         sizeTy, int32Ty, int8PtrTy, sizeTy, nullptr));
    40     }
     39                                                        sizeTy, int32Ty, voidPtrTy, sizeTy, nullptr));
     40    }
     41    buf = CreatePointerCast(buf, voidPtrTy);
    4142    return CreateCall(write, {fileDescriptor, buf, nbyte});
    4243}
    4344
    4445Value * CBuilder::CreateReadCall(Value * fileDescriptor, Value * buf, Value * nbyte) {
     46    PointerType * voidPtrTy = getVoidPtrTy();
    4547    Function * readFn = mMod->getFunction("read");
    4648    if (readFn == nullptr) {
    4749        IntegerType * sizeTy = getSizeTy();
    4850        IntegerType * int32Ty = getInt32Ty();
    49         PointerType * int8PtrTy = getInt8PtrTy();
    5051        readFn = cast<Function>(mMod->getOrInsertFunction("read",
    5152                                                         AttributeSet().addAttribute(getContext(), 2U, Attribute::NoAlias),
    52                                                          sizeTy, int32Ty, int8PtrTy, sizeTy, nullptr));
    53     }
     53                                                         sizeTy, int32Ty, voidPtrTy, sizeTy, nullptr));
     54    }
     55    buf = CreatePointerCast(buf, voidPtrTy);
    5456    return CreateCall(readFn, {fileDescriptor, buf, nbyte});
    5557}
  • icGREP/icgrep-devel/icgrep/IR_Gen/CBuilder.h

    r5427 r5429  
    3737        mMod = m;
    3838    }
    39 
    4039
    4140    llvm::Value * CreateMalloc(llvm::Value * size);
     
    154153   
    155154    llvm::PointerType * getVoidPtrTy() const;
     155
    156156    llvm::PointerType * getFILEptrTy();
    157157   
  • icGREP/icgrep-devel/icgrep/array-test.cpp

    r5425 r5429  
    77#include <IR_Gen/idisa_builder.h>                  // for IDISA_Builder
    88#include <IR_Gen/idisa_target.h>                   // for GetIDISA_Builder
    9 #include <kernels/mmap_kernel.h>                   // for MMapSourceKernel
     9#include <kernels/source_kernel.h>
    1010#include <kernels/s2p_kernel.h>                    // for S2PKernel
    1111#include <kernels/alignedprint.h>
     
    142142    const unsigned bufferSegments = codegen::BufferSegments;
    143143   
    144     ExternalFileBuffer ByteStream(iBuilder, iBuilder->getStreamSetTy(1, 8));
     144    SourceBuffer ByteStream(iBuilder, iBuilder->getStreamSetTy(1, 8));
    145145
    146146    MMapSourceKernel mmapK(iBuilder, segmentSize);
     
    170170    iBuilder->SetInsertPoint(BasicBlock::Create(mod->getContext(), "entry", main, 0));
    171171
    172     ByteStream.setStreamSetBuffer(inputStream);
     172 //   ByteStream.setExternalBuffer(inputStream);
    173173    BasisBits.allocateBuffer();
    174174    matches.allocateBuffer();
  • icGREP/icgrep-devel/icgrep/base64.cpp

    r5425 r5429  
    1414#include <IR_Gen/idisa_builder.h>
    1515#include <IR_Gen/idisa_target.h>
    16 #include <kernels/mmap_kernel.h>
     16#include <kernels/source_kernel.h>
    1717#include <kernels/streamset.h>
    1818#include <kernels/radix64.h>
     
    6262    const unsigned bufferSegments = codegen::BufferSegments;
    6363   
    64     SourceFileBuffer ByteStream(iBuilder, iBuilder->getStreamSetTy(1, 8));
     64    SourceBuffer ByteStream(iBuilder, iBuilder->getStreamSetTy(1, 8));
    6565
    6666    CircularBuffer Expanded3_4Out(iBuilder, iBuilder->getStreamSetTy(1, 8), segmentSize * 4/3 * bufferSegments);
  • icGREP/icgrep-devel/icgrep/editd/editd.cpp

    r5425 r5429  
    1212#include <llvm/IR/Function.h>
    1313#include <llvm/IR/Module.h>
    14 #include <llvm/ExecutionEngine/ExecutionEngine.h>
    15 #include "llvm/Linker/Linker.h"
    1614#include <llvm/Support/CommandLine.h>
    1715#include <cc/cc_compiler.h>
     
    2119#include <IR_Gen/idisa_target.h>
    2220#include <kernels/streamset.h>
    23 #include <kernels/mmap_kernel.h>
    24 #include <kernels/stdin_kernel.h>
     21#include <kernels/source_kernel.h>
    2522#include <kernels/s2p_kernel.h>
    2623#include <editd/editdscan_kernel.h>
     
    6865
    6966void run_second_filter(int total_len, int pattern_segs, float errRate){
    70    
     67
    7168    if(matchList.empty()) return;
    7269
     
    126123}
    127124
    128 extern "C" {
    129 std::mutex store_mutex;
    130 void wrapped_report_pos(size_t match_pos, int dist) {
    131         struct matchPosition curMatch;
    132         curMatch.pos = match_pos;
    133         curMatch.dist = dist;
    134 
    135         store_mutex.lock();
    136         matchList.push_back(curMatch);
    137         if(ShowPositions)
    138             std::cout << "pos: " << match_pos << ", dist:" << dist << "\n";
    139         store_mutex.unlock();
    140     }
    141 
    142 }
    143 
    144 void icgrep_Linking(Module * m, ExecutionEngine * e) {
    145     Module::FunctionListType & fns = m->getFunctionList();
    146     for (Module::FunctionListType::iterator it = fns.begin(), it_end = fns.end(); it != it_end; ++it) {
    147         std::string fnName = it->getName().str();
    148         if (fnName == "wrapped_report_pos") {
    149             e->addGlobalMapping(cast<GlobalValue>(it), (void *)&wrapped_report_pos);
    150         }
    151     }
    152 }
    153 
    154125void get_editd_pattern(int & pattern_segs, int & total_len) {
    155  
     126
    156127    if (PatternFilename != "") {
    157128        std::ifstream pattFile(PatternFilename.c_str());
     
    160131            while (std::getline(pattFile, r)) {
    161132                pattVector.push_back(r);
    162                 pattern_segs ++; 
    163                 total_len += r.size(); 
     133                pattern_segs ++;
     134                total_len += r.size();
    164135            }
    165136            std::sort(pattVector.begin(), pattVector.end());
     
    171142                    pattGroup.push_back(pattVector[i]);
    172143                    i++;
    173                 } 
     144                }
    174145                pattGroups.push_back(pattGroup);
    175146            }
     
    177148        }
    178149    }
    179    
     150
    180151    // if there are no regexes specified through -e or -f, the first positional argument
    181152    // must be a regex, not an input file.
    182    
     153
    183154    if (pattVector.size() == 0) {
    184155        pattVector.push_back(inputFiles[0]);
     
    205176}
    206177
     178std::mutex store_mutex;
     179void wrapped_report_pos(size_t match_pos, int dist) {
     180    struct matchPosition curMatch;
     181    curMatch.pos = match_pos;
     182    curMatch.dist = dist;
     183
     184    store_mutex.lock();
     185    matchList.push_back(curMatch);
     186    if(ShowPositions)
     187        std::cout << "pos: " << match_pos << ", dist:" << dist << "\n";
     188    store_mutex.unlock();
     189}
    207190
    208191void editdPipeline(ParabixDriver & pxDriver, const std::vector<std::string> & patterns) {
    209    
    210     IDISA::IDISA_Builder * const iBuilder = pxDriver.getIDISA_Builder();
    211     Module * const m = iBuilder->getModule();
    212     Type * const sizeTy = iBuilder->getSizeTy();
    213     Type * const voidTy = iBuilder->getVoidTy();
    214     Type * const inputType = PointerType::get(ArrayType::get(ArrayType::get(iBuilder->getBitBlockType(), 8), 1), 0);
    215    
     192
     193    IDISA::IDISA_Builder * const idb = pxDriver.getIDISA_Builder();
     194    Module * const m = idb->getModule();
     195    Type * const sizeTy = idb->getSizeTy();
     196    Type * const voidTy = idb->getVoidTy();
     197    Type * const inputType = PointerType::get(ArrayType::get(ArrayType::get(idb->getBitBlockType(), 8), 1), 0);
     198
     199    idb->LinkFunction("wrapped_report_pos", &wrapped_report_pos);
     200
    216201    Function * const main = cast<Function>(m->getOrInsertFunction("Main", voidTy, inputType, sizeTy, nullptr));
    217202    main->setCallingConv(CallingConv::C);
     
    221206    Value * const fileSize = &*(args++);
    222207    fileSize->setName("fileSize");
    223     iBuilder->SetInsertPoint(BasicBlock::Create(m->getContext(), "entry", main,0));
    224 
    225 
    226     auto ChStream = pxDriver.addBuffer(make_unique<SourceFileBuffer>(iBuilder, iBuilder->getStreamSetTy(4)));
    227 
    228     auto mmapK = pxDriver.addKernelInstance(make_unique<kernel::FileSourceKernel>(iBuilder, inputType));
     208    idb->SetInsertPoint(BasicBlock::Create(m->getContext(), "entry", main,0));
     209
     210    auto ChStream = pxDriver.addBuffer(make_unique<SourceBuffer>(idb, idb->getStreamSetTy(4)));
     211
     212    auto mmapK = pxDriver.addKernelInstance(make_unique<MemorySourceKernel>(idb, inputType));
    229213    mmapK->setInitialArguments({inputStream, fileSize});
    230214
    231215    pxDriver.makeKernelCall(mmapK, {}, {ChStream});
    232216
    233     auto MatchResults = pxDriver.addBuffer(make_unique<SingleBlockBuffer>(iBuilder, iBuilder->getStreamSetTy(editDistance + 1)));
     217    auto MatchResults = pxDriver.addBuffer(make_unique<SingleBlockBuffer>(idb, idb->getStreamSetTy(editDistance + 1)));
    234218
    235219    auto editdk = pxDriver.addKernelInstance(make_unique<PabloKernel>(
    236         iBuilder, "editd", std::vector<Binding>{{iBuilder->getStreamSetTy(4), "pat"}}, std::vector<Binding>{{iBuilder->getStreamSetTy(editDistance + 1), "E"}}
     220        idb, "editd", std::vector<Binding>{{idb->getStreamSetTy(4), "pat"}}, std::vector<Binding>{{idb->getStreamSetTy(editDistance + 1), "E"}}
    237221        ));
    238222
     
    240224    pxDriver.makeKernelCall(editdk, {ChStream}, {MatchResults});
    241225
    242     auto editdScanK = pxDriver.addKernelInstance(make_unique<editdScanKernel>(iBuilder, editDistance));
     226    auto editdScanK = pxDriver.addKernelInstance(make_unique<editdScanKernel>(idb, editDistance));
    243227    pxDriver.makeKernelCall(editdScanK, {MatchResults}, {});
    244        
     228
    245229    pxDriver.generatePipelineIR();
    246230
    247     iBuilder->CreateRetVoid();
     231    idb->CreateRetVoid();
    248232
    249233    pxDriver.linkAndFinalize();
     
    275259    Module * m = iBuilder->getModule();
    276260    Type * mBitBlockType = iBuilder->getBitBlockType();
    277    
     261
    278262
    279263    Type * const voidTy = iBuilder->getVoidTy();
    280264    Type * const int32Ty = iBuilder->getInt32Ty();
    281265    Type * const outputType = PointerType::get(ArrayType::get(mBitBlockType, 4), 0);
    282    
     266
    283267    Function * const main = cast<Function>(m->getOrInsertFunction("Main", voidTy, int32Ty, outputType, nullptr));
    284268    main->setCallingConv(CallingConv::C);
    285269    Function::arg_iterator args = main->arg_begin();
    286    
     270
    287271    Value * const fileDescriptor = &*(args++);
    288272    fileDescriptor->setName("fileDescriptor");
     
    292276    iBuilder->SetInsertPoint(BasicBlock::Create(m->getContext(), "entry", main));
    293277
    294     auto ByteStream = pxDriver.addBuffer(make_unique<SourceFileBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8)));
    295 
    296     auto mmapK = pxDriver.addKernelInstance(make_unique<kernel::MMapSourceKernel>(iBuilder, 1));
     278    auto ByteStream = pxDriver.addBuffer(make_unique<SourceBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8)));
     279
     280    auto mmapK = pxDriver.addKernelInstance(make_unique<MMapSourceKernel>(iBuilder, 1));
    297281    mmapK->setInitialArguments({fileDescriptor});
    298282    pxDriver.makeKernelCall(mmapK, {}, {ByteStream});
     
    302286    pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
    303287
    304     auto CCResults = pxDriver.addExternalBuffer(make_unique<ExternalFileBuffer>(iBuilder, iBuilder->getStreamSetTy(4)), outputStream);
    305 
    306 
     288    auto CCResults = pxDriver.addExternalBuffer(make_unique<ExternalBuffer>(iBuilder, iBuilder->getStreamSetTy(4), outputStream));
    307289
    308290    auto ccck = pxDriver.addKernelInstance(make_unique<PabloKernel>(
    309291        iBuilder, "ccc", std::vector<Binding>{{iBuilder->getStreamSetTy(8), "basis"}}, std::vector<Binding>{{iBuilder->getStreamSetTy(4), "pat"}}
    310         ));
     292    ));
     293
    311294    buildPreprocessKernel(reinterpret_cast<PabloKernel *>(ccck));
     295
    312296    pxDriver.makeKernelCall(ccck, {BasisBits}, {CCResults});
    313              
     297
    314298    pxDriver.generatePipelineIR();
    315299
     
    322306typedef void (*preprocessFunctionType)(const int fd, char * output_data);
    323307
    324 preprocessFunctionType preprocessCodeGen() {                           
     308preprocessFunctionType preprocessCodeGen() {
    325309    ParabixDriver pxDriver("preprocess");
    326310    preprocessPipeline(pxDriver);
     
    330314typedef void (*editdFunctionType)(char * byte_data, size_t filesize);
    331315
    332 editdFunctionType editdCodeGen(const std::vector<std::string> & patterns) {                           
     316editdFunctionType editdCodeGen(const std::vector<std::string> & patterns) {
    333317    ParabixDriver pxDriver("editd");
    334318    editdPipeline(pxDriver, patterns);
     
    362346    fn_ptr(fd, chStream);
    363347    close(fd);
    364     return chStream;   
     348    return chStream;
    365349}
    366350
    367351void editd(editdFunctionType fn_ptr, char * inputStream, size_t size) {
    368  
    369     if (size == 0) {
    370         inputStream = nullptr;
    371     }
    372 
    373352    fn_ptr(inputStream, size);
    374    
    375353}
    376354
     
    398376}
    399377
    400 #ifdef CUDA_ENABLED 
     378#ifdef CUDA_ENABLED
    401379
    402380#define GROUPTHREADS 64
    403381#define GROUPBLOCKS 64
    404382
    405 void editdGPUCodeGen(unsigned patternLen){ 
     383void editdGPUCodeGen(unsigned patternLen){
    406384    LLVMContext TheContext;
    407385    Module * M = new Module("editd-gpu", TheContext);
     
    426404    mmapK.generateKernel({}, {&CCStream});
    427405
    428     kernel::editdGPUKernel editdk(iBuilder, editDistance, patternLen); 
     406    kernel::editdGPUKernel editdk(iBuilder, editDistance, patternLen);
    429407    editdk.generateKernel({&CCStream}, {&ResultStream});
    430408
     
    432410    main->setCallingConv(CallingConv::C);
    433411    Function::arg_iterator args = main->arg_begin();
    434    
     412
    435413    Value * const inputStream = &*(args++);
    436414    inputStream->setName("input");
     
    443421    Value * const stridesPtr = &*(args++);
    444422    stridesPtr->setName("stridesPtr");
    445    
     423
    446424    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main,0));
    447425
     
    467445
    468446    editdk.setInitialArguments({pattStream, strideCarry});
    469    
     447
    470448    generatePipelineLoop(iBuilder, {&mmapK, &editdk});
    471        
     449
    472450    iBuilder->CreateRetVoid();
    473    
     451
    474452    MDNode * Node = MDNode::get(M->getContext(),
    475453                                {llvm::ValueAsMetadata::get(main),
    476                                  MDString::get(M->getContext(), "kernel"), 
     454                                 MDString::get(M->getContext(), "kernel"),
    477455                                 ConstantAsMetadata::get(ConstantInt::get(iBuilder->getInt32Ty(), 1))});
    478456    NamedMDNode *NMD = M->getOrInsertNamedMetadata("nvvm.annotations");
     
    499477    main->setCallingConv(CallingConv::C);
    500478    Function::arg_iterator args = main->arg_begin();
    501    
     479
    502480    Value * const resultStream = &*(args++);
    503481    resultStream->setName("resultStream");
     
    509487    BasicBlock * strideLoopBody = BasicBlock::Create(iBuilder->getContext(), "strideLoopBody", main, 0);
    510488    BasicBlock * stridesDone = BasicBlock::Create(iBuilder->getContext(), "stridesDone", main, 0);
    511    
     489
    512490    iBuilder->SetInsertPoint(entryBlock);
    513491
     
    528506    Value * notDone = iBuilder->CreateICmpULT(strideNo, strides);
    529507    iBuilder->CreateCondBr(notDone, strideLoopBody, stridesDone);
    530  
     508
    531509    iBuilder->SetInsertPoint(strideLoopBody);
    532510    Value * myResultStreamPtr = iBuilder->CreateGEP(resultStreamPtr, {iBuilder->CreateMul(strideBlocks, strideNo)});
     
    535513        Value * nextStreamPtr = iBuilder->CreateGEP(myResultStreamPtr, {iBuilder->CreateMul(outputBlocks, iBuilder->getInt32(i)), bid});
    536514        myResultStream = iBuilder->CreateOr(myResultStream, iBuilder->CreateLoad(nextStreamPtr));
    537     }   
     515    }
    538516    iBuilder->CreateStore(myResultStream, iBuilder->CreateGEP(myResultStreamPtr, {iBuilder->getInt32(0), bid}));
    539517    strideNo->addIncoming(iBuilder->CreateAdd(strideNo, ConstantInt::get(int32ty, 1)), strideLoopBody);
    540518    iBuilder->CreateBr(strideLoopCond);
    541    
     519
    542520    iBuilder->SetInsertPoint(stridesDone);
    543521    iBuilder->CreateRetVoid();
    544    
     522
    545523    MDNode * Node = MDNode::get(M->getContext(),
    546524                                {llvm::ValueAsMetadata::get(main),
    547                                  MDString::get(M->getContext(), "kernel"), 
     525                                 MDString::get(M->getContext(), "kernel"),
    548526                                 ConstantAsMetadata::get(ConstantInt::get(iBuilder->getInt32Ty(), 1))});
    549527    NamedMDNode *NMD = M->getOrInsertNamedMetadata("nvvm.annotations");
     
    555533
    556534editdFunctionType editdScanCPUCodeGen() {
    557                            
     535
    558536    LLVMContext TheContext;
    559537    Module * M = new Module("editd", TheContext);
     
    572550
    573551    kernel::editdScanKernel editdScanK(iBuilder, editDistance);
    574     editdScanK.generateKernel({&MatchResults}, {});               
    575    
     552    editdScanK.generateKernel({&MatchResults}, {});
     553
    576554    Function * const main = cast<Function>(M->getOrInsertFunction("CPU_Main", voidTy, inputType, size_ty, nullptr));
    577555    main->setCallingConv(CallingConv::C);
    578556    Function::arg_iterator args = main->arg_begin();
    579    
     557
    580558    Value * const inputStream = &*(args++);
    581559    inputStream->setName("input");
    582560    Value * const fileSize = &*(args++);
    583561    fileSize->setName("fileSize");
    584    
     562
    585563    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main,0));
    586564
    587565    MatchResults.setStreamSetBuffer(inputStream);
    588566    mmapK.setInitialArguments({fileSize});
    589    
     567
    590568    generatePipelineLoop(iBuilder, {&mmapK, &editdScanK});
    591        
     569
    592570    iBuilder->CreateRetVoid();
    593571
    594572    editdEngine = JIT_to_ExecutionEngine(M);
    595    
     573
    596574    editdEngine->finalizeObject();
    597575
     
    610588    get_editd_pattern(pattern_segs, total_len);
    611589
    612 #ifdef CUDA_ENABLED 
     590#ifdef CUDA_ENABLED
    613591    codegen::BlockSize = 64;
    614592#endif
     
    617595    preprocess(preprocess_ptr);
    618596
    619 #ifdef CUDA_ENABLED 
    620     setNVPTXOption();   
     597#ifdef CUDA_ENABLED
     598    setNVPTXOption();
    621599    if(codegen::NVPTX){
    622600
     
    625603            std::cerr << "Error: cannot open " << PatternFilename << " for processing. Skipped.\n";
    626604            exit(-1);
    627         } 
     605        }
    628606        std::string patterns((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
    629607
     
    637615
    638616        editd(editd_ptr, (char*)rslt, size);
    639        
     617
    640618        run_second_filter(pattern_segs, total_len, 0.15);
    641619
     
    643621    }
    644622#endif
    645    
     623
    646624    if(pattVector.size() == 1){
    647625        editdFunctionType editd_ptr = editdCodeGen(pattVector);
  • icGREP/icgrep-devel/icgrep/grep_engine.cpp

    r5425 r5429  
    77#include "grep_engine.h"
    88#include <llvm/IR/Module.h>
    9 #include <llvm/ExecutionEngine/MCJIT.h>
     9//#include <llvm/ExecutionEngine/MCJIT.h>
    1010#include <llvm/IR/Verifier.h>
    1111#include <llvm/Support/CommandLine.h>
     
    1919#include <kernels/streams_merge.h>
    2020#include <kernels/match_count.h>
    21 #include <kernels/mmap_kernel.h>
     21#include <kernels/source_kernel.h>
    2222#include <kernels/s2p_kernel.h>
    2323#include <kernels/scanmatchgen.h>
    2424#include <kernels/streamset.h>
    25 #include <kernels/stdin_kernel.h>
    2625#include <pablo/pablo_kernel.h>
    2726#include <re/re_cc.h>
     
    228227        fileIdx->setName("fileIdx");
    229228
    230         ByteStream = pxDriver.addBuffer(make_unique<SourceFileBuffer>(idb, idb->getStreamSetTy(1, 8)));
    231 
    232         sourceK = pxDriver.addKernelInstance(make_unique<kernel::FileSourceKernel>(idb, idb->getInt8PtrTy(), segmentSize));
     229        ByteStream = pxDriver.addBuffer(make_unique<SourceBuffer>(idb, idb->getStreamSetTy(1, 8)));
     230
     231        sourceK = pxDriver.addKernelInstance(make_unique<kernel::MemorySourceKernel>(idb, idb->getInt8PtrTy(), segmentSize));
    233232        sourceK->setInitialArguments({buffer, length});
    234233
     
    245244        fileIdx->setName("fileIdx");
    246245
     246        ByteStream = pxDriver.addBuffer(make_unique<SourceBuffer>(idb, idb->getStreamSetTy(1, 8)));
     247
    247248        if (grepSource == GrepSource::File) {
    248             ByteStream = pxDriver.addBuffer(make_unique<SourceFileBuffer>(idb, idb->getStreamSetTy(1, 8)));
    249249            sourceK = pxDriver.addKernelInstance(make_unique<kernel::MMapSourceKernel>(idb, segmentSize));
    250250            sourceK->setInitialArguments({fileDescriptor});
    251251        } else { // if (grepSource == GrepSource::StdIn) {
    252             ByteStream = pxDriver.addBuffer(make_unique<ExtensibleBuffer>(idb, idb->getStreamSetTy(1, 8), segmentSize));
    253             sourceK = pxDriver.addKernelInstance(make_unique<kernel::StdInKernel>(idb, segmentSize));
     252            sourceK = pxDriver.addKernelInstance(make_unique<kernel::ReadSourceKernel>(idb, segmentSize));
     253            sourceK->setInitialArguments({idb->getInt32(STDIN_FILENO)});
    254254        }
    255255    }
  • icGREP/icgrep-devel/icgrep/icgrep-devel.files

    r5425 r5429  
    5757kernels/match_count.cpp
    5858kernels/match_count.h
    59 kernels/mmap_kernel.cpp
    60 kernels/mmap_kernel.h
    6159kernels/p2s_kernel.cpp
    6260kernels/p2s_kernel.h
     
    6765kernels/scanmatchgen.cpp
    6866kernels/scanmatchgen.h
    69 kernels/stdin_kernel.cpp
    70 kernels/stdin_kernel.h
    7167kernels/stdout_kernel.cpp
    7268kernels/stdout_kernel.h
     
    248244wc.cpp
    249245CMakeLists.txt
     246kernels/source_kernel.h
     247kernels/source_kernel.cpp
  • icGREP/icgrep-devel/icgrep/kernels/kernel.cpp

    r5425 r5429  
    3434static const std::string BUFFER_PTR_SUFFIX = "_bufferPtr";
    3535
    36 static const std::string CONSUMER_SUFFIX = "_cls";
     36static const std::string CONSUMER_SUFFIX = "_consumerLocks";
    3737
    3838using namespace llvm;
     
    234234    }
    235235    for (auto binding : mStreamSetOutputs) {
    236         setConsumerState(binding.name, &*(args++));
     236        setConsumerLock(binding.name, &*(args++));
    237237    }
    238238    generateInitializeMethod();
     
    372372}
    373373
    374 llvm::Value * KernelBuilder::getConsumerState(const std::string & name) const {
     374llvm::Value * KernelBuilder::getConsumerLock(const std::string & name) const {
    375375    return getScalarField(name + CONSUMER_SUFFIX);
    376376}
    377377
    378 void KernelBuilder::setConsumerState(const std::string & name, llvm::Value * value) const {
     378void KernelBuilder::setConsumerLock(const std::string & name, llvm::Value * value) const {
    379379    setScalarField(name + CONSUMER_SUFFIX, value);
    380380}
     
    487487    const StreamSetBuffer * const buf = getOutputStreamSetBuffer(name);
    488488    buf->reserveBytes(getStreamSetBufferPtr(name), iBuilder->CreateAdd(itemCount, value));
     489}
     490
     491BasicBlock * KernelBuilder::CreateWaitForConsumers() const {
     492
     493    const auto consumers = getStreamOutputs();
     494    BasicBlock * const entry = iBuilder->GetInsertBlock();
     495    if (consumers.empty()) {
     496        return entry;
     497    } else {
     498        Function * const parent = entry->getParent();
     499        IntegerType * const sizeTy = iBuilder->getSizeTy();
     500        ConstantInt * const zero = iBuilder->getInt32(0);
     501        ConstantInt * const one = iBuilder->getInt32(1);
     502        ConstantInt * const size0 = iBuilder->getSize(0);
     503
     504        Value * const segNo = acquireLogicalSegmentNo();
     505        const auto n = consumers.size();
     506        BasicBlock * load[n + 1];
     507        BasicBlock * wait[n];
     508        for (unsigned i = 0; i < n; ++i) {
     509            load[i] = BasicBlock::Create(iBuilder->getContext(), consumers[i].name + "Load", parent);
     510            wait[i] = BasicBlock::Create(iBuilder->getContext(), consumers[i].name + "Wait", parent);
     511        }
     512        load[n] = BasicBlock::Create(iBuilder->getContext(), "Resume", parent);
     513        iBuilder->CreateBr(load[0]);
     514        for (unsigned i = 0; i < n; ++i) {
     515
     516            iBuilder->SetInsertPoint(load[i]);
     517            Value * const outputConsumers = getConsumerLock(consumers[i].name);
     518
     519            Value * const consumerCount = iBuilder->CreateLoad(iBuilder->CreateGEP(outputConsumers, {zero, zero}));
     520            Value * const consumerPtr = iBuilder->CreateLoad(iBuilder->CreateGEP(outputConsumers, {zero, one}));
     521            Value * const noConsumers = iBuilder->CreateICmpEQ(consumerCount, size0);
     522            iBuilder->CreateUnlikelyCondBr(noConsumers, load[i + 1], wait[i]);
     523
     524            iBuilder->SetInsertPoint(wait[i]);
     525            PHINode * const consumerPhi = iBuilder->CreatePHI(sizeTy, 2);
     526            consumerPhi->addIncoming(size0, load[i]);
     527
     528            Value * const conSegPtr = iBuilder->CreateLoad(iBuilder->CreateGEP(consumerPtr, consumerPhi));
     529            Value * const processedSegmentCount = iBuilder->CreateAtomicLoadAcquire(conSegPtr);
     530            Value * const ready = iBuilder->CreateICmpEQ(segNo, processedSegmentCount);
     531            assert (ready->getType() == iBuilder->getInt1Ty());
     532            Value * const nextConsumerIdx = iBuilder->CreateAdd(consumerPhi, iBuilder->CreateZExt(ready, sizeTy));
     533            consumerPhi->addIncoming(nextConsumerIdx, wait[i]);
     534            Value * const next = iBuilder->CreateICmpEQ(nextConsumerIdx, consumerCount);
     535            iBuilder->CreateCondBr(next, load[i + 1], wait[i]);
     536        }
     537
     538        BasicBlock * const exit = load[n];
     539        iBuilder->SetInsertPoint(exit);
     540        return exit;
     541    }
     542
    489543}
    490544
     
    548602        report_fatal_error("Cannot initialize " + getName() + " before calling createInstance()");
    549603    }
     604
    550605    std::vector<Value *> args;
    551606    args.reserve(1 + mInitialArguments.size() + mStreamSetInputBuffers.size() + (mStreamSetOutputBuffers.size() * 2));
     
    602657        args.push_back(outputConsumers);
    603658    }
    604 
    605659
    606660    iBuilder->CreateCall(getInitFunction(), args);
  • icGREP/icgrep-devel/icgrep/kernels/kernel.h

    r5425 r5429  
    135135    void releaseLogicalSegmentNo(llvm::Value * nextSegNo) const;
    136136
    137     llvm::Value * getConsumerState(const std::string & name) const;
    138 
    139137    // Get a parameter by name.
    140138    llvm::Argument * getParameter(llvm::Function * f, const std::string & name) const;
     
    258256    }
    259257
     258    llvm::BasicBlock * CreateWaitForConsumers() const;
    260259
    261260    llvm::BasicBlock * CreateBasicBlock(std::string && name) const;
     
    310309private:
    311310
    312     void setConsumerState(const std::string & name, llvm::Value * value) const;
     311    llvm::Value * getConsumerLock(const std::string & name) const;
     312
     313    void setConsumerLock(const std::string & name, llvm::Value * value) const;
    313314
    314315    llvm::Value * computeBlockIndex(const std::vector<Binding> & binding, const std::string & name, llvm::Value * itemCount) const;
  • icGREP/icgrep-devel/icgrep/kernels/streamset.cpp

    r5425 r5429  
    2929
    3030void StreamSetBuffer::allocateBuffer() {
    31     Type * const ty = getType();
    32     ConstantInt * blocks = iBuilder->getSize(mBufferBlocks);
    33     mStreamSetBufferPtr = iBuilder->CreateCacheAlignedAlloca(ty, iBuilder->getSize(mBufferBlocks));
    34     Constant * width = ConstantExpr::getMul(ConstantExpr::getSizeOf(ty), blocks);
    35     iBuilder->CreateMemZero(mStreamSetBufferPtr, width, iBuilder->getCacheAlignment());
     31    if (LLVM_LIKELY(mStreamSetBufferPtr == nullptr)) {
     32        Type * const ty = getType();
     33        mStreamSetBufferPtr = iBuilder->CreateCacheAlignedAlloca(ty, iBuilder->getSize(mBufferBlocks));
     34        iBuilder->CreateAlignedStore(Constant::getNullValue(ty), mStreamSetBufferPtr, iBuilder->getCacheAlignment());
     35    } else {
     36        report_fatal_error("StreamSetBuffer::allocateBuffer() was called twice on the same stream set");
     37    }
    3638}
    3739
    3840Value * StreamSetBuffer::getStreamBlockPtr(Value * self, Value * streamIndex, Value * blockIndex, const bool /* readOnly */) const {
    3941    iBuilder->CreateAssert(iBuilder->CreateICmpULT(streamIndex, getStreamSetCount(self)), "StreamSetBuffer: out-of-bounds stream access");
    40     return iBuilder->CreateGEP(getStreamSetBlockPtr(getBaseAddress(self), blockIndex), {iBuilder->getInt32(0), streamIndex});
     42    return iBuilder->CreateGEP(getStreamSetBlockPtr(self, blockIndex), {iBuilder->getInt32(0), streamIndex});
    4143}
    4244
    4345Value * StreamSetBuffer::getStreamPackPtr(Value * self, Value * streamIndex, Value * blockIndex, Value * packIndex, const bool /* readOnly */) const {
    4446    iBuilder->CreateAssert(iBuilder->CreateICmpULT(streamIndex, getStreamSetCount(self)), "StreamSetBuffer: out-of-bounds stream access");
    45     return iBuilder->CreateGEP(getStreamSetBlockPtr(getBaseAddress(self), blockIndex), {iBuilder->getInt32(0), streamIndex, packIndex});
     47    return iBuilder->CreateGEP(getStreamSetBlockPtr(self, blockIndex), {iBuilder->getInt32(0), streamIndex, packIndex});
    4648}
    4749
     
    148150}
    149151
    150 // External File Buffer
    151 void ExternalFileBuffer::setStreamSetBuffer(Value * ptr) {
    152     mStreamSetBufferPtr = iBuilder->CreatePointerBitCastOrAddrSpaceCast(ptr, getPointerType());
    153 }
    154 
    155 void ExternalFileBuffer::allocateBuffer() {
    156     report_fatal_error("External buffers cannot be allocated.");
    157 }
    158 
    159 Value * ExternalFileBuffer::getStreamSetBlockPtr(Value * self, Value * blockIndex) const {
    160     return iBuilder->CreateGEP(self, blockIndex);
    161 }
    162 
    163 Value * ExternalFileBuffer::getLinearlyAccessibleItems(Value * self, Value *) const {
    164     report_fatal_error("External buffers: getLinearlyAccessibleItems is not supported.");
    165 }
    166 
    167152// Source File Buffer
    168 Value * SourceFileBuffer::getBufferedSize(Value * self) const {
     153Value * SourceBuffer::getBufferedSize(Value * self) const {
    169154    Value * ptr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
    170155    return iBuilder->CreateLoad(ptr);
    171156}
    172157
    173 void SourceFileBuffer::setBufferedSize(Value * self, llvm::Value * size) const {
     158void SourceBuffer::setBufferedSize(Value * self, llvm::Value * size) const {
    174159    Value * ptr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
    175160    iBuilder->CreateStore(size, ptr);
    176161}
    177162
    178 void SourceFileBuffer::setBaseAddress(Value * self, Value * addr) const {
    179     Value * ptr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
     163void SourceBuffer::setBaseAddress(Value * self, Value * addr) const {
     164    Value * const ptr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
    180165    iBuilder->CreateStore(iBuilder->CreatePointerCast(addr, ptr->getType()->getPointerElementType()), ptr);
    181166}
    182167
    183 Value * SourceFileBuffer::getBaseAddress(Value * const self) const {
    184     Value * ptr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
    185     return iBuilder->CreateLoad(ptr);
    186 }
    187 
    188 Value * SourceFileBuffer::getStreamSetBlockPtr(Value * self, Value * blockIndex) const {
    189     return iBuilder->CreateGEP(self, blockIndex);
    190 }
    191 
    192 Value * SourceFileBuffer::getLinearlyAccessibleItems(Value * self, Value *) const {
     168Value * SourceBuffer::getBaseAddress(Value * const self) const {
     169    Value * const ptr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
     170    Value * const addr = iBuilder->CreateLoad(ptr);
     171    return addr;
     172}
     173
     174Value * SourceBuffer::getStreamSetBlockPtr(Value * self, Value * blockIndex) const {
     175    return iBuilder->CreateGEP(getBaseAddress(self), blockIndex);
     176}
     177
     178Value * SourceBuffer::getLinearlyAccessibleItems(Value * self, Value *) const {
    193179    report_fatal_error("External buffers: getLinearlyAccessibleItems is not supported.");
    194180}
    195181
    196 // ExtensibleBuffer
    197 Value * ExtensibleBuffer::getLinearlyAccessibleItems(Value * self, Value * fromPosition) const {
    198     Value * capacityPtr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
    199     Value * capacity = iBuilder->CreateLoad(capacityPtr);
    200     return iBuilder->CreateSub(capacity, fromPosition);
    201 }
    202 
    203 Value * ExtensibleBuffer::roundUpToPageSize(Value * const value) const {
    204     const auto pageSize = getpagesize();
    205     assert ((pageSize & (pageSize - 1)) == 0);
    206     Constant * const pageMask = ConstantInt::get(value->getType(), pageSize - 1);
    207     return iBuilder->CreateAnd(iBuilder->CreateAdd(value, pageMask), iBuilder->CreateNot(pageMask));
    208 }
    209 
    210 void ExtensibleBuffer::allocateBuffer() {
    211     Type * ty = getType();
    212     Value * instance = iBuilder->CreateCacheAlignedAlloca(ty);
    213     Value * const capacityPtr = iBuilder->CreateGEP(instance, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
    214 
    215     Type * const elementType = ty->getStructElementType(1)->getPointerElementType();
    216     Constant * size = ConstantExpr::getSizeOf(elementType);
    217     size = ConstantExpr::getMul(size, iBuilder->getSize(mBufferBlocks));
    218     size = ConstantExpr::getIntegerCast(size, iBuilder->getSizeTy(), false);
    219     Value * const initialSize = roundUpToPageSize(size);
    220 
    221     iBuilder->CreateStore(initialSize, capacityPtr);
    222     Value * addr = iBuilder->CreateAnonymousMMap(size);
    223     Value * const addrPtr = iBuilder->CreateGEP(instance, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
    224     addr = iBuilder->CreatePointerCast(addr, addrPtr->getType()->getPointerElementType());
    225     iBuilder->CreateStore(addr, addrPtr);
    226     Value * const bufferSizePtr = iBuilder->CreateGEP(instance, {iBuilder->getInt32(0), iBuilder->getInt32(2)});
    227     iBuilder->CreateStore(ConstantInt::getNullValue(bufferSizePtr->getType()->getPointerElementType()), bufferSizePtr);
    228     mStreamSetBufferPtr = instance;
    229 }
    230 
    231 Value * ExtensibleBuffer::getStreamSetBlockPtr(Value * self, Value * blockIndex) const {
    232     return iBuilder->CreateGEP(self, blockIndex);
    233 }
    234 
    235 void ExtensibleBuffer::reserveBytes(Value * const self, llvm::Value * const requiredSize) const {
    236 
    237     Value * const capacityPtr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
    238     Value * const currentSize = iBuilder->CreateLoad(capacityPtr);
    239     BasicBlock * const entry = iBuilder->GetInsertBlock();
    240     Function * const parent = entry->getParent();
    241     IntegerType * const sizeTy = iBuilder->getSizeTy();
    242     ConstantInt * const zero = iBuilder->getInt32(0);
    243     ConstantInt * const one = iBuilder->getInt32(1);
    244 
    245     BasicBlock * const expand = BasicBlock::Create(iBuilder->getContext(), "expand", parent);
    246     BasicBlock * const resume = BasicBlock::Create(iBuilder->getContext(), "resume", parent);
    247 
    248     Value * noExpansionNeeded = iBuilder->CreateICmpULT(requiredSize, currentSize);
    249 
    250     kernel::KernelBuilder * const kernel = getProducer();
    251     auto consumers = kernel->getStreamOutputs();
    252     if (consumers.empty()) {
    253         iBuilder->CreateLikelyCondBr(noExpansionNeeded, resume, expand);
    254     } else { // we cannot risk expanding this buffer until all of the consumers have finished reading the data
    255 
    256         ConstantInt * const size0 = iBuilder->getSize(0);
    257         Value * const segNo = kernel->acquireLogicalSegmentNo();
    258         const auto n = consumers.size();
    259 
    260         BasicBlock * load[n + 1];
    261         BasicBlock * wait[n];
    262         for (unsigned i = 0; i < n; ++i) {
    263             load[i] = BasicBlock::Create(iBuilder->getContext(), consumers[i].name + "Load", parent);
    264             wait[i] = BasicBlock::Create(iBuilder->getContext(), consumers[i].name + "Wait", parent);
    265         }
    266         load[n] = expand;
    267         iBuilder->CreateLikelyCondBr(noExpansionNeeded, resume, load[0]);
    268 
    269         for (unsigned i = 0; i < n; ++i) {
    270 
    271             iBuilder->SetInsertPoint(load[i]);
    272             Value * const outputConsumers = kernel->getConsumerState(consumers[i].name);
    273 
    274             Value * const consumerCount = iBuilder->CreateLoad(iBuilder->CreateGEP(outputConsumers, {zero, zero}));
    275             Value * const consumerPtr = iBuilder->CreateLoad(iBuilder->CreateGEP(outputConsumers, {zero, one}));
    276             Value * const noConsumers = iBuilder->CreateICmpEQ(consumerCount, size0);
    277             iBuilder->CreateUnlikelyCondBr(noConsumers, load[i + 1], wait[i]);
    278 
    279             iBuilder->SetInsertPoint(wait[i]);
    280             PHINode * const consumerPhi = iBuilder->CreatePHI(sizeTy, 2);
    281             consumerPhi->addIncoming(size0, load[i]);
    282 
    283             Value * const conSegPtr = iBuilder->CreateLoad(iBuilder->CreateGEP(consumerPtr, consumerPhi));
    284             Value * const processedSegmentCount = iBuilder->CreateAtomicLoadAcquire(conSegPtr);
    285             Value * const ready = iBuilder->CreateICmpEQ(segNo, processedSegmentCount);
    286             assert (ready->getType() == iBuilder->getInt1Ty());
    287             Value * const nextConsumerIdx = iBuilder->CreateAdd(consumerPhi, iBuilder->CreateZExt(ready, sizeTy));
    288             consumerPhi->addIncoming(nextConsumerIdx, wait[i]);
    289             Value * const next = iBuilder->CreateICmpEQ(nextConsumerIdx, consumerCount);
    290             iBuilder->CreateCondBr(next, load[i + 1], wait[i]);
    291 
    292         }
    293         expand->moveAfter(wait[n - 1]);
    294         resume->moveAfter(expand);
    295     }
    296     iBuilder->SetInsertPoint(expand);
    297     Value * const reservedSize = roundUpToPageSize(iBuilder->CreateShl(requiredSize, 1));
    298     Value * const baseAddrPtr = iBuilder->CreateGEP(self, {zero, one});
    299 
    300     Value * const baseAddr = iBuilder->CreateLoad(baseAddrPtr);
    301     Value * newAddr = iBuilder->CreateMRemap(baseAddr, currentSize, reservedSize);
    302     newAddr = iBuilder->CreatePointerCast(newAddr, baseAddr->getType());
    303     iBuilder->CreateStore(newAddr, baseAddrPtr);
    304     iBuilder->CreateStore(reservedSize, capacityPtr);
    305     iBuilder->CreateBr(resume);
    306     iBuilder->SetInsertPoint(resume);
    307 }
    308 
    309 Value * ExtensibleBuffer::getBufferedSize(Value * self) const {
    310     Value * ptr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), iBuilder->getInt32(2)});
    311     return iBuilder->CreateLoad(ptr);
    312 }
    313 
    314 void ExtensibleBuffer::setBufferedSize(Value * self, llvm::Value * size) const {
    315     Value * ptr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), iBuilder->getInt32(2)});
    316     iBuilder->CreateStore(size, ptr);
    317 }
    318 
    319 Value * ExtensibleBuffer::getBaseAddress(Value * const self) const {
    320     Value * ptr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
    321     return iBuilder->CreateLoad(ptr);
    322 }
    323 
    324 void ExtensibleBuffer::releaseBuffer(Value * self) const {
    325     Value * const sizePtr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
    326     Value * size = iBuilder->CreateLoad(sizePtr);
    327     iBuilder->CreateMUnmap(getBaseAddress(self), size);
     182// External File Buffer
     183void ExternalBuffer::allocateBuffer() {
     184    report_fatal_error("External buffers cannot be allocated.");
     185}
     186
     187Value * ExternalBuffer::getStreamSetBlockPtr(Value * self, Value * blockIndex) const {
     188    return iBuilder->CreateGEP(getBaseAddress(self), blockIndex);
     189}
     190
     191Value * ExternalBuffer::getLinearlyAccessibleItems(Value *, Value *) const {
     192    report_fatal_error("External buffers: getLinearlyAccessibleItems is not supported.");
    328193}
    329194
    330195// Circular Buffer
    331 
    332196Value * CircularBuffer::getStreamSetBlockPtr(Value * const self, Value * const blockIndex) const {
    333     return iBuilder->CreateGEP(self, modByBufferBlocks(blockIndex));
     197    return iBuilder->CreateGEP(getBaseAddress(self), modByBufferBlocks(blockIndex));
    334198}
    335199
    336200// CircularCopybackBuffer Buffer
    337 
    338201void CircularCopybackBuffer::allocateBuffer() {
    339202    mStreamSetBufferPtr = iBuilder->CreateCacheAlignedAlloca(getType(), iBuilder->getSize(mBufferBlocks + mOverflowBlocks));
     
    375238
    376239Value * CircularCopybackBuffer::getStreamSetBlockPtr(Value * self, Value * blockIndex) const {
    377     return iBuilder->CreateGEP(self, modByBufferBlocks(blockIndex));
     240    return iBuilder->CreateGEP(getBaseAddress(self), modByBufferBlocks(blockIndex));
    378241}
    379242
     
    420283
    421284Value * SwizzledCopybackBuffer::getStreamSetBlockPtr(Value * self, Value * blockIndex) const {
    422     return iBuilder->CreateGEP(self, modByBufferBlocks(blockIndex));
     285    return iBuilder->CreateGEP(getBaseAddress(self), modByBufferBlocks(blockIndex));
    423286}
    424287
     
    593456}
    594457
    595 ExternalFileBuffer::ExternalFileBuffer(IDISA::IDISA_Builder * b, Type * type, unsigned AddressSpace)
    596 : StreamSetBuffer(BufferKind::ExternalFileBuffer, b, type, resolveStreamSetType(b, type), 0, AddressSpace) {
     458SourceBuffer::SourceBuffer(IDISA::IDISA_Builder * b, Type * type, unsigned AddressSpace)
     459: StreamSetBuffer(BufferKind::SourceBuffer, b, type, StructType::get(resolveStreamSetType(b, type)->getPointerTo(), b->getSizeTy(), nullptr), 0, AddressSpace) {
     460    mUniqueID = "M"; // + std::to_string(bufferBlocks);
     461    if (AddressSpace > 0) mUniqueID += "@" + std::to_string(AddressSpace);
     462}
     463
     464ExternalBuffer::ExternalBuffer(IDISA::IDISA_Builder * b, Type * type, llvm::Value * addr, unsigned AddressSpace)
     465: StreamSetBuffer(BufferKind::ExternalBuffer, b, type, resolveStreamSetType(b, type), 0, AddressSpace) {
    597466    mUniqueID = "E";
    598467    if (AddressSpace > 0) mUniqueID += "@" + std::to_string(AddressSpace);
    599 }
    600 
    601 SourceFileBuffer::SourceFileBuffer(IDISA::IDISA_Builder * b, Type * type, unsigned AddressSpace)
    602 : StreamSetBuffer(BufferKind::SourceFileBuffer, b, type, StructType::get(resolveStreamSetType(b, type)->getPointerTo(), b->getSizeTy(), nullptr), 0, AddressSpace) {
    603 
    604 }
    605 
    606 ExtensibleBuffer::ExtensibleBuffer(IDISA::IDISA_Builder * b, Type * type, size_t bufferBlocks, unsigned AddressSpace)
    607 : StreamSetBuffer(BufferKind::ExtensibleBuffer, b, type, StructType::get(b->getSizeTy(), resolveStreamSetType(b, type)->getPointerTo(), b->getSizeTy(), nullptr), bufferBlocks, AddressSpace) {
    608     mUniqueID = "XT" + std::to_string(bufferBlocks);
    609     if (AddressSpace > 0) mUniqueID += "@" + std::to_string(AddressSpace);
     468    mStreamSetBufferPtr = b->CreatePointerBitCastOrAddrSpaceCast(addr, getPointerType());
    610469}
    611470
     
    614473    mUniqueID = "C" + std::to_string(bufferBlocks);
    615474    if (AddressSpace > 0) mUniqueID += "@" + std::to_string(AddressSpace);
    616 
    617475}
    618476
  • icGREP/icgrep-devel/icgrep/kernels/streamset.h

    r5411 r5429  
    2121    enum class BufferKind : unsigned {
    2222        BlockBuffer
    23         , ExternalFileBuffer
    24         , SourceFileBuffer
     23        , SourceBuffer
     24        , ExternalBuffer
    2525        , CircularBuffer
    2626        , CircularCopybackBuffer
    2727        , SwizzledCopybackBuffer
    2828        , ExpandableBuffer
    29         , ExtensibleBuffer
    3029    };
    3130
     
    143142};
    144143
    145 class ExternalFileBuffer final : public StreamSetBuffer {
    146 public:
    147     static inline bool classof(const StreamSetBuffer * b) {
    148         return b->getBufferKind() == BufferKind::ExternalFileBuffer;
    149     }
    150 
    151     ExternalFileBuffer(IDISA::IDISA_Builder * b, llvm::Type * type, unsigned AddressSpace = 0);
    152 
    153     void setStreamSetBuffer(llvm::Value * ptr);
     144class SourceBuffer final : public StreamSetBuffer {
     145public:
     146    static inline bool classof(const StreamSetBuffer * b) {
     147        return b->getBufferKind() == BufferKind::SourceBuffer;
     148    }
     149
     150    SourceBuffer(IDISA::IDISA_Builder * b, llvm::Type * type, unsigned AddressSpace = 0);
     151
     152    void setBaseAddress(llvm::Value * self, llvm::Value * addr) const override;
     153
     154    void setBufferedSize(llvm::Value * self, llvm::Value * size) const override;
     155
     156    llvm::Value * getBufferedSize(llvm::Value * self) const override;
     157
     158    llvm::Value * getLinearlyAccessibleItems(llvm::Value * self, llvm::Value * fromPosition) const override;
     159
     160protected:
     161
     162    llvm::Value * getBaseAddress(llvm::Value * self) const override;
     163
     164    llvm::Value * getStreamSetBlockPtr(llvm::Value * self, llvm::Value * blockNo) const override;
     165
     166};
     167
     168class ExternalBuffer final : public StreamSetBuffer {
     169public:
     170    static inline bool classof(const StreamSetBuffer * b) {
     171        return b->getBufferKind() == BufferKind::ExternalBuffer;
     172    }
     173
     174    ExternalBuffer(IDISA::IDISA_Builder * b, llvm::Type * type, llvm::Value * addr, unsigned AddressSpace = 0);
    154175
    155176    // Can't allocate - raise an error. */
     
    162183};
    163184
    164 class SourceFileBuffer final : public StreamSetBuffer {
    165 public:
    166     static inline bool classof(const StreamSetBuffer * b) {
    167         return b->getBufferKind() == BufferKind::SourceFileBuffer;
    168     }
    169 
    170     SourceFileBuffer(IDISA::IDISA_Builder * b, llvm::Type * type, unsigned AddressSpace = 0);
    171 
    172     void setBaseAddress(llvm::Value * self, llvm::Value * addr) const override;
    173 
    174     void setBufferedSize(llvm::Value * self, llvm::Value * size) const override;
    175 
    176     llvm::Value * getBufferedSize(llvm::Value * self) const override;
    177 
    178     llvm::Value * getLinearlyAccessibleItems(llvm::Value * self, llvm::Value * fromPosition) const override;
    179 
    180 protected:
    181 
    182     llvm::Value * getBaseAddress(llvm::Value * self) const override;
    183 
    184     llvm::Value * getStreamSetBlockPtr(llvm::Value * self, llvm::Value * blockNo) const override;
    185 };
    186 
    187 class ExtensibleBuffer final : public StreamSetBuffer {
    188 public:
    189     static inline bool classof(const StreamSetBuffer * b) {
    190         return b->getBufferKind() == BufferKind::ExtensibleBuffer;
    191     }
    192 
    193     ExtensibleBuffer(IDISA::IDISA_Builder * b, llvm::Type * type, size_t bufferBlocks, unsigned AddressSpace = 0);
    194 
    195     llvm::Value * getLinearlyAccessibleItems(llvm::Value * self,llvm::Value * fromPosition) const override;
    196 
    197     void allocateBuffer() override;
    198 
    199     void reserveBytes(llvm::Value * self, llvm::Value * required) const override;
    200 
    201     void setBufferedSize(llvm::Value * self, llvm::Value * size) const override;
    202 
    203     llvm::Value * getBufferedSize(llvm::Value * self) const override;
    204 
    205     void releaseBuffer(llvm::Value * self) const override;
    206 
    207 protected:
    208 
    209     llvm::Value * roundUpToPageSize(llvm::Value * const value) const;
    210 
    211     llvm::Value * getBaseAddress(llvm::Value * self) const override;
    212 
    213     llvm::Value * getStreamSetBlockPtr(llvm::Value * self, llvm::Value * blockNo) const override;
    214 
    215 };
    216    
    217185class CircularBuffer final : public StreamSetBuffer {
    218186public:
  • icGREP/icgrep-devel/icgrep/lz4d.cpp

    r5425 r5429  
    2424#include <kernels/streamset.h>
    2525#include <kernels/s2p_kernel.h>
    26 #include <kernels/stdin_kernel.h>
     26#include <kernels/source_kernel.h>
    2727#include <kernels/stdout_kernel.h>
    28 #include <kernels/mmap_kernel.h>
    2928#include <kernels/lz4_index_decoder.h>
    3029#include <kernels/lz4_bytestream_decoder.h>
     
    7372    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main, 0));
    7473
    75     StreamSetBuffer * const ByteStream = pxDriver.addBuffer(make_unique<SourceFileBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8)));
     74    StreamSetBuffer * const ByteStream = pxDriver.addBuffer(make_unique<SourceBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8)));
    7675    StreamSetBuffer * const BasisBits = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8, 1), segmentSize * bufferSegments));
    7776    StreamSetBuffer * const Extenders = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     
    8180
    8281   
    83     kernel::KernelBuilder * sourceK = pxDriver.addKernelInstance(make_unique<kernel::FileSourceKernel>(iBuilder, iBuilder->getInt8PtrTy(), segmentSize));
     82    kernel::KernelBuilder * sourceK = pxDriver.addKernelInstance(make_unique<kernel::MemorySourceKernel>(iBuilder, iBuilder->getInt8PtrTy(), segmentSize));
    8483    sourceK->setInitialArguments({inputStream, fileSize});
    8584    pxDriver.makeKernelCall(sourceK, {}, {ByteStream});
  • icGREP/icgrep-devel/icgrep/toolchain/toolchain.cpp

    r5425 r5429  
    217217}
    218218
    219 ExternalFileBuffer * ParabixDriver::addExternalBuffer(std::unique_ptr<ExternalFileBuffer> b, Value * externalBuf) {
    220     mOwnedBuffers.push_back(std::move(b));
    221     ExternalFileBuffer * rawBuf = cast<ExternalFileBuffer>(mOwnedBuffers.back().get());
    222     rawBuf->setStreamSetBuffer(externalBuf);
    223     return rawBuf;
     219ExternalBuffer * ParabixDriver::addExternalBuffer(std::unique_ptr<ExternalBuffer> b) {
     220    mOwnedBuffers.emplace_back(std::move(b));
     221    return cast<ExternalBuffer>(mOwnedBuffers.back().get());
    224222}
    225223
    226224StreamSetBuffer * ParabixDriver::addBuffer(std::unique_ptr<StreamSetBuffer> b) {
    227225    b->allocateBuffer();
    228     mOwnedBuffers.push_back(std::move(b));
     226    mOwnedBuffers.emplace_back(std::move(b));
    229227    return mOwnedBuffers.back().get();
    230228}
    231229
    232230kernel::KernelBuilder * ParabixDriver::addKernelInstance(std::unique_ptr<kernel::KernelBuilder> kb) {
    233     mOwnedKernels.push_back(std::move(kb));
     231    mOwnedKernels.emplace_back(std::move(kb));
    234232    return mOwnedKernels.back().get();
    235233}
     
    237235void ParabixDriver::addKernelCall(kernel::KernelBuilder & kb, const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs) {
    238236    assert ("addKernelCall or makeKernelCall was already run on this kernel." && (kb.getModule() == nullptr));
    239     mPipeline.push_back(&kb);
     237    mPipeline.emplace_back(&kb);
    240238    kb.createKernelStub(inputs, outputs);
    241239}
     
    243241void ParabixDriver::makeKernelCall(kernel::KernelBuilder * kb, const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs) {
    244242    assert ("addKernelCall or makeKernelCall was already run on this kernel." && (kb->getModule() == nullptr));
    245     mPipeline.push_back(kb);
     243    mPipeline.emplace_back(kb);
    246244    kb->createKernelStub(inputs, outputs);
    247245}
     
    263261        k->addKernelDeclarations(mMainModule);
    264262    }
     263
    265264    for (const auto & k : mPipeline) {
    266265        k->createInstance();
    267266    }
     267
    268268    for (const auto & k : mPipeline) {
    269269        k->initializeInstance();
    270270    }
     271
    271272    if (codegen::pipelineParallel) {
    272273        generateParallelPipeline(iBuilder, mPipeline);
     
    277278        generatePipelineLoop(iBuilder, mPipeline);
    278279    }
     280
    279281    for (const auto & k : mPipeline) {
    280282        k->finalizeInstance();
  • icGREP/icgrep-devel/icgrep/toolchain/toolchain.h

    r5425 r5429  
    6969    IDISA::IDISA_Builder * getIDISA_Builder() { return iBuilder.get(); }
    7070   
    71     parabix::ExternalFileBuffer * addExternalBuffer(std::unique_ptr<parabix::ExternalFileBuffer> b, llvm::Value * externalBuf);
     71    parabix::ExternalBuffer * addExternalBuffer(std::unique_ptr<parabix::ExternalBuffer> b);
    7272   
    7373    parabix::StreamSetBuffer * addBuffer(std::unique_ptr<parabix::StreamSetBuffer> b);
  • icGREP/icgrep-devel/icgrep/u8u16.cpp

    r5425 r5429  
    1010#include <kernels/deletion.h>                      // for DeletionKernel
    1111#include <kernels/swizzle.h>                      // for DeletionKernel
    12 #include <kernels/mmap_kernel.h>                   // for MMapSourceKernel
     12#include <kernels/source_kernel.h>
    1313#include <kernels/p2s_kernel.h>                    // for P2S16KernelWithCom...
    1414#include <kernels/s2p_kernel.h>                    // for S2PKernel
     
    285285   
    286286    // File data from mmap
    287     StreamSetBuffer * ByteStream = pxDriver.addBuffer(make_unique<SourceFileBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8)));
     287    StreamSetBuffer * ByteStream = pxDriver.addBuffer(make_unique<SourceBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8)));
    288288   
    289289    KernelBuilder * mmapK = pxDriver.addKernelInstance(make_unique<MMapSourceKernel>(iBuilder, segmentSize));
     
    341341    StreamSetBuffer * U16out = nullptr;
    342342    if (mMapBuffering || memAlignBuffering) {
    343         U16out = pxDriver.addExternalBuffer(make_unique<ExternalFileBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 16)), outputStream);
     343        U16out = pxDriver.addExternalBuffer(make_unique<ExternalBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 16), outputStream));
    344344    } else {
    345345        U16out = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 16), segmentSize * bufferSegments));
     
    378378
    379379    // File data from mmap
    380     StreamSetBuffer * ByteStream = pxDriver.addBuffer(make_unique<SourceFileBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8)));
     380    StreamSetBuffer * ByteStream = pxDriver.addBuffer(make_unique<SourceBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8)));
    381381   
    382382    KernelBuilder * mmapK = pxDriver.addKernelInstance(make_unique<MMapSourceKernel>(iBuilder, segmentSize));
     
    414414    StreamSetBuffer * U16out = nullptr;
    415415    if (mMapBuffering || memAlignBuffering) {
    416         U16out = pxDriver.addExternalBuffer(make_unique<ExternalFileBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 16)), outputStream);
     416        U16out = pxDriver.addExternalBuffer(make_unique<ExternalBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 16), outputStream));
    417417    } else {
    418418        U16out = pxDriver.addBuffer(make_unique<CircularCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 16), segmentSize * bufferSegments, 1 /*overflow block*/));
     
    427427    pxDriver.linkAndFinalize();
    428428}
    429 
    430 
    431429
    432430typedef void (*u8u16FunctionType)(uint32_t fd, char * output_data);
  • icGREP/icgrep-devel/icgrep/wc.cpp

    r5425 r5429  
    2020#include <IR_Gen/idisa_target.h>
    2121#include <kernels/streamset.h>
    22 #include <kernels/mmap_kernel.h>
     22#include <kernels/source_kernel.h>
    2323#include <kernels/s2p_kernel.h>
    2424#include <pablo/pablo_compiler.h>
     
    157157    iBuilder->SetInsertPoint(BasicBlock::Create(m->getContext(), "entry", main,0));
    158158
    159     StreamSetBuffer * const ByteStream = pxDriver.addBuffer(make_unique<SourceFileBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8)));
     159    StreamSetBuffer * const ByteStream = pxDriver.addBuffer(make_unique<SourceBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8)));
    160160
    161161    StreamSetBuffer * const BasisBits = pxDriver.addBuffer(make_unique<SingleBlockBuffer>(iBuilder, iBuilder->getStreamSetTy(8, 1)));
Note: See TracChangeset for help on using the changeset viewer.