Skip to content

Commit

Permalink
[IR] Change the default value of InstertElement to poison (1/4)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hyeongyukim committed Sep 28, 2021
1 parent 8bacfb9 commit 86bf234
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
10 changes: 10 additions & 0 deletions llvm/include/llvm/IR/IRBuilder.h
Expand Up @@ -2452,6 +2452,16 @@ class IRBuilderBase {
return CreateExtractElement(Vec, getInt64(Idx), Name);
}

Value *CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx,
const Twine &Name = "") {
return CreateInsertElement(PoisonValue::get(VecTy), NewElt, Idx, Name);
}

Value *CreateInsertElement(Type *VecTy, Value *NewElt, uint64_t Idx,
const Twine &Name = "") {
return CreateInsertElement(PoisonValue::get(VecTy), NewElt, Idx, Name);
}

Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
const Twine &Name = "") {
if (auto *VC = dyn_cast<Constant>(Vec))
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/IR/AutoUpgrade.cpp
Expand Up @@ -2417,7 +2417,7 @@ void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
EltTy->getPointerTo());
Value *Load = Builder.CreateLoad(EltTy, Cast);
Type *I32Ty = Type::getInt32Ty(C);
Rep = UndefValue::get(VecTy);
Rep = PoisonValue::get(VecTy);
for (unsigned I = 0; I < EltNum; ++I)
Rep = Builder.CreateInsertElement(Rep, Load,
ConstantInt::get(I32Ty, I));
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/NVPTX/NVPTXGenericToNVVM.cpp
Expand Up @@ -212,7 +212,7 @@ Value *GenericToNVVM::remapConstantVectorOrConstantAggregate(
// If any of the elements has been modified, construct the equivalent
// vector or aggregate value with a set instructions and the converted
// elements.
Value *NewValue = UndefValue::get(C->getType());
Value *NewValue = PoisonValue::get(C->getType());
if (isa<ConstantVector>(C)) {
for (unsigned i = 0; i < NumOperands; ++i) {
Value *Idx = ConstantInt::get(Type::getInt32Ty(M->getContext()), i);
Expand Down
3 changes: 1 addition & 2 deletions llvm/unittests/IR/IRBuilderTest.cpp
Expand Up @@ -878,8 +878,7 @@ TEST_F(IRBuilderTest, InsertExtractElement) {
auto VecTy = FixedVectorType::get(Builder.getInt64Ty(), 4);
auto Elt1 = Builder.getInt64(-1);
auto Elt2 = Builder.getInt64(-2);
Value *Vec = UndefValue::get(VecTy);
Vec = Builder.CreateInsertElement(Vec, Elt1, Builder.getInt8(1));
Value *Vec = Builder.CreateInsertElement(VecTy, Elt1, Builder.getInt8(1));
Vec = Builder.CreateInsertElement(Vec, Elt2, 2);
auto X1 = Builder.CreateExtractElement(Vec, 1);
auto X2 = Builder.CreateExtractElement(Vec, Builder.getInt32(2));
Expand Down
3 changes: 1 addition & 2 deletions llvm/unittests/IR/PatternMatch.cpp
Expand Up @@ -940,8 +940,7 @@ TEST_F(PatternMatchTest, VectorOps) {
VecElemIdxs.push_back(ConstantInt::get(i32, 2));
auto *IdxVec = ConstantVector::get(VecElemIdxs);

Value *UndefVec = UndefValue::get(VecTy);
Value *VI1 = IRB.CreateInsertElement(UndefVec, IRB.getInt8(1), (uint64_t)0);
Value *VI1 = IRB.CreateInsertElement(VecTy, IRB.getInt8(1), (uint64_t)0);
Value *VI2 = IRB.CreateInsertElement(VI1, Val2, Val);
Value *VI3 = IRB.CreateInsertElement(VI1, Val2, (uint64_t)1);
Value *VI4 = IRB.CreateInsertElement(VI1, IRB.getInt8(2), Val);
Expand Down

0 comments on commit 86bf234

Please sign in to comment.