Ignore:
Timestamp:
May 22, 2017, 12:14:19 PM (2 years ago)
Author:
nmedfort
Message:

Restructuring work for the Driver classes. Start of work to eliminate the memory leaks with the ExecutionEngine?. Replaced custom AlignedMalloc? with backend call to std::aligned_malloc. Salvaged some work on DistributionPass? for reevaluation.

File:
1 edited

Legend:

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

    r5454 r5464  
    1313#include <llvm/Support/raw_ostream.h>
    1414#include <toolchain/toolchain.h>
     15#include <toolchain/driver.h>
     16#include <stdlib.h>
    1517#include <sys/mman.h>
     18#include <unistd.h>
    1619
    1720using namespace llvm;
     
    218221Value * CBuilder::CreateMalloc(Value * size) {
    219222    Module * const m = getModule();
    220     DataLayout DL(m);
    221     IntegerType * const intTy = getIntPtrTy(DL);
    222     if (size->getType() != intTy) {
    223         if (isa<Constant>(size)) {
    224             size = ConstantExpr::getIntegerCast(cast<Constant>(size), intTy, false);
    225         } else {
    226             size = CreateZExtOrTrunc(size, intTy);
    227         }
    228     }   
    229     PointerType * const voidPtrTy = getVoidPtrTy();
     223    IntegerType * const sizeTy = getSizeTy();   
    230224    Function * malloc = m->getFunction("malloc");
    231225    if (malloc == nullptr) {
    232         FunctionType * fty = FunctionType::get(voidPtrTy, {intTy}, false);
     226//        malloc = LinkFunction("malloc", &std::malloc);
     227        PointerType * const voidPtrTy = getVoidPtrTy();
     228        FunctionType * fty = FunctionType::get(voidPtrTy, {sizeTy}, false);
    233229        malloc = Function::Create(fty, Function::ExternalLinkage, "malloc", m);
    234230        malloc->setCallingConv(CallingConv::C);
    235231        malloc->setDoesNotAlias(0);
    236232    }
    237     assert (size->getType() == intTy);
    238     CallInst * ci = CreateCall(malloc, size); assert (ci);
    239     ci->setTailCall();
    240     ci->setCallingConv(malloc->getCallingConv());
    241     Value * ptr = CreatePointerCast(ci, voidPtrTy); assert (ptr);
    242     CreateAssert(ptr, "FATAL ERROR: out of memory");
     233    size = CreateZExtOrTrunc(size, sizeTy);
     234    CallInst * const ptr = CreateCall(malloc, size);
     235    ptr->setTailCall();
     236    CreateAssert(ptr, "CreateMalloc: returned null pointer");
    243237    return ptr;
    244238}
     
    249243    }
    250244    Module * const m = getModule();
    251     DataLayout DL(m);
    252     IntegerType * const intTy = getIntPtrTy(DL);
    253     Function * aligned_malloc = m->getFunction("aligned_malloc" + std::to_string(alignment));
     245    Function * aligned_malloc = m->getFunction("aligned_alloc");
     246    IntegerType * const sizeTy = getSizeTy();
    254247    if (LLVM_UNLIKELY(aligned_malloc == nullptr)) {
    255         const auto ip = saveIP();
    256248        PointerType * const voidPtrTy = getVoidPtrTy();
    257         FunctionType * fty = FunctionType::get(voidPtrTy, {intTy}, false);
    258         aligned_malloc = Function::Create(fty, Function::InternalLinkage, "aligned_malloc" + std::to_string(alignment), m);
     249        FunctionType * const fty = FunctionType::get(voidPtrTy, {sizeTy, sizeTy}, false);
     250        aligned_malloc = Function::Create(fty, Function::ExternalLinkage, "aligned_alloc", m);
    259251        aligned_malloc->setCallingConv(CallingConv::C);
    260252        aligned_malloc->setDoesNotAlias(0);
    261         aligned_malloc->addFnAttr(Attribute::AlwaysInline);
    262         Value * size = &*aligned_malloc->arg_begin();
    263         SetInsertPoint(BasicBlock::Create(getContext(), "entry", aligned_malloc));
    264         const auto byteWidth = (intTy->getBitWidth() / 8);
    265         Constant * const offset = ConstantInt::get(intTy, alignment + byteWidth - 1);
    266         size = CreateAdd(size, offset);
    267         Value * unaligned = CreatePtrToInt(CreateMalloc(size), intTy);
    268         Value * aligned = CreateAnd(CreateAdd(unaligned, offset), ConstantExpr::getNot(ConstantInt::get(intTy, alignment - 1)));
    269         Value * prefix = CreateIntToPtr(CreateSub(aligned, ConstantInt::get(intTy, byteWidth)), intTy->getPointerTo());
    270         assert (unaligned->getType() == prefix->getType()->getPointerElementType());
    271         CreateAlignedStore(unaligned, prefix, byteWidth);
    272         CreateRet(CreateIntToPtr(aligned, voidPtrTy));
    273         restoreIP(ip);
    274     }
    275     return CreateCall(aligned_malloc, {CreateZExtOrTrunc(size, intTy)});
     253
     254        // aligned_malloc = LinkFunction("aligned_alloc", &aligned_alloc);
     255    }
     256
     257    size = CreateZExtOrTrunc(size, sizeTy);
     258    ConstantInt * const align = ConstantInt::get(sizeTy, alignment);
     259    if (codegen::EnableAsserts) {
     260        CreateAssert(CreateICmpEQ(CreateURem(size, align), ConstantInt::get(sizeTy, 0)),
     261                     "CreateAlignedMalloc: size must be an integral multiple of alignment.");
     262    }
     263    Value * const ptr = CreateCall(aligned_malloc, {align, size});
     264    CreateAssert(ptr, "CreateAlignedMalloc: returned null pointer.");
     265    return ptr;
    276266}
    277267
     
    440430}
    441431
    442 void CBuilder::CreateFree(Value * const ptr) {
     432void CBuilder::CreateFree(Value * ptr) {
    443433    assert (ptr->getType()->isPointerTy());
    444434    Module * const m = getModule();
     435    Type * const voidPtrTy =  getVoidPtrTy();
     436    Function * f = m->getFunction("free");
     437    if (f == nullptr) {
     438        FunctionType * fty = FunctionType::get(getVoidTy(), {voidPtrTy}, false);
     439        f = Function::Create(fty, Function::ExternalLinkage, "free", m);
     440        f->setCallingConv(CallingConv::C);
     441        // f = LinkFunction("free", &std::free);
     442    }
     443    ptr = CreatePointerCast(ptr, voidPtrTy);
     444    CreateCall(f, ptr)->setTailCall();
     445}
     446
     447Value * CBuilder::CreateRealloc(Value * const ptr, Value * size) {
     448    Module * const m = getModule();
     449    IntegerType * const sizeTy = getSizeTy();
    445450    PointerType * const voidPtrTy = getVoidPtrTy();
    446     Function * free = m->getFunction("free");
    447     if (free == nullptr) {
    448         FunctionType * fty = FunctionType::get(getVoidTy(), {voidPtrTy}, false);
    449         Module * const m = getModule();
    450         free = Function::Create(fty, Function::ExternalLinkage, "free", m);
    451         free->setCallingConv(CallingConv::C);
    452     }
    453     CallInst * const ci = CreateCall(free, CreatePointerCast(ptr, voidPtrTy));
     451    Function * f = m->getFunction("realloc");
     452    if (f == nullptr) {
     453        FunctionType * fty = FunctionType::get(voidPtrTy, {voidPtrTy, sizeTy}, false);
     454        f = Function::Create(fty, Function::ExternalLinkage, "realloc", m);
     455        f->setCallingConv(CallingConv::C);
     456        f->setDoesNotAlias(0);
     457        f->setDoesNotAlias(1);
     458        // f = LinkFunction("realloc", &realloc);
     459    }
     460    Value * const addr = CreatePointerCast(ptr, voidPtrTy);
     461    size = CreateZExtOrTrunc(size, sizeTy);
     462    CallInst * const ci = CreateCall(f, {addr, size});
    454463    ci->setTailCall();
    455     ci->setCallingConv(free->getCallingConv());
    456 }
    457 
    458 void CBuilder::CreateAlignedFree(Value * const ptr, const bool testForNullAddress) {
    459     // WARNING: this will segfault if the value of the ptr at runtime is null but testForNullAddress was not set
    460     PointerType * type = cast<PointerType>(ptr->getType());
    461     BasicBlock * exit = nullptr;
    462     if (testForNullAddress) {
    463         LLVMContext & C = getContext();
    464         BasicBlock * bb = GetInsertBlock();
    465         Function * f = bb->getParent();
    466         exit = BasicBlock::Create(C, "", f, bb);
    467         BasicBlock * entry = BasicBlock::Create(C, "", f, exit);
    468         Value * cond = CreateICmpEQ(ptr, ConstantPointerNull::get(type));
    469         CreateCondBr(cond, exit, entry);
    470         SetInsertPoint(entry);
    471     }
    472     DataLayout DL(getModule());
    473     IntegerType * const intTy = getIntPtrTy(DL);
    474     const auto byteWidth = (intTy->getBitWidth() / 8);
    475     Value * prefix = CreatePtrToInt(ptr, intTy);
    476     prefix = CreateSub(prefix, ConstantInt::get(intTy, byteWidth));
    477     prefix = CreateIntToPtr(prefix, intTy->getPointerTo());
    478     prefix = CreateIntToPtr(CreateAlignedLoad(prefix, byteWidth), type);
    479     CreateFree(prefix);
    480     if (testForNullAddress) {
    481         CreateBr(exit);
    482         SetInsertPoint(exit);
    483     }
    484 }
    485 
    486 Value * CBuilder::CreateRealloc(Value * ptr, Value * size) {
    487     Module * const m = getModule();
    488     DataLayout DL(m);
    489     IntegerType * const intTy = getIntPtrTy(DL);
    490     PointerType * type = cast<PointerType>(ptr->getType());
    491     if (size->getType() != intTy) {
    492         if (isa<Constant>(size)) {
    493             size = ConstantExpr::getIntegerCast(cast<Constant>(size), intTy, false);
    494         } else {
    495             size = CreateZExtOrTrunc(size, intTy);
    496         }
    497     }
    498     Function * realloc = m->getFunction("realloc");
    499     if (realloc == nullptr) {
    500         PointerType * const voidPtrTy = getVoidPtrTy();
    501         FunctionType * fty = FunctionType::get(voidPtrTy, {voidPtrTy, intTy}, false);       
    502         realloc = Function::Create(fty, Function::ExternalLinkage, "realloc", m);
    503         realloc->setCallingConv(CallingConv::C);
    504         realloc->setDoesNotAlias(1);
    505     }
    506     assert (size->getType() == intTy);
    507     CallInst * ci = CreateCall(realloc, {ptr, size});
    508     ci->setTailCall();
    509     ci->setCallingConv(realloc->getCallingConv());
    510     return CreateBitOrPointerCast(ci, type);
     464    return CreatePointerCast(ci, ptr->getType());
    511465}
    512466
    513467PointerType * CBuilder::getVoidPtrTy() const {
    514     return TypeBuilder<void *, true>::get(getContext());
     468    return TypeBuilder<void *, false>::get(getContext());
    515469}
    516470
     
    776730Function * CBuilder::LinkFunction(llvm::StringRef name, FunctionType * type, void * functionPtr) const {
    777731    assert (mDriver);
    778     return mDriver->LinkFunction(getModule(), name, type, functionPtr);
     732    return mDriver->addLinkFunction(getModule(), name, type, functionPtr);
    779733}
    780734
Note: See TracChangeset for help on using the changeset viewer.