From b5740105d270a2d76da8812cafb63e4b799ada73 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Wed, 20 May 2020 15:08:36 -0700 Subject: [PATCH] [BitcodeReader] Fix DelayedShuffle handling for ConstantExpr shuffles. The indexing was messed up, so the result was completely broken. Shuffle constant exprs are rare in practice; without vscale types, constant folding generally elminates them. So sort of hard to trip over. Fixes regression from D72467. Differential Revision: https://reviews.llvm.org/D80330 --- llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 18 +++++++++++------- llvm/test/Bitcode/vscale-shuffle.ll | 10 ++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 llvm/test/Bitcode/vscale-shuffle.ll diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index a428416244f9..1ee9f7b7a50c 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -2337,6 +2337,7 @@ Error BitcodeReader::parseConstants() { uint64_t Op0Idx; uint64_t Op1Idx; uint64_t Op2Idx; + unsigned CstNo; }; std::vector DelayedShuffles; while (true) { @@ -2350,9 +2351,6 @@ Error BitcodeReader::parseConstants() { case BitstreamEntry::Error: return error("Malformed block"); case BitstreamEntry::EndBlock: - if (NextCstNo != ValueList.size()) - return error("Invalid constant reference"); - // Once all the constants have been read, go through and resolve forward // references. // @@ -2365,6 +2363,7 @@ Error BitcodeReader::parseConstants() { uint64_t Op0Idx = DelayedShuffle.Op0Idx; uint64_t Op1Idx = DelayedShuffle.Op1Idx; uint64_t Op2Idx = DelayedShuffle.Op2Idx; + uint64_t CstNo = DelayedShuffle.CstNo; Constant *Op0 = ValueList.getConstantFwdRef(Op0Idx, OpTy); Constant *Op1 = ValueList.getConstantFwdRef(Op1Idx, OpTy); Type *ShufTy = @@ -2375,9 +2374,12 @@ Error BitcodeReader::parseConstants() { SmallVector Mask; ShuffleVectorInst::getShuffleMask(Op2, Mask); Value *V = ConstantExpr::getShuffleVector(Op0, Op1, Mask); - ValueList.assignValue(V, NextCstNo, DelayedShuffle.CurFullTy); - ++NextCstNo; + ValueList.assignValue(V, CstNo, DelayedShuffle.CurFullTy); } + + if (NextCstNo != ValueList.size()) + return error("Invalid constant reference"); + ValueList.resolveConstantForwardRefs(); return Error::success(); case BitstreamEntry::Record: @@ -2733,7 +2735,8 @@ Error BitcodeReader::parseConstants() { if (Record.size() < 3 || !OpTy) return error("Invalid record"); DelayedShuffles.push_back( - {OpTy, OpTy, CurFullTy, Record[0], Record[1], Record[2]}); + {OpTy, OpTy, CurFullTy, Record[0], Record[1], Record[2], NextCstNo}); + ++NextCstNo; continue; } case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval] @@ -2743,7 +2746,8 @@ Error BitcodeReader::parseConstants() { if (Record.size() < 4 || !RTy || !OpTy) return error("Invalid record"); DelayedShuffles.push_back( - {OpTy, RTy, CurFullTy, Record[1], Record[2], Record[3]}); + {OpTy, RTy, CurFullTy, Record[1], Record[2], Record[3], NextCstNo}); + ++NextCstNo; continue; } case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] diff --git a/llvm/test/Bitcode/vscale-shuffle.ll b/llvm/test/Bitcode/vscale-shuffle.ll new file mode 100644 index 000000000000..3f36209c7aaf --- /dev/null +++ b/llvm/test/Bitcode/vscale-shuffle.ll @@ -0,0 +1,10 @@ +; RUN: llvm-as < %s | llvm-dis -disable-output +; RUN: verify-uselistorder < %s + +define void @f() { + %l = call @l( shufflevector ( insertelement ( undef, i1 true, i32 0), undef, zeroinitializer)) + %i = add undef, shufflevector ( insertelement ( undef, i64 1, i32 0), undef, zeroinitializer) + unreachable +} + +declare @l()