Skip to content

Commit

Permalink
Tests for looking up an Instruction::Call in an OpMap with a CallInst
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerNowicki committed Jan 21, 2025
1 parent b8d471c commit fdd184a
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions test/unit/interface/OpMapIRTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ TEST_F(OpMapIRTestFixture, IntrinsicOpMatchesInstructionTest) {
EXPECT_EQ(map[AssumeDesc], "assume");

const auto &SideEffect = *B.CreateCall(
Intrinsic::getDeclaration(Mod.get(), Intrinsic::sideeffect));
Intrinsic::getOrInsertDeclaration(Mod.get(), Intrinsic::sideeffect));

const std::array<Value *, 1> AssumeArgs = {
ConstantInt::getBool(Type::getInt1Ty(Context), true)};
const auto &Assume = *B.CreateCall(
Intrinsic::getDeclaration(Mod.get(), Intrinsic::assume), AssumeArgs);
Intrinsic::getOrInsertDeclaration(Mod.get(), Intrinsic::assume),
AssumeArgs);

EXPECT_FALSE(map.lookup(SideEffect) == map.lookup(Assume));
EXPECT_EQ(map.lookup(SideEffect), "sideeffect");
Expand Down Expand Up @@ -171,7 +172,7 @@ TEST_F(OpMapIRTestFixture, MixedOpMatchesInstructionTest) {
EXPECT_EQ(map[SideEffectDesc], "sideeffect");

const auto &SideEffect = *B.CreateCall(
Intrinsic::getDeclaration(Mod.get(), Intrinsic::sideeffect));
Intrinsic::getOrInsertDeclaration(Mod.get(), Intrinsic::sideeffect));

EXPECT_EQ(map.lookup(SideEffect), "sideeffect");

Expand Down Expand Up @@ -252,3 +253,43 @@ TEST_F(OpMapIRTestFixture, DialectOpOverloadTests) {
EXPECT_EQ(map.lookup(Op1), "DialectOp4");
EXPECT_EQ(map.lookup(Op2), "DialectOp4");
}

TEST_F(OpMapIRTestFixture, CallInstCoreOpMatchesInstructionTest) {
// Declare %OpaqueTy = type opaque
StructType *OpaqueTy = StructType::create(Context, "OpaqueTy");

// Define types
PointerType *OpaquePtrTy = PointerType::get(OpaqueTy, 0);
IntegerType *I32Ty = Type::getInt32Ty(Context);

// Declare: %OpaqueTy* @ProcOpaqueHandle(i32, %OpaqueTy*)
FunctionType *ProcOpaqueHandleFuncTyTy =
FunctionType::get(OpaquePtrTy, {I32Ty, OpaquePtrTy}, false);
FunctionCallee ProcOpaqueHandleFuncTy =
Mod->getOrInsertFunction("ProcOpaqueHandle", ProcOpaqueHandleFuncTyTy);

IRBuilder<> Builder{Context};
Builder.SetInsertPoint(getEntryBlock());

// Create a dummy global variable of type %OpaqueTy*
GlobalVariable *GV = new GlobalVariable(
*Mod, OpaqueTy, false, GlobalValue::PrivateLinkage, nullptr, "handle");
GV->setInitializer(ConstantAggregateZero::get(OpaqueTy));
Value *Op2 = GV;

// Create a constant value (e.g., 123)
Value *Op1 = ConstantInt::get(I32Ty, 123);

// Build the call to ProcOpaqueHandle
Value *Args[] = {Op1, Op2};
const CallInst &CallInst = *Builder.CreateCall(ProcOpaqueHandleFuncTy, Args);

// Add Instruction::Call to OpMap
OpMap<StringRef> map;
const OpDescription CallDesc = OpDescription::fromCoreOp(Instruction::Call);
map[CallDesc] = "ProcOpaqueHandle";

// Look up the CallInst in the map and verify it finds the entry for
// Instruction::Call
EXPECT_EQ(map.lookup(CallInst), "ProcOpaqueHandle");
}

0 comments on commit fdd184a

Please sign in to comment.