Skip to content

Commit 86bf234

Browse files
committed
[IR] Change the default value of InstertElement to poison (1/4)
This patch is for fixing potential insertElement-related bugs like D93818. ``` V = UndefValue::get(VecTy); for(...) V = Builder.CreateInsertElementy(V, Elt, Idx); => V = PoisonValue::get(VecTy); for(...) V = Builder.CreateInsertElementy(V, Elt, Idx); ``` Like above, this patch changes the placeholder V to poison. The patch will be separated into several commits. Reviewed By: aqjune Differential Revision: https://reviews.llvm.org/D110311
1 parent 8bacfb9 commit 86bf234

File tree

5 files changed

+14
-6
lines changed

5 files changed

+14
-6
lines changed

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,6 +2452,16 @@ class IRBuilderBase {
24522452
return CreateExtractElement(Vec, getInt64(Idx), Name);
24532453
}
24542454

2455+
Value *CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx,
2456+
const Twine &Name = "") {
2457+
return CreateInsertElement(PoisonValue::get(VecTy), NewElt, Idx, Name);
2458+
}
2459+
2460+
Value *CreateInsertElement(Type *VecTy, Value *NewElt, uint64_t Idx,
2461+
const Twine &Name = "") {
2462+
return CreateInsertElement(PoisonValue::get(VecTy), NewElt, Idx, Name);
2463+
}
2464+
24552465
Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
24562466
const Twine &Name = "") {
24572467
if (auto *VC = dyn_cast<Constant>(Vec))

llvm/lib/IR/AutoUpgrade.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2417,7 +2417,7 @@ void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
24172417
EltTy->getPointerTo());
24182418
Value *Load = Builder.CreateLoad(EltTy, Cast);
24192419
Type *I32Ty = Type::getInt32Ty(C);
2420-
Rep = UndefValue::get(VecTy);
2420+
Rep = PoisonValue::get(VecTy);
24212421
for (unsigned I = 0; I < EltNum; ++I)
24222422
Rep = Builder.CreateInsertElement(Rep, Load,
24232423
ConstantInt::get(I32Ty, I));

llvm/lib/Target/NVPTX/NVPTXGenericToNVVM.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ Value *GenericToNVVM::remapConstantVectorOrConstantAggregate(
212212
// If any of the elements has been modified, construct the equivalent
213213
// vector or aggregate value with a set instructions and the converted
214214
// elements.
215-
Value *NewValue = UndefValue::get(C->getType());
215+
Value *NewValue = PoisonValue::get(C->getType());
216216
if (isa<ConstantVector>(C)) {
217217
for (unsigned i = 0; i < NumOperands; ++i) {
218218
Value *Idx = ConstantInt::get(Type::getInt32Ty(M->getContext()), i);

llvm/unittests/IR/IRBuilderTest.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,8 +878,7 @@ TEST_F(IRBuilderTest, InsertExtractElement) {
878878
auto VecTy = FixedVectorType::get(Builder.getInt64Ty(), 4);
879879
auto Elt1 = Builder.getInt64(-1);
880880
auto Elt2 = Builder.getInt64(-2);
881-
Value *Vec = UndefValue::get(VecTy);
882-
Vec = Builder.CreateInsertElement(Vec, Elt1, Builder.getInt8(1));
881+
Value *Vec = Builder.CreateInsertElement(VecTy, Elt1, Builder.getInt8(1));
883882
Vec = Builder.CreateInsertElement(Vec, Elt2, 2);
884883
auto X1 = Builder.CreateExtractElement(Vec, 1);
885884
auto X2 = Builder.CreateExtractElement(Vec, Builder.getInt32(2));

llvm/unittests/IR/PatternMatch.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -940,8 +940,7 @@ TEST_F(PatternMatchTest, VectorOps) {
940940
VecElemIdxs.push_back(ConstantInt::get(i32, 2));
941941
auto *IdxVec = ConstantVector::get(VecElemIdxs);
942942

943-
Value *UndefVec = UndefValue::get(VecTy);
944-
Value *VI1 = IRB.CreateInsertElement(UndefVec, IRB.getInt8(1), (uint64_t)0);
943+
Value *VI1 = IRB.CreateInsertElement(VecTy, IRB.getInt8(1), (uint64_t)0);
945944
Value *VI2 = IRB.CreateInsertElement(VI1, Val2, Val);
946945
Value *VI3 = IRB.CreateInsertElement(VI1, Val2, (uint64_t)1);
947946
Value *VI4 = IRB.CreateInsertElement(VI1, IRB.getInt8(2), Val);

0 commit comments

Comments
 (0)