Changeset 5245


Ignore:
Timestamp:
Jan 3, 2017, 3:59:33 PM (9 months ago)
Author:
nmedfort
Message:

Work on bracket matching problem

Location:
icGREP/icgrep-devel/icgrep
Files:
15 edited

Legend:

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

    r5241 r5245  
    160160
    161161SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3 -DNDEBUG")
    162 SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -g") #-fsanitize=address -fno-omit-frame-pointer
     162SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -g -fsanitize=address -fno-omit-frame-pointer")
    163163
    164164add_test(
  • icGREP/icgrep-devel/icgrep/IR_Gen/CBuilder.cpp

    r5243 r5245  
    7575    DataLayout DL(getModule());
    7676    Type * const intTy = getIntPtrTy(DL);
    77     Type * const voidPtrTy = getVoidPtrTy();
    78     Function * malloc = cast<Function>(getModule()->getOrInsertFunction("malloc", voidPtrTy, intTy, nullptr));
    79     malloc->setDoesNotAlias(0);
    80     const auto width = ConstantExpr::getSizeOf(type);
     77    Constant * const width = ConstantExpr::getSizeOf(type);
     78    if (size->getType() != intTy) {
     79        if (isa<Constant>(size)) {
     80            size = ConstantExpr::getIntegerCast(cast<Constant>(size), intTy, false);
     81        } else {
     82            size = CreateTruncOrBitCast(size, intTy);
     83        }
     84    }
    8185    if (!width->isOneValue()) {
    8286        if (isa<Constant>(size)) {
     
    8690        }
    8791    }
    88     size = CreateTruncOrBitCast(size, intTy);
    89     CallInst * ci = CreateCall(malloc, {size});
     92    Module * const m = getModule();
     93    Function * malloc = m->getFunction("malloc");
     94    if (malloc == nullptr) {
     95        Type * const voidPtrTy = getVoidPtrTy();
     96        malloc = cast<Function>(m->getOrInsertFunction("malloc", voidPtrTy, intTy, nullptr));
     97        malloc->setCallingConv(CallingConv::C);
     98        malloc->setDoesNotAlias(0);
     99    }
     100    CallInst * ci = CreateCall(malloc, size);
    90101    ci->setTailCall();
    91102    ci->setCallingConv(malloc->getCallingConv());
     
    98109    IntegerType * const intTy = getIntPtrTy(DL);
    99110    const auto byteWidth = (intTy->getBitWidth() / 8);
    100     const auto offset = ConstantInt::get(intTy, alignment + byteWidth - 1);
    101     const auto width = ConstantExpr::getSizeOf(type);
     111    Constant * const offset = ConstantInt::get(intTy, alignment + byteWidth - 1);
     112    Constant * const width = ConstantExpr::getSizeOf(type);
    102113    if (!width->isOneValue()) {
    103114        if (isa<Constant>(size)) {
     
    105116        } else {
    106117            size = CreateMul(size, width);
     118        }
     119    }
     120    if (size->getType() != intTy) {
     121        if (isa<Constant>(size)) {
     122            size = ConstantExpr::getIntegerCast(cast<Constant>(size), intTy, false);
     123        } else {
     124            size = CreateTruncOrBitCast(size, intTy);
    107125        }
    108126    }
     
    112130        size = CreateAdd(size, offset);
    113131    }
    114     size = CreateTruncOrBitCast(size, intTy);
    115     Value * unaligned = CreateMalloc(getInt8Ty(), size);
    116     Value * aligned = CreateBitOrPointerCast(unaligned, intTy);
    117     aligned = CreateAnd(CreateAdd(aligned, offset), ConstantExpr::getNot(ConstantInt::get(intTy, alignment - 1)));
    118     Value * ptr = CreateBitOrPointerCast(CreateSub(aligned, ConstantInt::get(intTy, byteWidth)), intTy->getPointerTo());
    119     CreateAlignedStore(CreateBitOrPointerCast(unaligned, intTy), ptr, byteWidth);
    120     return CreateBitOrPointerCast(aligned, type->getPointerTo());
    121 }
    122 
    123 void CBuilder::CreateFree(Value * ptr) {
     132    Value * unaligned = CreatePtrToInt(CreateMalloc(getInt8Ty(), size), intTy);
     133    Value * aligned = CreateAnd(CreateAdd(unaligned, offset), ConstantExpr::getNot(ConstantInt::get(intTy, alignment - 1)));
     134    Value * prefix = CreateIntToPtr(CreateSub(aligned, ConstantInt::get(intTy, byteWidth)), intTy->getPointerTo());
     135    assert (unaligned->getType() == prefix->getType()->getPointerElementType());
     136    CreateAlignedStore(unaligned, prefix, byteWidth);
     137    return CreateIntToPtr(aligned, type->getPointerTo());
     138}
     139
     140void CBuilder::CreateFree(Value * const ptr) {
     141    assert (ptr->getType()->isPointerTy());
     142    Module * const m = getModule();
    124143    PointerType * const voidPtrTy = getVoidPtrTy();
    125     Function * const free = cast<Function>(getModule()->getOrInsertFunction("free", getVoidTy(), voidPtrTy, nullptr));
    126     CallInst * const ci = CreateCall(free, {CreateBitOrPointerCast(ptr, voidPtrTy)});
     144    Function * free = m->getFunction("free");
     145    if (free == nullptr) {
     146        free = cast<Function>(getModule()->getOrInsertFunction("free", getVoidTy(), voidPtrTy, nullptr));
     147        free->setCallingConv(CallingConv::C);
     148    }
     149    CallInst * const ci = CreateCall(free, CreatePointerCast(ptr, voidPtrTy));
    127150    ci->setTailCall();
    128151    ci->setCallingConv(free->getCallingConv());
    129152}
    130153
    131 void CBuilder::CreateAlignedFree(Value * ptr) {
     154void CBuilder::CreateAlignedFree(Value * const ptr, const bool ptrMayBeNull) {
     155    // WARNING: this will cause a segfault if the value of the ptr at runtime is null but ptrMayBeNull was not set
     156    PointerType * type = cast<PointerType>(ptr->getType());
     157    BasicBlock * exit = nullptr;
     158    if (ptrMayBeNull) {
     159        LLVMContext & C = getContext();
     160        BasicBlock * bb = GetInsertBlock();
     161        Function * f = bb->getParent();
     162        exit = BasicBlock::Create(C, "", f, bb);
     163        BasicBlock * entry = BasicBlock::Create(C, "", f, exit);
     164        Value * cond = CreateICmpEQ(ptr, ConstantPointerNull::get(type));
     165        CreateCondBr(cond, exit, entry);
     166        SetInsertPoint(entry);
     167    }
    132168    DataLayout DL(getModule());
    133169    IntegerType * const intTy = getIntPtrTy(DL);
    134170    const auto byteWidth = (intTy->getBitWidth() / 8);
    135     ptr = CreateBitOrPointerCast(ptr, intTy);
    136     ptr = CreateSub(ptr, ConstantInt::get(intTy, byteWidth));
    137     ptr = CreateBitOrPointerCast(ptr, getInt8PtrTy());
    138     CreateFree(CreateAlignedLoad(ptr, byteWidth));
     171    Value * prefix = CreatePtrToInt(ptr, intTy);
     172    prefix = CreateSub(prefix, ConstantInt::get(intTy, byteWidth));
     173    prefix = CreateIntToPtr(prefix, intTy->getPointerTo());
     174    prefix = CreateIntToPtr(CreateAlignedLoad(prefix, byteWidth), type);
     175    CreateFree(prefix);
     176    if (ptrMayBeNull) {
     177        CreateBr(exit);
     178        SetInsertPoint(exit);
     179    }
    139180}
    140181
    141182Value * CBuilder::CreateRealloc(Value * ptr, Value * size) {
    142     assert (ptr->getType()->isPointerTy());
    143     DataLayout DL(getModule());
    144     IntegerType * const intTy = getIntPtrTy(DL);
    145     PointerType * const voidPtrTy = getVoidPtrTy();
    146     Function * realloc = cast<Function>(getModule()->getOrInsertFunction("realloc", voidPtrTy, voidPtrTy, intTy, nullptr));
    147     realloc->setDoesNotAlias(0);
    148     Type * const type = ptr->getType();
    149     // calculate our new size parameter
    150     size = CreateMul(size, ConstantExpr::getSizeOf(type->getPointerElementType()));
    151     size = CreateTruncOrBitCast(size, intTy);
    152     // call realloc with the pointer and adjusted size
     183    DataLayout DL(getModule());
     184    Type * const intTy = getIntPtrTy(DL);
     185    PointerType * type = cast<PointerType>(ptr->getType());
     186    Constant * const width = ConstantExpr::getSizeOf(type->getPointerElementType());
     187    if (size->getType() != intTy) {
     188        if (isa<Constant>(size)) {
     189            size = ConstantExpr::getIntegerCast(cast<Constant>(size), intTy, false);
     190        } else {
     191            size = CreateTruncOrBitCast(size, intTy);
     192        }
     193    }
     194    if (!width->isOneValue()) {
     195        if (isa<Constant>(size)) {
     196            size = ConstantExpr::getMul(cast<Constant>(size), width);
     197        } else {
     198            size = CreateMul(size, width);
     199        }
     200    }
     201    Module * const m = getModule();
     202    Function * realloc = m->getFunction("realloc");
     203    if (realloc == nullptr) {
     204        Type * const voidPtrTy = getVoidPtrTy();
     205        realloc = cast<Function>(m->getOrInsertFunction("realloc", voidPtrTy, voidPtrTy, intTy, nullptr));
     206        realloc->setCallingConv(CallingConv::C);
     207        realloc->setDoesNotAlias(1);
     208    }
    153209    CallInst * ci = CreateCall(realloc, {ptr, size});
    154210    ci->setTailCall();
     
    157213}
    158214
    159 Value * CBuilder::CreateAlignedRealloc(Value * ptr, Value * size, const unsigned alignment) {
    160     assert ((alignment & (alignment - 1)) == 0); // is power of 2
    161     assert (ptr->getType()->isPointerTy());
    162     DataLayout DL(getModule());
    163     IntegerType * const intTy = getIntPtrTy(DL);
    164     PointerType * const bpTy = getInt8PtrTy();
    165     Type * const type = ptr->getType();
    166     // calculate our new size parameter
    167     const auto byteWidth = (intTy->getBitWidth() / 8);
    168     const auto offset = ConstantInt::get(intTy, alignment + byteWidth - 1);
    169     const auto width = ConstantExpr::getSizeOf(type);
    170     if (!width->isOneValue()) {
    171         if (isa<Constant>(size)) {
    172             size = ConstantExpr::getMul(cast<Constant>(size), width);
    173         } else {
    174             size = CreateMul(size, width);
    175         }
    176     }
    177     if (isa<Constant>(size)) {
    178         size = ConstantExpr::getAdd(cast<Constant>(size), offset);
    179     } else {
    180         size = CreateAdd(size, offset);
    181     }
    182     size = CreateTruncOrBitCast(size, intTy);
    183     // calculate the offset containing the unaligned pointer address
    184     ptr = CreateBitOrPointerCast(ptr, bpTy);
    185     ptr = CreateSub(ptr, ConstantInt::get(intTy, byteWidth));
    186     ptr = CreateBitOrPointerCast(ptr, intTy->getPointerTo());
    187     // load the unaligned pointer as an uint8 *
    188     ptr = CreateAlignedLoad(ptr, byteWidth);
    189     ptr = CreateBitOrPointerCast(ptr, bpTy);
    190     // call realloc with the unaligned pointer and adjusted size
    191     Value * unaligned = CreateRealloc(ptr, size);
    192     Value * aligned = CreateBitOrPointerCast(unaligned, intTy);
    193     aligned = CreateAnd(CreateAdd(aligned, offset), ConstantExpr::getNot(ConstantInt::get(intTy, alignment - 1)));
    194     Value * prefix = CreateBitOrPointerCast(CreateSub(aligned, ConstantInt::get(intTy, byteWidth)), intTy->getPointerTo());
    195     CreateAlignedStore(CreateBitOrPointerCast(unaligned, intTy), prefix, byteWidth);
    196     return CreateBitOrPointerCast(aligned, type);
    197 }
    198 
    199215void CBuilder::CreateMemZero(Value * ptr, Value * size, const unsigned alignment) {
    200216    assert (ptr->getType()->isPointerTy() && size->getType()->isIntegerTy());
    201217    Type * const type = ptr->getType();
    202     const auto width = ConstantExpr::getSizeOf(type->getPointerElementType());
     218    Constant * const width = ConstantExpr::getSizeOf(type->getPointerElementType());
    203219    if (isa<Constant>(size)) {
    204220        size = ConstantExpr::getMul(cast<Constant>(size), width);
     
    206222        size = CreateMul(size, width);
    207223    }
    208     CreateMemSet(CreateBitOrPointerCast(ptr, getInt8PtrTy()), getInt8(0), size, alignment);
     224    CreateMemSet(CreatePointerCast(ptr, getInt8PtrTy()), getInt8(0), size, alignment);
    209225}
    210226
     
    233249//
    234250Value * CBuilder::CreatePThreadCreateCall(Value * thread, Value * attr, Function * start_routine, Value * arg) {
    235 
    236     Type * pthreadTy = getSizeTy();
    237     FunctionType * funVoidPtrVoidTy = FunctionType::get(getVoidTy(), getVoidPtrTy(), false);
    238    
    239     Function * pthreadCreateFunc = cast<Function>(mMod->getOrInsertFunction("pthread_create",
    240                                                                          getInt32Ty(),
    241                                                                          pthreadTy->getPointerTo(),
    242                                                                          getVoidPtrTy(),
    243                                                                          static_cast<Type *>(funVoidPtrVoidTy)->getPointerTo(),
    244                                                                          getVoidPtrTy(), nullptr));
    245     pthreadCreateFunc->setCallingConv(llvm::CallingConv::C);
     251    Function * pthreadCreateFunc = mMod->getFunction("pthread_create");
     252    if (pthreadCreateFunc == nullptr) {
     253        Type * pthreadTy = getSizeTy();
     254        FunctionType * funVoidPtrVoidTy = FunctionType::get(getVoidTy(), getVoidPtrTy(), false);
     255
     256        pthreadCreateFunc = cast<Function>(mMod->getOrInsertFunction("pthread_create",
     257                                                                     getInt32Ty(),
     258                                                                     pthreadTy->getPointerTo(),
     259                                                                     getVoidPtrTy(),
     260                                                                     static_cast<Type *>(funVoidPtrVoidTy)->getPointerTo(),
     261                                                                     getVoidPtrTy(), nullptr));
     262        pthreadCreateFunc->setCallingConv(llvm::CallingConv::C);
     263    }
    246264    return CreateCall(pthreadCreateFunc, {thread, attr, start_routine, arg});
    247265}
     
    251269
    252270Value * CBuilder::CreatePThreadExitCall(Value * value_ptr) {
    253     Function * pthreadExitFunc = cast<Function>(mMod->getOrInsertFunction("pthread_exit",
    254                                                                             getVoidTy(),
    255                                                                             getVoidPtrTy(), nullptr));
    256     pthreadExitFunc->addFnAttr(llvm::Attribute::NoReturn);
    257     pthreadExitFunc->setCallingConv(llvm::CallingConv::C);
    258     return CreateCall(pthreadExitFunc, {value_ptr});
     271    Function * pthreadExitFunc = mMod->getFunction("pthread_exit");
     272    if (pthreadExitFunc == nullptr) {
     273        pthreadExitFunc = cast<Function>(mMod->getOrInsertFunction("pthread_exit", getVoidTy(), getVoidPtrTy(), nullptr));
     274        pthreadExitFunc->addFnAttr(llvm::Attribute::NoReturn);
     275        pthreadExitFunc->setCallingConv(llvm::CallingConv::C);
     276    }
    259277    CallInst * exitThread = CreateCall(pthreadExitFunc, {value_ptr});
    260278    exitThread->setDoesNotReturn();
  • icGREP/icgrep-devel/icgrep/IR_Gen/CBuilder.h

    r5243 r5245  
    4242    Function * GetPrintf();
    4343    Value * CreateMalloc(Type * type, Value * size);
    44     Value * CreateAlignedMalloc(Type *type, Value * size, const unsigned alignment);
    45     void CreateFree(Value * ptr);
    46     void CreateAlignedFree(Value * ptr);
     44    Value * CreateAlignedMalloc(Type * type, Value * size, const unsigned alignment);
     45    void CreateFree(Value * const ptr);
     46    void CreateAlignedFree(Value * const ptr, const bool ptrMayBeNull = false);
    4747    Value * CreateRealloc(Value * ptr, Value * size);
    48     Value * CreateAlignedRealloc(Value * ptr, Value * size, const unsigned alignment);
    4948    void CreateMemZero(Value * ptr, Value * size, const unsigned alignment = 1);
    50    
     49
     50    inline llvm::AllocaInst * CreateCacheAlignedAlloca(llvm::Type * Ty, llvm::Value * ArraySize = nullptr) {
     51        llvm::AllocaInst * instr = CreateAlloca(Ty, ArraySize);
     52        instr->setAlignment(getCacheAlignment());
     53        return instr;
     54    }
    5155   
    5256    // Create calls to unistd.h functions.
     
    6569    Value * CreatePThreadJoinCall(Value * thread, Value * value_ptr);
    6670   
    67    
    68     void CallPrintRegister(const std::string & regName, Value * const value);
    6971    void CallPrintInt(const std::string & name, Value * const value);
    7072   
  • icGREP/icgrep-devel/icgrep/IR_Gen/idisa_builder.cpp

    r5240 r5245  
    3434}
    3535
    36    
     36
    3737void IDISA_Builder::CallPrintRegister(const std::string & name, Value * const value) {
    3838    Constant * printRegister = mMod->getFunction("PrintRegister");
    3939    if (LLVM_UNLIKELY(printRegister == nullptr)) {
    40         FunctionType *FT = FunctionType::get(getVoidTy(), { PointerType::get(getInt8Ty(), 0), mBitBlockType }, false);
     40        FunctionType *FT = FunctionType::get(getVoidTy(), { PointerType::get(getInt8Ty(), 0), getBitBlockType() }, false);
    4141        Function * function = Function::Create(FT, Function::InternalLinkage, "PrintRegister", mMod);
    4242        auto arg = function->arg_begin();
  • icGREP/icgrep-devel/icgrep/IR_Gen/idisa_builder.h

    r5240 r5245  
    6363    void CreateBlockAlignedStore(Value * const value, Value * const ptr, std::initializer_list<Value *> indices);
    6464
    65     void CallPrintRegister(const std::string & regName, Value * const value);
    66    
    6765    VectorType * fwVectorType(unsigned fw);
    6866
     
    135133    Type * getStreamTy(const unsigned FieldWidth = 1);
    136134
    137     inline llvm::AllocaInst * CreateCacheAlignedAlloca(llvm::Type * Ty, llvm::Value * ArraySize = nullptr) {
    138         llvm::AllocaInst * instr = CreateAlloca(Ty, ArraySize);
    139         instr->setAlignment(mCacheLineAlignment);
    140         return instr;
    141     }
     135    void CallPrintRegister(const std::string & regName, Value * const value);
    142136   
    143137protected:
  • icGREP/icgrep-devel/icgrep/array-test.cpp

    r5238 r5245  
    3333#include <pablo/pablo_compiler.h>
    3434#include <pablo/pablo_toolchain.h>
     35#include <kernels/s2p_kernel.h>
     36#include <kernels/stdout_kernel.h>
     37#include <boost/filesystem.hpp>
     38#include <boost/iostreams/device/mapped_file.hpp>
    3539
    36 #include <kernels/stdout_kernel.h>
    37 
    38 #include <llvm/Support/raw_os_ostream.h>
    39 
    40 #include <pthread.h>
     40static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<input file ...>"), cl::OneOrMore);
    4141
    4242using namespace pablo;
     
    4444using namespace parabix;
    4545
    46 //
    47 //
    48 
    4946void generate(PabloKernel * kernel, const unsigned size) {
    5047
    5148    PabloBuilder pb(kernel->getEntryBlock());
    5249
    53     Var * input = kernel->addInput("input", kernel->getStreamSetTy(size));
    54     Var * output = kernel->addOutput("output", kernel->getStreamSetTy(size));
    55     Var * index = pb.createVar("idx", pb.getInteger(0));
     50    Var * input = kernel->addInput("input", kernel->getStreamSetTy(8));
     51    Var * matches = kernel->addOutput("matches", kernel->getStreamSetTy(size));
     52
     53    PabloAST * basis[8];
     54    for (int i = 0; i < 8; ++i) {
     55        basis[i] = pb.createExtract(input, i);
     56    }
     57
     58    PabloAST * temp1 = pb.createOr(basis[0], basis[1]);
     59    PabloAST * temp2 = pb.createAnd(basis[2], pb.createNot(basis[3]));
     60    PabloAST * temp3 = pb.createAnd(temp2, pb.createNot(temp1));
     61    PabloAST * temp4 = pb.createAnd(basis[4], pb.createNot(basis[5]));
     62    PabloAST * temp5 = pb.createOr(basis[6], basis[7]);
     63    PabloAST * temp6 = pb.createAnd(temp4, pb.createNot(temp5));
     64    PabloAST * lparen = pb.createAnd(temp3, temp6, "lparens");
     65    PabloAST * temp7 = pb.createAnd(basis[7], pb.createNot(basis[6]));
     66    PabloAST * temp8 = pb.createAnd(temp4, temp7);
     67    PabloAST * rparen = pb.createAnd(temp3, temp8, "rparens");
     68    PabloAST * parens = pb.createOr(lparen, rparen);
     69
     70    PabloAST * pscan = pb.createScanTo(pb.createAdvance(lparen, 1), parens, "pscan");
     71
     72    PabloAST * closed = pb.createAnd(pscan, rparen, "closed");
     73
     74    pb.createAssign(pb.createExtract(matches, 0), closed);
     75
     76    Var * all_closed = pb.createVar("all_closed", closed);
     77    Var * pending_lparen = pb.createVar("pending_lparen", pb.createAnd(pscan, lparen));
     78    Var * unmatched_rparen = pb.createVar("unmatched_rparen", pb.createAnd(rparen, pb.createNot(closed)));
     79    Var * in_play = pb.createVar("in_play", pb.createOr(pending_lparen, unmatched_rparen));
     80
    5681
    5782    Integer * one = pb.getInteger(1);
    5883
     84    Var * index = pb.createVar("i", one);
     85
    5986    PabloBuilder body = PabloBuilder::Create(pb);
    6087
    61     pb.createWhile(pb.createLessThan(index, pb.getInteger(size)), body);
     88    pb.createWhile(pending_lparen, body);
    6289
    63         PabloAST * adv = body.createAdvance(body.createExtract(input, index), one, "adv");
    64         body.createAssign(body.createExtract(output, index), adv);
    65 
     90        pscan = body.createScanTo(body.createAdvance(pending_lparen, 1), in_play);
     91        closed = body.createAnd(pscan, rparen);
     92        body.createAssign(body.createExtract(matches, index), closed);
     93        body.createAssign(pending_lparen, body.createAnd(pscan, lparen));
     94        body.createAssign(all_closed, body.createOr(all_closed, closed));
     95        body.createAssign(unmatched_rparen, body.createAnd(rparen, body.createNot(all_closed)));
     96        body.createAssign(in_play, body.createOr(pending_lparen, unmatched_rparen));
    6697        body.createAssign(index, body.createAdd(index, one));
    6798
    6899
     100    pb.print(errs());
    69101
    70102}
    71103
    72 Function * pipeline(IDISA::IDISA_Builder * iBuilder, const unsigned count = 10) {
     104Function * pipeline(IDISA::IDISA_Builder * iBuilder, const unsigned count) {
    73105
    74     PabloKernel main(iBuilder, "at");
    75     generate(&main, count);
     106    Type * byteStreamTy = iBuilder->getStreamSetTy(1, 8);
    76107
    77     SingleBlockBuffer input(iBuilder, iBuilder->getStreamSetTy(count));
     108    ExternalFileBuffer ByteStream(iBuilder, byteStreamTy);
     109    SingleBlockBuffer BasisBits(iBuilder, iBuilder->getStreamSetTy(8, 1));
     110    SingleBlockBuffer matches(iBuilder, iBuilder->getStreamSetTy(count, 1));
    78111
    79     SingleBlockBuffer output(iBuilder, iBuilder->getStreamSetTy(count));
     112    S2PKernel  s2pk(iBuilder);
     113    s2pk.generateKernel({&ByteStream}, {&BasisBits});
    80114
    81     main.generateKernel({&input}, {&output});
     115    PabloKernel bm(iBuilder, "MatchParens");
     116    generate(&bm, count);
     117
     118    bm.generateKernel({&BasisBits}, {&matches});
    82119
    83120    Module * const mod = iBuilder->getModule();
    84121
    85     Function * const f = cast<Function>(mod->getOrInsertFunction("main", iBuilder->getVoidTy(), nullptr));
    86     f->setCallingConv(CallingConv::C);
     122    Function * const main = cast<Function>(mod->getOrInsertFunction("Main", iBuilder->getVoidTy(), byteStreamTy->getPointerTo(), iBuilder->getSizeTy(), nullptr));
     123    main->setCallingConv(CallingConv::C);
     124    Function::arg_iterator args = main->arg_begin();
    87125
    88     iBuilder->SetInsertPoint(BasicBlock::Create(mod->getContext(), "entry", f, 0));
    89     input.allocateBuffer();
    90     output.allocateBuffer();
    91     generatePipelineLoop(iBuilder, {&main});
     126    Value * const inputStream = &*(args++);
     127    inputStream->setName("input");
     128    Value * const fileSize = &*(args++);
     129    fileSize->setName("fileSize");
     130
     131    iBuilder->SetInsertPoint(BasicBlock::Create(mod->getContext(), "entry", main, 0));
     132
     133    ByteStream.setStreamSetBuffer(inputStream, fileSize);
     134    BasisBits.allocateBuffer();
     135    matches.allocateBuffer();
     136
     137    generatePipelineLoop(iBuilder, {&s2pk, &bm});
    92138    iBuilder->CreateRetVoid();
    93139
    94     return f;
     140    return main;
    95141}
    96142
    97 typedef void (*AtFunctionType)();
     143typedef void (*MatchParens)(char * byteStream, size_t fileSize);
    98144
    99 void * arrayTest(void *) {
     145MatchParens generateAlgorithm() {
    100146    LLVMContext ctx;
    101     Module * M = new Module("at", ctx);
     147    Module * M = new Module("mp", ctx);
    102148    IDISA::IDISA_Builder * idb = IDISA::GetIDISA_Builder(M);
    103149
    104     llvm::Function * main_IR = pipeline(idb);
     150    llvm::Function * f = pipeline(idb, 10);
    105151
    106152    verifyModule(*M, &dbgs());
     
    112158    delete idb;
    113159
    114     return wcEngine->getPointerToFunction(main_IR);
     160    return reinterpret_cast<MatchParens>(wcEngine->getPointerToFunction(f));
     161}
     162
     163void run(MatchParens f, const std::string & fileName) {
     164    const boost::filesystem::path file(fileName);
     165    if (exists(file)) {
     166        if (is_directory(file)) {
     167            return;
     168        }
     169        size_t fileSize = file_size(file);
     170        boost::iostreams::mapped_file_source mappedFile;
     171        if (fileSize > 0) {
     172            mappedFile.open(fileName);
     173            char * fileBuffer = const_cast<char *>(mappedFile.data());
     174            f(fileBuffer, fileSize);
     175            mappedFile.close();
     176        }
     177    } else {
     178        std::cerr << "Error: cannot open " << fileName << " for processing. Skipped.\n";
     179    }
    115180}
    116181
    117182int main(int argc, char *argv[]) {
    118 
    119183    cl::ParseCommandLineOptions(argc, argv);
    120 
    121     pthread_t t1, t2;
    122 
    123     pthread_create(&t1, nullptr, arrayTest, nullptr);
    124 
    125     pthread_create(&t2, nullptr, arrayTest, nullptr);
    126 
    127     void * r1, * r2;
    128 
    129     pthread_join(t1, &r1);
    130 
    131     pthread_join(t2, &r2);
    132 
     184    auto f = generateAlgorithm();
     185    for (const auto & inputFile : inputFiles) {
     186        run(f, inputFile);
     187    }
    133188    return 0;
    134189}
  • icGREP/icgrep-devel/icgrep/grep_engine.cpp

    r5242 r5245  
    239239    mGrepType = grepType;
    240240
    241     Type * const int32ty = iBuilder->getInt32Ty();
    242241    Type * const size_ty = iBuilder->getSizeTy();
    243242    Type * const int8PtrTy = iBuilder->getInt8PtrTy();
    244     Type * const voidTy = iBuilder->getVoidTy();
    245     Type * const voidPtrTy = iBuilder->getVoidPtrTy();
    246243    Type * const inputType = PointerType::get(ArrayType::get(ArrayType::get(iBuilder->getBitBlockType(), (UTF_16 ? 16 : 8)), 1), addrSpace);
    247244    Type * const resultTy = CountOnly ? size_ty : iBuilder->getVoidTy();
     
    414411
    415412const std::vector<std::string> & GrepEngine::grepPropertyValues(const std::string& propertyName) {
    416     AlignedAllocator<char, 32> alloc;
     413    enum { MaxSupportedVectorWidthInBytes = 32 };
     414    AlignedAllocator<char, MaxSupportedVectorWidthInBytes> alloc;
    417415    parsedPropertyValues.clear();
    418416    const std::string & str = UCD::getPropertyValueGrepString(propertyName);
    419417    const auto n = str.length();
    420     char * aligned = alloc.allocate(n + 32, 0);
     418    // NOTE: MaxSupportedVectorWidthInBytes of trailing 0s are needed to prevent the grep function from
     419    // erroneously matching garbage data when loading the final partial block.
     420    char * aligned = alloc.allocate(n + MaxSupportedVectorWidthInBytes, 0);
    421421    std::memcpy(aligned, str.data(), n);
    422     std::memset(aligned + n, 0, 32);
     422    std::memset(aligned + n, 0, MaxSupportedVectorWidthInBytes);
    423423    mGrepFunction(aligned, n, 0);
    424424    alloc.deallocate(aligned, 0);
     
    484484            resultStrs[idx].write(&buffer[line_start * index], (line_end - line_start) * index);
    485485            resultStrs[idx] << '\n';
    486         }
    487         else {   
     486        } else {
    488487            if ((!isUTF_16 && end_byte == 0x0D) || (isUTF_16 && (end_byte == 0x0D && penult_byte == 0x0))) {
    489488                // Check for line_end on first byte of CRLF;  note that we don't
     
    546545extern "C" {
    547546    void insert_property_values(size_t lineNum, size_t line_start, size_t line_end, const char * buffer) {
    548 //      When the error occurs, this is somehow getting an extra match:
    549 //        33: (261,269)
    550 //        138: (1235,1253)
    551 //        172: (1419,1423)
    552 //        278: (1949,2040) *****
    553 //        script : .*hir.*        (Alt[Name "hiragana" ,Name "katakanaorhiragana" ,Name "hira" ,Name "hiraganQ0ᅵᅵᅵᅵK" ])
    554547        parsedPropertyValues.emplace_back(buffer + line_start, buffer + line_end);
    555548    }
  • icGREP/icgrep-devel/icgrep/kernels/cc_kernel.h

    r5217 r5245  
    2222public:
    2323   
    24     DirectCharacterClassKernelBuilder(IDISA::IDISA_Builder * iBuilder, std::string ccSetName, std::vector<re::CC *> charClasses, unsigned codeUnitSize) :
    25     KernelBuilder(iBuilder, "cc",
     24    DirectCharacterClassKernelBuilder(IDISA::IDISA_Builder * iBuilder, std::string ccSetName, std::vector<re::CC *> charClasses, unsigned codeUnitSize)
     25    : KernelBuilder(iBuilder, "cc",
    2626                  {Binding{iBuilder->getStreamSetTy(1, 8 * codeUnitSize), "codeUnitStream"}},
    2727                  {Binding{iBuilder->getStreamSetTy(charClasses.size(), 1), "ccStream"}},
    28                   {}, {}, {}), mCharClasses(charClasses), mCodeUnitSize(codeUnitSize) {}
     28                  {}, {}, {})
     29    , mCharClasses(charClasses)
     30    , mCodeUnitSize(codeUnitSize) {
     31    }
    2932   
    3033   
  • icGREP/icgrep-devel/icgrep/kernels/interface.h

    r5238 r5245  
    9696    mInternalScalars(internal_scalars),
    9797    mKernelStateType(nullptr),
     98    mKernelInstance(nullptr),
    9899    mLookAheadPositions(0) {}
    99100   
  • icGREP/icgrep-devel/icgrep/pablo/builder.hpp

    r5231 r5245  
    153153    PabloAST * createScanThru(PabloAST * from, PabloAST * thru, const std::string & prefix);
    154154
     155    PabloAST * createScanTo(PabloAST * from, PabloAST * to) {
     156        return createScanThru(from, createNot(to));
     157    }
     158
     159    PabloAST * createScanTo(PabloAST * from, PabloAST * to, const std::string & prefix) {
     160        return createScanThru(from, createNot(to), prefix);
     161    }
     162
    155163    PabloAST * createSel(PabloAST * condition, PabloAST * trueExpr, PabloAST * falseExpr);
    156164
     
    255263    inline Integer * getInteger(const uint64_t value) const {
    256264        return mPb->getInteger(value);
     265    }
     266
     267    inline void print(raw_ostream & O, const bool expandNested = true) const {
     268        mPb->print(O, expandNested);
    257269    }
    258270
  • icGREP/icgrep-devel/icgrep/pablo/carry_manager.cpp

    r5233 r5245  
    107107    if (LLVM_UNLIKELY(mCarryInfo->variableLength)) {
    108108        // Check whether we need to resize the carry state
    109         PHINode * index = iBuilder->CreatePHI(iBuilder->getInt32Ty(), 2);
     109        PHINode * index = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
    110110        mLoopIndicies.push_back(index);
    111         index->addIncoming(iBuilder->getInt32(0), entryBlock);
     111        index->addIncoming(iBuilder->getSize(0), entryBlock);
    112112        Value * capacityPtr = iBuilder->CreateGEP(mCurrentFrame, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
    113         Value * capacity = iBuilder->CreateLoad(capacityPtr, false, "carryCapacity");
     113        Value * capacity = iBuilder->CreateLoad(capacityPtr, false, "capacity");
    114114        Value * arrayPtr = iBuilder->CreateGEP(mCurrentFrame, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
    115 
    116         BasicBlock * resizeBlock = BasicBlock::Create(iBuilder->getContext(), "", mFunction);
    117         BasicBlock * codeBlock = BasicBlock::Create(iBuilder->getContext(), "", mFunction);
     115        Value * array = iBuilder->CreateLoad(arrayPtr, false, "array");
     116
     117        LLVMContext & C = iBuilder->getContext();
     118
     119        BasicBlock * resizeBlock = BasicBlock::Create(C, "", mFunction);
     120        BasicBlock * cleanUpBlock = BasicBlock::Create(C, "", mFunction);
     121        BasicBlock * zeroBlock = BasicBlock::Create(C, "", mFunction);
     122        BasicBlock * codeBlock = BasicBlock::Create(C, "", mFunction);
    118123
    119124        Value * cond = iBuilder->CreateICmpULT(index, capacity);
     
    121126        iBuilder->SetInsertPoint(resizeBlock);
    122127
    123         Type * const carryStateType = arrayPtr->getType()->getPointerElementType()->getPointerElementType();
    124         Value * newCapacity = iBuilder->CreateMul(iBuilder->CreateAdd(index, iBuilder->getInt32(1)), iBuilder->getInt32(2));
    125         Value * newArrayPtr = iBuilder->CreateAlignedMalloc(carryStateType, newCapacity, iBuilder->getCacheAlignment());
    126         iBuilder->CreateMemCpy(newArrayPtr, arrayPtr, capacity, iBuilder->getCacheAlignment());
    127         iBuilder->CreateMemZero(iBuilder->CreateGEP(newArrayPtr, capacity), iBuilder->CreateSub(newCapacity, capacity), iBuilder->getCacheAlignment());
    128         iBuilder->CreateAlignedFree(arrayPtr);
     128        Type * const carryStateType = array->getType()->getPointerElementType();
     129        Value * newCapacity = iBuilder->CreateMul(iBuilder->CreateAdd(index, iBuilder->getSize(1)), iBuilder->getSize(2));
     130        Value * newArray = iBuilder->CreateAlignedMalloc(carryStateType, newCapacity, iBuilder->getCacheAlignment());
     131
     132        assert (newCapacity->getType() == capacity->getType());
     133        assert (newArray->getType() == array->getType());
     134
     135        Value * isNullCarryState = iBuilder->CreateICmpEQ(array, ConstantPointerNull::get(cast<PointerType>(array->getType())));
     136
     137        iBuilder->CreateCondBr(isNullCarryState, zeroBlock, cleanUpBlock);
     138        iBuilder->SetInsertPoint(cleanUpBlock);
     139
     140        iBuilder->CreateMemCpy(newArray, array, capacity, iBuilder->getCacheAlignment());
     141        iBuilder->CreateAlignedFree(array);
     142        iBuilder->CreateBr(zeroBlock);
     143
     144        iBuilder->SetInsertPoint(zeroBlock);
     145
     146        iBuilder->CreateMemZero(iBuilder->CreateGEP(newArray, capacity), iBuilder->CreateSub(newCapacity, capacity), iBuilder->getCacheAlignment());
    129147        iBuilder->CreateStore(newCapacity, capacityPtr);
    130         iBuilder->CreateStore(newArrayPtr, arrayPtr);
     148        iBuilder->CreateStore(newArray, arrayPtr);
     149
    131150        iBuilder->CreateBr(codeBlock);
    132151
     
    151170        assert (mLoopIndicies.size() > 0);
    152171        PHINode * index = mLoopIndicies.back();
    153         index->addIncoming(iBuilder->CreateAdd(index, iBuilder->getInt32(1)), exitBlock);
     172        index->addIncoming(iBuilder->CreateAdd(index, iBuilder->getSize(1)), exitBlock);
    154173        mLoopIndicies.pop_back();
    155174    }
  • icGREP/icgrep-devel/icgrep/pablo/codegenstate.cpp

    r5230 r5245  
    66
    77#include <pablo/codegenstate.h>
     8#include <pablo/printer_pablos.h>
    89
    910#define CHECK_SAME_TYPE(A, B) \
     
    313314}
    314315
    315 }
     316/** ------------------------------------------------------------------------------------------------------------- *
     317 * @brief print
     318 ** ------------------------------------------------------------------------------------------------------------- */
     319void PabloBlock::print(raw_ostream & O, const bool expandNested) const {
     320    PabloPrinter::print(this, O, expandNested);
     321}
     322
     323}
  • icGREP/icgrep-devel/icgrep/pablo/codegenstate.h

    r5231 r5245  
    175175
    176176    And * createAnd(PabloAST * expr1, PabloAST * expr2, const std::string & prefix) {
    177         return createAnd(expr1, expr2, nullptr);
     177        return createAnd(expr1, expr2, makeName(prefix));
    178178    }
    179179
     
    305305        return mParent->getInteger(value);
    306306    }
     307
     308    void print(raw_ostream & O, const bool expandNested = true) const;
    307309
    308310    virtual ~PabloBlock() {}
  • icGREP/icgrep-devel/icgrep/pablo/pablo_compiler.cpp

    r5238 r5245  
    484484        if (DebugOptionIsSet(DumpTrace)) {
    485485            assert (expr->getName());
    486             iBuilder->CallPrintRegister(expr->getName()->to_string(), value);
     486            if (value->getType()->isVectorTy()) {
     487                iBuilder->CallPrintRegister(expr->getName()->to_string(), value);
     488            } else if (value->getType()->isIntegerTy()) {
     489                iBuilder->CallPrintInt(expr->getName()->to_string(), value);
     490            }
    487491        }
    488492    }
  • icGREP/icgrep-devel/icgrep/pablo/pablo_kernel.cpp

    r5240 r5245  
    126126}
    127127
    128 void PabloKernel::initializeKernelState(Value * self) {
    129     iBuilder->CreateStore(Constant::getNullValue(mKernelStateType), self);
     128void PabloKernel::initializeKernelState(Value * self) {   
     129    iBuilder->CreateStore(ConstantAggregateZero::get(mKernelStateType), self);
    130130}
    131131
     
    134134, mPabloCompiler(new PabloCompiler(this))
    135135, mSymbolTable(new SymbolGenerator(mAllocator))
    136 , mEntryBlock(PabloBlock::Create(this))
    137 {
     136, mEntryBlock(PabloBlock::Create(this)) {
    138137
    139138}
Note: See TracChangeset for help on using the changeset viewer.