-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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 the InsertElementInst class #102404
Changes from 5 commits
9e5a404
870e1e5
c77e88c
1aa4ad4
e1757cc
fb857ed
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 |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
#include "llvm/SandboxIR/SandboxIR.h" | ||
#include "llvm/AsmParser/Parser.h" | ||
#include "llvm/IR/BasicBlock.h" | ||
#include "llvm/IR/Constants.h" | ||
#include "llvm/IR/DataLayout.h" | ||
#include "llvm/IR/Function.h" | ||
#include "llvm/IR/Instruction.h" | ||
|
@@ -630,6 +631,55 @@ define void @foo(i1 %c0, i8 %v0, i8 %v1, i1 %c1) { | |
} | ||
} | ||
|
||
TEST_F(SandboxIRTest, InsertElementInst) { | ||
parseIR(C, R"IR( | ||
define void @foo(i8 %v0, i8 %v1, <2 x i8> %vec) { | ||
%ins0 = insertelement <2 x i8> poison, i8 %v0, i32 0 | ||
%ins1 = insertelement <2 x i8> %ins0, i8 %v1, i32 1 | ||
ret void | ||
} | ||
)IR"); | ||
Function &LLVMF = *M->getFunction("foo"); | ||
sandboxir::Context Ctx(C); | ||
auto &F = *Ctx.createFunction(&LLVMF); | ||
auto *Arg0 = F.getArg(0); | ||
auto *Arg1 = F.getArg(1); | ||
auto *ArgVec = F.getArg(2); | ||
auto *BB = &*F.begin(); | ||
auto It = BB->begin(); | ||
auto *Ins0 = cast<sandboxir::InsertElementInst>(&*It++); | ||
auto *Ins1 = cast<sandboxir::InsertElementInst>(&*It++); | ||
auto *Ret = &*It++; | ||
|
||
EXPECT_EQ(Ins0->getOpcode(), sandboxir::Instruction::Opcode::InsertElement); | ||
EXPECT_EQ(Ins0->getOperand(1), Arg0); | ||
EXPECT_EQ(Ins1->getOperand(1), Arg1); | ||
EXPECT_EQ(Ins1->getOperand(0), Ins0); | ||
auto *Poison = Ins0->getOperand(0); | ||
auto *Idx = Ins0->getOperand(2); | ||
auto *NewI1 = | ||
cast<sandboxir::InsertElementInst>(sandboxir::InsertElementInst::create( | ||
Poison, Arg0, Idx, Ret, Ctx, "NewIns1")); | ||
EXPECT_EQ(NewI1->getOperand(0), Poison); | ||
EXPECT_EQ(NewI1->getNextNode(), Ret); | ||
|
||
auto *NewI2 = | ||
cast<sandboxir::InsertElementInst>(sandboxir::InsertElementInst::create( | ||
Poison, Arg0, Idx, BB, Ctx, "NewIns2")); | ||
EXPECT_EQ(NewI2->getPrevNode(), Ret); | ||
|
||
auto *LLVMArg0 = LLVMF.getArg(0); | ||
auto *LLVMArgVec = LLVMF.getArg(2); | ||
auto *Zero = sandboxir::Constant::createInt(Type::getInt8Ty(C), 0, Ctx); | ||
auto *LLVMZero = llvm::ConstantInt::get(Type::getInt8Ty(C), 0); | ||
EXPECT_EQ( | ||
sandboxir::InsertElementInst::isValidOperands(ArgVec, Arg0, Zero), | ||
llvm::InsertElementInst::isValidOperands(LLVMArgVec, LLVMArg0, LLVMZero)); | ||
EXPECT_EQ( | ||
sandboxir::InsertElementInst::isValidOperands(Arg0, ArgVec, Zero), | ||
llvm::InsertElementInst::isValidOperands(LLVMArg0, LLVMArgVec, LLVMZero)); | ||
} | ||
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. For the Then we would need to get both the sandboxir and the llvm arguments:
Then:
Finally I would add a check like:
To make sure we exercise this even more we can also try swapping the arguments:
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. Done, with some slight variation. In particular, I've gotten rid of the 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. It's fine with me. The reason I prefer a variable is because as I am modifying the test I may add a new N'th argument. If I am using hard-coded numbers in |
||
|
||
TEST_F(SandboxIRTest, BranchInst) { | ||
parseIR(C, R"IR( | ||
define void @foo(i1 %cond0, i1 %cond2) { | ||
|
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.
Could you also add a
static bool isValidOperands()
just like inllvm::InsertElementInst
which would justreturn cast<InsertElementInst>(Val)->isValidOperands(Vec, NewElt, Idx)
.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, by calling
llvm::InsertElementInst::isValidOperands(Vec->Val, NewElt->Val, Idx->Val)
. It's a static method so I can'tcast<InsertElementInst>(Val)
.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.
Could you also add a test for this? Something like:
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, following the other comment in the test.