Debian Codegraph @

llvm-project/mlir/unittests/IR/TypeTest.cpp

raw
1 //===- TypeTest.cpp - Type API unit tests ---------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "mlir/IR/BuiltinTypes.h"
10 #include "mlir/IR/Dialect.h"
11 #include "mlir/IR/Types.h"
12 #include "mlir/IR/Value.h"
13 #include "gtest/gtest.h"
14
15 using namespace mlir;
16
17 /// Mock implementations of a Type hierarchy
18 struct LeafType;
19
20 struct MiddleType : Type::TypeBase<MiddleType, Type, TypeStorage> {
21 using Base::Base;
22 static bool classof(Type ty) {
23 return ty.getTypeID() == TypeID::get<LeafType>() || Base::classof(ty);
24 }
25 };
26
27 struct LeafType : Type::TypeBase<LeafType, MiddleType, TypeStorage> {
28 using Base::Base;
29 };
30
31 struct FakeDialect : Dialect {
32 FakeDialect(MLIRContext *context)
33 : Dialect(getDialectNamespace(), context, TypeID::get<FakeDialect>()) {
34 addTypes<MiddleType, LeafType>();
35 }
36 static constexpr ::llvm::StringLiteral getDialectNamespace() {
37 return ::llvm::StringLiteral("fake");
38 }
39 };
40
41 TEST(Type, Casting) {
42 MLIRContext ctx;
43 ctx.loadDialect<FakeDialect>();
44
45 Type intTy = IntegerType::get(&ctx, 8);
46 Type nullTy;
47 MiddleType middleTy = MiddleType::get(&ctx);
48 MiddleType leafTy = LeafType::get(&ctx);
49 Type leaf2Ty = LeafType::get(&ctx);
50
51 EXPECT_TRUE(isa<IntegerType>(intTy));
52 EXPECT_FALSE(isa<FunctionType>(intTy));
53 EXPECT_FALSE(llvm::isa_and_present<IntegerType>(nullTy));
54 EXPECT_TRUE(isa<MiddleType>(middleTy));
55 EXPECT_FALSE(isa<LeafType>(middleTy));
56 EXPECT_TRUE(isa<MiddleType>(leafTy));
57 EXPECT_TRUE(isa<LeafType>(leaf2Ty));
58 EXPECT_TRUE(isa<LeafType>(leafTy));
59
60 EXPECT_TRUE(static_cast<bool>(dyn_cast<IntegerType>(intTy)));
61 EXPECT_FALSE(static_cast<bool>(dyn_cast<FunctionType>(intTy)));
62 EXPECT_FALSE(static_cast<bool>(llvm::cast_if_present<FunctionType>(nullTy)));
63 EXPECT_FALSE(
64 static_cast<bool>(llvm::dyn_cast_if_present<IntegerType>(nullTy)));
65
66 EXPECT_EQ(8u, cast<IntegerType>(intTy).getWidth());
67 }