-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[SandboxIR] Implement UndefValue #107628
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SandboxIR] Implement UndefValue #107628
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -642,6 +642,7 @@ define void @foo() { | |
// Check classof() and creation. | ||
auto *Poison = cast<sandboxir::PoisonValue>(I0->getOperand(0)); | ||
EXPECT_EQ(Poison->getType(), Int32Ty); | ||
EXPECT_TRUE(isa<sandboxir::UndefValue>(Poison)); // Poison is Undef | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same problem? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm what do yo mean? This just checks that a Poison is also an Undef. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is correct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM, Poison value is an undef. |
||
// Check get(). | ||
auto *NewPoison = sandboxir::PoisonValue::get(Int32Ty); | ||
EXPECT_EQ(NewPoison, Poison); | ||
|
@@ -670,6 +671,64 @@ define void @foo() { | |
sandboxir::PoisonValue::get(Int8Ty)); | ||
} | ||
|
||
TEST_F(SandboxIRTest, UndefValue) { | ||
parseIR(C, R"IR( | ||
define void @foo() { | ||
%i0 = add i32 undef, undef | ||
%i1 = add <2 x i32> undef, undef | ||
%i2 = extractvalue {i32, i8} undef, 0 | ||
ret void | ||
} | ||
)IR"); | ||
Function &LLVMF = *M->getFunction("foo"); | ||
sandboxir::Context Ctx(C); | ||
|
||
auto &F = *Ctx.createFunction(&LLVMF); | ||
auto &BB = *F.begin(); | ||
auto It = BB.begin(); | ||
auto *I0 = &*It++; | ||
auto *I1 = &*It++; | ||
auto *I2 = &*It++; | ||
auto *Int32Ty = sandboxir::Type::getInt32Ty(Ctx); | ||
auto *Int8Ty = sandboxir::Type::getInt8Ty(Ctx); | ||
auto *Zero32 = sandboxir::ConstantInt::get(Int32Ty, 0u); | ||
auto *One32 = sandboxir::ConstantInt::get(Int32Ty, 1u); | ||
|
||
// Check classof() and creation. | ||
auto *Undef = cast<sandboxir::UndefValue>(I0->getOperand(0)); | ||
EXPECT_EQ(Undef->getType(), Int32Ty); | ||
EXPECT_FALSE(isa<sandboxir::PoisonValue>(Undef)); // Undef is not Poison | ||
// Check get(). | ||
auto *NewUndef = sandboxir::UndefValue::get(Int32Ty); | ||
EXPECT_EQ(NewUndef, Undef); | ||
auto *NewUndef2 = | ||
sandboxir::UndefValue::get(sandboxir::PointerType::get(Ctx, 0u)); | ||
EXPECT_NE(NewUndef2, Undef); | ||
// Check getSequentialElement(). | ||
auto *UndefVector = cast<sandboxir::UndefValue>(I1->getOperand(0)); | ||
auto *SeqElm = UndefVector->getSequentialElement(); | ||
EXPECT_EQ(SeqElm->getType(), Int32Ty); | ||
// Check getStructElement(). | ||
auto *UndefStruct = cast<sandboxir::UndefValue>(I2->getOperand(0)); | ||
auto *StrElm0 = UndefStruct->getStructElement(0); | ||
auto *StrElm1 = UndefStruct->getStructElement(1); | ||
EXPECT_EQ(StrElm0->getType(), Int32Ty); | ||
EXPECT_EQ(StrElm1->getType(), Int8Ty); | ||
// Check getElementValue(Constant) | ||
EXPECT_EQ(UndefStruct->getElementValue(Zero32), | ||
sandboxir::UndefValue::get(Int32Ty)); | ||
EXPECT_EQ(UndefStruct->getElementValue(One32), | ||
sandboxir::UndefValue::get(Int8Ty)); | ||
// Check getElementValue(unsigned) | ||
EXPECT_EQ(UndefStruct->getElementValue(0u), | ||
sandboxir::UndefValue::get(Int32Ty)); | ||
EXPECT_EQ(UndefStruct->getElementValue(1u), | ||
sandboxir::UndefValue::get(Int8Ty)); | ||
// Check getNumElements(). | ||
EXPECT_EQ(UndefVector->getNumElements(), 2u); | ||
EXPECT_EQ(UndefStruct->getNumElements(), 2u); | ||
} | ||
|
||
TEST_F(SandboxIRTest, Use) { | ||
parseIR(C, R"IR( | ||
define i32 @foo(i32 %v0, i32 %v1) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isa<PoisonValue>(Undef) is true?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm did you mean
return From->getSubclassID() == ClassID::UndefValue || isa<PoisonValue(From)
?I would not prefer this because because it hides the fact that PoisonValue corresponds to just a single
ClassID::PoisonValue
.Hmm or did you mean checking the llvm-side with something like
isa<PoisonValue>(From->Val)
?The reason why I don't like this is that it looks like it is slightly slower: Because you need to get
From->Val
and then runclassof()
which does the comparisonID == UndefValue || ID == PoisonValue
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You modelled it correctly. Poison inherits from Undef, but an undef value/instance should not be poison.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes,
isa<>
works because it takes care of upcasting. Butclassof()
would be wrong, it should return true ifFrom
is aPoisonValue
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the current classof UndefValue is wrong. It should only test for ClassId::UndefValue.
should/must be false.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
classof() should return true when
From
is either the current class type (that isUndefValue
) or its sub-classes (that isPoisonValue
). This is implemented the same way as inllvm/IR/Constants.h
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are already testing that:
EXPECT_FALSE(isa<sandboxir::PoisonValue>(Val: Undef)); // Undef is not Poison
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM,