Ignore:
Timestamp:
Feb 9, 2016, 4:06:24 PM (4 years ago)
Author:
lindanl
Message:

Update kernel builder.

Location:
icGREP/icgrep-devel/icgrep/kernels
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/kernels/kernel.cpp

    r4924 r4926  
    4343void KernelBuilder::addKernelInputStream(int fw, std::string name = ""){
    4444    if (name=="")
     45        mInputStreamNames.push_back(mKernelName + "_inputstream_" + std::to_string(mInputStreams.size()));
     46    else
    4547        mInputStreamNames.push_back(name);
    46     else
    47         mInputStreamNames.push_back(mKernelName + "_inputstream_" + std::to_string(mInputStreams.size()));
    4848
    4949    if (fw == 1){
     
    5656void KernelBuilder::addKernelInputScalar(Type * t, std::string name = ""){
    5757    if (name=="")
     58        mInputScalarNames.push_back(mKernelName + "_inputscalar_" + std::to_string(mInputScalars.size()));
     59    else
    5860        mInputScalarNames.push_back(name);
    59     else
    60         mInputScalarNames.push_back(mKernelName + "_inputscalar_" + std::to_string(mInputScalars.size()));
    6161
    6262    mInputScalars.push_back(t);
    6363}
    6464
    65 
    66 //create doBlock method with empty body and load inputs
    67 std::vector<std::vector<Value *>> KernelBuilder::openDoBlock(){
     65Function* KernelBuilder::CreateDoBlockFunction(){
    6866    Type * inputStreamType = PointerType::get(ArrayType::get(StructType::get(mMod->getContext(), mInputStreams), mSegmentBlocks), 0);
    6967    Type * inputScalarType = PointerType::get(StructType::get(mMod->getContext(), mInputScalars), 0);
     
    7169    Type * outputAccumType = StructType::get(mMod->getContext(), mOutputAccums);
    7270    Type * stateType = StructType::create(mMod->getContext(), mStates, mKernelName);
    73     mKernelStructType = StructType::create(mMod->getContext(),std::vector<Type *>({stateType, outputStreamType, outputAccumType}), "KernelStruct");
     71    mKernelStructType = StructType::create(mMod->getContext(),std::vector<Type *>({stateType, outputStreamType, outputAccumType}), "KernelStruct_"+mKernelName);
    7472 
    7573   
    7674    FunctionType * functionType = FunctionType::get(Type::getVoidTy(mMod->getContext()),
    7775        std::vector<Type *>({PointerType::get(mKernelStructType, 0), inputStreamType, inputScalarType}), false);
    78    
    79    
     76       
    8077    mDoBlockFunction = Function::Create(functionType, GlobalValue::ExternalLinkage, mKernelName + "_DoBlock", mMod);
    8178    mDoBlockFunction->setCallingConv(CallingConv::C);
     79
     80    return mDoBlockFunction;
     81}
     82
     83//create doBlock method with empty body and load inputs
     84struct Inputs KernelBuilder::openDoBlock(){
     85   
    8286
    8387    Function::arg_iterator args = mDoBlockFunction->arg_begin();
     
    9195    iBuilder->SetInsertPoint(BasicBlock::Create(mMod->getContext(), "entry", mDoBlockFunction,0));
    9296
    93     std::vector<std::vector<Value *>> inputs;
     97    // std::vector<std::vector<valptr>> inputs;
     98    struct Inputs inputs;
    9499    for(int j = 0; j<mSegmentBlocks; j++){
    95100        for(int i = 0; i<mInputStreams.size(); i++){
     
    99104            if (t != mBitBlockType) {
    100105                int arraySize = t->getArrayNumElements();
    101                 inputs.resize(mSegmentBlocks, std::vector<Value *>(arraySize));
     106                inputs.streams.resize(mSegmentBlocks, std::vector<valptr>(arraySize));
    102107                for (int k=0; k<arraySize; k++){
    103108                    Value * gep_array_elem = iBuilder->CreateGEP(gep, {iBuilder->getInt32(0), iBuilder->getInt32(k)});
    104                     inputs[j][k] = iBuilder->CreateAlignedLoad(gep_array_elem, mBlockSize/8, false, mInputStreamNames.at(i));
     109                    inputs.streams[j][k] = iBuilder->CreateAlignedLoad(gep_array_elem, mBlockSize/8, false, mInputStreamNames.at(i));
    105110                }
    106111            }
    107112            else{
    108                 inputs.resize(mSegmentBlocks, std::vector<Value *>(mInputStreams.size()));
    109                 inputs[j][i] = iBuilder->CreateAlignedLoad(gep, mBlockSize/8, false, mInputStreamNames.at(i));
     113                inputs.streams.resize(mSegmentBlocks, std::vector<valptr>(mInputStreams.size()));
     114                inputs.streams[j][i] = iBuilder->CreateAlignedLoad(gep, mBlockSize/8, false, mInputStreamNames.at(i));
    110115            }
    111116           
     
    113118    }
    114119
    115     // for(int i = 0; i<mInputScalars.size(); i++){
    116     //     Value* indices[] = {iBuilder->getInt64(0), iBuilder->getInt32(i)};
    117     //     Value * gep = iBuilder->CreateGEP(input_scalar_param, indices);
    118     // }
     120    inputs.scalars.resize(mInputScalars.size());
     121    for(int i = 0; i<mInputScalars.size(); i++){
     122        Value* indices[] = {iBuilder->getInt64(0), iBuilder->getInt32(i)};
     123        Value * gep = iBuilder->CreateGEP(input_scalar_param, indices);
     124        inputs.scalars[i] = iBuilder->CreateAlignedLoad(gep, mBlockSize/8, false, mInputScalarNames.at(i));
     125    }
    119126
    120127    return inputs;
    121128}
    122 // void KernelBuilder::closeDoBlock(std::vector<std::vector<Value *>> result){
    123 
    124 void KernelBuilder::closeDoBlock(Value * result[][8]){
     129
     130void KernelBuilder::closeDoBlock(struct Outputs result){
     131   
     132
    125133    for(int j=0; j<mSegmentBlocks; j++){
    126         for(int i = 0; i<mOutputStreams.size(); i++){      
     134        for(int i = 0; i<mOutputStreams.size(); i++){   
    127135            Value* indices[] = {iBuilder->getInt64(0), iBuilder->getInt32(1), iBuilder->getInt32(j), iBuilder->getInt32(i)};
    128136            Value* gep = iBuilder->CreateGEP(mKernelStructParam, indices);
    129             iBuilder->CreateAlignedStore(result[j][i], gep, mBlockSize/8, false);
     137            iBuilder->CreateAlignedStore(result.streams[j][i], gep, mBlockSize/8, false);
    130138        }
    131139    }
    132140
    133     // for(int j=0; j<mSegmentBlocks; j++){
    134     //     for(int i = 0; i<mOutputAccums.size(); i++){
    135     //         Value* indices[] = {iBuilder->getInt64(0), iBuilder->getInt32(2), iBuilder->getInt32(j), iBuilder->getInt32(i)};
    136     //         Value* gep = iBuilder->CreateGEP(mKernelStructType, indices);
    137     //         iBuilder->CreateAlignedStore(result[j][i], gep, mBlockSize/8, false);
    138     //     }
    139     // }
     141    for(int i = 0; i<mOutputAccums.size(); i++){   
     142        Value* indices[] = {iBuilder->getInt64(0), iBuilder->getInt32(2), iBuilder->getInt32(i)};
     143        Value* gep = iBuilder->CreateGEP(mKernelStructParam, indices);
     144        iBuilder->CreateAlignedStore(result.accums[i], gep, mBlockSize/8, false);
     145    }
     146
    140147    iBuilder->CreateRetVoid();
     148}
     149
     150void KernelBuilder::changeKernelInternalState(int idx, Value * stateValue){
     151    Value* indices[] = {iBuilder->getInt64(0), iBuilder->getInt32(0), iBuilder->getInt32(idx)};
     152    Value* gep = iBuilder->CreateGEP(mKernelStructParam, indices);
     153    iBuilder->CreateAlignedStore(stateValue, gep, mBlockSize/8, false);
     154}
     155
     156Value * KernelBuilder::getKernelInternalState(int idx){
     157    Value* indices[] = {iBuilder->getInt64(0), iBuilder->getInt32(0), iBuilder->getInt32(idx)};
     158    Value* gep = iBuilder->CreateGEP(mKernelStructParam, indices);
     159    return iBuilder->CreateAlignedLoad(gep, mBlockSize/8, false, "state"+std::to_string(idx));
     160}
     161
     162Value * KernelBuilder::getKernelInternalStatePtr(int idx){
     163    Value* indices[] = {iBuilder->getInt64(0), iBuilder->getInt32(0), iBuilder->getInt32(idx)};
     164    Value* gep = iBuilder->CreateGEP(mKernelStructParam, indices);
     165    return gep;
    141166}
    142167
     
    159184    iBuilder->CreateStore(iBuilder->getInt64(0), gep);  //AvailableBlocks
    160185
    161     // while(i < mStates.size()){
    162     //     gep = iBuilder->CreateGEP(this_param, std::vector<Value *>({ iBuilder->getInt32(0), iBuilder->getInt32(i) }));
    163     //     iBuilder->CreateMemSet(gep, iBuilder->getInt8(0), DataLayout::getTypeAllocSize(mStates[i]), 4);
    164     // }
     186    while(i < mStates.size()){
     187        gep = iBuilder->CreateGEP(this_param, std::vector<Value *>({ iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(i++) }));
     188        // iBuilder->CreateStore(iBuilder->getInt64(0), gep);
     189        // std::cerr << "size = " << mMod->getDataLayout()->getTypeAllocSize(mStates[i]) << std::endl;
     190        // iBuilder->CreateMemSet(gep, iBuilder->getInt8(0), mMod->getDataLayout()->getTypeAllocSize(mStates[i]), 4);
     191    }
     192
    165193    iBuilder->CreateRetVoid();
    166194
  • icGREP/icgrep-devel/icgrep/kernels/kernel.h

    r4924 r4926  
    2525
    2626using namespace llvm;
    27        
     27
     28typedef Value* valptr;
     29
     30struct Inputs {
     31    std::vector<std::vector<valptr>> streams;
     32    std::vector<valptr> scalars;
     33};
     34
     35struct Outputs {
     36    std::vector<valptr *> streams;
     37    std::vector<valptr> accums;
     38};
     39
    2840class KernelBuilder{
    2941public:
     
    3749        void addKernelInputStream(int fw, std::string name);
    3850        void addKernelInputScalar(Type * t, std::string name);
    39         std::vector<std::vector<Value *>> openDoBlock();
    40         void closeDoBlock(Value * result[][8]);
     51    Function* CreateDoBlockFunction();
     52        struct Inputs openDoBlock();
     53        void closeDoBlock(struct Outputs);
    4154        void finalizeMethods();
    4255        void generateKernelInstance(int buffersize);
     
    4558    int getSegmentBlocks();
    4659
     60    void changeKernelInternalState(int idx, Value * stateValue);
     61    Value * getKernelInternalState(int idx);
     62    Value * getKernelInternalStatePtr(int idx);
     63
     64private:
    4765        Module *                            mMod;
    4866    IDISA::IDISA_Builder *              iBuilder;
Note: See TracChangeset for help on using the changeset viewer.