-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[SandboxIR] Add ExtractValueInst. #106613
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
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 |
---|---|---|
|
@@ -1261,6 +1261,106 @@ define void @foo(<2 x i8> %v1, <2 x i8> %v2) { | |
} | ||
} | ||
|
||
TEST_F(SandboxIRTest, ExtractValueInst) { | ||
parseIR(C, R"IR( | ||
define void @foo({i32, float} %agg) { | ||
%ext_simple = extractvalue {i32, float} %agg, 0 | ||
%ext_nested = extractvalue {float, {i32}} undef, 1, 0 | ||
%const1 = extractvalue {i32, float} {i32 0, float 99.0}, 0 | ||
ret void | ||
} | ||
)IR"); | ||
Function &LLVMF = *M->getFunction("foo"); | ||
sandboxir::Context Ctx(C); | ||
auto &F = *Ctx.createFunction(&LLVMF); | ||
auto *ArgAgg = F.getArg(0); | ||
auto *BB = &*F.begin(); | ||
auto It = BB->begin(); | ||
auto *ExtSimple = cast<sandboxir::ExtractValueInst>(&*It++); | ||
auto *ExtNested = cast<sandboxir::ExtractValueInst>(&*It++); | ||
auto *Const1 = cast<sandboxir::ExtractValueInst>(&*It++); | ||
auto *Ret = &*It++; | ||
|
||
EXPECT_EQ(ExtSimple->getOperand(0), ArgAgg); | ||
|
||
// create before instruction | ||
auto *NewExtBeforeRet = | ||
cast<sandboxir::ExtractValueInst>(sandboxir::ExtractValueInst::create( | ||
ArgAgg, ArrayRef<unsigned>({0}), Ret->getIterator(), Ret->getParent(), | ||
Ctx, "NewExtBeforeRet")); | ||
EXPECT_EQ(NewExtBeforeRet->getNextNode(), Ret); | ||
#ifndef NDEBUG | ||
EXPECT_EQ(NewExtBeforeRet->getName(), "NewExtBeforeRet"); | ||
#endif // NDEBUG | ||
|
||
// create at end of BB | ||
auto *NewExtAtEnd = | ||
cast<sandboxir::ExtractValueInst>(sandboxir::ExtractValueInst::create( | ||
ArgAgg, ArrayRef<unsigned>({0}), BB->end(), BB, Ctx, "NewExtAtEnd")); | ||
EXPECT_EQ(NewExtAtEnd->getPrevNode(), Ret); | ||
#ifndef NDEBUG | ||
EXPECT_EQ(NewExtAtEnd->getName(), "NewExtAtEnd"); | ||
#endif // NDEBUG | ||
|
||
// Test the path that creates a folded constant. | ||
auto *ShouldBeConstant = sandboxir::ExtractValueInst::create( | ||
Const1->getOperand(0), ArrayRef<unsigned>({0}), BB->end(), BB, Ctx); | ||
EXPECT_TRUE(isa<sandboxir::Constant>(ShouldBeConstant)); | ||
|
||
auto *Zero = sandboxir::ConstantInt::get(Type::getInt32Ty(C), 0, Ctx); | ||
EXPECT_EQ(ShouldBeConstant, Zero); | ||
|
||
// getIndexedType | ||
Type *AggType = ExtNested->getAggregateOperand()->getType(); | ||
EXPECT_EQ(sandboxir::ExtractValueInst::getIndexedType( | ||
AggType, ArrayRef<unsigned>({1, 0})), | ||
llvm::ExtractValueInst::getIndexedType(AggType, | ||
ArrayRef<unsigned>({1, 0}))); | ||
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. Could you have two checks for 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. Added a simple test for the nullptr case. Please take another look. |
||
|
||
EXPECT_EQ(sandboxir::ExtractValueInst::getIndexedType( | ||
AggType, ArrayRef<unsigned>({2})), | ||
nullptr); | ||
|
||
// idx_begin / idx_end | ||
{ | ||
SmallVector<int, 2> IndicesSimple(ExtSimple->idx_begin(), | ||
ExtSimple->idx_end()); | ||
EXPECT_THAT(IndicesSimple, testing::ElementsAre(0u)); | ||
|
||
SmallVector<int, 2> IndicesNested(ExtNested->idx_begin(), | ||
ExtNested->idx_end()); | ||
EXPECT_THAT(IndicesNested, testing::ElementsAre(1u, 0u)); | ||
} | ||
|
||
// indices | ||
{ | ||
SmallVector<int, 2> IndicesSimple(ExtSimple->indices()); | ||
EXPECT_THAT(IndicesSimple, testing::ElementsAre(0u)); | ||
|
||
SmallVector<int, 2> IndicesNested(ExtNested->indices()); | ||
EXPECT_THAT(IndicesNested, testing::ElementsAre(1u, 0u)); | ||
} | ||
|
||
// getAggregateOperand | ||
EXPECT_EQ(ExtSimple->getAggregateOperand(), ArgAgg); | ||
const auto *ConstExtSimple = ExtSimple; | ||
EXPECT_EQ(ConstExtSimple->getAggregateOperand(), ArgAgg); | ||
|
||
// getAggregateOperandIndex | ||
EXPECT_EQ(sandboxir::ExtractValueInst::getAggregateOperandIndex(), | ||
llvm::ExtractValueInst::getAggregateOperandIndex()); | ||
|
||
// getIndices | ||
EXPECT_EQ(ExtSimple->getIndices().size(), 1u); | ||
EXPECT_EQ(ExtSimple->getIndices()[0], 0u); | ||
|
||
// getNumIndices | ||
EXPECT_EQ(ExtSimple->getNumIndices(), 1u); | ||
|
||
// hasIndices | ||
EXPECT_EQ(ExtSimple->hasIndices(), true); | ||
} | ||
|
||
TEST_F(SandboxIRTest, InsertValueInst) { | ||
parseIR(C, R"IR( | ||
define void @foo({i32, float} %agg, i32 %i) { | ||
|
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.
nit: It is not straight forward what this function does, so I would copy LLVM IR's comments.
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.
Done.