@@ -88,6 +88,19 @@ class CIR_Op<string mnemonic, list<Trait> traits = []> :
8888 code extraLLVMLoweringPatternDecl = "";
8989}
9090
91+ //===----------------------------------------------------------------------===//
92+ // CIR Operation Traits
93+ //===----------------------------------------------------------------------===//
94+
95+ class HasAtMostOneOfAttrsPred<list<string> names> :
96+ CPred<!foldl("0", names, acc, name, acc # " + (" # name # " ? 1 : 0)")
97+ # " <= 1">;
98+
99+ class HasAtMostOneOfAttrs<list<string> names> : PredOpTrait<
100+ "has only one of the optional attributes: " # !interleave(names, ", "),
101+ HasAtMostOneOfAttrsPred<!foreach(name, names, "$" # name)>
102+ >;
103+
91104//===----------------------------------------------------------------------===//
92105// CastOp
93106//===----------------------------------------------------------------------===//
@@ -2422,9 +2435,17 @@ def CIR_GetMemberOp : CIR_Op<"get_member"> {
24222435// TODO(CIR): FuncOp is still a tiny shell of what it will become. Many more
24232436// properties and attributes will be added as upstreaming continues.
24242437
2438+ def CIR_OptionalPriorityAttr : OptionalAttr<
2439+ DefaultValuedAttr<
2440+ ConfinedAttr<I32Attr, [IntMinValue<101>, IntMaxValue<65535>]>,
2441+ "65535"
2442+ >
2443+ >;
2444+
24252445def CIR_FuncOp : CIR_Op<"func", [
24262446 AutomaticAllocationScope, CallableOpInterface, FunctionOpInterface,
24272447 DeclareOpInterfaceMethods<CIRGlobalValueInterface>,
2448+ HasAtMostOneOfAttrs<["global_ctor_priority", "global_dtor_priority"]>,
24282449 IsolatedFromAbove
24292450]> {
24302451 let summary = "Declare or define a function";
@@ -2449,6 +2470,12 @@ def CIR_FuncOp : CIR_Op<"func", [
24492470 without a prototype and, consequently, may contain calls with invalid
24502471 arguments and undefined behavior.
24512472
2473+ The `global_ctor` keyword indicates whether a function should execute before
2474+ `main()` function, as specified by `__attribute__((constructor))`. An
2475+ execution priority can also be specified `global_ctor(<priority>)`.
2476+ Similarly, for global destructors both `global_dtor` and
2477+ `global_dtor(<priority>)` are available.
2478+
24522479 Example:
24532480
24542481 ```mlir
@@ -2487,7 +2514,9 @@ def CIR_FuncOp : CIR_Op<"func", [
24872514 UnitAttr:$comdat,
24882515 OptionalAttr<DictArrayAttr>:$arg_attrs,
24892516 OptionalAttr<DictArrayAttr>:$res_attrs,
2490- OptionalAttr<FlatSymbolRefAttr>:$aliasee);
2517+ OptionalAttr<FlatSymbolRefAttr>:$aliasee,
2518+ CIR_OptionalPriorityAttr:$global_ctor_priority,
2519+ CIR_OptionalPriorityAttr:$global_dtor_priority);
24912520
24922521 let regions = (region AnyRegion:$body);
24932522
0 commit comments