Skip to content

Commit 96a37b4

Browse files
authored
[CIR] Align CastKind representation with upstream (#1695)
1 parent 3adac49 commit 96a37b4

File tree

1 file changed

+96
-41
lines changed

1 file changed

+96
-41
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 96 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -87,32 +87,78 @@ class CIR_Op<string mnemonic, list<Trait> traits = []> :
8787
//===----------------------------------------------------------------------===//
8888

8989
def CIR_CastKind : CIR_I32EnumAttr<"CastKind", "cast kind", [
90-
// The enumaration value isn't in sync with clang.
91-
I32EnumAttrCase<"int_to_bool", 0>,
92-
I32EnumAttrCase<"array_to_ptrdecay", 1>,
93-
I32EnumAttrCase<"integral", 2>,
94-
I32EnumAttrCase<"bitcast", 3>,
95-
I32EnumAttrCase<"floating", 4>,
96-
I32EnumAttrCase<"ptr_to_bool", 5>,
97-
I32EnumAttrCase<"float_to_int", 6>,
98-
I32EnumAttrCase<"int_to_ptr", 7>,
99-
I32EnumAttrCase<"ptr_to_int", 8>,
100-
I32EnumAttrCase<"float_to_bool", 9>,
101-
I32EnumAttrCase<"bool_to_int", 10>,
102-
I32EnumAttrCase<"int_to_float", 11>,
103-
I32EnumAttrCase<"bool_to_float", 12>,
104-
I32EnumAttrCase<"address_space", 13>,
105-
I32EnumAttrCase<"float_to_complex", 14>,
106-
I32EnumAttrCase<"int_to_complex", 15>,
107-
I32EnumAttrCase<"float_complex_to_real", 16>,
108-
I32EnumAttrCase<"int_complex_to_real", 17>,
109-
I32EnumAttrCase<"float_complex_to_bool", 18>,
110-
I32EnumAttrCase<"int_complex_to_bool", 19>,
111-
I32EnumAttrCase<"float_complex", 20>,
112-
I32EnumAttrCase<"float_complex_to_int_complex", 21>,
113-
I32EnumAttrCase<"int_complex", 22>,
114-
I32EnumAttrCase<"int_complex_to_float_complex", 23>,
115-
I32EnumAttrCase<"member_ptr_to_bool", 24>
90+
I32EnumAttrCase<"bitcast", 1>,
91+
// CK_LValueBitCast
92+
// CK_LValueToRValueBitCast
93+
// CK_LValueToRValue
94+
// CK_NoOp
95+
// CK_BaseToDerived
96+
// CK_DerivedToBase
97+
// CK_UncheckedDerivedToBase
98+
// CK_Dynamic
99+
// CK_ToUnion
100+
I32EnumAttrCase<"array_to_ptrdecay", 11>,
101+
// CK_FunctionToPointerDecay
102+
// CK_NullToPointer
103+
// CK_NullToMemberPointer
104+
// CK_BaseToDerivedMemberPointer
105+
// CK_DerivedToBaseMemberPointer
106+
I32EnumAttrCase<"member_ptr_to_bool", 17>,
107+
// CK_ReinterpretMemberPointer
108+
// CK_UserDefinedConversion
109+
// CK_ConstructorConversion
110+
I32EnumAttrCase<"int_to_ptr", 21>,
111+
I32EnumAttrCase<"ptr_to_int", 22>,
112+
I32EnumAttrCase<"ptr_to_bool", 23>,
113+
// CK_ToVoid
114+
// CK_MatrixCast
115+
// CK_VectorSplat
116+
I32EnumAttrCase<"integral", 27>,
117+
I32EnumAttrCase<"int_to_bool", 28>,
118+
I32EnumAttrCase<"int_to_float", 29>,
119+
// CK_FloatingToFixedPoint
120+
// CK_FixedPointToFloating
121+
// CK_FixedPointCast
122+
// CK_FixedPointToIntegral
123+
// CK_IntegralToFixedPoint
124+
// CK_FixedPointToBoolean
125+
I32EnumAttrCase<"float_to_int", 36>,
126+
I32EnumAttrCase<"float_to_bool", 37>,
127+
I32EnumAttrCase<"bool_to_int", 38>,
128+
I32EnumAttrCase<"floating", 39>,
129+
// CK_CPointerToObjCPointerCast
130+
// CK_BlockPointerToObjCPointerCast
131+
// CK_AnyPointerToBlockPointerCast
132+
// CK_ObjCObjectLValueCast
133+
I32EnumAttrCase<"float_to_complex", 44>,
134+
I32EnumAttrCase<"float_complex_to_real", 45>,
135+
I32EnumAttrCase<"float_complex_to_bool", 46>,
136+
I32EnumAttrCase<"float_complex", 47>,
137+
I32EnumAttrCase<"float_complex_to_int_complex", 48>,
138+
I32EnumAttrCase<"int_to_complex", 49>,
139+
I32EnumAttrCase<"int_complex_to_real", 50>,
140+
I32EnumAttrCase<"int_complex_to_bool", 51>,
141+
I32EnumAttrCase<"int_complex", 52>,
142+
I32EnumAttrCase<"int_complex_to_float_complex", 53>,
143+
// CK_ARCProduceObject
144+
// CK_ARCConsumeObject
145+
// CK_ARCReclaimReturnedObject
146+
// CK_ARCExtendBlockObject
147+
// CK_AtomicToNonAtomic
148+
// CK_NonAtomicToAtomic
149+
// CK_CopyAndAutoreleaseBlockObject
150+
// CK_BuiltinFnToFnPtr
151+
// CK_ZeroToOCLOpaqueType
152+
I32EnumAttrCase<"address_space", 63>,
153+
// CK_IntToOCLSampler
154+
// CK_HLSLVectorTruncation
155+
// CK_HLSLArrayRValue
156+
// CK_HLSLElementwiseCast
157+
// CK_HLSLAggregateSplatCast
158+
159+
// Enums below are specific to CIR and don't have a correspondence to classic
160+
// codegen:
161+
I32EnumAttrCase<"bool_to_float", 1000>,
116162
]>;
117163

118164
def CastOp : CIR_Op<"cast",
@@ -121,39 +167,48 @@ def CastOp : CIR_Op<"cast",
121167
// FIXME: not all conversions are free of side effects.
122168
let summary = "Conversion between values of different types";
123169
let description = [{
124-
Apply C/C++ usual conversions rules between values. Currently supported kinds:
170+
Apply the usual C/C++ conversion rules between values. This operation models
171+
a subset of conversions as defined in Clang's `OperationKinds.def`
172+
(`llvm-project/clang/include/clang/AST/OperationKinds.def`).
173+
174+
Note: not all conversions are implemented using `cir.cast`. For instance,
175+
lvalue-to-rvalue conversion is modeled as a `cir.load` instead. Currently
176+
supported kinds:
125177

126-
- `array_to_ptrdecay`
127178
- `bitcast`
179+
- `array_to_ptrdecay`
180+
- `member_ptr_to_bool
181+
- `int_to_ptr`
182+
- `ptr_to_int`
183+
- `ptr_to_bool`
128184
- `integral`
129185
- `int_to_bool`
130186
- `int_to_float`
131-
- `floating`
132187
- `float_to_int`
133188
- `float_to_bool`
134-
- `ptr_to_int`
135-
- `ptr_to_bool`
136189
- `bool_to_int`
137-
- `bool_to_float`
138-
- `address_space`
190+
- `floating`
139191
- `float_to_complex`
140-
- `int_to_complex`
141192
- `float_complex_to_real`
142-
- `int_complex_to_real`
143193
- `float_complex_to_bool`
144-
- `int_complex_to_bool`
145194
- `float_complex`
146195
- `float_complex_to_int_complex`
196+
- `int_to_complex`
197+
- `int_complex_to_real`
198+
- `int_complex_to_bool`
147199
- `int_complex`
148200
- `int_complex_to_float_complex`
201+
- `address_space`
202+
203+
CIR also supports some additional conversions that are not part of the classic
204+
Clang codegen:
149205

150-
This is effectively a subset of the rules from
151-
`llvm-project/clang/include/clang/AST/OperationKinds.def`; but note that some
152-
of the conversions aren't implemented in terms of `cir.cast`, `lvalue-to-rvalue`
153-
for instance is modeled as a regular `cir.load`.
206+
- `bool_to_float`
207+
208+
Example:
154209

155210
```mlir
156-
%4 = cir.cast (int_to_bool, %3 : i32), !cir.bool
211+
%4 = cir.cast(int_to_bool, %3 : i32), !cir.bool
157212
...
158213
%x = cir.cast(array_to_ptrdecay, %0 : !cir.ptr<!cir.array<i32 x 10>>), !cir.ptr<i32>
159214
```

0 commit comments

Comments
 (0)