From 423da7d660081f4501ac7e31079324e5560a4069 Mon Sep 17 00:00:00 2001 From: Schrodinger ZHU Yifan Date: Wed, 23 Oct 2024 17:55:27 -0400 Subject: [PATCH 1/5] [gccjit] add more operations --- include/mlir-gccjit/IR/GCCJITOps.td | 208 +++++++++++++++++++++++++++- src/GCCJITOps.cpp | 36 +++++ 2 files changed, 239 insertions(+), 5 deletions(-) diff --git a/include/mlir-gccjit/IR/GCCJITOps.td b/include/mlir-gccjit/IR/GCCJITOps.td index 3d54b3b..25ed121 100644 --- a/include/mlir-gccjit/IR/GCCJITOps.td +++ b/include/mlir-gccjit/IR/GCCJITOps.td @@ -331,13 +331,211 @@ def GlobalOp : GCCJIT_Op<"global", [IsolatedFromAbove]> { let assemblyFormat = [{ $glb_kind $sym_name - (`reg` `(` $reg_name^ `)`)? - (`align` `(` $alignment^ `)`)? - (`tls_model` `(` $tls_model^ `)`)? - (`link_section` `(` $link_section^ `)`)? - (`visibility` `(` $visibility^ `)`)? + oilist ( + `reg` `(` $reg_name `)` | + `align` `(` $alignment `)` | + `tls_model` `(` $tls_model `)` | + `link_section` `(` $link_section `)` | + `visibility` `(` $visibility `)` + ) custom($initializer, $body) `:` $type attr-dict }]; } +//===----------------------------------------------------------------------===// +// RValue Expressions +//===----------------------------------------------------------------------===// +// Value construction operations. +//===----------------------------------------------------------------------===// + +def SizeOfOp : GCCJIT_Op<"sizeof", [Pure]> { + let summary = "Size of a type"; + let description = [{ + The "sizeof" operation returns the size of a type in bytes. + ```mlir + %size = gccjit.sizeof !gccjit.int : !gccjit.int + ``` + }]; + let arguments = (ins TypeAttr:$type); + let results = (outs GCCJIT_IntType:$size); + let assemblyFormat = [{ + $type `:` type($size) attr-dict + }]; +} + +def ArrayOp : GCCJIT_Op<"array"> { + let summary = "Construct an array"; + let description = [{ + The "array" operation constructs an array from a list of elements. + ```mlir + %array = gccjit.array !gccjit.array [%1, %2, %3] + ``` + }]; + let arguments = (ins Variadic:$elements); + let results = (outs GCCJIT_ArrayType:$array); + let assemblyFormat = [{ + type($array) `[` custom(ref(type($array)), $elements, type($elements)) `]` attr-dict + }]; +} + +def VectorOp : GCCJIT_Op<"vector"> { + let summary = "Construct a vector"; + let description = [{ + The "vector" operation constructs a vector from a list of elements. + ```mlir + %vector = gccjit.vector !gccjit.vector [%1, %2, %3, %4] + ``` + }]; + let arguments = (ins Variadic:$elements); + let results = (outs GCCJIT_VectorType:$vector); + let assemblyFormat = [{ + type($vector) `[` custom(ref(type($vector)), $elements, type($elements)) `]` attr-dict + }]; +} + +//===----------------------------------------------------------------------===// +// Unary Operations +//===----------------------------------------------------------------------===// + +def UOp_Minus : I32EnumAttrCase<"Minus", 0, "minus">; +def UOp_BitwiseNegate : I32EnumAttrCase<"BitwiseNegate", 1, "bitwise_negate">; +def UOp_LogicalNegate : I32EnumAttrCase<"LogicalNegate", 2, "logical_negate">; +def UOp_Abs : I32EnumAttrCase<"Abs", 3, "abs">; +def UOpAttr : I32EnumAttr<"UOp", "unary operation", [ + UOp_Minus, UOp_BitwiseNegate, UOp_LogicalNegate, UOp_Abs +]> { + let cppNamespace = "::mlir::gccjit"; +} + +def UnaryOp : GCCJIT_Op<"unary"> { + let summary = "Unary operation"; + let description = [{ + The "unary" operation represents a unary operation. + ```mlir + %res = gccjit.unary minus ( %operand : !gccjit.int ) : !gccjit.int + ``` + }]; + let arguments = (ins UOpAttr:$op, AnyType:$value); + let results = (outs AnyType:$result); + let assemblyFormat = [{ + $op `(` $value `:` type($value) `)` `:` type($result) attr-dict + }]; +} + +//===----------------------------------------------------------------------===// +// Binary Operations +//===----------------------------------------------------------------------===// + +def BOp_Plus : I32EnumAttrCase<"Plus", 0, "plus">; +def BOp_Minus : I32EnumAttrCase<"Minus", 1, "minus">; +def BOp_Mult : I32EnumAttrCase<"Mult", 2, "mult">; +def BOp_Divide : I32EnumAttrCase<"Divide", 3, "divide">; +def BOp_Modulo : I32EnumAttrCase<"Modulo", 4, "modulo">; +def BOp_BitwiseAnd : I32EnumAttrCase<"BitwiseAnd", 5, "bitwise_and">; +def BOp_BitwiseOr : I32EnumAttrCase<"BitwiseXor", 6, "bitwise_xor">; +def BOp_BitwiseXor : I32EnumAttrCase<"BitwiseOr", 7, "bitwise_or">; +def BOp_LogicalAnd : I32EnumAttrCase<"LogicalAnd", 8, "logical_and">; +def BOp_LogicalOr : I32EnumAttrCase<"LogicalOr", 9, "logical_or">; +def BOp_LShift : I32EnumAttrCase<"LShift", 10, "lshift">; +def BOp_RShift : I32EnumAttrCase<"RShift", 11, "rshift">; + +def BOpAttr : I32EnumAttr<"BOp", "binary operation", [ + BOp_Plus, BOp_Minus, BOp_Mult, BOp_Divide, BOp_Modulo, + BOp_BitwiseAnd, BOp_BitwiseOr, BOp_BitwiseXor, + BOp_LogicalAnd, BOp_LogicalOr, BOp_LShift, BOp_RShift +]> { + let cppNamespace = "::mlir::gccjit"; +} + +def BinaryOp : GCCJIT_Op<"binary"> { + let summary = "Binary operation"; + let description = [{ + The "binary" operation represents a binary operation. + ```mlir + %res = gccjit.binary plus ( %lhs : !gccjit.int, %rhs : !gccjit.int ) : !gccjit.int + ``` + }]; + let arguments = (ins BOpAttr:$op, AnyType:$lhs, AnyType:$rhs); + let results = (outs AnyType:$result); + let assemblyFormat = [{ + $op `(` $lhs `:` type($lhs) `,` $rhs `:` type($rhs) `)` `:` type($result) attr-dict + }]; +} + +//===----------------------------------------------------------------------===// +// Comparison Operations +//===----------------------------------------------------------------------===// +def CmpOp_Eq : I32EnumAttrCase<"Eq", 0, "eq">; +def CmpOp_Ne : I32EnumAttrCase<"Ne", 1, "ne">; +def CmpOp_Lt : I32EnumAttrCase<"Lt", 2, "lt">; +def CmpOp_Le : I32EnumAttrCase<"Le", 3, "le">; +def CmpOp_Gt : I32EnumAttrCase<"Gt", 4, "gt">; +def CmpOp_Ge : I32EnumAttrCase<"Ge", 5, "ge">; + +def CmpOpAttr : I32EnumAttr<"CmpOp", "comparison operation", [ + CmpOp_Eq, CmpOp_Ne, CmpOp_Lt, CmpOp_Le, CmpOp_Gt, CmpOp_Ge +]> { + let cppNamespace = "::mlir::gccjit"; +} + +def CompareOp : GCCJIT_Op<"compare"> { + let summary = "Comparison operation"; + let description = [{ + The "compare" operation represents a comparison operation. + ```mlir + %res = gccjit.compare eq ( %lhs : !gccjit.int, %rhs : !gccjit.int ) : !gccjit.bool + ``` + }]; + let arguments = (ins CmpOpAttr:$op, AnyType:$lhs, AnyType:$rhs); + let results = (outs GCCJIT_BoolType:$result); + let assemblyFormat = [{ + $op `(` $lhs `:` type($lhs) `,` $rhs `:` type($rhs) `)` `:` type($result) attr-dict + }]; +} + +//===----------------------------------------------------------------------===// +// Function Call +//===----------------------------------------------------------------------===// +def CallOp : GCCJIT_Op<"call"> { + let summary = "Function call"; + let description = [{ + The "call" operation represents a function call. + ```mlir + %res = gccjit.call @foo ( %arg0 : !gccjit.int, %arg1 : !gccjit.int ) : !gccjit.int + ``` + If tail call is required, the `tail` attribute should be set to true. + ```mlir + %res = gccjit.call tail @foo ( %arg0 : !gccjit.int, %arg1 : !gccjit.int ) : !gccjit.int + ``` + }]; + let arguments = (ins SymbolRefAttr:$callee, Variadic:$args, UnitAttr:$tail); + let results = (outs Optional:$result); + let assemblyFormat = [{ + custom($tail) + $callee `(` $args `)` `:` functional-type($args, $result) attr-dict + }]; +} + +def PtrCallOp : GCCJIT_Op<"ptr_call"> { + let summary = "Function pointer call"; + let description = [{ + The "ptr_call" operation represents a function pointer call. + ```mlir + %res = gccjit.ptr_call %fn ( %arg0 : !gccjit.int, %arg1 : !gccjit.int ) : !gccjit.int + ``` + If tail call is required, the `tail` attribute should be set to true. + ```mlir + %res = gccjit.ptr_call tail %fn ( %arg0 : !gccjit.int, %arg1 : !gccjit.int ) : !gccjit.int + ``` + }]; + let arguments = (ins GCCJIT_PointerType:$callee, Variadic:$args, UnitAttr:$tail); + let results = (outs Optional:$result); + let assemblyFormat = [{ + custom($tail) + $callee `(` $args `)` `:` functional-type($args, $result) + custom(ref(type($args)), ref(type($result)), type($callee)) + attr-dict + }]; +} + #endif // GCCJIT_OPS diff --git a/src/GCCJITOps.cpp b/src/GCCJITOps.cpp index 81d3a2d..3e5d8e0 100644 --- a/src/GCCJITOps.cpp +++ b/src/GCCJITOps.cpp @@ -43,6 +43,7 @@ #include "mlir/Support/LLVM.h" #include "mlir/Support/LogicalResult.h" #include "llvm/ADT/SmallVector.h" +#include using namespace mlir; using namespace mlir::gccjit; @@ -256,6 +257,37 @@ void printGlobalInitializer(OpAsmPrinter &p, Operation *op, } } +ParseResult parseArrayOrVectorElements( + OpAsmParser &parser, Type expectedType, + llvm::SmallVectorImpl &elementValues, + llvm::SmallVectorImpl &elementTypes) { + llvm_unreachable("Not implemented"); +} + +void printArrayOrVectorElements(OpAsmPrinter &p, Operation *op, + Type expectedType, OperandRange elementValues, + ValueTypeRange elementTypes) { + llvm_unreachable("Not implemented"); +} + +ParseResult parseTailCallAttr(OpAsmParser &parser, UnitAttr &tailCallAttr) { + if (parser.parseOptionalKeyword("tail")) + tailCallAttr = UnitAttr::get(parser.getContext()); + return success(); +} +void printTailCallAttr(OpAsmPrinter &p, Operation *, UnitAttr tailCallAttr) { + if (tailCallAttr) + p << " tail"; +} +ParseResult parsePtrCalleeTypeInference(OpAsmParser &parser, TypeRange argTypes, + Type resultType, Type &calleeType) { + llvm_unreachable("Not implemented"); +} + +void printPtrCalleeTypeInference(OpAsmPrinter &, Operation *, + ValueTypeRange, Type, Type) { + // nothing to print +} } // namespace #define GET_OP_CLASSES @@ -352,3 +384,7 @@ LogicalResult EvalOp::verify() { return emitOpError("operand should be an rvalue"); return success(); } + +//===----------------------------------------------------------------------===// +// RValue Expressions +//===----------------------------------------------------------------------===// From 50c07c05dd30e226be69569b1eae3a9db5882199 Mon Sep 17 00:00:00 2001 From: Schrodinger ZHU Yifan Date: Wed, 23 Oct 2024 19:28:24 -0400 Subject: [PATCH 2/5] fix --- src/GCCJITOps.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/GCCJITOps.cpp b/src/GCCJITOps.cpp index 3e5d8e0..dab7cd4 100644 --- a/src/GCCJITOps.cpp +++ b/src/GCCJITOps.cpp @@ -43,7 +43,6 @@ #include "mlir/Support/LLVM.h" #include "mlir/Support/LogicalResult.h" #include "llvm/ADT/SmallVector.h" -#include using namespace mlir; using namespace mlir::gccjit; From 960e495fbb71eae2cfb3679db56196d4f765bdf3 Mon Sep 17 00:00:00 2001 From: Schrodinger ZHU Yifan Date: Wed, 23 Oct 2024 19:53:06 -0400 Subject: [PATCH 3/5] [gccjit] more --- include/mlir-gccjit/IR/GCCJITAttrs.td | 4 +- include/mlir-gccjit/IR/GCCJITOps.td | 171 +++++++++++++++++++++++++- src/GCCJITOps.cpp | 21 +--- src/Translation/TranslateToGCCJIT.cpp | 2 +- 4 files changed, 176 insertions(+), 22 deletions(-) diff --git a/include/mlir-gccjit/IR/GCCJITAttrs.td b/include/mlir-gccjit/IR/GCCJITAttrs.td index a7029c8..d21ce3e 100644 --- a/include/mlir-gccjit/IR/GCCJITAttrs.td +++ b/include/mlir-gccjit/IR/GCCJITAttrs.td @@ -257,9 +257,9 @@ def VisibilityAttr : GCCJIT_Attr<"Visibility", "visibility"> { } //===----------------------------------------------------------------------===// -// StringInitializer Attr +// StringLiteral Attr //===----------------------------------------------------------------------===// -def StringInitializerAttr : GCCJIT_Attr<"StringInitializer", "string_initializer"> { +def StringLiteralAttr : GCCJIT_Attr<"StringLiteral", "str"> { let summary = "String initializer"; let parameters = (ins "StringAttr":$initializer); let description = [{ diff --git a/include/mlir-gccjit/IR/GCCJITOps.td b/include/mlir-gccjit/IR/GCCJITOps.td index 25ed121..3553e03 100644 --- a/include/mlir-gccjit/IR/GCCJITOps.td +++ b/include/mlir-gccjit/IR/GCCJITOps.td @@ -325,7 +325,7 @@ def GlobalOp : GCCJIT_Op<"global", [IsolatedFromAbove]> { OptionalAttr:$tls_model, OptionalAttr:$link_section, OptionalAttr:$visibility, - OptionalAttr>:$initializer + OptionalAttr>:$initializer ); let regions = (region AnyRegion:$body); let assemblyFormat = [{ @@ -532,9 +532,172 @@ def PtrCallOp : GCCJIT_Op<"ptr_call"> { let results = (outs Optional:$result); let assemblyFormat = [{ custom($tail) - $callee `(` $args `)` `:` functional-type($args, $result) - custom(ref(type($args)), ref(type($result)), type($callee)) - attr-dict + $callee `(` $args `)` `:` functional-type(operands, results) attr-dict + }]; +} + +//===----------------------------------------------------------------------===// +// Casting +//===----------------------------------------------------------------------===// +def CastOp : GCCJIT_Op<"cast"> { + let summary = "Cast operation"; + let description = [{ + The "cast" operation represents a casting operation. + ```mlir + %res = gccjit.cast %value : !gccjit.int to !gccjit.int + ``` + Currently only a limited set of conversions are possible: + - `int <-> float` + - `int <-> pointer` + - `pointer <-> pointer` + }]; + let arguments = (ins AnyType:$value); + let results = (outs AnyType:$result); + let assemblyFormat = [{ + $value `:` type($value) `to` type($result) attr-dict + }]; +} + +def BitCast : GCCJIT_Op<"bitcast"> { + let summary = "Bitcast operation"; + let description = [{ + Given an rvalue of T, bitcast it to another type, + meaning that this will generate a new rvalue by interpreting + the bits of rvalue to the layout of type. + + The type of rvalue must be the same size as the size of type. + ```mlir + %res = gccjit.bitcast %value : !gccjit.int to !gccjit.int + ``` + }]; + let arguments = (ins AnyType:$value); + let results = (outs AnyType:$result); + let assemblyFormat = [{ + $value `:` type($value) `to` type($result) attr-dict + }]; +} + +//===----------------------------------------------------------------------===// +// LiteralOp +//===----------------------------------------------------------------------===// +def LiteralOp : GCCJIT_Op<"literal"> { + let summary = "Literal operation"; + let description = [{ + The "literal" operation represents a literal value. + ```mlir + !str = !gccjit.qualified, const> + %res = gccjit.literal #gccjit.str<"Hello, World!"> : !str + ``` + }]; + let arguments = (ins StringLiteralAttr:$value); + let results = (outs GCCJIT_QualifiedType:$result); + let assemblyFormat = [{ + $value `:` type($result) attr-dict + }]; +} + +//===----------------------------------------------------------------------===// +// Addr Operation +//===----------------------------------------------------------------------===// +def AddrOp : GCCJIT_Op<"addr"> { + let summary = "Address of an lvalue"; + let description = [{ + The "addr" operation returns the address of an lvalue. + ```mlir + %res = gccjit.addr (%lvalue : !gccjit.lvalue) : !gccjit.ptr + ``` + }]; + let arguments = (ins GCCJIT_LValueType:$lvalue); + let results = (outs GCCJIT_PointerType:$result); + let assemblyFormat = [{ + `(` $lvalue `:` type($lvalue) `)` `:` type($result) attr-dict + }]; +} + +//===----------------------------------------------------------------------===// +// Deref Operation +//===----------------------------------------------------------------------===// +def DerefOp : GCCJIT_Op<"deref"> { + let summary = "Dereference a pointer"; + let description = [{ + The "deref" operation dereferences a pointer. + ```mlir + %res = gccjit.deref (%ptr : !gccjit.ptr) : !gccjit.lvalue + ``` + }]; + let arguments = (ins GCCJIT_PointerType:$ptr); + let results = (outs GCCJIT_LValueType:$result); + let assemblyFormat = [{ + `(` $ptr `:` type($ptr) `)` `:` type($result) attr-dict + }]; +} + +//===----------------------------------------------------------------------===// +// Array Access +//===----------------------------------------------------------------------===// +def ArrayAccessOp : GCCJIT_Op<"array_access"> { + let summary = "Array indexing"; + let description = [{ + The "array_idx" operation represents an array indexing operation. + ```mlir + %res = gccjit.array_idx %array[%idx] : (!gccjit.array, !i32) -> !gccjit.lvalue + ``` + }]; + let arguments = (ins GCCJIT_PointerType:$array, GCCJIT_IntType:$idx); + let results = (outs GCCJIT_LValueType:$result); + let assemblyFormat = [{ + $array `[` $idx `]` `:` functional-type(operands, results) attr-dict + }]; +} + +//===----------------------------------------------------------------------===// +// Assignment Operation +//===----------------------------------------------------------------------===// +def AssignOp : GCCJIT_Op<"assign"> { + let summary = "Assignment operation"; + let description = [{ + The "assign" operation represents an assignment operation. + ```mlir + gccjit.assign %rvalue to $lvalue : !gccjit.lvalue, !i32 + ``` + }]; + let arguments = (ins AnyType:$rvalue, GCCJIT_LValueType:$lvalue); + let assemblyFormat = [{ + $rvalue `to` $lvalue `:` type($rvalue) `,` type($lvalue) attr-dict + }]; +} + +//===----------------------------------------------------------------------===// +// Update Operation +//===----------------------------------------------------------------------===// +def UpdateOp : GCCJIT_Op<"update"> { + let summary = "Update operation"; + let description = [{ + The "update" operation represents an update operation. + ```mlir + gccjit.update minus %rvalue to $lvalue : !gccjit.lvalue, !i32 + ``` + }]; + let arguments = (ins BOpAttr:$op, AnyType:$rvalue, GCCJIT_LValueType:$lvalue); + let assemblyFormat = [{ + $op $rvalue `to` $lvalue `:` type($rvalue) `,` type($lvalue) attr-dict + }]; +} + +//===----------------------------------------------------------------------===// +// Comment Operation +//===----------------------------------------------------------------------===// +def CommentOp : GCCJIT_Op<"comment", [Pure]> { + let summary = "Comment operation"; + let description = [{ + The "comment" operation represents a comment. + ```mlir + gccjit.comment #gccjit.str<"This is a comment"> + ``` + }]; + let arguments = (ins StringLiteralAttr:$comment); + let assemblyFormat = [{ + $comment attr-dict }]; } diff --git a/src/GCCJITOps.cpp b/src/GCCJITOps.cpp index dab7cd4..413c4ec 100644 --- a/src/GCCJITOps.cpp +++ b/src/GCCJITOps.cpp @@ -190,11 +190,11 @@ ParseResult parseGlobalInitializer(OpAsmParser &parser, Attribute &initializer, if (parser.parseLParen()) return parser.emitError(parser.getCurrentLocation(), "expected '(' after 'literal'"); - StringInitializerAttr stringInitializer; - if (parser.parseCustomAttributeWithFallback(stringInitializer)) + StringLiteralAttr StringLiteral; + if (parser.parseCustomAttributeWithFallback(StringLiteral)) return parser.emitError(parser.getCurrentLocation(), "expected string initializer"); - initializer = stringInitializer; + initializer = StringLiteral; if (parser.parseRParen()) return parser.emitError(parser.getCurrentLocation(), "expected ')' after string initializer"); @@ -233,10 +233,10 @@ ParseResult parseGlobalInitializer(OpAsmParser &parser, Attribute &initializer, void printGlobalInitializer(OpAsmPrinter &p, Operation *op, Attribute initializer, Region &body) { - if (auto stringInitializer = - dyn_cast_if_present(initializer)) { + if (auto StringLiteral = + dyn_cast_if_present(initializer)) { p << "literal("; - p.printStrippedAttrOrType(stringInitializer); + p.printStrippedAttrOrType(StringLiteral); p << ")"; return; } @@ -278,15 +278,6 @@ void printTailCallAttr(OpAsmPrinter &p, Operation *, UnitAttr tailCallAttr) { if (tailCallAttr) p << " tail"; } -ParseResult parsePtrCalleeTypeInference(OpAsmParser &parser, TypeRange argTypes, - Type resultType, Type &calleeType) { - llvm_unreachable("Not implemented"); -} - -void printPtrCalleeTypeInference(OpAsmPrinter &, Operation *, - ValueTypeRange, Type, Type) { - // nothing to print -} } // namespace #define GET_OP_CLASSES diff --git a/src/Translation/TranslateToGCCJIT.cpp b/src/Translation/TranslateToGCCJIT.cpp index 4f17be8..5584793 100644 --- a/src/Translation/TranslateToGCCJIT.cpp +++ b/src/Translation/TranslateToGCCJIT.cpp @@ -333,7 +333,7 @@ void Translator::declareAllFunctionAndGlobals() { visibility->getVisibility().str().c_str()); if (auto initializer = global.getInitializer()) { llvm::TypeSwitch(*initializer) - .Case([&](StringInitializerAttr attr) { + .Case([&](StringLiteralAttr attr) { auto str = attr.getInitializer(); auto blob = str.str(); gcc_jit_global_set_initializer(globalHandle, blob.c_str(), From 8eec254e14846ac02d8bac1b15936da34ad04c34 Mon Sep 17 00:00:00 2001 From: Schrodinger ZHU Yifan Date: Wed, 23 Oct 2024 20:07:01 -0400 Subject: [PATCH 4/5] more --- include/mlir-gccjit/IR/GCCJITOps.td | 55 +++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/include/mlir-gccjit/IR/GCCJITOps.td b/include/mlir-gccjit/IR/GCCJITOps.td index 3553e03..075cd52 100644 --- a/include/mlir-gccjit/IR/GCCJITOps.td +++ b/include/mlir-gccjit/IR/GCCJITOps.td @@ -291,9 +291,11 @@ def LocalOp : GCCJIT_Op<"local", [ParentOneOf<["FuncOp"]>]> { ); let results = (outs GCCJIT_LValueType:$var); let assemblyFormat = [{ - (`reg` `(` $reg_name^ `)`)? - (`align` `(` $alignment^ `)`)? - (`tls_model` `(` $tls_model^ `)`)? + oilist ( + `reg` `(` $reg_name `)` | + `align` `(` $alignment `)` | + `tls_model` `(` $tls_model `)` + ) `:` type($var) attr-dict }]; } @@ -363,12 +365,12 @@ def SizeOfOp : GCCJIT_Op<"sizeof", [Pure]> { }]; } -def ArrayOp : GCCJIT_Op<"array"> { +def NewArrayOp : GCCJIT_Op<"new_array"> { let summary = "Construct an array"; let description = [{ The "array" operation constructs an array from a list of elements. ```mlir - %array = gccjit.array !gccjit.array [%1, %2, %3] + %array = gccjit.new_array !gccjit.array [%1, %2, %3] ``` }]; let arguments = (ins Variadic:$elements); @@ -378,12 +380,51 @@ def ArrayOp : GCCJIT_Op<"array"> { }]; } -def VectorOp : GCCJIT_Op<"vector"> { +def NewStructOp : GCCJIT_Op<"new_struct"> { + let summary = "Construct a struct"; + let description = [{ + The "struct" operation constructs a struct from a list of elements. + Each value has to have the same unqualified type as the field it is applied to. + ```mlir + %struct = gccjit.new_struct [0, 1] [%1, %2] : (!i32, !i32) -> !gccjit.struct<(!i32, !i32)> + ``` + }]; + let arguments = (ins + DenseI32ArrayAttr:$indices, + Variadic:$elements + ); + // TODO: Add support for struct type. + let results = (outs AnyType:$result); + let assemblyFormat = [{ + $indices `[` $elements `]` `:` functional-type(operands, results) attr-dict + }]; +} + +def NewUnionOp : GCCJIT_Op<"new_union"> { + let summary = "Construct a union"; + let description = [{ + The "union" operation constructs a union from a list of elements. + Each value has to have the same unqualified type as the field it is applied to. + ```mlir + %union = gccjit.new_union %1 at 0 : !i32, !gccjit.union<(!i32)> + ``` + }]; + let arguments = (ins + AnyType:$element, + IndexAttr:$index + ); + let results = (outs AnyType:$result); + let assemblyFormat = [{ + $element `at` $index `:` type($element) `,` type($result) attr-dict + }]; +} + +def NewVectorOp : GCCJIT_Op<"new_vector"> { let summary = "Construct a vector"; let description = [{ The "vector" operation constructs a vector from a list of elements. ```mlir - %vector = gccjit.vector !gccjit.vector [%1, %2, %3, %4] + %vector = gccjit.new_vector !gccjit.vector [%1, %2, %3, %4] ``` }]; let arguments = (ins Variadic:$elements); From 362160d7045b689275c5eb6ebe7a5ce3172c974f Mon Sep 17 00:00:00 2001 From: Schrodinger ZHU Yifan Date: Wed, 23 Oct 2024 20:13:46 -0400 Subject: [PATCH 5/5] more --- include/mlir-gccjit/IR/GCCJITOps.td | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/include/mlir-gccjit/IR/GCCJITOps.td b/include/mlir-gccjit/IR/GCCJITOps.td index 075cd52..d22d9b7 100644 --- a/include/mlir-gccjit/IR/GCCJITOps.td +++ b/include/mlir-gccjit/IR/GCCJITOps.td @@ -742,4 +742,42 @@ def CommentOp : GCCJIT_Op<"comment", [Pure]> { }]; } +//===----------------------------------------------------------------------===// +// Access field +//===----------------------------------------------------------------------===// +def AccessFieldOp : GCCJIT_Op<"access_field"> { + let summary = "Access field operation"; + let description = [{ + The "access_field" operation represents an access field operation. + LValue field access returns an lvalue. + RValue field access returns an rvalue. + ```mlir + %res0 = gccjit.access_field %struct [0] : !gccjit.lvalue> -> !gccjit.lvalue + %res1 = gccjit.access_field %struct [1] : !gccjit.struct<(!i32, !i32)> -> !i32 + ``` + }]; + let arguments = (ins AnyType:$composite, IndexAttr:$field); + let results = (outs GCCJIT_LValueType:$result); + let assemblyFormat = [{ + $composite `[` $field `]` `:` functional-type(operands, results) attr-dict + }]; +} + +//===----------------------------------------------------------------------===// +// Deref Field +//===----------------------------------------------------------------------===// +def DerefFieldOp : GCCJIT_Op<"deref_field"> { + let summary = "Dereference field operation"; + let description = [{ + The "deref_field" operation represents a dereference field operation. + ```mlir + %res = gccjit.deref_field %ptr [0] : !gccjit.ptr> -> !gccjit.lvalue + ``` + }]; + let arguments = (ins GCCJIT_PointerType:$ptr, IndexAttr:$field); + let results = (outs GCCJIT_LValueType:$result); + let assemblyFormat = [{ + $ptr `[` $field `]` `:` functional-type(operands, results) attr-dict + }]; +} #endif // GCCJIT_OPS