216 lines · plain
1//===-- TestInterfaces.td - Test dialect interfaces --------*- tablegen -*-===//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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef MLIR_TEST_DIALECT_TEST_INTERFACES10#define MLIR_TEST_DIALECT_TEST_INTERFACES11 12include "mlir/IR/OpBase.td"13include "mlir/Interfaces/SideEffectInterfaceBase.td"14 15// A set of type interfaces used to test interface inheritance.16def TestBaseTypeInterfacePrintTypeA : TypeInterface<"TestBaseTypeInterfacePrintTypeA"> {17 let cppNamespace = "::test";18 let methods = [19 InterfaceMethod<"Prints the type name.",20 "void", "printTypeA", (ins "::mlir::Location":$loc), [{21 emitRemark(loc) << $_type << " - TestA";22 }]23 >24 ];25}26def TestBaseTypeInterfacePrintTypeB27 : TypeInterface<"TestBaseTypeInterfacePrintTypeB", [TestBaseTypeInterfacePrintTypeA]> {28 let cppNamespace = "::test";29 let methods = [30 InterfaceMethod<"Prints the type name.",31 "void", "printTypeB", (ins "::mlir::Location":$loc),32 [{}], /*defaultImplementation=*/[{33 emitRemark(loc) << $_type << " - TestB";34 }]35 >36 ];37}38 39// A type interface used to test the ODS generation of type interfaces.40def TestTypeInterface41 : TypeInterface<"TestTypeInterface", [TestBaseTypeInterfacePrintTypeB]> {42 let cppNamespace = "::test";43 let methods = [44 InterfaceMethod<"Prints the type name.",45 "void", "printTypeC", (ins "::mlir::Location":$loc)46 >,47 // Check that we can have multiple method with the same name.48 InterfaceMethod<"Prints the type name, with a value prefixed.",49 "void", "printTypeC", (ins "::mlir::Location":$loc, "int":$value)50 >,51 InterfaceMethod<"Prints the type name, with a value prefixed.",52 "void", "printTypeC", (ins "::mlir::Location":$loc, "float":$value),53 [{}], /*defaultImplementation=*/[{54 emitRemark(loc) << $_type << " - " << value << " - Float TestC";55 }]56 >,57 // It should be possible to use the interface type name as result type58 // as well as in the implementation.59 InterfaceMethod<"Prints the type name and returns the type as interface.",60 "TestTypeInterface", "printTypeRet", (ins "::mlir::Location":$loc),61 [{}], /*defaultImplementation=*/[{62 emitRemark(loc) << $_type << " - TestRet";63 return $_type;64 }]65 >,66 ];67 let extraClassDeclaration = [{68 /// Prints the type name.69 void printTypeD(::mlir::Location loc) const {70 emitRemark(loc) << *this << " - TestD";71 }72 }];73 let extraTraitClassDeclaration = [{74 /// Prints the type name.75 void printTypeE(::mlir::Location loc) const {76 emitRemark(loc) << $_type << " - TestE";77 }78 }];79}80 81def TestExternalTypeInterface : TypeInterface<"TestExternalTypeInterface"> {82 let cppNamespace = "::mlir";83 let methods = [84 InterfaceMethod<"Returns the bitwidth of the type plus 'arg'.",85 "unsigned", "getBitwidthPlusArg", (ins "unsigned":$arg)>,86 StaticInterfaceMethod<"Returns some value plus 'arg'.",87 "unsigned", "staticGetSomeValuePlusArg", (ins "unsigned":$arg)>,88 InterfaceMethod<"Returns the argument doubled.",89 "unsigned", "getBitwidthPlusDoubleArgument", (ins "unsigned":$arg), "",90 "return $_type.getIntOrFloatBitWidth() + 2 * arg;">,91 StaticInterfaceMethod<"Returns the argument.",92 "unsigned", "staticGetArgument", (ins "unsigned":$arg), "",93 "return arg;">,94 ];95}96 97def TestExternalFallbackTypeInterface98 : TypeInterface<"TestExternalFallbackTypeInterface"> {99 let cppNamespace = "::mlir";100 let methods = [101 InterfaceMethod<"Returns the bitwidth of the given integer type.",102 "unsigned", "getBitwidth", (ins), "", "return $_type.getWidth();">,103 ];104}105 106def TestExternalAttrInterface : AttrInterface<"TestExternalAttrInterface"> {107 let cppNamespace = "::mlir";108 let methods = [109 InterfaceMethod<"Gets the dialect pointer.", "const ::mlir::Dialect *",110 "getDialectPtr">,111 StaticInterfaceMethod<"Returns some number.", "int", "getSomeNumber">,112 ];113}114 115def TestExternalOpInterface : OpInterface<"TestExternalOpInterface"> {116 let cppNamespace = "::mlir";117 let methods = [118 InterfaceMethod<"Returns the length of the operation name plus arg.",119 "unsigned", "getNameLengthPlusArg", (ins "unsigned":$arg)>,120 StaticInterfaceMethod<121 "Returns the length of the operation name plus arg twice.", "unsigned",122 "getNameLengthPlusArgTwice", (ins "unsigned":$arg)>,123 InterfaceMethod<124 "Returns the length of the product of the operation name and arg.",125 "unsigned", "getNameLengthTimesArg", (ins "unsigned":$arg), "",126 "return arg * $_op->getName().getStringRef().size();">,127 StaticInterfaceMethod<"Returns the length of the operation name minus arg.",128 "unsigned", "getNameLengthMinusArg", (ins "unsigned":$arg), "",129 "return ConcreteOp::getOperationName().size() - arg;">,130 ];131}132 133def TestEffectOpInterface134 : EffectOpInterfaceBase<"TestEffectOpInterface",135 "::mlir::TestEffects::Effect"> {136 let cppNamespace = "::mlir";137}138 139class TestEffect<string effectName>140 : SideEffect<TestEffectOpInterface, effectName, DefaultResource, 0,141 PartialEffect>;142 143class TestEffects<list<TestEffect> effects = []>144 : SideEffectsTraitBase<TestEffectOpInterface, effects>;145 146def TestConcreteEffect : TestEffect<"TestEffects::Concrete">;147 148def TestOptionallyImplementedOpInterface149 : OpInterface<"TestOptionallyImplementedOpInterface"> {150 let cppNamespace = "::mlir";151 152 let methods = [153 InterfaceMethod<"", "bool", "getImplementsInterface", (ins)>,154 ];155 156 let extraClassOf = [{157 return $_op.getImplementsInterface();158 }];159}160 161def TestOptionallyImplementedAttrInterface162 : AttrInterface<"TestOptionallyImplementedAttrInterface"> {163 let cppNamespace = "::mlir";164 165 let methods = [166 InterfaceMethod<"", "bool", "getImplementsInterface", (ins)>,167 ];168 169 let extraClassOf = [{170 return $_attr.getImplementsInterface();171 }];172}173 174def TestOptionallyImplementedTypeInterface175 : TypeInterface<"TestOptionallyImplementedTypeInterface"> {176 let cppNamespace = "::mlir";177 178 let methods = [179 InterfaceMethod<"", "bool", "getImplementsInterface", (ins)>,180 ];181 182 let extraClassOf = [{183 return $_type.getImplementsInterface();184 }];185}186 187// Dummy type interface "A" that requires type interface "B" to be complete.188def TestCyclicTypeInterfaceA : TypeInterface<"TestCyclicTypeInterfaceA"> {189 let cppNamespace = "::mlir";190 let methods = [191 InterfaceMethod<"",192 "::mlir::FailureOr<::mlir::TestCyclicTypeInterfaceB>",193 /*methodName=*/"returnB",194 (ins),195 /*methodBody=*/"",196 /*defaultImpl=*/"return mlir::failure();"197 >,198 ];199}200 201// Dummy type interface "B" that requires type interface "A" to be complete.202def TestCyclicTypeInterfaceB : TypeInterface<"TestCyclicTypeInterfaceB"> {203 let cppNamespace = "::mlir";204 let methods = [205 InterfaceMethod<"",206 "::mlir::FailureOr<::mlir::TestCyclicTypeInterfaceA>",207 /*methodName=*/"returnA",208 (ins),209 /*methodBody=*/"",210 /*defaultImpl=*/"return mlir::failure();"211 >,212 ];213}214 215#endif // MLIR_TEST_DIALECT_TEST_INTERFACES216