Changeset 5239 for icGREP


Ignore:
Timestamp:
Dec 26, 2016, 10:17:09 PM (3 years ago)
Author:
cameron
Message:

CBuilder initial check-in

Location:
icGREP/icgrep-devel/icgrep
Files:
2 added
3 edited

Legend:

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

    r5238 r5239  
    4949
    5050SET(KERNEL_SRC kernels/kernel.cpp kernels/s2p_kernel.cpp kernels/streamset.cpp kernels/interface.cpp)
    51 SET(IDISA_SRC IR_Gen/types/streamtype.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)
     51SET(IDISA_SRC IR_Gen/CBuilder.cpp IR_Gen/types/streamtype.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)
    5252
    5353SET(PABLO_SRC pablo/pabloAST.cpp pablo/branch.cpp pablo/codegenstate.cpp pablo/builder.cpp pablo/symbol_generator.cpp pablo/printer_pablos.cpp pablo/pablo_toolchain.cpp)
  • icGREP/icgrep-devel/icgrep/IR_Gen/idisa_builder.cpp

    r5231 r5239  
    3535
    3636   
    37 static Function * create_printf(Module * const mod, IDISA_Builder * builder) {
    38     Function * printf = mod->getFunction("printf");
    39     if (printf == nullptr) {
    40         printf = cast<Function>(mod->getOrInsertFunction("printf"
    41                                 , FunctionType::get(builder->getInt32Ty(), {builder->getInt8PtrTy()}, true)
    42                                 , AttributeSet().addAttribute(mod->getContext(), 1U, Attribute::NoAlias)));
    43 
    44     }
    45     return printf;
    46 }
    47 
    4837void IDISA_Builder::CallPrintRegister(const std::string & name, Value * const value) {
    4938    Constant * printRegister = mMod->getFunction("PrintRegister");
     
    7362            args.push_back(builder.CreateExtractElement(value, builder.getInt32(i - 1)));
    7463        }
    75         builder.CreateCall(create_printf(mMod, this), args);
     64        builder.CreateCall(GetPrintf(), args);
    7665        builder.CreateRetVoid();
    7766
     
    8372}
    8473
    85 void IDISA_Builder::CallPrintInt(const std::string & name, Value * const value) {
    86     Constant * printRegister = mMod->getFunction("PrintInt");
    87     if (LLVM_UNLIKELY(printRegister == nullptr)) {
    88         FunctionType *FT = FunctionType::get(getVoidTy(), { PointerType::get(getInt8Ty(), 0), getSizeTy() }, false);
    89         Function * function = Function::Create(FT, Function::InternalLinkage, "PrintInt", mMod);
    90         auto arg = function->arg_begin();
    91         std::string out = "%-40s = %" PRIx64 "\n";
    92         BasicBlock * entry = BasicBlock::Create(mMod->getContext(), "entry", function);
    93         IRBuilder<> builder(entry);
    94         std::vector<Value *> args;
    95         args.push_back(CreateGlobalStringPtr(out.c_str()));
    96         Value * const name = &*(arg++);
    97         name->setName("name");
    98         args.push_back(name);
    99         Value * value = &*arg;
    100         value->setName("value");
    101         args.push_back(value);
    102         builder.CreateCall(create_printf(mMod, this), args);
    103         builder.CreateRetVoid();
    104 
    105         printRegister = function;
    106     }
    107     Value * num = nullptr;
    108     if (value->getType()->isPointerTy()) {
    109         num = CreatePtrToInt(value, getSizeTy());
    110     } else {
    111         num = CreateZExtOrBitCast(value, getSizeTy());
    112     }
    113     assert (num->getType()->isIntegerTy());
    114     CreateCall(printRegister, {CreateGlobalStringPtr(name.c_str()), num});
    115 }
    116 
    117 Value * IDISA_Builder::CreateMalloc(Type * type, Value * size) {
    118     DataLayout DL(getModule());
    119     Type * const intTy = getIntPtrTy(DL);
    120     Type * const voidPtrTy = getVoidPtrTy();
    121     Function * malloc = cast<Function>(getModule()->getOrInsertFunction("malloc", voidPtrTy, intTy, nullptr));
    122     malloc->setDoesNotAlias(0);
    123     const auto width = ConstantExpr::getSizeOf(type);
    124     if (!width->isOneValue()) {
    125         if (isa<Constant>(size)) {
    126             size = ConstantExpr::getMul(cast<Constant>(size), width);
    127         } else {
    128             size = CreateMul(size, width);
    129         }
    130     }
    131     size = CreateTruncOrBitCast(size, intTy);
    132     CallInst * ci = CreateCall(malloc, {size});
    133     ci->setTailCall();
    134     ci->setCallingConv(malloc->getCallingConv());
    135     return CreateBitOrPointerCast(ci, type->getPointerTo());
    136 }
    137 
    138 Value * IDISA_Builder::CreateAlignedMalloc(Type * type, Value * size, const unsigned alignment) {
    139     assert ((alignment & (alignment - 1)) == 0); // is power of 2
    140     DataLayout DL(getModule());
    141     IntegerType * const intTy = getIntPtrTy(DL);
    142     const auto byteWidth = (intTy->getBitWidth() / 8);
    143     const auto offset = ConstantInt::get(intTy, alignment + byteWidth - 1);
    144     const auto width = ConstantExpr::getSizeOf(type);
    145     if (!width->isOneValue()) {
    146         if (isa<Constant>(size)) {
    147             size = ConstantExpr::getMul(cast<Constant>(size), width);
    148         } else {
    149             size = CreateMul(size, width);
    150         }
    151     }
    152     if (isa<Constant>(size)) {
    153         size = ConstantExpr::getAdd(cast<Constant>(size), offset);
    154     } else {
    155         size = CreateAdd(size, offset);
    156     }
    157     size = CreateTruncOrBitCast(size, intTy);
    158     Value * unaligned = CreateMalloc(getInt8Ty(), size);
    159     Value * aligned = CreateBitOrPointerCast(unaligned, intTy);
    160     aligned = CreateAnd(CreateAdd(aligned, offset), ConstantExpr::getNot(ConstantInt::get(intTy, alignment - 1)));
    161     Value * ptr = CreateBitOrPointerCast(CreateSub(aligned, ConstantInt::get(intTy, byteWidth)), intTy->getPointerTo());
    162     CreateAlignedStore(CreateBitOrPointerCast(unaligned, intTy), ptr, byteWidth);
    163     return CreateBitOrPointerCast(aligned, type->getPointerTo());
    164 }
    165 
    166 void IDISA_Builder::CreateFree(Value * ptr) {
    167     PointerType * const voidPtrTy = getVoidPtrTy();
    168     Function * const free = cast<Function>(getModule()->getOrInsertFunction("free", getVoidTy(), voidPtrTy, nullptr));
    169     CallInst * const ci = CreateCall(free, {CreateBitOrPointerCast(ptr, voidPtrTy)});
    170     ci->setTailCall();
    171     ci->setCallingConv(free->getCallingConv());
    172 }
    173 
    174 void IDISA_Builder::CreateAlignedFree(Value * ptr) {
    175     DataLayout DL(getModule());
    176     IntegerType * const intTy = getIntPtrTy(DL);
    177     const auto byteWidth = (intTy->getBitWidth() / 8);
    178     ptr = CreateBitOrPointerCast(ptr, intTy);
    179     ptr = CreateSub(ptr, ConstantInt::get(intTy, byteWidth));
    180     ptr = CreateBitOrPointerCast(ptr, getInt8PtrTy());
    181     CreateFree(CreateAlignedLoad(ptr, byteWidth));
    182 }
    183 
    184 Value * IDISA_Builder::CreateRealloc(Value * ptr, Value * size) {
    185     assert (ptr->getType()->isPointerTy());
    186     DataLayout DL(getModule());
    187     IntegerType * const intTy = getIntPtrTy(DL);
    188     PointerType * const voidPtrTy = getVoidPtrTy();
    189     Function * realloc = cast<Function>(getModule()->getOrInsertFunction("realloc", voidPtrTy, voidPtrTy, intTy, nullptr));
    190     realloc->setDoesNotAlias(0);
    191     Type * const type = ptr->getType();
    192     // calculate our new size parameter
    193     size = CreateMul(size, ConstantExpr::getSizeOf(type->getPointerElementType()));
    194     size = CreateTruncOrBitCast(size, intTy);
    195     // call realloc with the pointer and adjusted size
    196     CallInst * ci = CreateCall(realloc, {ptr, size});
    197     ci->setTailCall();
    198     ci->setCallingConv(realloc->getCallingConv());
    199     return CreateBitOrPointerCast(ci, type);
    200 }
    201 
    202 Value * IDISA_Builder::CreateAlignedRealloc(Value * ptr, Value * size, const unsigned alignment) {
    203     assert ((alignment & (alignment - 1)) == 0); // is power of 2
    204     assert (ptr->getType()->isPointerTy());
    205     DataLayout DL(getModule());
    206     IntegerType * const intTy = getIntPtrTy(DL);
    207     PointerType * const bpTy = getInt8PtrTy();
    208     Type * const type = ptr->getType();
    209     // calculate our new size parameter
    210     const auto byteWidth = (intTy->getBitWidth() / 8);
    211     const auto offset = ConstantInt::get(intTy, alignment + byteWidth - 1);
    212     const auto width = ConstantExpr::getSizeOf(type);
    213     if (!width->isOneValue()) {
    214         if (isa<Constant>(size)) {
    215             size = ConstantExpr::getMul(cast<Constant>(size), width);
    216         } else {
    217             size = CreateMul(size, width);
    218         }
    219     }
    220     if (isa<Constant>(size)) {
    221         size = ConstantExpr::getAdd(cast<Constant>(size), offset);
    222     } else {
    223         size = CreateAdd(size, offset);
    224     }
    225     size = CreateTruncOrBitCast(size, intTy);
    226     // calculate the offset containing the unaligned pointer address
    227     ptr = CreateBitOrPointerCast(ptr, bpTy);
    228     ptr = CreateSub(ptr, ConstantInt::get(intTy, byteWidth));
    229     ptr = CreateBitOrPointerCast(ptr, intTy->getPointerTo());
    230     // load the unaligned pointer as an uint8 *
    231     ptr = CreateAlignedLoad(ptr, byteWidth);
    232     ptr = CreateBitOrPointerCast(ptr, bpTy);
    233     // call realloc with the unaligned pointer and adjusted size
    234     Value * unaligned = CreateRealloc(ptr, size);
    235     Value * aligned = CreateBitOrPointerCast(unaligned, intTy);
    236     aligned = CreateAnd(CreateAdd(aligned, offset), ConstantExpr::getNot(ConstantInt::get(intTy, alignment - 1)));
    237     Value * prefix = CreateBitOrPointerCast(CreateSub(aligned, ConstantInt::get(intTy, byteWidth)), intTy->getPointerTo());
    238     CreateAlignedStore(CreateBitOrPointerCast(unaligned, intTy), prefix, byteWidth);
    239     return CreateBitOrPointerCast(aligned, type);
    240 }
    241 
    242 void IDISA_Builder::CreateMemZero(Value * ptr, Value * size, const unsigned alignment) {
    243     assert (ptr->getType()->isPointerTy() && size->getType()->isIntegerTy());
    244     Type * const type = ptr->getType();
    245     const auto width = ConstantExpr::getSizeOf(type->getPointerElementType());
    246     if (isa<Constant>(size)) {
    247         size = ConstantExpr::getMul(cast<Constant>(size), width);
    248     } else {
    249         size = CreateMul(size, width);
    250     }
    251     CreateMemSet(CreateBitOrPointerCast(ptr, getInt8PtrTy()), getInt8(0), size, alignment);
    252 }
    253 
    254 PointerType * IDISA_Builder::getVoidPtrTy() const {
    255     return TypeBuilder<void *, false>::get(getContext());
    256 }
    25774
    25875Constant * IDISA_Builder::simd_himask(unsigned fw) {
     
    561378}
    562379
    563 LoadInst * IDISA_Builder::CreateAtomicLoadAcquire(Value * ptr) {
    564     unsigned alignment = dyn_cast<PointerType>(ptr->getType())->getElementType()->getPrimitiveSizeInBits()/8;
    565     LoadInst * inst = CreateAlignedLoad(ptr, alignment);
    566     inst->setOrdering(AtomicOrdering::Acquire);
    567     return inst;
    568    
    569 }
    570 StoreInst * IDISA_Builder::CreateAtomicStoreRelease(Value * val, Value * ptr) {
    571     unsigned alignment = dyn_cast<PointerType>(ptr->getType())->getElementType()->getPrimitiveSizeInBits()/8;
    572     StoreInst * inst = CreateAlignedStore(val, ptr, alignment);
    573     inst->setOrdering(AtomicOrdering::Release);
    574     return inst;
    575 }
    576 
    577380Type * IDISA_Builder::getStreamTy(const unsigned FieldWidth) {
    578381    const auto f = mStreamTypes.find(FieldWidth);
  • icGREP/icgrep-devel/icgrep/IR_Gen/idisa_builder.h

    r5238 r5239  
    1414#include <llvm/Support/Host.h>
    1515#include <llvm/ADT/Triple.h>
     16#include <IR_Gen/CBuilder.h>
    1617#include <IR_Gen/types/streamtype.h>
    1718#include <boost/container/flat_map.hpp>
     
    2122namespace IDISA {
    2223
    23 class IDISA_Builder : public IRBuilder<> {
     24class IDISA_Builder : public CBuilder {
    2425
    2526    using StreamTypes = boost::container::flat_map<unsigned, StreamType *>;
     
    2829
    2930    IDISA_Builder(Module * m, unsigned archBitWidth, unsigned bitBlockWidth, unsigned stride, unsigned CacheAlignment=64)
    30     : IRBuilder<>(m->getContext())
    31     , mMod(m)
    32     , mCacheLineAlignment(CacheAlignment)
     31    : CBuilder(m, archBitWidth, CacheAlignment)
    3332    , mBitBlockWidth(bitBlockWidth)
    3433    , mStride(stride)
    35     , mSizeType(getIntNTy(archBitWidth))
    3634    , mBitBlockType(VectorType::get(IntegerType::get(getContext(), 64), bitBlockWidth / 64))
    3735    , mZeroInitializer(Constant::getNullValue(mBitBlockType))
     
    5755    }
    5856
    59     Module * getModule() const {
    60         return mMod;
    61     }
    62    
    63     void setModule(Module * m)  {
    64         mMod = m;
    65     }
    66    
    6757    Constant * allZeroes() const {
    6858        return mZeroInitializer;
     
    8272    void CreateBlockAlignedStore(Value * const value, Value * const ptr, std::initializer_list<Value *> indices);
    8373
    84     Value * CreateMalloc(Type * type, Value * size);
    85     Value * CreateAlignedMalloc(Type *type, Value * size, const unsigned alignment);
    86     void CreateFree(Value * ptr);
    87     void CreateAlignedFree(Value * ptr);
    88     Value * CreateRealloc(Value * ptr, Value * size);
    89     Value * CreateAlignedRealloc(Value * ptr, Value * size, const unsigned alignment);
    90     void CreateMemZero(Value * ptr, Value * size, const unsigned alignment = 1);
    91 
    9274    void CallPrintRegister(const std::string & regName, Value * const value);
    93     void CallPrintInt(const std::string & name, Value * const value);
    94 
     75   
    9576    VectorType * fwVectorType(unsigned fw);
    9677
     
    153134    Value * fwCast(unsigned fw, Value * a);
    154135   
    155     inline llvm::IntegerType * getSizeTy() const {
    156         return mSizeType;
    157     }
    158 
    159     inline llvm::ConstantInt * getSize(const size_t value) const {
    160         return ConstantInt::get(getSizeTy(), value);
    161     }
    162 
    163     PointerType * getVoidPtrTy() const;
    164 
    165136    inline VectorType * getBitBlockType() const {
    166137        return mBitBlockType;
    167     }
    168 
    169     inline unsigned getCacheAlignment() const {
    170         return mCacheLineAlignment;
    171138    }
    172139
     
    183150    }
    184151   
    185     virtual llvm::LoadInst* CreateAtomicLoadAcquire(Value * ptr);
    186     virtual llvm::StoreInst *  CreateAtomicStoreRelease(Value * val, Value * ptr);
    187    
    188152protected:
    189     Module *            mMod;
    190     unsigned            mCacheLineAlignment;
    191153    unsigned            mBitBlockWidth;
    192154    unsigned            mStride;
    193     IntegerType *       mSizeType;
    194155    VectorType *        mBitBlockType;
    195156
     
    197158    Constant *          mOneInitializer;
    198159    Constant *          mPrintRegisterFunction;
    199    
    200160    StreamTypes         mStreamTypes;
    201161};
     
    209169}
    210170
    211 inline LoadInst * IDISA_Builder::CreateBlockAlignedLoad(Value * const ptr, std::initializer_list<Value *> indicies) {
    212     return CreateBlockAlignedLoad(CreateGEP(ptr, indicies));
     171inline LoadInst * IDISA_Builder::CreateBlockAlignedLoad(Value * const ptr, std::initializer_list<Value *> indices) {
     172    return CreateBlockAlignedLoad(CreateGEP(ptr, indices));
    213173}
    214174
     
    221181}
    222182
    223 inline void IDISA_Builder::CreateBlockAlignedStore(Value * const value, Value * const ptr, std::initializer_list<Value *> indicies) {
    224     CreateBlockAlignedStore(value, CreateGEP(ptr, indicies));
     183inline void IDISA_Builder::CreateBlockAlignedStore(Value * const value, Value * const ptr, std::initializer_list<Value *> indices) {
     184    CreateBlockAlignedStore(value, CreateGEP(ptr, indices));
    225185}
    226186   
Note: See TracChangeset for help on using the changeset viewer.