mutateType on AllocaInst problem

Hi,

I’m trying to replace pointer typed alloca instructions to allocate a vector of pointers. The idea is to change from [1] to [2]:

[1]: %0 = alloca i*
[2]: %0 = alloca <3 x i8*>

I did it this way:

  • first, create and insert [2] (for debug purpose, not necessary to insert)

  • mutate type so that replaceAllUsesWith can be called in a correctly typed way

  • replace all uses of the old allocation to the new one (not necessary too)

Here’s the associated code running on each alloca instructions from each function given to the function pass:

void __replace_alloca_vector_IRBuilder(AllocaInst *AI) {
if(AI->getAllocatedType()->isPointerTy()){
LLVMContext *Context = &(AI->getContext());
IRBuilder<> IRB(*Context);

VectorType *FDP_fatvector = VectorType::get(
PointerType::getInt8PtrTy(*Context),
3
);

// two different ways depending on wether to create and insert or just create

// AllocaInst *FDP_AI = new AllocaInst(cast(FDP_fatvector));
IRB.SetInsertPoint(AI);
AllocaInst *FDP_AI = IRB.CreateAlloca(cast(FDP_fatvector));

cast(AI)->mutateType(cast(FDP_AI)->getType());
cast(AI)->replaceAllUsesWith(cast(FDP_AI));
}
}

I am getting an error at optimization time. After my pass ran, during the Bitcode Writer pass time:

opt: /home/pierre/Desktop/llvm/lib/Bitcode/Writer/ValueEnumerator.h:162: unsigned int llvm::ValueEnumerator::getTypeID(llvm::Type*) const: Assertion `I != TypeMap.end() && “Type not in ValueEnumerator!”’ failed.
#0 0x0000000002a95b5c llvm::sys::PrintStackTrace(llvm::raw_ostream&) /home/pierre/Desktop/llvm/lib/Support/Unix/Signals.inc:400:0
#1 0x0000000002a95edf PrintStackTraceSignalHandler(void*) /home/pierre/Desktop/llvm/lib/Support/Unix/Signals.inc:468:0
#2 0x0000000002a94145 llvm::sys::RunSignalHandlers() /home/pierre/Desktop/llvm/lib/Support/Signals.cpp:44:0
#3 0x0000000002a953a8 SignalHandler(int) /home/pierre/Desktop/llvm/lib/Support/Unix/Signals.inc:254:0
#4 0x00007f04b0294d10 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x10d10)
#5 0x00007f04af6c2267 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x35267)
#6 0x00007f04af6c3eca abort (/lib/x86_64-linux-gnu/libc.so.6+0x36eca)
#7 0x00007f04af6bb03d (/lib/x86_64-linux-gnu/libc.so.6+0x2e03d)
#8 0x00007f04af6bb0f2 (/lib/x86_64-linux-gnu/libc.so.6+0x2e0f2)
#9 0x000000000204de7e llvm::ValueEnumerator::getTypeID(llvm::Type*) const /home/pierre/Desktop/llvm/lib/Bitcode/Writer/ValueEnumerator.h:162:0
#10 0x00000000020454cc (anonymous namespace)::ModuleBitcodeWriter::writeInstruction(llvm::Instruction const&, unsigned int, llvm::SmallVectorImpl&) /home/pierre/Desktop/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp:2509:0
#11 0x00000000020474eb (anonymous namespace)::ModuleBitcodeWriter::writeFunction(llvm::Function const&, llvm::DenseMap<llvm::Function const*, unsigned long, llvm::DenseMapInfo<llvm::Function const*>, llvm::detail::DenseMapPair<llvm::Function const*, unsigned long> >&) /home/pierre/Desktop/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp:2866:0
#12 0x000000000204be9d (anonymous namespace)::ModuleBitcodeWriter::writeModule() /home/pierre/Desktop/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp:3588:0
#13 0x000000000204bc70 (anonymous namespace)::ModuleBitcodeWriter::writeBlocks() /home/pierre/Desktop/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp:3537:0
#14 0x000000000204bc49 (anonymous namespace)::BitcodeWriter::write() /home/pierre/Desktop/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp:3532:0
#15 0x000000000204c419 llvm::WriteBitcodeToFile(llvm::Module const*, llvm::raw_ostream&, bool, llvm::ModuleSummaryIndex const*, bool) /home/pierre/Desktop/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp:3703:0
#16 0x0000000002037ed6 (anonymous namespace)::WriteBitcodePass::runOnModule(llvm::Module&) /home/pierre/Desktop/llvm/lib/Bitcode/Writer/BitcodeWriterPass.cpp:61:0
#17 0x00000000024da603 (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) /home/pierre/Desktop/llvm/lib/IR/LegacyPassManager.cpp:1603:0
#18 0x00000000024dad6d llvm::legacy::PassManagerImpl::run(llvm::Module&) /home/pierre/Desktop/llvm/lib/IR/LegacyPassManager.cpp:1706:0
#19 0x00000000024dafad llvm::legacy::PassManager::run(llvm::Module&) /home/pierre/Desktop/llvm/lib/IR/LegacyPassManager.cpp:1738:0
#20 0x00000000011b6d68 main /home/pierre/Desktop/llvm/tools/opt/opt.cpp:673:0
#21 0x00007f04af6ada40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x20a40)
#22 0x000000000119b2b9 _start (/home/pierre/Desktop/build/bin/opt+0x119b2b9)
Stack dump:
0. Program arguments: opt -load …/…/…/build/lib/LLVMFDP.so -fdp-func type_out.bc -o type_out_func.bc

  1. Running pass ‘Bitcode Writer’ on module ‘type_out.bc’.
    Aborted (core dumped)

What is wrong with the vector type? Note that the exact same problem occurs when I just try to mutate the type (removing replaceAllUsesWith) with the new alloca instruction created but not inserted with IRBuilder.

Thanks a lot,

Pierre