Changeset 5467 for icGREP/icgrep-devel


Ignore:
Timestamp:
May 22, 2017, 2:15:24 PM (2 years ago)
Author:
nmedfort
Message:

Another attempt at using posix_memalign when aligned_alloc does not exist.

File:
1 edited

Legend:

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

    r5466 r5467  
    222222    Module * const m = getModule();
    223223    IntegerType * const sizeTy = getSizeTy();   
    224     Function * malloc = m->getFunction("malloc");
    225     if (malloc == nullptr) {
    226 //        malloc = LinkFunction("malloc", &std::malloc);
     224    Function * f = m->getFunction("malloc");
     225    if (f == nullptr) {
     226        // f = LinkFunction("malloc", &malloc);
    227227        PointerType * const voidPtrTy = getVoidPtrTy();
    228228        FunctionType * fty = FunctionType::get(voidPtrTy, {sizeTy}, false);
    229         malloc = Function::Create(fty, Function::ExternalLinkage, "malloc", m);
    230         malloc->setCallingConv(CallingConv::C);
    231         malloc->setDoesNotAlias(0);
     229        f = Function::Create(fty, Function::ExternalLinkage, "malloc", m);
     230        f->setCallingConv(CallingConv::C);
     231        f->setDoesNotAlias(0);
    232232    }
    233233    size = CreateZExtOrTrunc(size, sizeTy);
    234     CallInst * const ptr = CreateCall(malloc, size);
     234    CallInst * const ptr = CreateCall(f, size);
    235235    ptr->setTailCall();
    236236    CreateAssert(ptr, "CreateMalloc: returned null pointer");
     
    238238}
    239239
    240 #ifndef STDLIB_HAS_ALIGNED_ALLOC
    241 void * aligned_alloc(const size_t alignment, const size_t size) {
    242     void * ptr;
    243     if (LLVM_UNLIKELY(::posix_memalign(&ptr, alignment, size) != 0)) {
    244         throw std::bad_alloc();
    245     }
    246     return ptr;
    247 }
    248 #endif
    249 
    250240Value * CBuilder::CreateAlignedMalloc(Value * size, const unsigned alignment) {
    251241    if (LLVM_UNLIKELY((alignment & (alignment - 1)) != 0)) {
     
    253243    }
    254244    Module * const m = getModule();
     245    IntegerType * const sizeTy = getSizeTy();
     246    PointerType * const voidPtrTy = getVoidPtrTy();
     247    #ifdef STDLIB_HAS_ALIGNED_ALLOC
    255248    Function * f = m->getFunction("aligned_alloc");
    256     IntegerType * const sizeTy = getSizeTy();
    257249    if (LLVM_UNLIKELY(f == nullptr)) {
    258         f = LinkFunction("aligned_alloc", &aligned_alloc);
    259     }
    260 
     250        FunctionType * const fty = FunctionType::get(voidPtrTy, {sizeTy, sizeTy}, false);
     251        f = Function::Create(fty, Function::ExternalLinkage, "aligned_alloc", m);
     252        f->setCallingConv(CallingConv::C);
     253        f->setDoesNotAlias(0);
     254    }
     255    #else
     256    Function * f = m->getFunction("posix_memalign");
     257    if (LLVM_UNLIKELY(f == nullptr)) {
     258        FunctionType * const fty = FunctionType::get(getInt32Ty(), {voidPtrTy->getPointerTo(), sizeTy, sizeTy}, false);
     259        f = Function::Create(fty, Function::ExternalLinkage, "posix_memalign", m);
     260        f->setCallingConv(CallingConv::C);
     261        f->setDoesNotAlias(0);
     262    }
     263    #endif
    261264    size = CreateZExtOrTrunc(size, sizeTy);
    262265    ConstantInt * const align = ConstantInt::get(sizeTy, alignment);
     
    265268                     "CreateAlignedMalloc: size must be an integral multiple of alignment.");
    266269    }
     270    #ifdef STDLIB_HAS_ALIGNED_ALLOC
    267271    Value * const ptr = CreateCall(f, {align, size});
     272    #else
     273    Value * ptr = CreateAlloca(voidPtrTy);
     274    Value * success = CreateCall(f, {ptr, align, size});
     275    if (codegen::EnableAsserts) {
     276        CreateAssert(CreateICmpEQ(success, getInt32(0)), "CreateAlignedMalloc: posix_memalign reported bad allocation");
     277    }
     278    ptr = CreateLoad(ptr);
     279    #endif
    268280    CreateAssert(ptr, "CreateAlignedMalloc: returned null pointer.");
    269281    return ptr;
Note: See TracChangeset for help on using the changeset viewer.