From e05262f88321ef3c96f1572e378b37473b0f0964 Mon Sep 17 00:00:00 2001 From: Sirui Mu Date: Tue, 9 Jan 2024 08:14:10 +0800 Subject: [PATCH] [CIR] Replace AnyType with CIR_AnyType (#371) This PR addresses #90. It introduces a new type constraint `CIR_AnyType` which allows CIR types and MLIR floating-point types. Present `AnyType` constraints are replaced with the new `CIR_AnyType` constraint. --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 132 +++++++++--------- .../include/clang/CIR/Dialect/IR/CIRTypes.td | 12 ++ 2 files changed, 78 insertions(+), 66 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index cf04425e132e..7903161b7abf 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -98,8 +98,8 @@ def CastOp : CIR_Op<"cast", [Pure]> { ``` }]; - let arguments = (ins CastKind:$kind, AnyType:$src); - let results = (outs AnyType:$result); + let arguments = (ins CastKind:$kind, CIR_AnyType:$src); + let results = (outs CIR_AnyType:$result); let assemblyFormat = [{ `(` $kind `,` $src `:` type($src) `)` @@ -168,10 +168,10 @@ def PtrDiffOp : CIR_Op<"ptr_diff", [Pure, SameTypeOperands]> { }]; let results = (outs CIR_IntType:$result); - let arguments = (ins AnyType:$lhs, AnyType:$rhs); + let arguments = (ins CIR_PointerType:$lhs, CIR_PointerType:$rhs); let assemblyFormat = [{ - `(` $lhs `,` $rhs `)` `:` type($lhs) `->` type($result) attr-dict + `(` $lhs `,` $rhs `)` `:` qualified(type($lhs)) `->` qualified(type($result)) attr-dict }]; // Already covered by the traits @@ -198,12 +198,12 @@ def PtrStrideOp : CIR_Op<"ptr_stride", ``` }]; - let arguments = (ins AnyType:$base, CIR_IntType:$stride); - let results = (outs AnyType:$result); + let arguments = (ins CIR_PointerType:$base, CIR_IntType:$stride); + let results = (outs CIR_PointerType:$result); let assemblyFormat = [{ - `(` $base `:` type($base) `,` $stride `:` qualified(type($stride)) `)` - `,` type($result) attr-dict + `(` $base `:` qualified(type($base)) `,` $stride `:` qualified(type($stride)) `)` + `,` qualified(type($result)) attr-dict }]; let extraClassDeclaration = [{ @@ -241,8 +241,8 @@ def ConstantOp : CIR_Op<"const", // The constant operation takes an attribute as the only input. let arguments = (ins TypedAttrInterface:$value); - // The constant operation returns a single value of AnyType. - let results = (outs AnyType:$res); + // The constant operation returns a single value of CIR_AnyType. + let results = (outs CIR_AnyType:$res); let assemblyFormat = [{ `(` custom($value) `)` attr-dict `:` type($res) @@ -389,7 +389,7 @@ def LoadOp : CIR_Op<"load", [ let arguments = (ins Arg:$addr, UnitAttr:$isDeref); - let results = (outs AnyType:$result); + let results = (outs CIR_AnyType:$result); // FIXME: we should not be printing `cir.ptr` below, that should come // from the pointer type directly. @@ -423,7 +423,7 @@ def StoreOp : CIR_Op<"store", [ ``` }]; - let arguments = (ins AnyType:$value, + let arguments = (ins CIR_AnyType:$value, Arg:$addr); @@ -458,7 +458,7 @@ def ReturnOp : CIR_Op<"return", [HasParent<"FuncOp, ScopeOp, IfOp, SwitchOp, Loo // The return operation takes an optional input operand to return. This // value must match the return type of the enclosing function. - let arguments = (ins Variadic:$input); + let arguments = (ins Variadic:$input); // The return operation only emits the input in the format if it is present. let assemblyFormat = "($input^ `:` type($input))? attr-dict "; @@ -561,7 +561,7 @@ def TernaryOp : CIR_Op<"ternary", let arguments = (ins CIR_BoolType:$cond); let regions = (region SizedRegion<1>:$trueRegion, SizedRegion<1>:$falseRegion); - let results = (outs Optional:$result); + let results = (outs Optional:$result); let skipDefaultBuilders = 1; let builders = [ @@ -671,7 +671,7 @@ def YieldOp : CIR_Op<"yield", [ReturnLike, Terminator, }]; let arguments = (ins OptionalAttr:$kind, - Variadic:$args); + Variadic:$args); let builders = [ OpBuilder<(ins), [{ /* nothing to do */ }]>, OpBuilder<(ins "YieldOpKind":$kind), [{ @@ -739,7 +739,7 @@ def ScopeOp : CIR_Op<"scope", [ will be inserted implicitly. }]; - let results = (outs Optional:$results); + let results = (outs Optional:$results); let regions = (region AnyRegion:$scopeRegion); let hasVerifier = 1; @@ -799,8 +799,8 @@ def UnaryOp : CIR_Op<"unary", [Pure, SameOperandsAndResultType]> { ``` }]; - let results = (outs AnyType:$result); - let arguments = (ins Arg:$kind, Arg:$input); + let results = (outs CIR_AnyType:$result); + let arguments = (ins Arg:$kind, Arg:$input); let assemblyFormat = [{ `(` $kind `,` $input `)` `:` type($input) `,` type($result) attr-dict @@ -852,10 +852,10 @@ def BinOp : CIR_Op<"binop", [Pure, ``` }]; - // TODO: get more accurate than AnyType - let results = (outs AnyType:$result); + // TODO: get more accurate than CIR_AnyType + let results = (outs CIR_AnyType:$result); let arguments = (ins Arg:$kind, - AnyType:$lhs, AnyType:$rhs); + CIR_AnyType:$lhs, CIR_AnyType:$rhs); let assemblyFormat = [{ `(` $kind `,` $lhs `,` $rhs `)` `:` type($lhs) attr-dict @@ -929,10 +929,10 @@ def CmpOp : CIR_Op<"cmp", [Pure, SameTypeOperands]> { ``` }]; - // TODO: get more accurate than AnyType - let results = (outs AnyType:$result); + // TODO: get more accurate than CIR_AnyType + let results = (outs CIR_AnyType:$result); let arguments = (ins Arg:$kind, - AnyType:$lhs, AnyType:$rhs); + CIR_AnyType:$lhs, CIR_AnyType:$rhs); let assemblyFormat = [{ `(` $kind `,` $lhs `,` $rhs `)` `:` type($lhs) `,` type($result) attr-dict @@ -1066,7 +1066,7 @@ def BrOp : CIR_Op<"br", }]> ]; - let arguments = (ins Variadic:$destOperands); + let arguments = (ins Variadic:$destOperands); let successors = (successor AnySuccessor:$dest); let assemblyFormat = [{ $dest (`(` $destOperands^ `:` type($destOperands) `)`)? attr-dict @@ -1111,8 +1111,8 @@ def BrCondOp : CIR_Op<"brcond", ]; let arguments = (ins CIR_BoolType:$cond, - Variadic:$destOperandsTrue, - Variadic:$destOperandsFalse); + Variadic:$destOperandsTrue, + Variadic:$destOperandsFalse); let successors = (successor AnySuccessor:$destTrue, AnySuccessor:$destFalse); let assemblyFormat = [{ $cond @@ -1420,7 +1420,7 @@ def VTableAddrPointOp : CIR_Op<"vtable.address_point", }]; let arguments = (ins OptionalAttr:$name, - Optional:$sym_addr, + Optional:$sym_addr, I32Attr:$vtable_index, I32Attr:$address_point_index); let results = (outs Res:$addr); @@ -1447,13 +1447,13 @@ def VTableAddrPointOp : CIR_Op<"vtable.address_point", def SetBitfieldOp : CIR_Op<"set_bitfield"> { let summary = "Set a bitfield"; - let description = [{ - The `cir.set_bitfield` operation provides a store-like access to + let description = [{ + The `cir.set_bitfield` operation provides a store-like access to a bit field of a record. It expects an address of a storage where to store, a type of the storage, a value being stored, a name of a bit field, a pointer to the storage in the - base record, a size of the storage, a size the bit field, an offset + base record, a size of the storage, a size the bit field, an offset of the bit field and a sign. Returns a value being stored. Example. @@ -1487,29 +1487,29 @@ def SetBitfieldOp : CIR_Op<"set_bitfield"> { }]; let arguments = (ins - AnyType:$dst, - AnyType:$src, + CIR_PointerType:$dst, + CIR_AnyType:$src, BitfieldInfoAttr:$bitfield_info ); let results = (outs CIR_IntType:$result); - let assemblyFormat = [{ `(`$bitfield_info`,` $dst`:`type($dst)`,` + let assemblyFormat = [{ `(`$bitfield_info`,` $dst`:`qualified(type($dst))`,` $src`:`type($src) `)` attr-dict `->` type($result) }]; - + let builders = [ OpBuilder<(ins "Type":$type, "Value":$dst, "Type":$storage_type, "Value":$src, "StringRef":$name, - "unsigned":$size, + "unsigned":$size, "unsigned":$offset, "bool":$is_signed ), - [{ - BitfieldInfoAttr info = - BitfieldInfoAttr::get($_builder.getContext(), + [{ + BitfieldInfoAttr info = + BitfieldInfoAttr::get($_builder.getContext(), name, storage_type, size, offset, is_signed); build($_builder, $_state, type, dst, src, info); @@ -1523,7 +1523,7 @@ def SetBitfieldOp : CIR_Op<"set_bitfield"> { def GetBitfieldOp : CIR_Op<"get_bitfield"> { let summary = "Get a bitfield"; - let description = [{ + let description = [{ The `cir.get_bitfield` operation provides a load-like access to a bit field of a record. @@ -1561,14 +1561,14 @@ def GetBitfieldOp : CIR_Op<"get_bitfield"> { }]; let arguments = (ins - AnyType:$addr, + CIR_PointerType:$addr, BitfieldInfoAttr:$bitfield_info ); let results = (outs CIR_IntType:$result); - let assemblyFormat = [{ `(`$bitfield_info `,` $addr attr-dict `:` - type($addr) `)` `->` type($result) }]; + let assemblyFormat = [{ `(`$bitfield_info `,` $addr attr-dict `:` + qualified(type($addr)) `)` `->` type($result) }]; let builders = [ OpBuilder<(ins "Type":$type, @@ -1580,8 +1580,8 @@ def GetBitfieldOp : CIR_Op<"get_bitfield"> { "bool":$is_signed ), [{ - BitfieldInfoAttr info = - BitfieldInfoAttr::get($_builder.getContext(), + BitfieldInfoAttr info = + BitfieldInfoAttr::get($_builder.getContext(), name, storage_type, size, offset, is_signed); build($_builder, $_state, type, addr, info); @@ -1669,7 +1669,7 @@ def VecExtractOp : CIR_Op<"vec.extract", [Pure, }]; let arguments = (ins CIR_VectorType:$vec, CIR_IntType:$index); - let results = (outs AnyType:$result); + let results = (outs CIR_AnyType:$result); let assemblyFormat = [{ $vec `[` $index `:` type($index) `]` type($vec) `->` type($result) attr-dict @@ -1691,7 +1691,7 @@ def VecCreateOp : CIR_Op<"vec.create", [Pure]> { in the vector type. }]; - let arguments = (ins Variadic:$elements); + let arguments = (ins Variadic:$elements); let results = (outs CIR_VectorType:$result); let assemblyFormat = [{ @@ -1922,9 +1922,9 @@ def CallOp : CIR_Op<"call", }]; let arguments = (ins OptionalAttr:$callee, - Variadic:$operands, + Variadic:$operands, OptionalAttr:$ast); - let results = (outs Variadic); + let results = (outs Variadic); let builders = [ OpBuilder<(ins "FuncOp":$callee, CArg<"ValueRange", "{}">:$operands), [{ @@ -2125,7 +2125,7 @@ def TryOp : CIR_Op<"try", let regions = (region SizedRegion<1>:$body); // FIXME: should be exception type. - let results = (outs AnyType:$result); + let results = (outs CIR_AnyType:$result); let assemblyFormat = [{ `{` @@ -2161,7 +2161,7 @@ def CatchOp : CIR_Op<"catch", let description = [{ }]; - let arguments = (ins AnyType:$exception_info, + let arguments = (ins CIR_AnyType:$exception_info, OptionalAttr:$catchers); let regions = (region VariadicRegion:$regions); @@ -2306,11 +2306,11 @@ def SameFirstSecondOperandAndResultType : def StdFindOp : CIR_Op<"std.find", [SameFirstSecondOperandAndResultType]> { let arguments = (ins FlatSymbolRefAttr:$original_fn, - AnyType:$first, - AnyType:$last, - AnyType:$pattern); + CIR_AnyType:$first, + CIR_AnyType:$last, + CIR_AnyType:$pattern); let summary = "std:find()"; - let results = (outs AnyType:$result); + let results = (outs CIR_AnyType:$result); let description = [{ Search for `pattern` in data range from `first` to `last`. This currently @@ -2342,9 +2342,9 @@ def StdFindOp : CIR_Op<"std.find", [SameFirstSecondOperandAndResultType]> { //===----------------------------------------------------------------------===// def IterBeginOp : CIR_Op<"iterator_begin"> { - let arguments = (ins FlatSymbolRefAttr:$original_fn, AnyType:$container); + let arguments = (ins FlatSymbolRefAttr:$original_fn, CIR_AnyType:$container); let summary = "Returns an iterator to the first element of a container"; - let results = (outs AnyType:$result); + let results = (outs CIR_AnyType:$result); let assemblyFormat = [{ `(` $original_fn `,` $container `:` type($container) @@ -2354,10 +2354,10 @@ def IterBeginOp : CIR_Op<"iterator_begin"> { } def IterEndOp : CIR_Op<"iterator_end"> { - let arguments = (ins FlatSymbolRefAttr:$original_fn, AnyType:$container); + let arguments = (ins FlatSymbolRefAttr:$original_fn, CIR_AnyType:$container); let summary = "Returns an iterator to the element following the last element" " of a container"; - let results = (outs AnyType:$result); + let results = (outs CIR_AnyType:$result); let assemblyFormat = [{ `(` $original_fn `,` $container `:` type($container) @@ -2416,7 +2416,7 @@ def VACopyOp : CIR_Op<"va.copy">, } def VAArgOp : CIR_Op<"va.arg">, - Results<(outs AnyType:$result)>, + Results<(outs CIR_AnyType:$result)>, Arguments<(ins CIR_PointerType:$arg_list)> { let summary = "Fetches next variadic element as a given type"; let assemblyFormat = "$arg_list attr-dict `:` functional-type(operands, $result)"; @@ -2498,7 +2498,7 @@ def ThrowOp : CIR_Op<"throw", ``` }]; - let arguments = (ins Optional:$exception_ptr, + let arguments = (ins Optional:$exception_ptr, OptionalAttr:$type_info, OptionalAttr:$dtor); @@ -2593,19 +2593,19 @@ def CIR_InlineAsmOp : CIR_Op<"asm", [RecursiveMemoryEffects]> { ``` ```mlir - cir.asm(x86_att, {"xyz"}) -> !void + cir.asm(x86_att, {"xyz"}) -> !void ``` }]; - let results = (outs Optional:$res); + let results = (outs Optional:$res); let arguments = ( ins StrAttr:$asm_string, - AsmFlavor:$asm_flavor); + AsmFlavor:$asm_flavor); let assemblyFormat = [{ `(`$asm_flavor`,` `{` $asm_string `}` `)` attr-dict `:` type($res) - }]; + }]; } -#endif // MLIR_CIR_DIALECT_CIR_OPS \ No newline at end of file +#endif // MLIR_CIR_DIALECT_CIR_OPS diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td index 0d568c2d504c..464ef2100d96 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td @@ -253,4 +253,16 @@ def VoidPtr : Type< "mlir::cir::VoidType::get($_builder.getContext()))"> { } +//===----------------------------------------------------------------------===// +// Global type constraints +//===----------------------------------------------------------------------===// + +def CIR_StructType : Type()">, + "CIR struct type">; + +def CIR_AnyType : AnyTypeOf<[ + CIR_IntType, CIR_PointerType, CIR_BoolType, CIR_ArrayType, CIR_VectorType, + CIR_FuncType, CIR_VoidType, CIR_StructType, AnyFloat, +]>; + #endif // MLIR_CIR_DIALECT_CIR_TYPES