15#include "mlir/IR/DialectImplementation.h"
16#include "llvm/ADT/TypeSwitch.h"
24 mlir::ArrayAttr &members);
31 cir::IntTypeInterface ty);
34 cir::IntTypeInterface ty);
41static mlir::ParseResult
43 mlir::FailureOr<llvm::APFloat> &value,
44 cir::FPTypeInterface fpType);
51 cir::TargetAddressSpaceAttr &attr);
54 cir::TargetAddressSpaceAttr attr);
57 mlir::IntegerAttr &value);
61#define GET_ATTRDEF_CLASSES
62#include "clang/CIR/Dialect/IR/CIROpsAttributes.cpp.inc"
72 mlir::ArrayAttr members) {
74 llvm::interleaveComma(members, printer);
79 mlir::ArrayAttr &members) {
82 auto delimiter = AsmParser::Delimiter::Braces;
83 auto result = parser.parseCommaSeparatedList(delimiter, [&]() {
85 if (parser.parseAttribute(attr).failed())
86 return mlir::failure();
88 return mlir::success();
92 return mlir::failure();
94 members = mlir::ArrayAttr::get(parser.getContext(), elts);
95 return mlir::success();
103ConstRecordAttr::verify(function_ref<InFlightDiagnostic()> emitError,
104 mlir::Type type, ArrayAttr members) {
105 auto sTy = mlir::dyn_cast_if_present<cir::RecordType>(type);
107 return emitError() <<
"expected !cir.record type";
109 if (sTy.getMembers().size() != members.size())
110 return emitError() <<
"number of elements must match";
112 unsigned attrIdx = 0;
113 for (
auto &member : sTy.getMembers()) {
114 auto m = mlir::cast<mlir::TypedAttr>(members[attrIdx]);
115 if (member != m.getType())
116 return emitError() <<
"element at index " << attrIdx <<
" has type "
118 <<
" but the expected type for this element is "
130LogicalResult OptInfoAttr::verify(function_ref<InFlightDiagnostic()> emitError,
131 unsigned level,
unsigned size) {
134 <<
"optimization level must be between 0 and 3 inclusive";
137 <<
"size optimization level must be between 0 and 2 inclusive";
147static ParseResult
parseConstPtr(AsmParser &parser, mlir::IntegerAttr &value) {
149 if (parser.parseOptionalKeyword(
"null").succeeded()) {
150 value = parser.getBuilder().getI64IntegerAttr(0);
154 return parser.parseAttribute(value);
168template <
typename IntT>
170 if constexpr (std::is_signed_v<IntT>) {
171 return value.getSExtValue() != expectedValue;
173 return value.getZExtValue() != expectedValue;
177template <
typename IntT>
180 cir::IntTypeInterface ty) {
182 const bool isSigned = ty.isSigned();
183 if (p.parseInteger(ivalue))
184 return p.emitError(p.getCurrentLocation(),
"expected integer value");
186 value = mlir::APInt(ty.getWidth(), ivalue, isSigned,
true);
188 return p.emitError(p.getCurrentLocation(),
189 "integer value too large for the given type");
195 cir::IntTypeInterface ty) {
202 cir::IntTypeInterface ty) {
204 p << value.getSExtValue();
206 p << value.getZExtValue();
209LogicalResult IntAttr::verify(function_ref<InFlightDiagnostic()> emitError,
210 cir::IntTypeInterface type, llvm::APInt value) {
211 if (value.getBitWidth() != type.getWidth())
212 return emitError() <<
"type and value bitwidth mismatch: "
213 << type.getWidth() <<
" != " << value.getBitWidth();
226 FailureOr<APFloat> &value,
227 cir::FPTypeInterface fpType) {
229 APFloat parsedValue(0.0);
230 if (parser.parseFloat(fpType.getFloatSemantics(), parsedValue))
233 value.emplace(parsedValue);
237FPAttr FPAttr::getZero(Type type) {
240 mlir::cast<cir::FPTypeInterface>(type).getFloatSemantics()));
243LogicalResult FPAttr::verify(function_ref<InFlightDiagnostic()> emitError,
244 cir::FPTypeInterface fpType, APFloat value) {
245 if (APFloat::SemanticsToEnum(fpType.getFloatSemantics()) !=
246 APFloat::SemanticsToEnum(value.getSemantics()))
247 return emitError() <<
"floating-point semantics mismatch";
257ConstComplexAttr::verify(function_ref<InFlightDiagnostic()> emitError,
258 cir::ComplexType type, mlir::TypedAttr real,
259 mlir::TypedAttr imag) {
260 mlir::Type elemType = type.getElementType();
261 if (real.getType() != elemType)
263 <<
"type of the real part does not match the complex type";
265 if (imag.getType() != elemType)
267 <<
"type of the imaginary part does not match the complex type";
277DataMemberAttr::verify(function_ref<InFlightDiagnostic()> emitError,
278 cir::DataMemberType ty,
279 std::optional<unsigned> memberIndex) {
281 if (!memberIndex.has_value())
284 cir::RecordType recTy = ty.getClassTy();
285 if (recTy.isIncomplete())
287 <<
"incomplete 'cir.record' cannot be used to build a non-null "
288 "data member pointer";
290 unsigned memberIndexValue = memberIndex.value();
291 if (memberIndexValue >= recTy.getNumElements())
293 <<
"member index of a #cir.data_member attribute is out of range";
295 mlir::Type memberTy = recTy.getMembers()[memberIndexValue];
296 if (memberTy != ty.getMemberTy())
298 <<
"member type of a #cir.data_member attribute must match the "
309ConstArrayAttr::verify(function_ref<InFlightDiagnostic()> emitError, Type type,
310 Attribute elts,
int trailingZerosNum) {
312 if (!(mlir::isa<ArrayAttr, StringAttr>(elts)))
313 return emitError() <<
"constant array expects ArrayAttr or StringAttr";
315 if (
auto strAttr = mlir::dyn_cast<StringAttr>(elts)) {
316 const auto arrayTy = mlir::cast<ArrayType>(type);
317 const auto intTy = mlir::dyn_cast<IntType>(arrayTy.getElementType());
320 if (!intTy || intTy.getWidth() != 8)
322 <<
"constant array element for string literals expects "
323 "!cir.int<u, 8> element type";
327 assert(mlir::isa<ArrayAttr>(elts));
328 const auto arrayAttr = mlir::cast<mlir::ArrayAttr>(elts);
329 const auto arrayTy = mlir::cast<ArrayType>(type);
332 if (arrayTy.getSize() != arrayAttr.size() + trailingZerosNum)
333 return emitError() <<
"constant array size should match type size";
337Attribute ConstArrayAttr::parse(AsmParser &parser, Type type) {
338 mlir::FailureOr<Type> resultTy;
339 mlir::FailureOr<Attribute> resultVal;
342 if (parser.parseLess())
346 resultVal = FieldParser<Attribute>::parse(parser);
347 if (failed(resultVal)) {
349 parser.getCurrentLocation(),
350 "failed to parse ConstArrayAttr parameter 'value' which is "
351 "to be a `Attribute`");
356 if (mlir::isa<ArrayAttr>(*resultVal)) {
358 if (parser.parseOptionalColon().failed()) {
361 resultTy = FieldParser<Type>::parse(parser);
362 if (failed(resultTy)) {
364 parser.getCurrentLocation(),
365 "failed to parse ConstArrayAttr parameter 'type' which is "
366 "to be a `::mlir::Type`");
371 auto ta = mlir::cast<TypedAttr>(*resultVal);
372 resultTy = ta.getType();
373 if (mlir::isa<mlir::NoneType>(*resultTy)) {
374 parser.emitError(parser.getCurrentLocation(),
375 "expected type declaration for string literal");
381 if (parser.parseOptionalComma().succeeded()) {
382 if (parser.parseOptionalKeyword(
"trailing_zeros").succeeded()) {
384 mlir::cast<cir::ArrayType>(resultTy.value()).getSize();
385 mlir::Attribute elts = resultVal.value();
386 if (
auto str = mlir::dyn_cast<mlir::StringAttr>(elts))
387 zeros = typeSize - str.size();
389 zeros = typeSize - mlir::cast<mlir::ArrayAttr>(elts).size();
396 if (parser.parseGreater())
399 return parser.getChecked<ConstArrayAttr>(
400 parser.getCurrentLocation(), parser.getContext(), resultTy.value(),
401 resultVal.value(), zeros);
404void ConstArrayAttr::print(AsmPrinter &printer)
const {
406 printer.printStrippedAttrOrType(getElts());
407 if (getTrailingZerosNum())
408 printer <<
", trailing_zeros";
417cir::ConstVectorAttr::verify(function_ref<InFlightDiagnostic()> emitError,
418 Type type, ArrayAttr elts) {
420 if (!mlir::isa<cir::VectorType>(type))
421 return emitError() <<
"type of cir::ConstVectorAttr is not a "
425 const auto vecType = mlir::cast<cir::VectorType>(type);
427 if (vecType.getSize() != elts.size())
429 <<
"number of constant elements should match vector size";
432 LogicalResult elementTypeCheck =
success();
433 elts.walkImmediateSubElements(
434 [&](Attribute element) {
435 if (elementTypeCheck.failed()) {
439 auto typedElement = mlir::dyn_cast<TypedAttr>(element);
441 typedElement.getType() != vecType.getElementType()) {
442 elementTypeCheck = failure();
443 emitError() <<
"constant type should match vector element type";
448 return elementTypeCheck;
455LogicalResult cir::VTableAttr::verify(
456 llvm::function_ref<mlir::InFlightDiagnostic()> emitError, mlir::Type type,
457 mlir::ArrayAttr data) {
458 auto sTy = mlir::dyn_cast_if_present<cir::RecordType>(type);
460 return emitError() <<
"expected !cir.record type result";
461 if (sTy.getMembers().empty() || data.empty())
462 return emitError() <<
"expected record type with one or more subtype";
464 if (cir::ConstRecordAttr::verify(emitError, type, data).failed())
467 for (
const auto &element : data.getAsRange<mlir::Attribute>()) {
468 const auto &constArrayAttr = mlir::dyn_cast<cir::ConstArrayAttr>(element);
470 return emitError() <<
"expected constant array subtype";
472 LogicalResult eltTypeCheck =
success();
473 auto arrayElts = mlir::cast<ArrayAttr>(constArrayAttr.getElts());
474 arrayElts.walkImmediateSubElements(
475 [&](mlir::Attribute attr) {
476 if (mlir::isa<ConstPtrAttr, GlobalViewAttr>(attr))
479 eltTypeCheck = emitError()
480 <<
"expected GlobalViewAttr or ConstPtrAttr";
482 [&](mlir::Type type) {});
483 if (eltTypeCheck.failed())
493std::string DynamicCastInfoAttr::getAlias()
const {
496 std::string alias =
"dyn_cast_info_";
498 alias.append(getSrcRtti().getSymbol().getValue());
499 alias.push_back(
'_');
500 alias.append(getDestRtti().getSymbol().getValue());
505LogicalResult DynamicCastInfoAttr::verify(
506 function_ref<InFlightDiagnostic()> emitError, cir::GlobalViewAttr srcRtti,
507 cir::GlobalViewAttr destRtti, mlir::FlatSymbolRefAttr runtimeFunc,
508 mlir::FlatSymbolRefAttr badCastFunc, cir::IntAttr offsetHint) {
509 auto isRttiPtr = [](mlir::Type ty) {
512 auto ptrTy = mlir::dyn_cast<cir::PointerType>(ty);
516 auto pointeeIntTy = mlir::dyn_cast<cir::IntType>(ptrTy.getPointee());
520 return pointeeIntTy.isUnsigned() && pointeeIntTy.getWidth() == 8;
523 if (!isRttiPtr(srcRtti.getType()))
524 return emitError() <<
"srcRtti must be an RTTI pointer";
526 if (!isRttiPtr(destRtti.getType()))
527 return emitError() <<
"destRtti must be an RTTI pointer";
536void CIRDialect::registerAttributes() {
538#define GET_ATTRDEF_LIST
539#include "clang/CIR/Dialect/IR/CIROpsAttributes.cpp.inc"
static mlir::ParseResult parseFloatLiteral(mlir::AsmParser &parser, mlir::FailureOr< llvm::APFloat > &value, cir::FPTypeInterface fpType)
static mlir::ParseResult parseIntLiteralImpl(mlir::AsmParser &p, llvm::APInt &value, cir::IntTypeInterface ty)
static void printConstPtr(mlir::AsmPrinter &p, mlir::IntegerAttr value)
static void printRecordMembers(mlir::AsmPrinter &p, mlir::ArrayAttr members)
static mlir::ParseResult parseIntLiteral(mlir::AsmParser &parser, llvm::APInt &value, cir::IntTypeInterface ty)
mlir::ParseResult parseTargetAddressSpace(mlir::AsmParser &p, cir::TargetAddressSpaceAttr &attr)
static void printIntLiteral(mlir::AsmPrinter &p, llvm::APInt value, cir::IntTypeInterface ty)
static mlir::ParseResult parseConstPtr(mlir::AsmParser &parser, mlir::IntegerAttr &value)
static bool isTooLargeForType(const mlir::APInt &value, IntT expectedValue)
static void printFloatLiteral(mlir::AsmPrinter &p, llvm::APFloat value, mlir::Type ty)
static mlir::ParseResult parseRecordMembers(mlir::AsmParser &parser, mlir::ArrayAttr &members)
void printTargetAddressSpace(mlir::AsmPrinter &p, cir::TargetAddressSpaceAttr attr)