[llvm] b190108 - [Bitcode] Encode alloca address space

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 11 07:08:48 PST 2022


Author: Nikita Popov
Date: 2022-03-11T16:08:38+01:00
New Revision: b190108693066d94181014018fbc96624453dbe2

URL: https://github.com/llvm/llvm-project/commit/b190108693066d94181014018fbc96624453dbe2
DIFF: https://github.com/llvm/llvm-project/commit/b190108693066d94181014018fbc96624453dbe2.diff

LOG: [Bitcode] Encode alloca address space

Since D101045, allocas are no longer required to be part of the
default alloca address space. There may be allocas in multiple
different address spaces. However, the bitcode reader would
simply assume the default alloca address space, resulting in
either an error or incorrect IR.

Add an optional record for allocas which encodes the address
space.

Added: 
    llvm/test/Bitcode/alloca-addrspace.ll

Modified: 
    llvm/lib/Bitcode/Reader/BitcodeReader.cpp
    llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 6dd2fdb0f1ed0..f6f0c924cf222 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -5294,7 +5294,7 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
     }
 
     case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
-      if (Record.size() != 4)
+      if (Record.size() != 4 && Record.size() != 5)
         return error("Invalid record");
       using APV = AllocaPackedValues;
       const uint64_t Rec = Record[3];
@@ -5321,9 +5321,8 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
       if (!Ty || !Size)
         return error("Invalid record");
 
-      // FIXME: Make this an optional field.
       const DataLayout &DL = TheModule->getDataLayout();
-      unsigned AS = DL.getAllocaAddrSpace();
+      unsigned AS = Record.size() == 5 ? Record[4] : DL.getAllocaAddrSpace();
 
       SmallPtrSet<Type *, 4> Visited;
       if (!Align && !Ty->isSized(&Visited))

diff  --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index c847118a352b1..ec109942e7218 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -3072,6 +3072,10 @@ void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
     Bitfield::set<APV::ExplicitType>(Record, true);
     Bitfield::set<APV::SwiftError>(Record, AI.isSwiftError());
     Vals.push_back(Record);
+
+    unsigned AS = AI.getAddressSpace();
+    if (AS != M.getDataLayout().getAllocaAddrSpace())
+      Vals.push_back(AS);
     break;
   }
 

diff  --git a/llvm/test/Bitcode/alloca-addrspace.ll b/llvm/test/Bitcode/alloca-addrspace.ll
new file mode 100644
index 0000000000000..48af7ab5015df
--- /dev/null
+++ b/llvm/test/Bitcode/alloca-addrspace.ll
@@ -0,0 +1,18 @@
+; RUN: llvm-as < %s | llvm-dis | FileCheck %s
+
+target datalayout = "A2"
+
+; CHECK-LABEL: define i8 addrspace(2)* @alloca_addrspace_2() {
+; CHECK: %alloca = alloca i8, align 1, addrspace(2)
+define i8 addrspace(2)* @alloca_addrspace_2() {
+  %alloca = alloca i8, addrspace(2)
+  ret i8 addrspace(2)* %alloca
+}
+
+; CHECK-LABEL: define i8 addrspace(5)* @alloca_addrspace_5() {
+; CHECK: %alloca = alloca i8, align 1, addrspace(5)
+define i8 addrspace(5)* @alloca_addrspace_5() {
+  %alloca = alloca i8, addrspace(5)
+  ret i8 addrspace(5)* %alloca
+}
+


        


More information about the llvm-commits mailing list