[PATCH] D79131: [IRBuilder] Don't crash when creating alloca/load/store in unparented block.

Mark Lacey via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 29 15:06:29 PDT 2020


rudkx created this revision.
rudkx added a reviewer: efriedma.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Previously it was possible to create allocas/loads/stores in blocks
that had not previously been inserted in the function. A recent
change (https://reviews.llvm.org/D77984) uses getModule() on
the block to look up the DataLayout and automatically compute
an alignment in the cases where it isn't passed in. That results
in a crash in cases where the block hasn't already been inserted
into a function.

Instead, we'll now check that we successfully retrieved the
module before dereferencing it, and if we aren't able to retrieve
it we'll fall back on having no explicit alignment.

In one implementation of CreateAlloca we were also looking up
the default address space from the DataLayout. We don't have
have a way to avoid looking up the Module in order to get the
DataLayout, and don't have a way to otherwise default the address
space, so that implementation will still crash if called on
a block that hasn't yet been inserted into a function.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D79131

Files:
  llvm/include/llvm/IR/IRBuilder.h


Index: llvm/include/llvm/IR/IRBuilder.h
===================================================================
--- llvm/include/llvm/IR/IRBuilder.h
+++ 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));
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79131.261047.patch
Type: text/x-patch
Size: 2218 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200429/b1b8a3ce/attachment.bin>


More information about the llvm-commits mailing list