1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
//===- InterfaceAttachmentTest.cpp - Test attaching interfaces ------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This implements the tests for attaching interfaces to attributes and types
// without having to specify them on the attribute or type class directly.
//
//===----------------------------------------------------------------------===//
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinDialect.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/BuiltinTypes.h"
#include "gtest/gtest.h"
#include "../../test/lib/Dialect/Test/TestAttributes.h"
#include "../../test/lib/Dialect/Test/TestDialect.h"
#include "../../test/lib/Dialect/Test/TestTypes.h"
using namespace mlir;
using namespace mlir::test;
namespace {
/// External interface model for the integer type. Only provides non-default
/// methods.
struct Model
: public TestExternalTypeInterface::ExternalModel<Model, IntegerType> {
unsigned getBitwidthPlusArg(Type type, unsigned arg) const {
return type.getIntOrFloatBitWidth() + arg;
}
static unsigned staticGetSomeValuePlusArg(unsigned arg) { return 42 + arg; }
};
/// External interface model for the float type. Provides non-deafult and
/// overrides default methods.
struct OverridingModel
: public TestExternalTypeInterface::ExternalModel<OverridingModel,
FloatType> {
unsigned getBitwidthPlusArg(Type type, unsigned arg) const {
return type.getIntOrFloatBitWidth() + arg;
}
static unsigned staticGetSomeValuePlusArg(unsigned arg) { return 42 + arg; }
unsigned getBitwidthPlusDoubleArgument(Type type, unsigned arg) const {
return 128;
}
static unsigned staticGetArgument(unsigned arg) { return 420; }
};
TEST(InterfaceAttachment, Type) {
MLIRContext context;
// Check that the type has no interface.
IntegerType i8 = IntegerType::get(&context, 8);
ASSERT_FALSE(i8.isa<TestExternalTypeInterface>());
// Attach an interface and check that the type now has the interface.
IntegerType::attachInterface<Model>(context);
TestExternalTypeInterface iface = i8.dyn_cast<TestExternalTypeInterface>();
ASSERT_TRUE(iface != nullptr);
EXPECT_EQ(iface.getBitwidthPlusArg(10), 18u);
EXPECT_EQ(iface.staticGetSomeValuePlusArg(0), 42u);
EXPECT_EQ(iface.getBitwidthPlusDoubleArgument(2), 12u);
EXPECT_EQ(iface.staticGetArgument(17), 17u);
// Same, but with the default implementation overridden.
FloatType flt = Float32Type::get(&context);
ASSERT_FALSE(flt.isa<TestExternalTypeInterface>());
Float32Type::attachInterface<OverridingModel>(context);
iface = flt.dyn_cast<TestExternalTypeInterface>();
ASSERT_TRUE(iface != nullptr);
EXPECT_EQ(iface.getBitwidthPlusArg(10), 42u);
EXPECT_EQ(iface.staticGetSomeValuePlusArg(10), 52u);
EXPECT_EQ(iface.getBitwidthPlusDoubleArgument(3), 128u);
EXPECT_EQ(iface.staticGetArgument(17), 420u);
// Other contexts shouldn't have the attribute attached.
MLIRContext other;
IntegerType i8other = IntegerType::get(&other, 8);
EXPECT_FALSE(i8other.isa<TestExternalTypeInterface>());
}
/// External interface model for the test type from the test dialect.
struct TestTypeModel
: public TestExternalTypeInterface::ExternalModel<TestTypeModel,
test::TestType> {
unsigned getBitwidthPlusArg(Type type, unsigned arg) const { return arg; }
static unsigned staticGetSomeValuePlusArg(unsigned arg) { return 10 + arg; }
};
TEST(InterfaceAttachment, TypeDelayedContextConstruct) {
// Put the interface in the registry.
DialectRegistry registry;
registry.insert<test::TestDialect>();
registry.addTypeInterface<test::TestDialect, test::TestType, TestTypeModel>();
// Check that when a context is constructed with the given registry, the type
// interface gets registered.
MLIRContext context(registry);
context.loadDialect<test::TestDialect>();
test::TestType testType = test::TestType::get(&context);
auto iface = testType.dyn_cast<TestExternalTypeInterface>();
ASSERT_TRUE(iface != nullptr);
EXPECT_EQ(iface.getBitwidthPlusArg(42), 42u);
EXPECT_EQ(iface.staticGetSomeValuePlusArg(10), 20u);
}
TEST(InterfaceAttachment, TypeDelayedContextAppend) {
// Put the interface in the registry.
DialectRegistry registry;
registry.insert<test::TestDialect>();
registry.addTypeInterface<test::TestDialect, test::TestType, TestTypeModel>();
// Check that when the registry gets appended to the context, the interface
// becomes available for objects in loaded dialects.
MLIRContext context;
context.loadDialect<test::TestDialect>();
test::TestType testType = test::TestType::get(&context);
EXPECT_FALSE(testType.isa<TestExternalTypeInterface>());
context.appendDialectRegistry(registry);
EXPECT_TRUE(testType.isa<TestExternalTypeInterface>());
}
TEST(InterfaceAttachment, RepeatedRegistration) {
DialectRegistry registry;
registry.addTypeInterface<BuiltinDialect, IntegerType, Model>();
MLIRContext context(registry);
// Should't fail on repeated registration through the dialect registry.
context.appendDialectRegistry(registry);
}
TEST(InterfaceAttachment, TypeBuiltinDelayed) {
// Builtin dialect needs to registration or loading, but delayed interface
// registration must still work.
DialectRegistry registry;
registry.addTypeInterface<BuiltinDialect, IntegerType, Model>();
MLIRContext context(registry);
IntegerType i16 = IntegerType::get(&context, 16);
EXPECT_TRUE(i16.isa<TestExternalTypeInterface>());
MLIRContext initiallyEmpty;
IntegerType i32 = IntegerType::get(&initiallyEmpty, 32);
EXPECT_FALSE(i32.isa<TestExternalTypeInterface>());
initiallyEmpty.appendDialectRegistry(registry);
EXPECT_TRUE(i32.isa<TestExternalTypeInterface>());
}
/// The interface provides a default implementation that expects
/// ConcreteType::getWidth to exist, which is the case for IntegerType. So this
/// just derives from the ExternalModel.
struct TestExternalFallbackTypeIntegerModel
: public TestExternalFallbackTypeInterface::ExternalModel<
TestExternalFallbackTypeIntegerModel, IntegerType> {};
/// The interface provides a default implementation that expects
/// ConcreteType::getWidth to exist, which is *not* the case for VectorType. Use
/// FallbackModel instead to override this and make sure the code still compiles
/// because we never instantiate the ExternalModel class template with a
/// template argument that would have led to compilation failures.
struct TestExternalFallbackTypeVectorModel
: public TestExternalFallbackTypeInterface::FallbackModel<
TestExternalFallbackTypeVectorModel> {
unsigned getBitwidth(Type type) const {
IntegerType elementType = type.cast<VectorType>()
.getElementType()
.dyn_cast_or_null<IntegerType>();
return elementType ? elementType.getWidth() : 0;
}
};
TEST(InterfaceAttachment, Fallback) {
MLIRContext context;
// Just check that we can attach the interface.
IntegerType i8 = IntegerType::get(&context, 8);
ASSERT_FALSE(i8.isa<TestExternalFallbackTypeInterface>());
IntegerType::attachInterface<TestExternalFallbackTypeIntegerModel>(context);
ASSERT_TRUE(i8.isa<TestExternalFallbackTypeInterface>());
// Call the method so it is guaranteed not to be instantiated.
VectorType vec = VectorType::get({42}, i8);
ASSERT_FALSE(vec.isa<TestExternalFallbackTypeInterface>());
VectorType::attachInterface<TestExternalFallbackTypeVectorModel>(context);
ASSERT_TRUE(vec.isa<TestExternalFallbackTypeInterface>());
EXPECT_EQ(vec.cast<TestExternalFallbackTypeInterface>().getBitwidth(), 8u);
}
/// External model for attribute interfaces.
struct TestExternalIntegerAttrModel
: public TestExternalAttrInterface::ExternalModel<
TestExternalIntegerAttrModel, IntegerAttr> {
const Dialect *getDialectPtr(Attribute attr) const {
return &attr.cast<IntegerAttr>().getDialect();
}
static int getSomeNumber() { return 42; }
};
TEST(InterfaceAttachment, Attribute) {
MLIRContext context;
// Attribute interfaces use the exact same mechanism as types, so just check
// that the basics work for attributes.
IntegerAttr attr = IntegerAttr::get(IntegerType::get(&context, 32), 42);
ASSERT_FALSE(attr.isa<TestExternalAttrInterface>());
IntegerAttr::attachInterface<TestExternalIntegerAttrModel>(context);
auto iface = attr.dyn_cast<TestExternalAttrInterface>();
ASSERT_TRUE(iface != nullptr);
EXPECT_EQ(iface.getDialectPtr(), &attr.getDialect());
EXPECT_EQ(iface.getSomeNumber(), 42);
}
/// External model for an interface attachable to a non-builtin attribute.
struct TestExternalSimpleAAttrModel
: public TestExternalAttrInterface::ExternalModel<
TestExternalSimpleAAttrModel, test::SimpleAAttr> {
const Dialect *getDialectPtr(Attribute attr) const {
return &attr.getDialect();
}
static int getSomeNumber() { return 21; }
};
TEST(InterfaceAttachmentTest, AttributeDelayed) {
// Attribute interfaces use the exact same mechanism as types, so just check
// that the delayed registration work for attributes.
DialectRegistry registry;
registry.insert<test::TestDialect>();
registry.addAttrInterface<test::TestDialect, test::SimpleAAttr,
TestExternalSimpleAAttrModel>();
MLIRContext context(registry);
context.loadDialect<test::TestDialect>();
auto attr = test::SimpleAAttr::get(&context);
EXPECT_TRUE(attr.isa<TestExternalAttrInterface>());
MLIRContext initiallyEmpty;
initiallyEmpty.loadDialect<test::TestDialect>();
attr = test::SimpleAAttr::get(&initiallyEmpty);
EXPECT_FALSE(attr.isa<TestExternalAttrInterface>());
initiallyEmpty.appendDialectRegistry(registry);
EXPECT_TRUE(attr.isa<TestExternalAttrInterface>());
}
/// External interface model for the module operation. Only provides non-default
/// methods.
struct TestExternalOpModel
: public TestExternalOpInterface::ExternalModel<TestExternalOpModel,
ModuleOp> {
unsigned getNameLengthPlusArg(Operation *op, unsigned arg) const {
return op->getName().getStringRef().size() + arg;
}
static unsigned getNameLengthPlusArgTwice(unsigned arg) {
return ModuleOp::getOperationName().size() + 2 * arg;
}
};
/// External interface model for the func operation. Provides non-deafult and
/// overrides default methods.
struct TestExternalOpOverridingModel
: public TestExternalOpInterface::FallbackModel<
TestExternalOpOverridingModel> {
unsigned getNameLengthPlusArg(Operation *op, unsigned arg) const {
return op->getName().getStringRef().size() + arg;
}
static unsigned getNameLengthPlusArgTwice(unsigned arg) {
return FuncOp::getOperationName().size() + 2 * arg;
}
unsigned getNameLengthTimesArg(Operation *op, unsigned arg) const {
return 42;
}
static unsigned getNameLengthMinusArg(unsigned arg) { return 21; }
};
TEST(InterfaceAttachment, Operation) {
MLIRContext context;
// Initially, the operation doesn't have the interface.
auto moduleOp = ModuleOp::create(UnknownLoc::get(&context));
ASSERT_FALSE(isa<TestExternalOpInterface>(moduleOp.getOperation()));
// We can attach an external interface and now the operaiton has it.
ModuleOp::attachInterface<TestExternalOpModel>(context);
auto iface = dyn_cast<TestExternalOpInterface>(moduleOp.getOperation());
ASSERT_TRUE(iface != nullptr);
EXPECT_EQ(iface.getNameLengthPlusArg(10), 16u);
EXPECT_EQ(iface.getNameLengthTimesArg(3), 18u);
EXPECT_EQ(iface.getNameLengthPlusArgTwice(18), 42u);
EXPECT_EQ(iface.getNameLengthMinusArg(5), 1u);
// Default implementation can be overridden.
auto funcOp = FuncOp::create(UnknownLoc::get(&context), "function",
FunctionType::get(&context, {}, {}));
ASSERT_FALSE(isa<TestExternalOpInterface>(funcOp.getOperation()));
FuncOp::attachInterface<TestExternalOpOverridingModel>(context);
iface = dyn_cast<TestExternalOpInterface>(funcOp.getOperation());
ASSERT_TRUE(iface != nullptr);
EXPECT_EQ(iface.getNameLengthPlusArg(10), 14u);
EXPECT_EQ(iface.getNameLengthTimesArg(0), 42u);
EXPECT_EQ(iface.getNameLengthPlusArgTwice(8), 20u);
EXPECT_EQ(iface.getNameLengthMinusArg(1000), 21u);
// Another context doesn't have the interfaces registered.
MLIRContext other;
auto otherModuleOp = ModuleOp::create(UnknownLoc::get(&other));
ASSERT_FALSE(isa<TestExternalOpInterface>(otherModuleOp.getOperation()));
}
struct TestExternalTestOpModel
: public TestExternalOpInterface::ExternalModel<TestExternalTestOpModel,
test::OpJ> {
unsigned getNameLengthPlusArg(Operation *op, unsigned arg) const {
return op->getName().getStringRef().size() + arg;
}
static unsigned getNameLengthPlusArgTwice(unsigned arg) {
return test::OpJ::getOperationName().size() + 2 * arg;
}
};
TEST(InterfaceAttachment, OperationDelayedContextConstruct) {
DialectRegistry registry;
registry.insert<test::TestDialect>();
registry.addOpInterface<ModuleOp, TestExternalOpModel>();
registry.addOpInterface<test::OpJ, TestExternalTestOpModel>();
// Construct the context directly from a registry. The interfaces are expected
// to be readily available on operations.
MLIRContext context(registry);
context.loadDialect<test::TestDialect>();
ModuleOp module = ModuleOp::create(UnknownLoc::get(&context));
OpBuilder builder(module);
auto op =
builder.create<test::OpJ>(builder.getUnknownLoc(), builder.getI32Type());
EXPECT_TRUE(isa<TestExternalOpInterface>(module.getOperation()));
EXPECT_TRUE(isa<TestExternalOpInterface>(op.getOperation()));
}
TEST(InterfaceAttachment, OperationDelayedContextAppend) {
DialectRegistry registry;
registry.insert<test::TestDialect>();
registry.addOpInterface<ModuleOp, TestExternalOpModel>();
registry.addOpInterface<test::OpJ, TestExternalTestOpModel>();
// Construct the context, create ops, and only then append the registry. The
// interfaces are expected to be available after appending the registry.
MLIRContext context;
context.loadDialect<test::TestDialect>();
ModuleOp module = ModuleOp::create(UnknownLoc::get(&context));
OpBuilder builder(module);
auto op =
builder.create<test::OpJ>(builder.getUnknownLoc(), builder.getI32Type());
EXPECT_FALSE(isa<TestExternalOpInterface>(module.getOperation()));
EXPECT_FALSE(isa<TestExternalOpInterface>(op.getOperation()));
context.appendDialectRegistry(registry);
EXPECT_TRUE(isa<TestExternalOpInterface>(module.getOperation()));
EXPECT_TRUE(isa<TestExternalOpInterface>(op.getOperation()));
}
} // end namespace
|