Skip to content

Commit 749ddd2

Browse files
author
guopeilin
committed
[BitcodeReader] Delay select until all constants resolved
Like the shuffle, we should treat the select delayed so that all constants can be resolved. Reviewed By: efriedma Differential Revision: https://reviews.llvm.org/D109053
1 parent df975e4 commit 749ddd2

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,6 +2355,15 @@ Error BitcodeReader::parseConstants() {
23552355
unsigned CstNo;
23562356
};
23572357
std::vector<DelayedShufTy> DelayedShuffles;
2358+
struct DelayedSelTy {
2359+
Type *OpTy;
2360+
uint64_t Op0Idx;
2361+
uint64_t Op1Idx;
2362+
uint64_t Op2Idx;
2363+
unsigned CstNo;
2364+
};
2365+
std::vector<DelayedSelTy> DelayedSelectors;
2366+
23582367
while (true) {
23592368
Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
23602369
if (!MaybeEntry)
@@ -2391,6 +2400,27 @@ Error BitcodeReader::parseConstants() {
23912400
Value *V = ConstantExpr::getShuffleVector(Op0, Op1, Mask);
23922401
ValueList.assignValue(V, CstNo);
23932402
}
2403+
for (auto &DelayedSelector : DelayedSelectors) {
2404+
Type *OpTy = DelayedSelector.OpTy;
2405+
Type *SelectorTy = Type::getInt1Ty(Context);
2406+
uint64_t Op0Idx = DelayedSelector.Op0Idx;
2407+
uint64_t Op1Idx = DelayedSelector.Op1Idx;
2408+
uint64_t Op2Idx = DelayedSelector.Op2Idx;
2409+
uint64_t CstNo = DelayedSelector.CstNo;
2410+
Constant *Op1 = ValueList.getConstantFwdRef(Op1Idx, OpTy);
2411+
Constant *Op2 = ValueList.getConstantFwdRef(Op2Idx, OpTy);
2412+
// The selector might be an i1 or an <n x i1>
2413+
// Get the type from the ValueList before getting a forward ref.
2414+
if (VectorType *VTy = dyn_cast<VectorType>(OpTy)) {
2415+
Value *V = ValueList[Op0Idx];
2416+
assert(V);
2417+
if (SelectorTy != V->getType())
2418+
SelectorTy = VectorType::get(SelectorTy, VTy->getElementCount());
2419+
}
2420+
Constant *Op0 = ValueList.getConstantFwdRef(Op0Idx, SelectorTy);
2421+
Value *V = ConstantExpr::getSelect(Op0, Op1, Op2);
2422+
ValueList.assignValue(V, CstNo);
2423+
}
23942424

23952425
if (NextCstNo != ValueList.size())
23962426
return error("Invalid constant reference");
@@ -2687,21 +2717,11 @@ Error BitcodeReader::parseConstants() {
26872717
if (Record.size() < 3)
26882718
return error("Invalid record");
26892719

2690-
Type *SelectorTy = Type::getInt1Ty(Context);
2691-
2692-
// The selector might be an i1, an <n x i1>, or a <vscale x n x i1>
2693-
// Get the type from the ValueList before getting a forward ref.
2694-
if (VectorType *VTy = dyn_cast<VectorType>(CurTy))
2695-
if (Value *V = ValueList[Record[0]])
2696-
if (SelectorTy != V->getType())
2697-
SelectorTy = VectorType::get(SelectorTy,
2698-
VTy->getElementCount());
2699-
2700-
V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
2701-
SelectorTy),
2702-
ValueList.getConstantFwdRef(Record[1],CurTy),
2703-
ValueList.getConstantFwdRef(Record[2],CurTy));
2704-
break;
2720+
DelayedSelectors.push_back(
2721+
{CurTy, Record[0], Record[1], Record[2], NextCstNo});
2722+
(void)ValueList.getConstantFwdRef(NextCstNo, CurTy);
2723+
++NextCstNo;
2724+
continue;
27052725
}
27062726
case bitc::CST_CODE_CE_EXTRACTELT
27072727
: { // CE_EXTRACTELT: [opty, opval, opty, opval]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
; RUN: llvm-as < %s -preserve-bc-uselistorder=false | llvm-dis -disable-output
2+
@a = external dso_local global i32, align 4
3+
@c = external dso_local global [3 x i32], align 4
4+
@b = external dso_local local_unnamed_addr global i32, align 4
5+
6+
define dso_local i32 @main() local_unnamed_addr {
7+
middle.block:
8+
br label %for.cond.for.end_crit_edge
9+
10+
for.cond.for.end_crit_edge: ; preds = %middle.block
11+
store i32 extractelement (<4 x i32> select (<4 x i1> select (<4 x i1> <i1 icmp sle (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0), i1 icmp sle (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0), i1 icmp sle (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0), i1 icmp sle (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0)>, <4 x i1> <i1 icmp sge (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0), i1 icmp sge (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0), i1 icmp sge (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0), i1 icmp sge (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0)>, <4 x i1> zeroinitializer), <4 x i32> zeroinitializer, <4 x i32> select (<4 x i1> select (<4 x i1> <i1 icmp sgt (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0), i1 icmp sgt (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0), i1 icmp sgt (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0), i1 icmp sgt (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 0)>, <4 x i1> <i1 icmp eq (i32 sdiv (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 3), i32 0), i1 icmp eq (i32 sdiv (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 3), i32 0), i1 icmp eq (i32 sdiv (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 3), i32 0), i1 icmp eq (i32 sdiv (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 3), i32 0)>, <4 x i1> zeroinitializer), <4 x i32> <i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32)>, <4 x i32> <i32 lshr (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 31), i32 lshr (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 31), i32 lshr (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 31), i32 lshr (i32 zext (i1 icmp eq (i32* getelementptr inbounds ([3 x i32], [3 x i32]* @c, i64 0, i64 2), i32* @a) to i32), i32 31)>)), i32 3), i32* @b, align 4
12+
ret i32 undef
13+
}

0 commit comments

Comments
 (0)