|
| 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