|
| 1 | +/* |
| 2 | + *********************************************************************************************************************** |
| 3 | + * Copyright (c) 2026 Advanced Micro Devices, Inc. All Rights Reserved. |
| 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/IR/LLVMContext.h" |
| 21 | +#include "llvm/IR/Type.h" |
| 22 | +#include "gtest/gtest.h" |
| 23 | + |
| 24 | +using namespace llvm; |
| 25 | +using namespace test; |
| 26 | + |
| 27 | +// Test custom llvmTypeNameOverride for TargetExtType |
| 28 | +TEST(DialectTypeTest, CustomNameTargetExtType) { |
| 29 | + LLVMContext ctx; |
| 30 | + |
| 31 | + auto *type = CustomNameTargetExtType::get(ctx, 10, 20); |
| 32 | + ASSERT_NE(type, nullptr); |
| 33 | + |
| 34 | + // Verify it's recognized as the correct type via classof |
| 35 | + EXPECT_TRUE(isa<CustomNameTargetExtType>(type)); |
| 36 | + EXPECT_TRUE(CustomNameTargetExtType::classof(type)); |
| 37 | + |
| 38 | + // Verify the type name is the custom one, not the default |
| 39 | + auto *targetExtType = cast<TargetExtType>(type); |
| 40 | + EXPECT_EQ(targetExtType->getName(), "custom.renamed.target"); |
| 41 | + |
| 42 | + // Verify parameters are accessible |
| 43 | + EXPECT_EQ(type->getParam1(), 10u); |
| 44 | + EXPECT_EQ(type->getParam2(), 20u); |
| 45 | +} |
| 46 | + |
| 47 | +// Test custom llvmTypeNameOverride for struct-backed type |
| 48 | +TEST(DialectTypeTest, CustomNameStructType) { |
| 49 | + LLVMContext ctx; |
| 50 | + |
| 51 | + auto *type = CustomNameStructType::get(ctx, 16, 16); |
| 52 | + ASSERT_NE(type, nullptr); |
| 53 | + |
| 54 | + // Verify it's recognized as the correct type via classof |
| 55 | + EXPECT_TRUE(isa<CustomNameStructType>(type)); |
| 56 | + EXPECT_TRUE(CustomNameStructType::classof(type)); |
| 57 | + |
| 58 | + // Verify the struct type name has the custom prefix |
| 59 | + auto *structType = cast<StructType>(type); |
| 60 | + EXPECT_TRUE(structType->getName().starts_with("custom.renamed.struct.")); |
| 61 | + |
| 62 | + // Verify parameters are accessible |
| 63 | + EXPECT_EQ(type->getRows(), 16u); |
| 64 | + EXPECT_EQ(type->getCols(), 16u); |
| 65 | +} |
| 66 | + |
| 67 | +// Test default naming behavior (no llvmTypeNameOverride specified) |
| 68 | +TEST(DialectTypeTest, DefaultNameTargetExtType) { |
| 69 | + LLVMContext ctx; |
| 70 | + |
| 71 | + auto *type = DefaultNameTargetExtType::get(ctx, 42); |
| 72 | + ASSERT_NE(type, nullptr); |
| 73 | + |
| 74 | + // Verify it's recognized as the correct type |
| 75 | + EXPECT_TRUE(isa<DefaultNameTargetExtType>(type)); |
| 76 | + EXPECT_TRUE(DefaultNameTargetExtType::classof(type)); |
| 77 | + |
| 78 | + // Verify the type name uses the default dialect.mnemonic format |
| 79 | + auto *targetExtType = cast<TargetExtType>(type); |
| 80 | + EXPECT_EQ(targetExtType->getName(), "test.default.target"); |
| 81 | + |
| 82 | + // Verify parameter is accessible |
| 83 | + EXPECT_EQ(type->getValue(), 42u); |
| 84 | +} |
| 85 | + |
| 86 | +// Test that different custom types are distinguishable |
| 87 | +TEST(DialectTypeTest, TypeDistinction) { |
| 88 | + LLVMContext ctx; |
| 89 | + |
| 90 | + auto *customTarget = CustomNameTargetExtType::get(ctx, 1, 2); |
| 91 | + auto *defaultTarget = DefaultNameTargetExtType::get(ctx, 3); |
| 92 | + auto *customStruct = CustomNameStructType::get(ctx, 4, 5); |
| 93 | + |
| 94 | + // Types should be distinct |
| 95 | + EXPECT_NE(static_cast<llvm::Type *>(customTarget), |
| 96 | + static_cast<llvm::Type *>(defaultTarget)); |
| 97 | + EXPECT_NE(static_cast<llvm::Type *>(customTarget), |
| 98 | + static_cast<llvm::Type *>(customStruct)); |
| 99 | + EXPECT_NE(static_cast<llvm::Type *>(defaultTarget), |
| 100 | + static_cast<llvm::Type *>(customStruct)); |
| 101 | + |
| 102 | + // classof should work correctly |
| 103 | + EXPECT_TRUE(CustomNameTargetExtType::classof(customTarget)); |
| 104 | + EXPECT_FALSE(CustomNameTargetExtType::classof(defaultTarget)); |
| 105 | + EXPECT_FALSE(CustomNameTargetExtType::classof(customStruct)); |
| 106 | + |
| 107 | + EXPECT_FALSE(DefaultNameTargetExtType::classof(customTarget)); |
| 108 | + EXPECT_TRUE(DefaultNameTargetExtType::classof(defaultTarget)); |
| 109 | + EXPECT_FALSE(DefaultNameTargetExtType::classof(customStruct)); |
| 110 | + |
| 111 | + EXPECT_FALSE(CustomNameStructType::classof(customTarget)); |
| 112 | + EXPECT_FALSE(CustomNameStructType::classof(defaultTarget)); |
| 113 | + EXPECT_TRUE(CustomNameStructType::classof(customStruct)); |
| 114 | +} |
0 commit comments