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 "
308Attribute MethodAttr::parse(AsmParser &parser, Type odsType) {
309 auto ty = mlir::cast<cir::MethodType>(odsType);
311 if (parser.parseLess().failed())
315 if (parser.parseOptionalKeyword(
"null").succeeded()) {
316 if (parser.parseGreater().failed())
323 FlatSymbolRefAttr symbol;
324 mlir::OptionalParseResult parseSymbolRefResult =
325 parser.parseOptionalAttribute(symbol);
326 if (parseSymbolRefResult.has_value()) {
327 if (parseSymbolRefResult.value().failed())
329 if (parser.parseGreater().failed())
331 return get(ty, symbol);
337void MethodAttr::print(AsmPrinter &printer)
const {
338 auto symbol = getSymbol();
341 if (symbol.has_value()) {
354ConstArrayAttr::verify(function_ref<InFlightDiagnostic()> emitError, Type type,
355 Attribute elts,
int trailingZerosNum) {
357 if (!(mlir::isa<ArrayAttr, StringAttr>(elts)))
358 return emitError() <<
"constant array expects ArrayAttr or StringAttr";
360 if (
auto strAttr = mlir::dyn_cast<StringAttr>(elts)) {
361 const auto arrayTy = mlir::cast<ArrayType>(type);
362 const auto intTy = mlir::dyn_cast<IntType>(arrayTy.getElementType());
365 if (!intTy || intTy.getWidth() != 8)
367 <<
"constant array element for string literals expects "
368 "!cir.int<u, 8> element type";
372 assert(mlir::isa<ArrayAttr>(elts));
373 const auto arrayAttr = mlir::cast<mlir::ArrayAttr>(elts);
374 const auto arrayTy = mlir::cast<ArrayType>(type);
377 if (arrayTy.getSize() != arrayAttr.size() + trailingZerosNum)
378 return emitError() <<
"constant array size should match type size";
382Attribute ConstArrayAttr::parse(AsmParser &parser, Type type) {
383 mlir::FailureOr<Type> resultTy;
384 mlir::FailureOr<Attribute> resultVal;
387 if (parser.parseLess())
391 resultVal = FieldParser<Attribute>::parse(parser);
392 if (failed(resultVal)) {
394 parser.getCurrentLocation(),
395 "failed to parse ConstArrayAttr parameter 'value' which is "
396 "to be a `Attribute`");
401 if (mlir::isa<ArrayAttr>(*resultVal)) {
403 if (parser.parseOptionalColon().failed()) {
406 resultTy = FieldParser<Type>::parse(parser);
407 if (failed(resultTy)) {
409 parser.getCurrentLocation(),
410 "failed to parse ConstArrayAttr parameter 'type' which is "
411 "to be a `::mlir::Type`");
416 auto ta = mlir::cast<TypedAttr>(*resultVal);
417 resultTy = ta.getType();
418 if (mlir::isa<mlir::NoneType>(*resultTy)) {
419 parser.emitError(parser.getCurrentLocation(),
420 "expected type declaration for string literal");
426 if (parser.parseOptionalComma().succeeded()) {
427 if (parser.parseOptionalKeyword(
"trailing_zeros").succeeded()) {
429 mlir::cast<cir::ArrayType>(resultTy.value()).getSize();
430 mlir::Attribute elts = resultVal.value();
431 if (
auto str = mlir::dyn_cast<mlir::StringAttr>(elts))
432 zeros = typeSize - str.size();
434 zeros = typeSize - mlir::cast<mlir::ArrayAttr>(elts).size();
441 if (parser.parseGreater())
444 return parser.getChecked<ConstArrayAttr>(
445 parser.getCurrentLocation(), parser.getContext(), resultTy.value(),
446 resultVal.value(), zeros);
449void ConstArrayAttr::print(AsmPrinter &printer)
const {
451 printer.printStrippedAttrOrType(getElts());
452 if (getTrailingZerosNum())
453 printer <<
", trailing_zeros";
462cir::ConstVectorAttr::verify(function_ref<InFlightDiagnostic()> emitError,
463 Type type, ArrayAttr elts) {
465 if (!mlir::isa<cir::VectorType>(type))
466 return emitError() <<
"type of cir::ConstVectorAttr is not a "
470 const auto vecType = mlir::cast<cir::VectorType>(type);
472 if (vecType.getSize() != elts.size())
474 <<
"number of constant elements should match vector size";
477 LogicalResult elementTypeCheck =
success();
478 elts.walkImmediateSubElements(
479 [&](Attribute element) {
480 if (elementTypeCheck.failed()) {
484 auto typedElement = mlir::dyn_cast<TypedAttr>(element);
486 typedElement.getType() != vecType.getElementType()) {
487 elementTypeCheck = failure();
488 emitError() <<
"constant type should match vector element type";
493 return elementTypeCheck;
500LogicalResult cir::VTableAttr::verify(
501 llvm::function_ref<mlir::InFlightDiagnostic()> emitError, mlir::Type type,
502 mlir::ArrayAttr data) {
503 auto sTy = mlir::dyn_cast_if_present<cir::RecordType>(type);
505 return emitError() <<
"expected !cir.record type result";
506 if (sTy.getMembers().empty() || data.empty())
507 return emitError() <<
"expected record type with one or more subtype";
509 if (cir::ConstRecordAttr::verify(emitError, type, data).failed())
512 for (
const auto &element : data.getAsRange<mlir::Attribute>()) {
513 const auto &constArrayAttr = mlir::dyn_cast<cir::ConstArrayAttr>(element);
515 return emitError() <<
"expected constant array subtype";
517 LogicalResult eltTypeCheck =
success();
518 auto arrayElts = mlir::cast<ArrayAttr>(constArrayAttr.getElts());
519 arrayElts.walkImmediateSubElements(
520 [&](mlir::Attribute attr) {
521 if (mlir::isa<ConstPtrAttr, GlobalViewAttr>(attr))
524 eltTypeCheck = emitError()
525 <<
"expected GlobalViewAttr or ConstPtrAttr";
527 [&](mlir::Type type) {});
528 if (eltTypeCheck.failed())
538std::string DynamicCastInfoAttr::getAlias()
const {
541 std::string alias =
"dyn_cast_info_";
543 alias.append(getSrcRtti().getSymbol().getValue());
544 alias.push_back(
'_');
545 alias.append(getDestRtti().getSymbol().getValue());
550LogicalResult DynamicCastInfoAttr::verify(
551 function_ref<InFlightDiagnostic()> emitError, cir::GlobalViewAttr srcRtti,
552 cir::GlobalViewAttr destRtti, mlir::FlatSymbolRefAttr runtimeFunc,
553 mlir::FlatSymbolRefAttr badCastFunc, cir::IntAttr offsetHint) {
554 auto isRttiPtr = [](mlir::Type ty) {
557 auto ptrTy = mlir::dyn_cast<cir::PointerType>(ty);
561 auto pointeeIntTy = mlir::dyn_cast<cir::IntType>(ptrTy.getPointee());
565 return pointeeIntTy.isUnsigned() && pointeeIntTy.getWidth() == 8;
568 if (!isRttiPtr(srcRtti.getType()))
569 return emitError() <<
"srcRtti must be an RTTI pointer";
571 if (!isRttiPtr(destRtti.getType()))
572 return emitError() <<
"destRtti must be an RTTI pointer";
581void CIRDialect::registerAttributes() {
583#define GET_ATTRDEF_LIST
584#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)