[llvm] d452d5e - [Bitcode] Fix constexpr autoupgrade for arrays and structs

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 7 03:47:36 PDT 2022


Author: Nikita Popov
Date: 2022-09-07T12:47:21+02:00
New Revision: d452d5e2de70a180e5af26dbda40cfb8b2506590

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

LOG: [Bitcode] Fix constexpr autoupgrade for arrays and structs

While vectors use insertelement, structs and arrays should use
insertvalue.

Added: 
    

Modified: 
    llvm/lib/Bitcode/Reader/BitcodeReader.cpp
    llvm/test/Bitcode/constexpr-to-instr.ll
    llvm/test/Bitcode/constexpr-to-instr.ll.bc

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 321f2b0d64208..049fc0f2f0e81 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -1577,8 +1577,6 @@ Expected<Value *> BitcodeReader::materializeValue(unsigned StartValID,
         I->setIsExact();
     } else {
       switch (BC->Opcode) {
-      case BitcodeConstant::ConstantStructOpcode:
-      case BitcodeConstant::ConstantArrayOpcode:
       case BitcodeConstant::ConstantVectorOpcode: {
         Type *IdxTy = Type::getInt32Ty(BC->getContext());
         Value *V = PoisonValue::get(BC->getType());
@@ -1590,6 +1588,15 @@ Expected<Value *> BitcodeReader::materializeValue(unsigned StartValID,
         I = cast<Instruction>(V);
         break;
       }
+      case BitcodeConstant::ConstantStructOpcode:
+      case BitcodeConstant::ConstantArrayOpcode: {
+        Value *V = PoisonValue::get(BC->getType());
+        for (auto Pair : enumerate(Ops))
+          V = InsertValueInst::Create(V, Pair.value(), Pair.index(),
+                                      "constexpr.ins", InsertBB);
+        I = cast<Instruction>(V);
+        break;
+      }
       case Instruction::ICmp:
       case Instruction::FCmp:
         I = CmpInst::Create((Instruction::OtherOps)BC->Opcode,

diff  --git a/llvm/test/Bitcode/constexpr-to-instr.ll b/llvm/test/Bitcode/constexpr-to-instr.ll
index 07accafb83826..c099273de810f 100644
--- a/llvm/test/Bitcode/constexpr-to-instr.ll
+++ b/llvm/test/Bitcode/constexpr-to-instr.ll
@@ -70,6 +70,24 @@ define <3 x i64> @test_vector() {
   ret <3 x i64> <i64 5, i64 ptrtoint (ptr @g to i64), i64 7>
 }
 
+define [3 x i64] @test_array() {
+; CHECK-LABEL: define [3 x i64] @test_array() {
+; CHECK-NEXT: %constexpr = ptrtoint ptr @g to i64
+; CHECK-NEXT: %constexpr.ins = insertvalue [3 x i64] poison, i64 5, 0
+; CHECK-NEXT: %constexpr.ins1 = insertvalue [3 x i64] %constexpr.ins, i64 %constexpr, 1
+; CHECK-NEXT: %constexpr.ins2 = insertvalue [3 x i64] %constexpr.ins1, i64 7, 2
+  ret [3 x i64] [i64 5, i64 ptrtoint (ptr @g to i64), i64 7]
+}
+
+define { i64, i64, i64 } @test_struct() {
+; CHECK-LABEL: define { i64, i64, i64 } @test_struct() {
+; CHECK-NEXT: %constexpr = ptrtoint ptr @g to i64
+; CHECK-NEXT: %constexpr.ins = insertvalue { i64, i64, i64 } poison, i64 5, 0
+; CHECK-NEXT: %constexpr.ins1 = insertvalue { i64, i64, i64 } %constexpr.ins, i64 %constexpr, 1
+; CHECK-NEXT: %constexpr.ins2 = insertvalue { i64, i64, i64 } %constexpr.ins1, i64 7, 2
+  ret { i64, i64, i64 } {i64 5, i64 ptrtoint (ptr @g to i64), i64 7}
+}
+
 define i64 @test_reused_expr() {
 ; CHECK-LABEL: define i64 @test_reused_expr() {
 ; CHECK-NEXT: %constexpr = ptrtoint ptr @g to i64

diff  --git a/llvm/test/Bitcode/constexpr-to-instr.ll.bc b/llvm/test/Bitcode/constexpr-to-instr.ll.bc
index 7fae9c05fbd75..c77968ce9207a 100644
Binary files a/llvm/test/Bitcode/constexpr-to-instr.ll.bc and b/llvm/test/Bitcode/constexpr-to-instr.ll.bc 
diff er


        


More information about the llvm-commits mailing list