diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h --- a/llvm/include/llvm/IR/IRBuilder.h +++ b/llvm/include/llvm/IR/IRBuilder.h @@ -1568,11 +1568,18 @@ AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace, Value *ArraySize = nullptr, const Twine &Name = "") { - const DataLayout &DL = BB->getModule()->getDataLayout(); - Align AllocaAlign = DL.getPrefTypeAlign(Ty); + Module *Mod = BB->getModule(); + MaybeAlign AllocaAlign; + if (Mod) { + const DataLayout &DL = Mod->getDataLayout(); + AllocaAlign = DL.getPrefTypeAlign(Ty); + } return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name); } + /// Only valid for builders created with blocks that have already been + /// inserted into a function. It otherwise necessarily crashes when trying to + /// retrieve the module to get the alignment and address space. AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr, const Twine &Name = "") { const DataLayout &DL = BB->getModule()->getDataLayout(); @@ -1648,8 +1655,11 @@ LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, bool isVolatile, const Twine &Name = "") { if (!Align) { - const DataLayout &DL = BB->getModule()->getDataLayout(); - Align = DL.getABITypeAlign(Ty); + Module *Mod = BB->getModule(); + if (Mod) { + const DataLayout &DL = Mod->getDataLayout(); + Align = DL.getABITypeAlign(Ty); + } } return Insert(new LoadInst(Ty, Ptr, Twine(), isVolatile, *Align), Name); } @@ -1706,8 +1716,11 @@ StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align, bool isVolatile = false) { if (!Align) { - const DataLayout &DL = BB->getModule()->getDataLayout(); - Align = DL.getABITypeAlign(Val->getType()); + Module *Mod = BB->getModule(); + if (Mod) { + const DataLayout &DL = Mod->getDataLayout(); + Align = DL.getABITypeAlign(Val->getType()); + } } return Insert(new StoreInst(Val, Ptr, isVolatile, Align)); }