Skip to content

Commit 7724912

Browse files
jasilvanusFlakebi
authored andcommitted
Precommit basic unit test for visitors
1 parent 678c2a9 commit 7724912

3 files changed

Lines changed: 112 additions & 1 deletion

File tree

test/unit/dialect/TestDialect.td

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,26 @@ def DefaultNameTargetExtType : DialectType<TestDialect, "default.target"> {
135135
Type name should be "test.default.target".
136136
}];
137137
}
138+
139+
def SomeBaseOpClass : OpClass<TestDialect> {
140+
let arguments = (ins);
141+
let summary = "family of operations";
142+
let description = [{
143+
Illustrate the use of the OpClass feature.
144+
}];
145+
}
146+
147+
class ConcreteOpClassMemberOp<string op>
148+
: TestOp<"some.op.class.subop." # op, []> {
149+
let superclass = SomeBaseOpClass;
150+
let results = (outs);
151+
let arguments = (ins superclass);
152+
153+
let summary = "perform the " # op # " operation";
154+
let description = [{
155+
Illustrate the use of the OpClass feature.
156+
}];
157+
}
158+
159+
def AddOp : ConcreteOpClassMemberOp<"add">;
160+
def MulOp : ConcreteOpClassMemberOp<"mul">;

test/unit/interface/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ add_dialects_unit_test(DialectsADTTests
2121
OpSetTests.cpp
2222
OpMapTests.cpp
2323
OpMapIRTests.cpp
24-
DialectTypeTests.cpp)
24+
DialectTypeTests.cpp
25+
VisitorIRTests.cpp)
2526

2627
add_dependencies(DialectsADTTests TestDialectTableGen)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
***********************************************************************************************************************
3+
* Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
***********************************************************************************************************************
17+
*/
18+
19+
#include "TestDialect.h"
20+
#include "llvm-dialects/Dialect/Builder.h"
21+
#include "llvm-dialects/Dialect/Dialect.h"
22+
#include "llvm-dialects/Dialect/Visitor.h"
23+
#include "llvm/ADT/StringRef.h"
24+
#include "llvm/IR/BasicBlock.h"
25+
#include "llvm/IR/Function.h"
26+
#include "llvm/IR/Intrinsics.h"
27+
#include "llvm/IR/Module.h"
28+
#include "gtest/gtest.h"
29+
30+
#include <memory>
31+
32+
using namespace llvm;
33+
using namespace llvm_dialects;
34+
35+
class VisitorIRTestFixture : public testing::Test {
36+
protected:
37+
void SetUp() override {
38+
setupDialectsContext();
39+
makeModule();
40+
}
41+
42+
LLVMContext Context;
43+
std::unique_ptr<DialectContext> DC;
44+
std::unique_ptr<Module> Mod;
45+
Function *EP = nullptr;
46+
47+
BasicBlock *getEntryBlock() { return EntryBlock; }
48+
49+
private:
50+
BasicBlock *EntryBlock = nullptr;
51+
52+
void makeModule() {
53+
Mod = std::make_unique<Module>("dialects_test", Context);
54+
const std::array<Type *, 1> Args = {Type::getInt32Ty(Mod->getContext())};
55+
FunctionCallee FC = Mod->getOrInsertFunction(
56+
"main",
57+
FunctionType::get(Type::getVoidTy(Mod->getContext()), Args, false));
58+
EP = cast<Function>(FC.getCallee());
59+
EntryBlock = BasicBlock::Create(Mod->getContext(), "entry", EP);
60+
}
61+
62+
void setupDialectsContext() {
63+
DC = DialectContext::make<test::TestDialect>(Context);
64+
}
65+
};
66+
67+
TEST_F(VisitorIRTestFixture, VisitOp) {
68+
llvm_dialects::Builder Builder{Context};
69+
Builder.SetInsertPoint(getEntryBlock());
70+
71+
auto *Mul1 = Builder.create<test::MulOp>();
72+
auto *Mul2 = Builder.create<test::MulOp>();
73+
auto *Add1 = Builder.create<test::AddOp>();
74+
auto *Add2 = Builder.create<test::AddOp>();
75+
auto *DialectOp1 = Builder.create<test::DialectOp1>();
76+
77+
DenseSet<test::MulOp *> Ops;
78+
static const auto Visitor =
79+
llvm_dialects::VisitorBuilder<DenseSet<test::MulOp *>>()
80+
.add<test::MulOp>([](auto &Ops, test::MulOp &Op) { Ops.insert(&Op); })
81+
.build();
82+
Visitor.visit(Ops, *Mod);
83+
EXPECT_EQ(Ops.size(), 2);
84+
for (const auto *Op : Ops) {
85+
EXPECT_TRUE(Op == Mul1 || Op == Mul2);
86+
}
87+
}

0 commit comments

Comments
 (0)