forked from llvm/clangir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCIROps.td
3795 lines (3133 loc) · 125 KB
/
CIROps.td
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===-- CIROps.td - CIR dialect definition -----------------*- tablegen -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// Definition of the CIR dialect
///
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_CIR_DIALECT_IR_CIROPS
#define LLVM_CLANG_CIR_DIALECT_IR_CIROPS
include "clang/CIR/Dialect/IR/CIRDialect.td"
include "clang/CIR/Dialect/IR/CIRTypes.td"
include "clang/CIR/Dialect/IR/CIRAttrs.td"
include "clang/CIR/Interfaces/ASTAttrInterfaces.td"
include "clang/CIR/Interfaces/CIROpInterfaces.td"
include "clang/CIR/Interfaces/CIRLoopOpInterface.td"
include "mlir/Interfaces/ControlFlowInterfaces.td"
include "mlir/Interfaces/FunctionInterfaces.td"
include "mlir/Interfaces/InferTypeOpInterface.td"
include "mlir/Interfaces/LoopLikeInterface.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
include "mlir/IR/BuiltinAttributeInterfaces.td"
include "mlir/IR/EnumAttr.td"
include "mlir/IR/SymbolInterfaces.td"
include "mlir/IR/CommonAttrConstraints.td"
//===----------------------------------------------------------------------===//
// CIR Ops
//===----------------------------------------------------------------------===//
class CIR_Op<string mnemonic, list<Trait> traits = []> :
Op<CIR_Dialect, mnemonic, traits>;
//===----------------------------------------------------------------------===//
// CIR Op Traits
//===----------------------------------------------------------------------===//
def SameFirstOperandAndResultType :
NativeOpTrait<"SameFirstOperandAndResultType">;
def SameSecondOperandAndResultType :
NativeOpTrait<"SameSecondOperandAndResultType">;
def SameFirstSecondOperandAndResultType :
NativeOpTrait<"SameFirstSecondOperandAndResultType">;
//===----------------------------------------------------------------------===//
// CastOp
//===----------------------------------------------------------------------===//
// The enumaration value isn't in sync with clang.
def CK_IntegralToBoolean : I32EnumAttrCase<"int_to_bool", 1>;
def CK_ArrayToPointerDecay : I32EnumAttrCase<"array_to_ptrdecay", 2>;
def CK_IntegralCast : I32EnumAttrCase<"integral", 3>;
def CK_BitCast : I32EnumAttrCase<"bitcast", 4>;
def CK_FloatingCast : I32EnumAttrCase<"floating", 5>;
def CK_PtrToBoolean : I32EnumAttrCase<"ptr_to_bool", 6>;
def CK_FloatToIntegral : I32EnumAttrCase<"float_to_int", 7>;
def CK_IntegralToPointer : I32EnumAttrCase<"int_to_ptr", 8>;
def CK_PointerToIntegral : I32EnumAttrCase<"ptr_to_int", 9>;
def CK_FloatToBoolean : I32EnumAttrCase<"float_to_bool", 10>;
def CK_BooleanToIntegral : I32EnumAttrCase<"bool_to_int", 11>;
def CK_IntegralToFloat : I32EnumAttrCase<"int_to_float", 12>;
def CK_BooleanToFloat : I32EnumAttrCase<"bool_to_float", 13>;
def CastKind : I32EnumAttr<
"CastKind",
"cast kind",
[CK_IntegralToBoolean, CK_ArrayToPointerDecay, CK_IntegralCast,
CK_BitCast, CK_FloatingCast, CK_PtrToBoolean, CK_FloatToIntegral,
CK_IntegralToPointer, CK_PointerToIntegral, CK_FloatToBoolean,
CK_BooleanToIntegral, CK_IntegralToFloat, CK_BooleanToFloat]> {
let cppNamespace = "::mlir::cir";
}
def CastOp : CIR_Op<"cast", [Pure]> {
// FIXME: not all conversions are free of side effects.
let summary = "Conversion between values of different types";
let description = [{
Apply C/C++ usual conversions rules between values. Currently supported kinds:
- `array_to_ptrdecay`
- `bitcast`
- `integral`
- `int_to_bool`
- `int_to_float`
- `floating`
- `float_to_int`
- `float_to_bool`
- `ptr_to_int`
- `ptr_to_bool`
- `bool_to_int`
- `bool_to_float`
This is effectively a subset of the rules from
`llvm-project/clang/include/clang/AST/OperationKinds.def`; but note that some
of the conversions aren't implemented in terms of `cir.cast`, `lvalue-to-rvalue`
for instance is modeled as a regular `cir.load`.
```mlir
%4 = cir.cast (int_to_bool, %3 : i32), !cir.bool
...
%x = cir.cast(array_to_ptrdecay, %0 : !cir.ptr<!cir.array<i32 x 10>>), !cir.ptr<i32>
```
}];
let arguments = (ins CastKind:$kind, CIR_AnyType:$src);
let results = (outs CIR_AnyType:$result);
let assemblyFormat = [{
`(` $kind `,` $src `:` type($src) `)`
`,` type($result) attr-dict
}];
// The input and output types should match the cast kind.
let hasVerifier = 1;
let hasFolder = 1;
}
//===----------------------------------------------------------------------===//
// DynamicCastOp
//===----------------------------------------------------------------------===//
def DCK_PtrCast : I32EnumAttrCase<"ptr", 1>;
def DCK_RefCast : I32EnumAttrCase<"ref", 2>;
def DynamicCastKind : I32EnumAttr<
"DynamicCastKind", "dynamic cast kind", [DCK_PtrCast, DCK_RefCast]> {
let cppNamespace = "::mlir::cir";
}
def DynamicCastOp : CIR_Op<"dyn_cast"> {
let summary = "Perform dynamic cast on struct pointers";
let description = [{
The `cir.dyn_cast` operation models part of the semantics of the
`dynamic_cast` operator in C++. It can be used to perform 2 kinds of casts
on struct pointers:
- Down-cast, which casts a base class pointer to a derived class pointer;
- Side-cast, which casts a class pointer to a sibling class pointer.
The input of the operation must be a struct pointer. The result of the
operation is also a struct pointer.
The parameter `kind` specifies the semantics of this operation. If its value
is `ptr`, then the operation models dynamic casts on pointers. Otherwise, if
its value is `ref`, the operation models dynamic casts on references.
Specifically:
- When the input pointer is a null pointer value:
- If `kind` is `ref`, the operation will invoke undefined behavior. A
sanitizer check will be emitted if sanitizer is on.
- Otherwise, the operation will return a null pointer value as its result.
- When the runtime type check fails:
- If `kind` is `ref`, the operation will throw a `bad_cast` exception.
- Otherwise, the operation will return a null pointer value as its result.
The `info` argument gives detailed information about the requested dynamic
cast operation.
}];
let arguments = (ins DynamicCastKind:$kind,
StructPtr:$src,
DynamicCastInfoAttr:$info);
let results = (outs StructPtr:$result);
let assemblyFormat = [{
`(` $kind `,` $src `:` type($src) `,` qualified($info) `)`
`->` type($result) attr-dict
}];
let extraClassDeclaration = [{
/// Determine whether this operation models reference casting in C++.
bool isRefcast() {
return getKind() == ::mlir::cir::DynamicCastKind::ref;
}
}];
}
//===----------------------------------------------------------------------===//
// ObjSizeOp
//===----------------------------------------------------------------------===//
def SizeInfoTypeMin : I32EnumAttrCase<"min", 0>;
def SizeInfoTypeMax : I32EnumAttrCase<"max", 1>;
def SizeInfoType : I32EnumAttr<
"SizeInfoType",
"size info type",
[SizeInfoTypeMin, SizeInfoTypeMax]> {
let cppNamespace = "::mlir::cir";
}
def ObjSizeOp : CIR_Op<"objsize", [Pure]> {
let summary = "Conversion between values of different types";
let description = [{
}];
let arguments = (ins CIR_PointerType:$ptr, SizeInfoType:$kind,
UnitAttr:$dynamic);
let results = (outs PrimitiveInt:$result);
let assemblyFormat = [{
`(`
$ptr `:` type($ptr) `,`
$kind
(`,` `dynamic` $dynamic^)?
`)`
`->` type($result) attr-dict
}];
// Nothing to verify that isn't already covered by constraints.
let hasVerifier = 0;
}
//===----------------------------------------------------------------------===//
// PtrDiffOp
//===----------------------------------------------------------------------===//
def PtrDiffOp : CIR_Op<"ptr_diff", [Pure, SameTypeOperands]> {
let summary = "Pointer subtraction arithmetic";
let description = [{
`cir.ptr_diff` performs a subtraction between two pointer types with the
same element type and produces a `mlir::cir::IntType` result.
Note that the result considers the pointer size according to the ABI for
the pointee sizes, e.g. the subtraction between two `!cir.ptr<!u64i>` might
yield 1, meaning 8 bytes, whereas for `void` or function type pointees,
yielding 8 means 8 bytes.
```mlir
%7 = "cir.ptr_diff"(%0, %1) : !cir.ptr<!u64i> -> !u64i
```
}];
let results = (outs PrimitiveInt:$result);
let arguments = (ins CIR_PointerType:$lhs, CIR_PointerType:$rhs);
let assemblyFormat = [{
`(` $lhs `,` $rhs `)` `:` qualified(type($lhs)) `->` qualified(type($result)) attr-dict
}];
// Already covered by the traits
let hasVerifier = 0;
}
//===----------------------------------------------------------------------===//
// PtrStrideOp
//===----------------------------------------------------------------------===//
def PtrStrideOp : CIR_Op<"ptr_stride",
[Pure, SameFirstOperandAndResultType]> {
let summary = "Pointer access with stride";
let description = [{
Given a base pointer as first operand, provides a new pointer after applying
a stride (second operand).
```mlir
%3 = cir.const 0 : i32
%4 = cir.ptr_stride(%2 : !cir.ptr<i32>, %3 : i32), !cir.ptr<i32>
```
}];
let arguments = (ins CIR_PointerType:$base, PrimitiveInt:$stride);
let results = (outs CIR_PointerType:$result);
let assemblyFormat = [{
`(` $base `:` qualified(type($base)) `,` $stride `:` qualified(type($stride)) `)`
`,` qualified(type($result)) attr-dict
}];
let extraClassDeclaration = [{
// Get type pointed by the base pointer.
mlir::Type getElementTy() {
return getBase().getType().cast<mlir::cir::PointerType>().getPointee();
}
}];
// SameFirstOperandAndResultType already checks all we need.
let hasVerifier = 0;
}
//===----------------------------------------------------------------------===//
// ConstantOp
//===----------------------------------------------------------------------===//
def ConstantOp : CIR_Op<"const",
[ConstantLike, Pure, AllTypesMatch<["value", "res"]>]> {
// FIXME: Use SameOperandsAndResultType or similar and prevent eye bleeding
// type repetition in the assembly form.
let summary = "Defines a CIR constant";
let description = [{
The `cir.const` operation turns a literal into an SSA value. The data is
attached to the operation as an attribute.
```mlir
%0 = cir.const 42 : i32
%1 = cir.const 4.2 : f32
%2 = cir.const nullptr : !cir.ptr<i32>
```
}];
// The constant operation takes an attribute as the only input.
let arguments = (ins TypedAttrInterface:$value);
// The constant operation returns a single value of CIR_AnyType.
let results = (outs CIR_AnyType:$res);
let assemblyFormat = "attr-dict $value";
let hasVerifier = 1;
let extraClassDeclaration = [{
bool isNullPtr() {
if (const auto ptrAttr = getValue().dyn_cast<mlir::cir::ConstPtrAttr>())
return ptrAttr.isNullValue();
return false;
}
}];
let hasFolder = 1;
}
//===----------------------------------------------------------------------===//
// C/C++ memory order definitions
//===----------------------------------------------------------------------===//
def MemOrderRelaxed : I32EnumAttrCase<"Relaxed", 0, "relaxed">;
def MemOrderConsume : I32EnumAttrCase<"Consume", 1, "consume">;
def MemOrderAcquire : I32EnumAttrCase<"Acquire", 2, "acquire">;
def MemOrderRelease : I32EnumAttrCase<"Release", 3, "release">;
def MemOrderAcqRel : I32EnumAttrCase<"AcquireRelease", 4, "acq_rel">;
def MemOrderSeqCst : I32EnumAttrCase<"SequentiallyConsistent", 5, "seq_cst">;
def MemOrder : I32EnumAttr<
"MemOrder",
"Memory order according to C++11 memory model",
[MemOrderRelaxed, MemOrderConsume, MemOrderAcquire,
MemOrderRelease, MemOrderAcqRel, MemOrderSeqCst]> {
let cppNamespace = "::mlir::cir";
}
//===----------------------------------------------------------------------===//
// AllocaOp
//===----------------------------------------------------------------------===//
class AllocaTypesMatchWith<string summary, string lhsArg, string rhsArg,
string transform, string comparator = "std::equal_to<>()">
: PredOpTrait<summary, CPred<
comparator # "(" #
!subst("$_self", "$" # lhsArg # ".getType()", transform) #
", $" # rhsArg # ")">> {
string lhs = lhsArg;
string rhs = rhsArg;
string transformer = transform;
}
def AllocaOp : CIR_Op<"alloca", [
AllocaTypesMatchWith<"'allocaType' matches pointee type of 'addr'",
"addr", "allocaType",
"$_self.cast<PointerType>().getPointee()">]> {
let summary = "Defines a scope-local variable";
let description = [{
The `cir.alloca` operation defines a scope-local variable.
The presence `init` attribute indicates that the local variable represented
by this alloca was originally initialized in C/C++ source code. In such
cases, the first use contains the initialization (a cir.store, a cir.call
to a ctor, etc).
The `dynAllocSize` specifies the size to dynamically allocate on the stack
and ignores the allocation size based on the original type. This is useful
when handling VLAs and is omitted when declaring regular local variables.
The result type is a pointer to the input's type.
Example:
```mlir
// int count = 3;
%0 = cir.alloca i32, !cir.ptr<i32>, ["count", init] {alignment = 4 : i64}
// int *ptr;
%1 = cir.alloca !cir.ptr<i32>, !cir.ptr<!cir.ptr<i32>>, ["ptr"] {alignment = 8 : i64}
...
```
}];
let arguments = (ins
Optional<PrimitiveInt>:$dynAllocSize,
TypeAttr:$allocaType,
StrAttr:$name,
UnitAttr:$init,
ConfinedAttr<OptionalAttr<I64Attr>, [IntMinValue<0>]>:$alignment,
OptionalAttr<ASTVarDeclInterface>:$ast
);
let results = (outs Res<CIR_PointerType, "",
[MemAlloc<AutomaticAllocationScopeResource>]>:$addr);
let skipDefaultBuilders = 1;
let builders = [
OpBuilder<(ins "Type":$addr, "Type":$allocaType,
"StringRef":$name,
"IntegerAttr":$alignment)>,
OpBuilder<(ins "Type":$addr,
"Type":$allocaType,
"StringRef":$name,
"IntegerAttr":$alignment,
"Value":$dynAllocSize),
[{
if (dynAllocSize)
$_state.addOperands(dynAllocSize);
build($_builder, $_state, addr, allocaType, name, alignment);
}]>
];
let extraClassDeclaration = [{
// Whether the alloca input type is a pointer.
bool isPointerType() { return getAllocaType().isa<::mlir::cir::PointerType>(); }
bool isDynamic() { return (bool)getDynAllocSize(); }
}];
let assemblyFormat = [{
$allocaType `,` qualified(type($addr)) `,`
($dynAllocSize^ `:` type($dynAllocSize) `,`)?
`[` $name
(`,` `init` $init^)?
`]`
(`ast` $ast^)? attr-dict
}];
let hasVerifier = 0;
}
//===----------------------------------------------------------------------===//
// LoadOp
//===----------------------------------------------------------------------===//
def LoadOp : CIR_Op<"load", [
TypesMatchWith<"type of 'result' matches pointee type of 'addr'",
"addr", "result",
"$_self.cast<PointerType>().getPointee()">]> {
let summary = "Load value from memory adddress";
let description = [{
`cir.load` reads a value (lvalue to rvalue conversion) given an address
backed up by a `cir.ptr` type. A unit attribute `deref` can be used to
mark the resulting value as used by another operation to dereference
a pointer. A unit attribute `volatile` can be used to indicate a volatile
loading. Load can be marked atomic by using `atomic(<mem_order>)`.
Example:
```mlir
// Read from local variable, address in %0.
%1 = cir.load %0 : !cir.ptr<i32>, i32
// Load address from memory at address %0. %3 is used by at least one
// operation that dereferences a pointer.
%3 = cir.load deref %0 : !cir.ptr<!cir.ptr<i32>>
// Perform a volatile load from address in %0.
%4 = cir.load volatile %0 : !cir.ptr<i32>, i32
```
}];
let arguments = (ins Arg<CIR_PointerType, "the address to load from",
[MemRead]>:$addr, UnitAttr:$isDeref,
UnitAttr:$is_volatile,
OptionalAttr<MemOrder>:$mem_order);
let results = (outs CIR_AnyType:$result);
let assemblyFormat = [{
(`deref` $isDeref^)?
(`volatile` $is_volatile^)?
(`atomic` `(` $mem_order^ `)`)?
$addr `:` qualified(type($addr)) `,` type($result) attr-dict
}];
// FIXME: add verifier.
}
//===----------------------------------------------------------------------===//
// StoreOp
//===----------------------------------------------------------------------===//
def StoreOp : CIR_Op<"store", [
TypesMatchWith<"type of 'value' matches pointee type of 'addr'",
"addr", "value",
"$_self.cast<PointerType>().getPointee()">]> {
let summary = "Store value to memory address";
let description = [{
`cir.store` stores a value (first operand) to the memory address specified
in the second operand. A unit attribute `volatile` can be used to indicate
a volatile store. Store's can be marked atomic by using
`atomic(<mem_order>)`.
Example:
```mlir
// Store a function argument to local storage, address in %0.
cir.store %arg0, %0 : i32, !cir.ptr<i32>
// Perform a volatile store into memory location at the address in %0.
cir.store volatile %arg0, %0 : i32, !cir.ptr<i32>
```
}];
let arguments = (ins CIR_AnyType:$value,
Arg<CIR_PointerType, "the address to store the value",
[MemWrite]>:$addr,
UnitAttr:$is_volatile,
OptionalAttr<MemOrder>:$mem_order);
let assemblyFormat = [{
(`volatile` $is_volatile^)?
(`atomic` `(` $mem_order^ `)`)?
$value `,` $addr attr-dict `:` type($value) `,` qualified(type($addr))
}];
// FIXME: add verifier.
}
//===----------------------------------------------------------------------===//
// ReturnOp
//===----------------------------------------------------------------------===//
def ReturnOp : CIR_Op<"return", [ParentOneOf<["FuncOp", "ScopeOp", "IfOp",
"SwitchOp", "DoWhileOp",
"WhileOp", "ForOp"]>,
Terminator]> {
let summary = "Return from function";
let description = [{
The "return" operation represents a return operation within a function.
The operation takes an optional operand and produces no results.
The operand type must match the signature of the function that contains
the operation.
```mlir
func @foo() -> i32 {
...
cir.return %0 : i32
}
```
}];
// 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<CIR_AnyType>:$input);
// The return operation only emits the input in the format if it is present.
let assemblyFormat = "($input^ `:` type($input))? attr-dict ";
// Allow building a ReturnOp with no return operand.
let builders = [
OpBuilder<(ins), [{ build($_builder, $_state, std::nullopt); }]>
];
// Provide extra utility definitions on the c++ operation class definition.
let extraClassDeclaration = [{
bool hasOperand() { return getNumOperands() != 0; }
}];
let hasVerifier = 1;
}
//===----------------------------------------------------------------------===//
// IfOp
//===----------------------------------------------------------------------===//
def IfOp : CIR_Op<"if",
[DeclareOpInterfaceMethods<RegionBranchOpInterface>,
RecursivelySpeculatable, AutomaticAllocationScope, NoRegionArguments]> {
let summary = "The if-then-else operation";
let description = [{
The `cir.if` operation represents an if-then-else construct for
conditionally executing two regions of code. The operand is a `cir.bool`
type.
Examples:
```mlir
cir.if %b {
...
} else {
...
}
cir.if %c {
...
}
cir.if %c {
...
cir.br ^a
^a:
cir.yield
}
```
`cir.if` defines no values and the 'else' can be omitted. `cir.yield` must
explicitly terminate the region if it has more than one block.
}];
let arguments = (ins CIR_BoolType:$condition);
let regions = (region AnyRegion:$thenRegion, AnyRegion:$elseRegion);
let hasCustomAssemblyFormat = 1;
let hasVerifier = 1;
let skipDefaultBuilders = 1;
let builders = [
OpBuilder<(ins "Value":$cond, "bool":$withElseRegion,
CArg<"function_ref<void(OpBuilder &, Location)>",
"buildTerminatedBody">:$thenBuilder,
CArg<"function_ref<void(OpBuilder &, Location)>",
"nullptr">:$elseBuilder)>
];
}
//===----------------------------------------------------------------------===//
// TernaryOp
//===----------------------------------------------------------------------===//
def TernaryOp : CIR_Op<"ternary",
[DeclareOpInterfaceMethods<RegionBranchOpInterface>,
RecursivelySpeculatable, AutomaticAllocationScope, NoRegionArguments]> {
let summary = "The `cond ? a : b` C/C++ ternary operation";
let description = [{
The `cir.ternary` operation represents C/C++ ternary, much like a `select`
operation. First argument is a `cir.bool` condition to evaluate, followed
by two regions to execute (true or false). This is different from `cir.if`
since each region is one block sized and the `cir.yield` closing the block
scope should have one argument.
Example:
```mlir
// x = cond ? a : b;
%x = cir.ternary (%cond, true_region {
...
cir.yield %a : i32
}, false_region {
...
cir.yield %b : i32
}) -> i32
```
}];
let arguments = (ins CIR_BoolType:$cond);
let regions = (region SizedRegion<1>:$trueRegion,
SizedRegion<1>:$falseRegion);
let results = (outs Optional<CIR_AnyType>:$result);
let skipDefaultBuilders = 1;
let builders = [
OpBuilder<(ins "Value":$cond,
"function_ref<void(OpBuilder &, Location)>":$trueBuilder,
"function_ref<void(OpBuilder &, Location)>":$falseBuilder)
>
];
// All constraints already verified elsewhere.
let hasVerifier = 0;
let assemblyFormat = [{
`(` $cond `,`
`true` $trueRegion `,`
`false` $falseRegion
`)` `:` functional-type(operands, results) attr-dict
}];
}
//===----------------------------------------------------------------------===//
// ConditionOp
//===----------------------------------------------------------------------===//
def ConditionOp : CIR_Op<"condition", [
Terminator,
DeclareOpInterfaceMethods<RegionBranchTerminatorOpInterface,
["getSuccessorRegions"]>
]> {
let summary = "Loop continuation condition.";
let description = [{
The `cir.condition` terminates conditional regions. It takes a single
`cir.bool` operand and, depending on its value, may branch to different
regions:
- When in the `cond` region of a `cir.loop`, it continues the loop
if true, or exits it if false.
- When in the `ready` region of a `cir.await`, it branches to the `resume`
region when true, and to the `suspend` region when false.
Example:
```mlir
cir.loop for(cond : {
cir.condition(%arg0) // Branches to `step` region or exits.
}, step : {
[...]
}) {
[...]
}
cir.await(user, ready : {
cir.condition(%arg0) // Branches to `resume` or `suspend` region.
}, suspend : {
[...]
}, resume : {
[...]
},)
```
}];
let arguments = (ins CIR_BoolType:$condition);
let assemblyFormat = " `(` $condition `)` attr-dict ";
let hasVerifier = 1;
}
//===----------------------------------------------------------------------===//
// YieldOp
//===----------------------------------------------------------------------===//
def YieldOp : CIR_Op<"yield", [ReturnLike, Terminator,
ParentOneOf<["IfOp", "ScopeOp", "SwitchOp", "WhileOp", "ForOp", "AwaitOp",
"TernaryOp", "GlobalOp", "DoWhileOp", "CatchOp", "TryOp",
"ArrayCtor", "ArrayDtor"]>]> {
let summary = "Represents the default branching behaviour of a region";
let description = [{
The `cir.yield` operation terminates regions on different CIR operations,
and it is used to represent the default branching behaviour of a region.
Said branching behaviour is determinted by the parent operation. For
example, a yield in a `switch-case` region implies a fallthrough, while
a yield in a `cir.if` region implies a branch to the exit block, and so
on.
In some cases, it might yield an SSA value and the semantics of how the
values are yielded is defined by the parent operation. For example, a
`cir.ternary` operation yields a value from one of its regions.
As a general rule, `cir.yield` must be explicitly used whenever a region has
more than one block and no terminator, or within `cir.switch` regions not
`cir.return` terminated.
Examples:
```mlir
cir.if %4 {
...
cir.yield
}
cir.switch (%5) [
case (equal, 3) {
...
cir.yield
}, ...
]
cir.scope {
...
cir.yield
}
%x = cir.scope {
...
cir.yield %val
}
%y = cir.ternary {
...
cir.yield %val : i32
} : i32
```
}];
let arguments = (ins Variadic<CIR_AnyType>:$args);
let assemblyFormat = "($args^ `:` type($args))? attr-dict";
let builders = [
OpBuilder<(ins), [{ /* nothing to do */ }]>,
];
}
//===----------------------------------------------------------------------===//
// BreakOp
//===----------------------------------------------------------------------===//
def BreakOp : CIR_Op<"break", [Terminator]> {
let summary = "C/C++ `break` statement equivalent";
let description = [{
The `cir.break` operation is used to cease the control flow to the parent
operation, exiting its region's control flow. It is only allowed if it is
within a breakable operation (loops and `switch`).
}];
let assemblyFormat = "attr-dict";
let hasVerifier = 1;
}
//===----------------------------------------------------------------------===//
// ContinueOp
//===----------------------------------------------------------------------===//
def ContinueOp : CIR_Op<"continue", [Terminator]> {
let summary = "C/C++ `continue` statement equivalent";
let description = [{
The `cir.continue` operation is used to continue execution to the next
iteration of a loop. It is only allowed within `cir.loop` regions.
}];
let assemblyFormat = "attr-dict";
let hasVerifier = 1;
}
//===----------------------------------------------------------------------===//
// Resume
//===----------------------------------------------------------------------===//
def ResumeOp : CIR_Op<"resume", [ReturnLike, Terminator,
ParentOneOf<["CatchOp"]>]> {
let summary = "Resumes execution after not catching exceptions";
let description = [{
The `cir.resume` operation terminates a region on `cir.catch`, "resuming"
or continuing the unwind process.
Examples:
```mlir
cir.catch ... {
...
fallback { cir.resume };
}
```
}];
let arguments = (ins);
let assemblyFormat = "attr-dict";
}
//===----------------------------------------------------------------------===//
// ScopeOp
//===----------------------------------------------------------------------===//
def ScopeOp : CIR_Op<"scope", [
DeclareOpInterfaceMethods<RegionBranchOpInterface>,
RecursivelySpeculatable, AutomaticAllocationScope,
NoRegionArguments]> {
let summary = "Represents a C/C++ scope";
let description = [{
`cir.scope` contains one region and defines a strict "scope" for all new
values produced within its blocks.
The region can contain an arbitrary number of blocks but usually defaults
to one and can optionally return a value (useful for representing values
coming out of C++ full-expressions) via `cir.yield`:
```mlir
%rvalue = cir.scope {
...
cir.yield %value
}
```
If `cir.scope` yields no value, the `cir.yield` can be left out, and
will be inserted implicitly.
}];
let results = (outs Optional<CIR_AnyType>:$results);
let regions = (region AnyRegion:$scopeRegion);
let hasVerifier = 1;
let skipDefaultBuilders = 1;
let assemblyFormat = [{
custom<OmittedTerminatorRegion>($scopeRegion) (`:` type($results)^)? attr-dict
}];
let builders = [
// Scopes for yielding values.
OpBuilder<(ins
"function_ref<void(OpBuilder &, Type &, Location)>":$scopeBuilder)>,
// Scopes without yielding values.
OpBuilder<(ins "function_ref<void(OpBuilder &, Location)>":$scopeBuilder)>
];
}
//===----------------------------------------------------------------------===//
// UnaryOp
//===----------------------------------------------------------------------===//
def UnaryOpKind_Inc : I32EnumAttrCase<"Inc", 1, "inc">;
def UnaryOpKind_Dec : I32EnumAttrCase<"Dec", 2, "dec">;
def UnaryOpKind_Plus : I32EnumAttrCase<"Plus", 3, "plus">;
def UnaryOpKind_Minus : I32EnumAttrCase<"Minus", 4, "minus">;
def UnaryOpKind_Not : I32EnumAttrCase<"Not", 5, "not">;
def UnaryOpKind : I32EnumAttr<
"UnaryOpKind",
"unary operation kind",
[UnaryOpKind_Inc,
UnaryOpKind_Dec,
UnaryOpKind_Plus,
UnaryOpKind_Minus,
UnaryOpKind_Not,
]> {
let cppNamespace = "::mlir::cir";
}
// FIXME: Pure won't work when we add overloading.
def UnaryOp : CIR_Op<"unary", [Pure, SameOperandsAndResultType]> {
let summary = "Unary operations";
let description = [{
`cir.unary` performs the unary operation according to
the specified opcode kind: [inc, dec, plus, minus, not].
It requires one input operand and has one result, both types
should be the same.
```mlir
%7 = cir.unary(inc, %1) : i32 -> i32
%8 = cir.unary(dec, %2) : i32 -> i32
```
}];
let results = (outs CIR_AnyType:$result);
let arguments = (ins Arg<UnaryOpKind, "unary op kind">:$kind, Arg<CIR_AnyType>:$input);
let assemblyFormat = [{
`(` $kind `,` $input `)` `:` type($input) `,` type($result) attr-dict
}];
let hasVerifier = 1;
}
//===----------------------------------------------------------------------===//
// BinOp
//===----------------------------------------------------------------------===//
// FIXME: represent Commutative, Idempotent traits for appropriate binops
def BinOpKind_Mul : I32EnumAttrCase<"Mul", 1, "mul">;
def BinOpKind_Div : I32EnumAttrCase<"Div", 2, "div">;
def BinOpKind_Rem : I32EnumAttrCase<"Rem", 3, "rem">;
def BinOpKind_Add : I32EnumAttrCase<"Add", 4, "add">;
def BinOpKind_Sub : I32EnumAttrCase<"Sub", 5, "sub">;
def BinOpKind_And : I32EnumAttrCase<"And", 8, "and">;
def BinOpKind_Xor : I32EnumAttrCase<"Xor", 9, "xor">;
def BinOpKind_Or : I32EnumAttrCase<"Or", 10, "or">;
def BinOpKind : I32EnumAttr<
"BinOpKind",
"binary operation (arith and logic) kind",
[BinOpKind_Mul, BinOpKind_Div, BinOpKind_Rem,
BinOpKind_Add, BinOpKind_Sub,
BinOpKind_And, BinOpKind_Xor,
BinOpKind_Or]> {
let cppNamespace = "::mlir::cir";
}
// FIXME: Pure won't work when we add overloading.
def BinOp : CIR_Op<"binop", [Pure,
SameTypeOperands, SameOperandsAndResultType]> {
let summary = "Binary operations (arith and logic)";
let description = [{
cir.binop performs the binary operation according to
the specified opcode kind: [mul, div, rem, add, sub,
and, xor, or].
It requires two input operands and has one result, all types
should be the same.
```mlir
%7 = cir.binop(add, %1, %2) : !s32i
%7 = cir.binop(mul, %1, %2) : !u8i
```
}];
// TODO: get more accurate than CIR_AnyType
let results = (outs CIR_AnyType:$result);
let arguments = (ins Arg<BinOpKind, "binop kind">:$kind,
CIR_AnyType:$lhs, CIR_AnyType:$rhs,
UnitAttr:$no_unsigned_wrap,
UnitAttr:$no_signed_wrap);
let assemblyFormat = [{
`(` $kind `,` $lhs `,` $rhs `)`
(`nsw` $no_signed_wrap^)?
(`nuw` $no_unsigned_wrap^)?
`:` type($lhs) attr-dict
}];
let hasVerifier = 1;