Ignore:
Timestamp:
Mar 13, 2017, 3:02:13 PM (2 years ago)
Author:
nmedfort
Message:

Work on non-carry collapsing mode.

Location:
icGREP/icgrep-devel/icgrep/IR_Gen
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/IR_Gen/CBuilder.cpp

    r5353 r5361  
    1313#include <fcntl.h>
    1414#include <toolchain.h>
     15
     16#include <llvm/Support/raw_ostream.h>
    1517
    1618using namespace llvm;
     
    106108}
    107109
    108 Value * CBuilder::CreateMalloc(Type * type, Value * size) {
    109     DataLayout DL(getModule());
     110Value * CBuilder::CreateMalloc(Value * size) {
     111    Module * const m = getModule();
     112    DataLayout DL(m);
    110113    IntegerType * const intTy = getIntPtrTy(DL);
    111114    if (size->getType() != intTy) {
     
    116119        }
    117120    }   
    118     Constant * width = ConstantExpr::getSizeOf(type);
    119     if (LLVM_UNLIKELY(width->getType() != intTy)) {
    120         width = ConstantExpr::getIntegerCast(width, intTy, false);
    121     }
    122     if (!width->isOneValue()) {
    123         if (isa<Constant>(size)) {
    124             size = ConstantExpr::getMul(cast<Constant>(size), width);
    125         } else {
    126             size = CreateMul(size, width);
    127         }
    128     }
    129     Module * const m = getModule();
     121    PointerType * const voidPtrTy = getVoidPtrTy();
    130122    Function * malloc = m->getFunction("malloc");
    131123    if (malloc == nullptr) {
    132         PointerType * const voidPtrTy = getVoidPtrTy();
    133124        FunctionType * fty = FunctionType::get(voidPtrTy, {intTy}, false);
    134125        malloc = Function::Create(fty, Function::ExternalLinkage, "malloc", mMod);
     
    137128    }
    138129    assert (size->getType() == intTy);
    139     CallInst * ci = CreateCall(malloc, size);
     130    CallInst * ci = CreateCall(malloc, size); assert (ci);
    140131    ci->setTailCall();
    141132    ci->setCallingConv(malloc->getCallingConv());
    142     Value * ptr = CreateBitOrPointerCast(ci, type->getPointerTo());
     133    Value * ptr = CreatePointerCast(ci, voidPtrTy); assert (ptr);
    143134    CreateAssert(ptr, "FATAL ERROR: out of memory");
    144135    return ptr;
    145136}
    146137
    147 Value * CBuilder::CreateAlignedMalloc(Type * type, Value * size, const unsigned alignment) {
    148     assert ((alignment & (alignment - 1)) == 0); // is power of 2
    149     DataLayout DL(getModule());
     138Value * CBuilder::CreateAlignedMalloc(Value * size, const unsigned alignment) {
     139    if (LLVM_UNLIKELY((alignment & (alignment - 1)) != 0)) {
     140        report_fatal_error("CreateAlignedMalloc: alignment must be a power of 2");
     141    }
     142    DataLayout DL(mMod);
    150143    IntegerType * const intTy = getIntPtrTy(DL);
    151     if (size->getType() != intTy) {
    152         if (isa<Constant>(size)) {
    153             size = ConstantExpr::getIntegerCast(cast<Constant>(size), intTy, false);
    154         } else {
    155             size = CreateZExtOrTrunc(size, intTy);
    156         }
    157     }
    158     const auto byteWidth = (intTy->getBitWidth() / 8);
    159     Constant * const offset = ConstantInt::get(intTy, alignment + byteWidth - 1);
    160     Constant * width = ConstantExpr::getSizeOf(type);
    161     if (LLVM_UNLIKELY(width->getType() != intTy)) {
    162         width = ConstantExpr::getIntegerCast(width, intTy, false);
    163     }
    164     if (!width->isOneValue()) {
    165         if (isa<Constant>(size)) {
    166             size = ConstantExpr::getMul(cast<Constant>(size), width);
    167         } else {
    168             size = CreateMul(size, width);
    169         }
    170     }
    171     if (isa<Constant>(size)) {
    172         size = ConstantExpr::getAdd(cast<Constant>(size), offset);
    173     } else {
     144    Function * aligned_malloc = mMod->getFunction("aligned_malloc" + std::to_string(alignment));
     145    if (LLVM_UNLIKELY(aligned_malloc == nullptr)) {
     146        const auto ip = saveIP();
     147        PointerType * const voidPtrTy = getVoidPtrTy();
     148        FunctionType * fty = FunctionType::get(voidPtrTy, {intTy}, false);
     149        aligned_malloc = Function::Create(fty, Function::InternalLinkage, "aligned_malloc" + std::to_string(alignment), mMod);
     150        aligned_malloc->setCallingConv(CallingConv::C);
     151        aligned_malloc->setDoesNotAlias(0);
     152        aligned_malloc->addFnAttr(Attribute::AlwaysInline);
     153        Value * size = &*aligned_malloc->arg_begin();
     154        SetInsertPoint(BasicBlock::Create(mMod->getContext(), "entry", aligned_malloc));
     155        const auto byteWidth = (intTy->getBitWidth() / 8);
     156        Constant * const offset = ConstantInt::get(intTy, alignment + byteWidth - 1);
    174157        size = CreateAdd(size, offset);
    175     }
    176     assert (size->getType() == intTy);
    177     Value * unaligned = CreatePtrToInt(CreateMalloc(getInt8Ty(), size), intTy);
    178     Value * aligned = CreateAnd(CreateAdd(unaligned, offset), ConstantExpr::getNot(ConstantInt::get(intTy, alignment - 1)));
    179     Value * prefix = CreateIntToPtr(CreateSub(aligned, ConstantInt::get(intTy, byteWidth)), intTy->getPointerTo());
    180     assert (unaligned->getType() == prefix->getType()->getPointerElementType());
    181     CreateAlignedStore(unaligned, prefix, byteWidth);
    182     return CreateIntToPtr(aligned, type->getPointerTo());
     158        Value * unaligned = CreatePtrToInt(CreateMalloc(size), intTy);
     159        Value * aligned = CreateAnd(CreateAdd(unaligned, offset), ConstantExpr::getNot(ConstantInt::get(intTy, alignment - 1)));
     160        Value * prefix = CreateIntToPtr(CreateSub(aligned, ConstantInt::get(intTy, byteWidth)), intTy->getPointerTo());
     161        assert (unaligned->getType() == prefix->getType()->getPointerElementType());
     162        CreateAlignedStore(unaligned, prefix, byteWidth);
     163        CreateRet(CreateIntToPtr(aligned, voidPtrTy));
     164        restoreIP(ip);
     165    }
     166    return CreateCall(aligned_malloc, {CreateZExtOrTrunc(size, intTy)});
    183167}
    184168
     
    230214    IntegerType * const intTy = getIntPtrTy(DL);
    231215    PointerType * type = cast<PointerType>(ptr->getType());
    232     Constant * width = ConstantExpr::getSizeOf(type->getPointerElementType());
    233     if (LLVM_UNLIKELY(width->getType() != intTy)) {
    234         width = ConstantExpr::getIntegerCast(width, intTy, false);
    235     }
    236216    if (size->getType() != intTy) {
    237217        if (isa<Constant>(size)) {
     
    239219        } else {
    240220            size = CreateZExtOrTrunc(size, intTy);
    241         }
    242     }
    243     if (!width->isOneValue()) {
    244         if (isa<Constant>(size)) {
    245             size = ConstantExpr::getMul(cast<Constant>(size), width);
    246         } else {
    247             size = CreateMul(size, width);
    248221        }
    249222    }
     
    275248   
    276249}
     250
    277251StoreInst * CBuilder::CreateAtomicStoreRelease(Value * val, Value * ptr) {
    278252    const auto alignment = ptr->getType()->getPointerElementType()->getPrimitiveSizeInBits() / 8;
     
    281255    return inst;
    282256}
    283 
    284257
    285258PointerType * CBuilder::getFILEptrTy() {
     
    390363            Value * len = CreateAdd(sz, getSize(21));
    391364            ConstantInt * _11 = getSize(11);
    392             Value * bytes = CreateMalloc(getInt8Ty(), len);
     365            Value * bytes = CreatePointerCast(CreateMalloc(len), getInt8PtrTy());
    393366            CreateMemCpy(bytes, CreateGlobalStringPtr("Assertion `"), _11, 1);
    394367            CreateMemCpy(CreateGEP(bytes, _11), msg, sz, 1);
     
    424397}
    425398
     399llvm::Value * CBuilder::CreateCeilLog2(llvm::Value * value) {
     400    IntegerType * ty = cast<IntegerType>(value->getType());
     401    CreateAssert(value, "CreateCeilLog2: value cannot be zero");
     402    Value * m = CreateCall(Intrinsic::getDeclaration(mMod, Intrinsic::ctlz, ty), {value, ConstantInt::getFalse(getContext())});
     403    Value * isPowOf2 = CreateICmpEQ(CreateAnd(value, CreateSub(value, ConstantInt::get(ty, 1))), ConstantInt::getNullValue(ty));
     404    m = CreateSub(ConstantInt::get(m->getType(), ty->getBitWidth() - 1), m);
     405    return CreateSelect(isPowOf2, m, CreateAdd(m, ConstantInt::get(m->getType(), 1)));
     406}
     407
    426408CBuilder::CBuilder(Module * const m, const unsigned GeneralRegisterWidthInBits, const bool SupportsIndirectBr, const unsigned CacheLineAlignmentInBytes)
    427409: IRBuilder<>(m->getContext())
  • icGREP/icgrep-devel/icgrep/IR_Gen/CBuilder.h

    r5353 r5361  
    3131        mMod = m;
    3232    }
    33    
    34     llvm::Value * CreateMalloc(llvm::Type * type, llvm::Value * size);
    3533
    36     llvm::Value * CreateAlignedMalloc(llvm::Type * type, llvm::Value * size, const unsigned alignment);
     34    llvm::Value * CreateMalloc(llvm::Value * size);
     35
     36    llvm::Value * CreateAlignedMalloc(llvm::Value * size, const unsigned alignment);
    3737   
    3838    void CreateFree(llvm::Value * const ptr);
     
    123123    }
    124124
     125    llvm::Value * CreateCeilLog2(llvm::Value * value);
     126
    125127protected:
    126     llvm::Module *      mMod;
     128    llvm::Module *      mMod;   
    127129    unsigned            mCacheLineAlignment;
    128130    llvm::IntegerType * mSizeType;
  • icGREP/icgrep-devel/icgrep/IR_Gen/idisa_target.cpp

    r5329 r5361  
    1515namespace IDISA {
    1616   
    17 IDISA_Builder * GetIDISA_Builder(llvm::Module * mod) {
     17IDISA_Builder * GetIDISA_Builder(llvm::Module * const mod) {
    1818    if (LLVM_UNLIKELY(mod == nullptr)) {
    1919        report_fatal_error("GetIDISA_Builder: module cannot be null");
     
    2222        mod->setTargetTriple(llvm::sys::getProcessTriple());
    2323    }
     24    unsigned registerWidth = 0;
    2425    Triple T(mod->getTargetTriple());
    25     unsigned registerWidth = 32;
    2626    if (T.isArch64Bit()) {
    2727        registerWidth = 64;
     
    4545}
    4646
    47 IDISA_Builder * GetIDISA_GPU_Builder(llvm::Module * mod) {
     47IDISA_Builder * GetIDISA_GPU_Builder(llvm::Module * const mod) {
    4848    return new IDISA_NVPTX20_Builder(mod, 64);
    4949}
  • icGREP/icgrep-devel/icgrep/IR_Gen/idisa_target.h

    r5260 r5361  
    1212namespace IDISA {
    1313   
    14 IDISA::IDISA_Builder * GetIDISA_Builder(llvm::Module * m);
     14IDISA::IDISA_Builder * GetIDISA_Builder(llvm::Module * const m);
    1515
    16 IDISA::IDISA_Builder * GetIDISA_GPU_Builder(llvm::Module * m);
     16IDISA::IDISA_Builder * GetIDISA_GPU_Builder(llvm::Module * const m);
    1717
    1818}
Note: See TracChangeset for help on using the changeset viewer.