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
|
//===- CIRAttrs.cpp - MLIR CIR Attributes ---------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file defines the attributes in the CIR dialect.
//
//===----------------------------------------------------------------------===//
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "mlir/IR/DialectImplementation.h"
#include "llvm/ADT/TypeSwitch.h"
//===-----------------------------------------------------------------===//
// IntLiteral
//===-----------------------------------------------------------------===//
static void printIntLiteral(mlir::AsmPrinter &p, llvm::APInt value,
cir::IntTypeInterface ty);
static mlir::ParseResult parseIntLiteral(mlir::AsmParser &parser,
llvm::APInt &value,
cir::IntTypeInterface ty);
//===-----------------------------------------------------------------===//
// FloatLiteral
//===-----------------------------------------------------------------===//
static void printFloatLiteral(mlir::AsmPrinter &p, llvm::APFloat value,
mlir::Type ty);
static mlir::ParseResult
parseFloatLiteral(mlir::AsmParser &parser,
mlir::FailureOr<llvm::APFloat> &value,
cir::FPTypeInterface fpType);
static mlir::ParseResult parseConstPtr(mlir::AsmParser &parser,
mlir::IntegerAttr &value);
static void printConstPtr(mlir::AsmPrinter &p, mlir::IntegerAttr value);
#define GET_ATTRDEF_CLASSES
#include "clang/CIR/Dialect/IR/CIROpsAttributes.cpp.inc"
using namespace mlir;
using namespace cir;
//===----------------------------------------------------------------------===//
// General CIR parsing / printing
//===----------------------------------------------------------------------===//
Attribute CIRDialect::parseAttribute(DialectAsmParser &parser,
Type type) const {
llvm::SMLoc typeLoc = parser.getCurrentLocation();
llvm::StringRef mnemonic;
Attribute genAttr;
OptionalParseResult parseResult =
generatedAttributeParser(parser, &mnemonic, type, genAttr);
if (parseResult.has_value())
return genAttr;
parser.emitError(typeLoc, "unknown attribute in CIR dialect");
return Attribute();
}
void CIRDialect::printAttribute(Attribute attr, DialectAsmPrinter &os) const {
if (failed(generatedAttributePrinter(attr, os)))
llvm_unreachable("unexpected CIR type kind");
}
//===----------------------------------------------------------------------===//
// OptInfoAttr definitions
//===----------------------------------------------------------------------===//
LogicalResult OptInfoAttr::verify(function_ref<InFlightDiagnostic()> emitError,
unsigned level, unsigned size) {
if (level > 3)
return emitError()
<< "optimization level must be between 0 and 3 inclusive";
if (size > 2)
return emitError()
<< "size optimization level must be between 0 and 2 inclusive";
return success();
}
//===----------------------------------------------------------------------===//
// ConstPtrAttr definitions
//===----------------------------------------------------------------------===//
// TODO(CIR): Consider encoding the null value differently and use conditional
// assembly format instead of custom parsing/printing.
static ParseResult parseConstPtr(AsmParser &parser, mlir::IntegerAttr &value) {
if (parser.parseOptionalKeyword("null").succeeded()) {
value = parser.getBuilder().getI64IntegerAttr(0);
return success();
}
return parser.parseAttribute(value);
}
static void printConstPtr(AsmPrinter &p, mlir::IntegerAttr value) {
if (!value.getInt())
p << "null";
else
p << value;
}
//===----------------------------------------------------------------------===//
// IntAttr definitions
//===----------------------------------------------------------------------===//
template <typename IntT>
static bool isTooLargeForType(const mlir::APInt &value, IntT expectedValue) {
if constexpr (std::is_signed_v<IntT>) {
return value.getSExtValue() != expectedValue;
} else {
return value.getZExtValue() != expectedValue;
}
}
template <typename IntT>
static mlir::ParseResult parseIntLiteralImpl(mlir::AsmParser &p,
llvm::APInt &value,
cir::IntTypeInterface ty) {
IntT ivalue;
const bool isSigned = ty.isSigned();
if (p.parseInteger(ivalue))
return p.emitError(p.getCurrentLocation(), "expected integer value");
value = mlir::APInt(ty.getWidth(), ivalue, isSigned, /*implicitTrunc=*/true);
if (isTooLargeForType(value, ivalue))
return p.emitError(p.getCurrentLocation(),
"integer value too large for the given type");
return success();
}
mlir::ParseResult parseIntLiteral(mlir::AsmParser &parser, llvm::APInt &value,
cir::IntTypeInterface ty) {
if (ty.isSigned())
return parseIntLiteralImpl<int64_t>(parser, value, ty);
return parseIntLiteralImpl<uint64_t>(parser, value, ty);
}
void printIntLiteral(mlir::AsmPrinter &p, llvm::APInt value,
cir::IntTypeInterface ty) {
if (ty.isSigned())
p << value.getSExtValue();
else
p << value.getZExtValue();
}
LogicalResult IntAttr::verify(function_ref<InFlightDiagnostic()> emitError,
cir::IntTypeInterface type, llvm::APInt value) {
if (value.getBitWidth() != type.getWidth())
return emitError() << "type and value bitwidth mismatch: "
<< type.getWidth() << " != " << value.getBitWidth();
return success();
}
//===----------------------------------------------------------------------===//
// FPAttr definitions
//===----------------------------------------------------------------------===//
static void printFloatLiteral(AsmPrinter &p, APFloat value, Type ty) {
p << value;
}
static ParseResult parseFloatLiteral(AsmParser &parser,
FailureOr<APFloat> &value,
cir::FPTypeInterface fpType) {
APFloat parsedValue(0.0);
if (parser.parseFloat(fpType.getFloatSemantics(), parsedValue))
return failure();
value.emplace(parsedValue);
return success();
}
FPAttr FPAttr::getZero(Type type) {
return get(type,
APFloat::getZero(
mlir::cast<cir::FPTypeInterface>(type).getFloatSemantics()));
}
LogicalResult FPAttr::verify(function_ref<InFlightDiagnostic()> emitError,
cir::FPTypeInterface fpType, APFloat value) {
if (APFloat::SemanticsToEnum(fpType.getFloatSemantics()) !=
APFloat::SemanticsToEnum(value.getSemantics()))
return emitError() << "floating-point semantics mismatch";
return success();
}
//===----------------------------------------------------------------------===//
// ConstComplexAttr definitions
//===----------------------------------------------------------------------===//
LogicalResult
ConstComplexAttr::verify(function_ref<InFlightDiagnostic()> emitError,
cir::ComplexType type, mlir::TypedAttr real,
mlir::TypedAttr imag) {
mlir::Type elemType = type.getElementType();
if (real.getType() != elemType)
return emitError()
<< "type of the real part does not match the complex type";
if (imag.getType() != elemType)
return emitError()
<< "type of the imaginary part does not match the complex type";
return success();
}
//===----------------------------------------------------------------------===//
// CIR ConstArrayAttr
//===----------------------------------------------------------------------===//
LogicalResult
ConstArrayAttr::verify(function_ref<InFlightDiagnostic()> emitError, Type type,
Attribute elts, int trailingZerosNum) {
if (!(mlir::isa<ArrayAttr, StringAttr>(elts)))
return emitError() << "constant array expects ArrayAttr or StringAttr";
if (auto strAttr = mlir::dyn_cast<StringAttr>(elts)) {
const auto arrayTy = mlir::cast<ArrayType>(type);
const auto intTy = mlir::dyn_cast<IntType>(arrayTy.getElementType());
// TODO: add CIR type for char.
if (!intTy || intTy.getWidth() != 8)
return emitError()
<< "constant array element for string literals expects "
"!cir.int<u, 8> element type";
return success();
}
assert(mlir::isa<ArrayAttr>(elts));
const auto arrayAttr = mlir::cast<mlir::ArrayAttr>(elts);
const auto arrayTy = mlir::cast<ArrayType>(type);
// Make sure both number of elements and subelement types match type.
if (arrayTy.getSize() != arrayAttr.size() + trailingZerosNum)
return emitError() << "constant array size should match type size";
return success();
}
Attribute ConstArrayAttr::parse(AsmParser &parser, Type type) {
mlir::FailureOr<Type> resultTy;
mlir::FailureOr<Attribute> resultVal;
// Parse literal '<'
if (parser.parseLess())
return {};
// Parse variable 'value'
resultVal = FieldParser<Attribute>::parse(parser);
if (failed(resultVal)) {
parser.emitError(
parser.getCurrentLocation(),
"failed to parse ConstArrayAttr parameter 'value' which is "
"to be a `Attribute`");
return {};
}
// ArrayAttrrs have per-element type, not the type of the array...
if (mlir::isa<ArrayAttr>(*resultVal)) {
// Array has implicit type: infer from const array type.
if (parser.parseOptionalColon().failed()) {
resultTy = type;
} else { // Array has explicit type: parse it.
resultTy = FieldParser<Type>::parse(parser);
if (failed(resultTy)) {
parser.emitError(
parser.getCurrentLocation(),
"failed to parse ConstArrayAttr parameter 'type' which is "
"to be a `::mlir::Type`");
return {};
}
}
} else {
auto ta = mlir::cast<TypedAttr>(*resultVal);
resultTy = ta.getType();
if (mlir::isa<mlir::NoneType>(*resultTy)) {
parser.emitError(parser.getCurrentLocation(),
"expected type declaration for string literal");
return {};
}
}
unsigned zeros = 0;
if (parser.parseOptionalComma().succeeded()) {
if (parser.parseOptionalKeyword("trailing_zeros").succeeded()) {
unsigned typeSize =
mlir::cast<cir::ArrayType>(resultTy.value()).getSize();
mlir::Attribute elts = resultVal.value();
if (auto str = mlir::dyn_cast<mlir::StringAttr>(elts))
zeros = typeSize - str.size();
else
zeros = typeSize - mlir::cast<mlir::ArrayAttr>(elts).size();
} else {
return {};
}
}
// Parse literal '>'
if (parser.parseGreater())
return {};
return parser.getChecked<ConstArrayAttr>(
parser.getCurrentLocation(), parser.getContext(), resultTy.value(),
resultVal.value(), zeros);
}
void ConstArrayAttr::print(AsmPrinter &printer) const {
printer << "<";
printer.printStrippedAttrOrType(getElts());
if (getTrailingZerosNum())
printer << ", trailing_zeros";
printer << ">";
}
//===----------------------------------------------------------------------===//
// CIR ConstVectorAttr
//===----------------------------------------------------------------------===//
LogicalResult
cir::ConstVectorAttr::verify(function_ref<InFlightDiagnostic()> emitError,
Type type, ArrayAttr elts) {
if (!mlir::isa<cir::VectorType>(type))
return emitError() << "type of cir::ConstVectorAttr is not a "
"cir::VectorType: "
<< type;
const auto vecType = mlir::cast<cir::VectorType>(type);
if (vecType.getSize() != elts.size())
return emitError()
<< "number of constant elements should match vector size";
// Check if the types of the elements match
LogicalResult elementTypeCheck = success();
elts.walkImmediateSubElements(
[&](Attribute element) {
if (elementTypeCheck.failed()) {
// An earlier element didn't match
return;
}
auto typedElement = mlir::dyn_cast<TypedAttr>(element);
if (!typedElement ||
typedElement.getType() != vecType.getElementType()) {
elementTypeCheck = failure();
emitError() << "constant type should match vector element type";
}
},
[&](Type) {});
return elementTypeCheck;
}
//===----------------------------------------------------------------------===//
// CIR Dialect
//===----------------------------------------------------------------------===//
void CIRDialect::registerAttributes() {
addAttributes<
#define GET_ATTRDEF_LIST
#include "clang/CIR/Dialect/IR/CIROpsAttributes.cpp.inc"
>();
}
|