1// RUN: mlir-tblgen -gen-python-op-bindings -bind-dialect=test -I %S/../../include %s | FileCheck %s 2 3include "mlir/IR/OpBase.td" 4include "mlir/Bindings/Python/Attributes.td" 5 6// CHECK: @_cext.register_dialect 7// CHECK: class _Dialect(_ir.Dialect): 8 // CHECK: DIALECT_NAMESPACE = "test" 9 // CHECK: pass 10def Test_Dialect : Dialect { 11 let name = "test"; 12 let cppNamespace = "Test"; 13} 14class TestOp<string mnemonic, list<OpTrait> traits = []> : 15 Op<Test_Dialect, mnemonic, traits>; 16 17// CHECK: @_cext.register_operation(_Dialect) 18// CHECK: class AttrSizedOperandsOp(_ir.OpView): 19// CHECK-LABEL: OPERATION_NAME = "test.attr_sized_operands" 20def AttrSizedOperandsOp : TestOp<"attr_sized_operands", 21 [AttrSizedOperandSegments]> { 22 // CHECK: def __init__(self, variadic1, non_variadic, variadic2, loc=None, ip=None): 23 // CHECK: operands = [] 24 // CHECK: results = [] 25 // CHECK: attributes = {} 26 // CHECK: operand_segment_sizes = array.array('L') 27 // CHECK: operands += [*variadic1] 28 // CHECK: operand_segment_sizes.append(len(variadic1)) 29 // CHECK: operands.append(non_variadic) 30 // CHECK: operand_segment_sizes.append(1) 31 // CHECK: if variadic2 is not None: operands.append(variadic2) 32 // CHECK: operand_segment_sizes.append(0 if variadic2 is None else 1) 33 // CHECK: attributes["operand_segment_sizes"] = _ir.DenseElementsAttr.get(operand_segment_sizes, 34 // CHECK: context=_get_default_loc_context(loc)) 35 // CHECK: super().__init__(_ir.Operation.create( 36 // CHECK: "test.attr_sized_operands", attributes=attributes, operands=operands, results=results, 37 // CHECK: loc=loc, ip=ip)) 38 39 // CHECK: @property 40 // CHECK: def variadic1(self): 41 // CHECK: operand_range = _segmented_accessor( 42 // CHECK: self.operation.operands, 43 // CHECK: self.operation.attributes["operand_segment_sizes"], 0) 44 // CHECK: return operand_range 45 // 46 // CHECK: @property 47 // CHECK: def non_variadic(self): 48 // CHECK: operand_range = _segmented_accessor( 49 // CHECK: self.operation.operands, 50 // CHECK: self.operation.attributes["operand_segment_sizes"], 1) 51 // CHECK: return operand_range[0] 52 // 53 // CHECK: @property 54 // CHECK: def variadic2(self): 55 // CHECK: operand_range = _segmented_accessor( 56 // CHECK: self.operation.operands, 57 // CHECK: self.operation.attributes["operand_segment_sizes"], 2) 58 // CHECK: return operand_range[0] if len(operand_range) > 0 else None 59 let arguments = (ins Variadic<AnyType>:$variadic1, AnyType:$non_variadic, 60 Optional<AnyType>:$variadic2); 61} 62 63// CHECK: @_cext.register_operation(_Dialect) 64// CHECK: class AttrSizedResultsOp(_ir.OpView): 65// CHECK-LABEL: OPERATION_NAME = "test.attr_sized_results" 66def AttrSizedResultsOp : TestOp<"attr_sized_results", 67 [AttrSizedResultSegments]> { 68 // CHECK: def __init__(self, variadic1, non_variadic, variadic2, loc=None, ip=None): 69 // CHECK: operands = [] 70 // CHECK: results = [] 71 // CHECK: attributes = {} 72 // CHECK: result_segment_sizes = array.array('L') 73 // CHECK: if variadic1 is not None: results.append(variadic1) 74 // CHECK: result_segment_sizes.append(0 if variadic1 is None else 1) 75 // CHECK: results.append(non_variadic) 76 // CHECK: result_segment_sizes.append(1) # non_variadic 77 // CHECK: if variadic2 is not None: results.append(variadic2) 78 // CHECK: result_segment_sizes.append(0 if variadic2 is None else 1) 79 // CHECK: attributes["result_segment_sizes"] = _ir.DenseElementsAttr.get(result_segment_sizes, 80 // CHECK: context=_get_default_loc_context(loc)) 81 // CHECK: super().__init__(_ir.Operation.create( 82 // CHECK: "test.attr_sized_results", attributes=attributes, operands=operands, results=results, 83 // CHECK: loc=loc, ip=ip)) 84 85 // CHECK: @property 86 // CHECK: def variadic1(self): 87 // CHECK: result_range = _segmented_accessor( 88 // CHECK: self.operation.results, 89 // CHECK: self.operation.attributes["result_segment_sizes"], 0) 90 // CHECK: return result_range[0] if len(result_range) > 0 else None 91 // 92 // CHECK: @property 93 // CHECK: def non_variadic(self): 94 // CHECK: result_range = _segmented_accessor( 95 // CHECK: self.operation.results, 96 // CHECK: self.operation.attributes["result_segment_sizes"], 1) 97 // CHECK: return result_range[0] 98 // 99 // CHECK: @property 100 // CHECK: def variadic2(self): 101 // CHECK: result_range = _segmented_accessor( 102 // CHECK: self.operation.results, 103 // CHECK: self.operation.attributes["result_segment_sizes"], 2) 104 // CHECK: return result_range 105 let results = (outs Optional<AnyType>:$variadic1, AnyType:$non_variadic, 106 Optional<AnyType>:$variadic2); 107} 108 109 110// CHECK: @_cext.register_operation(_Dialect) 111// CHECK: class AttributedOp(_ir.OpView): 112// CHECK-LABEL: OPERATION_NAME = "test.attributed_op" 113def AttributedOp : TestOp<"attributed_op"> { 114 // CHECK: def __init__(self, i32attr, optionalF32Attr, unitAttr, in_, loc=None, ip=None): 115 // CHECK: operands = [] 116 // CHECK: results = [] 117 // CHECK: attributes = {} 118 // CHECK: attributes["i32attr"] = i32attr 119 // CHECK: if optionalF32Attr is not None: attributes["optionalF32Attr"] = optionalF32Attr 120 // CHECK: if bool(unitAttr): attributes["unitAttr"] = _ir.UnitAttr.get( 121 // CHECK: _get_default_loc_context(loc)) 122 // CHECK: attributes["in"] = in_ 123 // CHECK: super().__init__(_ir.Operation.create( 124 // CHECK: "test.attributed_op", attributes=attributes, operands=operands, results=results, 125 // CHECK: loc=loc, ip=ip)) 126 127 // CHECK: @property 128 // CHECK: def i32attr(self): 129 // CHECK: return _ir.IntegerAttr(self.operation.attributes["i32attr"]) 130 131 // CHECK: @property 132 // CHECK: def optionalF32Attr(self): 133 // CHECK: if "optionalF32Attr" not in self.operation.attributes: 134 // CHECK: return None 135 // CHECK: return _ir.FloatAttr(self.operation.attributes["optionalF32Attr"]) 136 137 // CHECK: @property 138 // CHECK: def unitAttr(self): 139 // CHECK: return "unitAttr" in self.operation.attributes 140 141 // CHECK: @property 142 // CHECK: def in_(self): 143 // CHECK: return _ir.IntegerAttr(self.operation.attributes["in"]) 144 let arguments = (ins I32Attr:$i32attr, OptionalAttr<F32Attr>:$optionalF32Attr, 145 UnitAttr:$unitAttr, I32Attr:$in); 146} 147 148// CHECK: @_cext.register_operation(_Dialect) 149// CHECK: class AttributedOpWithOperands(_ir.OpView): 150// CHECK-LABEL: OPERATION_NAME = "test.attributed_op_with_operands" 151def AttributedOpWithOperands : TestOp<"attributed_op_with_operands"> { 152 // CHECK: def __init__(self, _gen_arg_0, in_, _gen_arg_2, is_, loc=None, ip=None): 153 // CHECK: operands = [] 154 // CHECK: results = [] 155 // CHECK: attributes = {} 156 // CHECK: operands.append(_gen_arg_0) 157 // CHECK: operands.append(_gen_arg_2) 158 // CHECK: if bool(in_): attributes["in"] = _ir.UnitAttr.get( 159 // CHECK: _get_default_loc_context(loc)) 160 // CHECK: if is_ is not None: attributes["is"] = is_ 161 // CHECK: super().__init__(_ir.Operation.create( 162 // CHECK: "test.attributed_op_with_operands", attributes=attributes, operands=operands, results=results, 163 // CHECK: loc=loc, ip=ip)) 164 165 // CHECK: @property 166 // CHECK: def in_(self): 167 // CHECK: return "in" in self.operation.attributes 168 169 // CHECK: @property 170 // CHECK: def is_(self): 171 // CHECK: if "is" not in self.operation.attributes: 172 // CHECK: return None 173 // CHECK: return _ir.FloatAttr(self.operation.attributes["is"]) 174 let arguments = (ins I32, UnitAttr:$in, F32, OptionalAttr<F32Attr>:$is); 175} 176 177 178// CHECK: @_cext.register_operation(_Dialect) 179// CHECK: class EmptyOp(_ir.OpView): 180// CHECK-LABEL: OPERATION_NAME = "test.empty" 181def EmptyOp : TestOp<"empty">; 182 // CHECK: def __init__(self, loc=None, ip=None): 183 // CHECK: operands = [] 184 // CHECK: results = [] 185 // CHECK: attributes = {} 186 // CHECK: super().__init__(_ir.Operation.create( 187 // CHECK: "test.empty", attributes=attributes, operands=operands, results=results, 188 // CHECK: loc=loc, ip=ip)) 189 190// CHECK: @_cext.register_operation(_Dialect) 191// CHECK: class MissingNamesOp(_ir.OpView): 192// CHECK-LABEL: OPERATION_NAME = "test.missing_names" 193def MissingNamesOp : TestOp<"missing_names"> { 194 // CHECK: def __init__(self, i32, _gen_res_1, i64, _gen_arg_0, f32, _gen_arg_2, loc=None, ip=None): 195 // CHECK: operands = [] 196 // CHECK: results = [] 197 // CHECK: attributes = {} 198 // CHECK: results.append(i32) 199 // CHECK: results.append(_gen_res_1) 200 // CHECK: results.append(i64) 201 // CHECK: operands.append(_gen_arg_0) 202 // CHECK: operands.append(f32) 203 // CHECK: operands.append(_gen_arg_2) 204 // CHECK: super().__init__(_ir.Operation.create( 205 // CHECK: "test.missing_names", attributes=attributes, operands=operands, results=results, 206 // CHECK: loc=loc, ip=ip)) 207 208 // CHECK: @property 209 // CHECK: def f32(self): 210 // CHECK: return self.operation.operands[1] 211 let arguments = (ins I32, F32:$f32, I64); 212 213 // CHECK: @property 214 // CHECK: def i32(self): 215 // CHECK: return self.operation.results[0] 216 // 217 // CHECK: @property 218 // CHECK: def i64(self): 219 // CHECK: return self.operation.results[2] 220 let results = (outs I32:$i32, F32, I64:$i64); 221} 222 223// CHECK: @_cext.register_operation(_Dialect) 224// CHECK: class OneVariadicOperandOp(_ir.OpView): 225// CHECK-LABEL: OPERATION_NAME = "test.one_variadic_operand" 226def OneVariadicOperandOp : TestOp<"one_variadic_operand"> { 227 // CHECK: def __init__(self, non_variadic, variadic, loc=None, ip=None): 228 // CHECK: operands = [] 229 // CHECK: results = [] 230 // CHECK: attributes = {} 231 // CHECK: operands.append(non_variadic) 232 // CHECK: operands += [*variadic] 233 // CHECK: super().__init__(_ir.Operation.create( 234 // CHECK: "test.one_variadic_operand", attributes=attributes, operands=operands, results=results, 235 // CHECK: loc=loc, ip=ip)) 236 237 // CHECK: @property 238 // CHECK: def non_variadic(self): 239 // CHECK: return self.operation.operands[0] 240 // 241 // CHECK: @property 242 // CHECK: def variadic(self): 243 // CHECK: variadic_group_length = len(self.operation.operands) - 2 + 1 244 // CHECK: return self.operation.operands[1:1 + variadic_group_length] 245 let arguments = (ins AnyType:$non_variadic, Variadic<AnyType>:$variadic); 246} 247 248// CHECK: @_cext.register_operation(_Dialect) 249// CHECK: class OneVariadicResultOp(_ir.OpView): 250// CHECK-LABEL: OPERATION_NAME = "test.one_variadic_result" 251def OneVariadicResultOp : TestOp<"one_variadic_result"> { 252 // CHECK: def __init__(self, variadic, non_variadic, loc=None, ip=None): 253 // CHECK: operands = [] 254 // CHECK: results = [] 255 // CHECK: attributes = {} 256 // CHECK: results += [*variadic] 257 // CHECK: results.append(non_variadic) 258 // CHECK: super().__init__(_ir.Operation.create( 259 // CHECK: "test.one_variadic_result", attributes=attributes, operands=operands, results=results, 260 // CHECK: loc=loc, ip=ip)) 261 262 // CHECK: @property 263 // CHECK: def variadic(self): 264 // CHECK: variadic_group_length = len(self.operation.results) - 2 + 1 265 // CHECK: return self.operation.results[0:0 + variadic_group_length] 266 // 267 // CHECK: @property 268 // CHECK: def non_variadic(self): 269 // CHECK: variadic_group_length = len(self.operation.results) - 2 + 1 270 // CHECK: return self.operation.results[1 + variadic_group_length - 1] 271 let results = (outs Variadic<AnyType>:$variadic, AnyType:$non_variadic); 272} 273 274// CHECK: @_cext.register_operation(_Dialect) 275// CHECK: class PythonKeywordOp(_ir.OpView): 276// CHECK-LABEL: OPERATION_NAME = "test.python_keyword" 277def PythonKeywordOp : TestOp<"python_keyword"> { 278 // CHECK: def __init__(self, in_, loc=None, ip=None): 279 // CHECK: operands = [] 280 // CHECK: results = [] 281 // CHECK: attributes = {} 282 // CHECK: operands.append(in_) 283 // CHECK: super().__init__(_ir.Operation.create( 284 // CHECK: "test.python_keyword", attributes=attributes, operands=operands, results=results, 285 // CHECK: loc=loc, ip=ip)) 286 287 // CHECK: @property 288 // CHECK: def in_(self): 289 // CHECK: return self.operation.operands[0] 290 let arguments = (ins AnyType:$in); 291} 292 293// CHECK: @_cext.register_operation(_Dialect) 294// CHECK: class SameVariadicOperandSizeOp(_ir.OpView): 295// CHECK-LABEL: OPERATION_NAME = "test.same_variadic_operand" 296def SameVariadicOperandSizeOp : TestOp<"same_variadic_operand", 297 [SameVariadicOperandSize]> { 298 // CHECK: @property 299 // CHECK: def variadic1(self): 300 // CHECK: start, pg = _equally_sized_accessor(operation.operands, 2, 0, 0) 301 // CHECK: return self.operation.operands[start:start + pg] 302 // 303 // CHECK: @property 304 // CHECK: def non_variadic(self): 305 // CHECK: start, pg = _equally_sized_accessor(operation.operands, 2, 0, 1) 306 // CHECK: return self.operation.operands[start] 307 // 308 // CHECK: @property 309 // CHECK: def variadic2(self): 310 // CHECK: start, pg = _equally_sized_accessor(operation.operands, 2, 1, 1) 311 // CHECK: return self.operation.operands[start:start + pg] 312 let arguments = (ins Variadic<AnyType>:$variadic1, AnyType:$non_variadic, 313 Variadic<AnyType>:$variadic2); 314} 315 316// CHECK: @_cext.register_operation(_Dialect) 317// CHECK: class SameVariadicResultSizeOp(_ir.OpView): 318// CHECK-LABEL: OPERATION_NAME = "test.same_variadic_result" 319def SameVariadicResultSizeOp : TestOp<"same_variadic_result", 320 [SameVariadicResultSize]> { 321 // CHECK: @property 322 // CHECK: def variadic1(self): 323 // CHECK: start, pg = _equally_sized_accessor(operation.results, 2, 0, 0) 324 // CHECK: return self.operation.results[start:start + pg] 325 // 326 // CHECK: @property 327 // CHECK: def non_variadic(self): 328 // CHECK: start, pg = _equally_sized_accessor(operation.results, 2, 0, 1) 329 // CHECK: return self.operation.results[start] 330 // 331 // CHECK: @property 332 // CHECK: def variadic2(self): 333 // CHECK: start, pg = _equally_sized_accessor(operation.results, 2, 1, 1) 334 // CHECK: return self.operation.results[start:start + pg] 335 let results = (outs Variadic<AnyType>:$variadic1, AnyType:$non_variadic, 336 Variadic<AnyType>:$variadic2); 337} 338 339// CHECK: @_cext.register_operation(_Dialect) 340// CHECK: class SimpleOp(_ir.OpView): 341// CHECK-LABEL: OPERATION_NAME = "test.simple" 342def SimpleOp : TestOp<"simple"> { 343 // CHECK: def __init__(self, i64, f64, i32, f32, loc=None, ip=None): 344 // CHECK: operands = [] 345 // CHECK: results = [] 346 // CHECK: attributes = {} 347 // CHECK: results.append(i64) 348 // CHECK: results.append(f64) 349 // CHECK: operands.append(i32) 350 // CHECK: operands.append(f32) 351 // CHECK: super().__init__(_ir.Operation.create( 352 // CHECK: "test.simple", attributes=attributes, operands=operands, results=results, 353 // CHECK: loc=loc, ip=ip)) 354 355 // CHECK: @property 356 // CHECK: def i32(self): 357 // CHECK: return self.operation.operands[0] 358 // 359 // CHECK: @property 360 // CHECK: def f32(self): 361 // CHECK: return self.operation.operands[1] 362 let arguments = (ins I32:$i32, F32:$f32); 363 364 // CHECK: @property 365 // CHECK: def i64(self): 366 // CHECK: return self.operation.results[0] 367 // 368 // CHECK: @property 369 // CHECK: def f64(self): 370 // CHECK: return self.operation.results[1] 371 let results = (outs I64:$i64, F64:$f64); 372} 373