<div dir="ltr"><div><div><div><div><div><div><div><div>Hi,<br><br></div>I'm trying to replace pointer typed alloca instructions to allocate a vector of pointers. The idea is to change from [1] to [2]:<br><br>[1]: %0 = alloca i<N>*<br>[2]: %0 = alloca <3 x i8*><br><br></div>I did it this way:<br></div>  - first, create and insert [2] (for debug purpose, not necessary to insert)<br></div><div>  - mutate type so that replaceAllUsesWith can be called in a correctly typed way<br></div><div>  - replace all uses of the old allocation to the new one (not necessary too)<br></div><div><br></div>Here's the associated code running on each alloca instructions from each function given to the function pass:<br><br>void __replace_alloca_vector_IRBuilder(AllocaInst *AI) {<br>  if(AI->getAllocatedType()->isPointerTy()){<br>    LLVMContext *Context = &(AI->getContext());<br>    IRBuilder<> IRB(*Context);<br>    <br>    VectorType *FDP_fatvector = VectorType::get(<br>      PointerType::getInt8PtrTy(*Context),<br>      3<br>    );<br>    <br></div><div>    // two different ways depending on wether to create and insert or just create<br></div><div>    // AllocaInst *FDP_AI = new AllocaInst(cast<Type>(FDP_fatvector));<br>    IRB.SetInsertPoint(AI);<br>    AllocaInst *FDP_AI = IRB.CreateAlloca(cast<Type>(FDP_fatvector));<br>    <br>    cast<Value>(AI)->mutateType(cast<Value>(FDP_AI)->getType());<br>    cast<Value>(AI)->replaceAllUsesWith(cast<Value>(FDP_AI));<br>  }<br>}<br><br></div>I am getting an error at optimization time. After my pass ran, during the Bitcode Writer pass time:<br><br>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.<br>#0 0x0000000002a95b5c llvm::sys::PrintStackTrace(llvm::raw_ostream&) /home/pierre/Desktop/llvm/lib/Support/Unix/Signals.inc:400:0<br>#1 0x0000000002a95edf PrintStackTraceSignalHandler(void*) /home/pierre/Desktop/llvm/lib/Support/Unix/Signals.inc:468:0<br>#2 0x0000000002a94145 llvm::sys::RunSignalHandlers() /home/pierre/Desktop/llvm/lib/Support/Signals.cpp:44:0<br>#3 0x0000000002a953a8 SignalHandler(int) /home/pierre/Desktop/llvm/lib/Support/Unix/Signals.inc:254:0<br>#4 0x00007f04b0294d10 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x10d10)<br>#5 0x00007f04af6c2267 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x35267)<br>#6 0x00007f04af6c3eca abort (/lib/x86_64-linux-gnu/libc.so.6+0x36eca)<br>#7 0x00007f04af6bb03d (/lib/x86_64-linux-gnu/libc.so.6+0x2e03d)<br>#8 0x00007f04af6bb0f2 (/lib/x86_64-linux-gnu/libc.so.6+0x2e0f2)<br>#9 0x000000000204de7e llvm::ValueEnumerator::getTypeID(llvm::Type*) const /home/pierre/Desktop/llvm/lib/Bitcode/Writer/ValueEnumerator.h:162:0<br>#10 0x00000000020454cc (anonymous namespace)::ModuleBitcodeWriter::writeInstruction(llvm::Instruction const&, unsigned int, llvm::SmallVectorImpl<unsigned int>&) /home/pierre/Desktop/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp:2509:0<br>#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<br>#12 0x000000000204be9d (anonymous namespace)::ModuleBitcodeWriter::writeModule() /home/pierre/Desktop/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp:3588:0<br>#13 0x000000000204bc70 (anonymous namespace)::ModuleBitcodeWriter::writeBlocks() /home/pierre/Desktop/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp:3537:0<br>#14 0x000000000204bc49 (anonymous namespace)::BitcodeWriter::write() /home/pierre/Desktop/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp:3532:0<br>#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<br>#16 0x0000000002037ed6 (anonymous namespace)::WriteBitcodePass::runOnModule(llvm::Module&) /home/pierre/Desktop/llvm/lib/Bitcode/Writer/BitcodeWriterPass.cpp:61:0<br>#17 0x00000000024da603 (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) /home/pierre/Desktop/llvm/lib/IR/LegacyPassManager.cpp:1603:0<br>#18 0x00000000024dad6d llvm::legacy::PassManagerImpl::run(llvm::Module&) /home/pierre/Desktop/llvm/lib/IR/LegacyPassManager.cpp:1706:0<br>#19 0x00000000024dafad llvm::legacy::PassManager::run(llvm::Module&) /home/pierre/Desktop/llvm/lib/IR/LegacyPassManager.cpp:1738:0<br>#20 0x00000000011b6d68 main /home/pierre/Desktop/llvm/tools/opt/opt.cpp:673:0<br>#21 0x00007f04af6ada40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x20a40)<br>#22 0x000000000119b2b9 _start (/home/pierre/Desktop/build/bin/opt+0x119b2b9)<br>Stack dump:<br>0.    Program arguments: opt -load ../../../build/lib/LLVMFDP.so -fdp-func type_out.bc -o type_out_func.bc <br>1.    Running pass 'Bitcode Writer' on module 'type_out.bc'.<br>Aborted (core dumped)<br><br></div>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.<br><br></div>Thanks a lot,<br></div>Pierre<br></div>