13#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
16#include "mlir/IR/DialectImplementation.h"
17#include "llvm/ADT/TypeSwitch.h"
25 mlir::ArrayAttr &members);
32 cir::IntTypeInterface ty);
35 cir::IntTypeInterface ty);
42static mlir::ParseResult
44 mlir::FailureOr<llvm::APFloat> &value,
45 cir::FPTypeInterface fpType);
52 cir::LangAddressSpace &addrSpace) {
53 llvm::SMLoc loc = p.getCurrentLocation();
54 mlir::FailureOr<cir::LangAddressSpace> result =
55 mlir::FieldParser<cir::LangAddressSpace>::parse(p);
56 if (mlir::failed(result))
57 return p.emitError(loc,
"expected address space keyword");
58 addrSpace = result.value();
59 return mlir::success();
63 cir::LangAddressSpace addrSpace) {
64 p << cir::stringifyEnum(addrSpace);
68 mlir::IntegerAttr &value);
72#define GET_ATTRDEF_CLASSES
73#include "clang/CIR/Dialect/IR/CIROpsAttributes.cpp.inc"
83bool LangAddressSpaceAttr::isValidLoad(
84 mlir::Type type, mlir::ptr::AtomicOrdering ordering,
85 std::optional<int64_t> alignment,
const mlir::DataLayout *dataLayout,
86 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
87 llvm_unreachable(
"isValidLoad for LangAddressSpaceAttr NYI");
90bool LangAddressSpaceAttr::isValidStore(
91 mlir::Type type, mlir::ptr::AtomicOrdering ordering,
92 std::optional<int64_t> alignment,
const mlir::DataLayout *dataLayout,
93 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
94 llvm_unreachable(
"isValidStore for LangAddressSpaceAttr NYI");
97bool LangAddressSpaceAttr::isValidAtomicOp(
98 mlir::ptr::AtomicBinOp op, mlir::Type type,
99 mlir::ptr::AtomicOrdering ordering, std::optional<int64_t> alignment,
100 const mlir::DataLayout *dataLayout,
101 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
102 llvm_unreachable(
"isValidAtomicOp for LangAddressSpaceAttr NYI");
105bool LangAddressSpaceAttr::isValidAtomicXchg(
106 mlir::Type type, mlir::ptr::AtomicOrdering successOrdering,
107 mlir::ptr::AtomicOrdering failureOrdering, std::optional<int64_t> alignment,
108 const mlir::DataLayout *dataLayout,
109 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
110 llvm_unreachable(
"isValidAtomicXchg for LangAddressSpaceAttr NYI");
113bool LangAddressSpaceAttr::isValidAddrSpaceCast(
114 mlir::Type tgt, mlir::Type src,
115 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
116 llvm_unreachable(
"isValidAddrSpaceCast for LangAddressSpaceAttr NYI");
119bool LangAddressSpaceAttr::isValidPtrIntCast(
120 mlir::Type intLikeTy, mlir::Type ptrLikeTy,
121 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
122 llvm_unreachable(
"isValidPtrIntCast for LangAddressSpaceAttr NYI");
125bool TargetAddressSpaceAttr::isValidLoad(
126 mlir::Type type, mlir::ptr::AtomicOrdering ordering,
127 std::optional<int64_t> alignment,
const mlir::DataLayout *dataLayout,
128 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
129 llvm_unreachable(
"isValidLoad for TargetAddressSpaceAttr NYI");
132bool TargetAddressSpaceAttr::isValidStore(
133 mlir::Type type, mlir::ptr::AtomicOrdering ordering,
134 std::optional<int64_t> alignment,
const mlir::DataLayout *dataLayout,
135 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
136 llvm_unreachable(
"isValidStore for TargetAddressSpaceAttr NYI");
139bool TargetAddressSpaceAttr::isValidAtomicOp(
140 mlir::ptr::AtomicBinOp op, mlir::Type type,
141 mlir::ptr::AtomicOrdering ordering, std::optional<int64_t> alignment,
142 const mlir::DataLayout *dataLayout,
143 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
144 llvm_unreachable(
"isValidAtomicOp for TargetAddressSpaceAttr NYI");
147bool TargetAddressSpaceAttr::isValidAtomicXchg(
148 mlir::Type type, mlir::ptr::AtomicOrdering successOrdering,
149 mlir::ptr::AtomicOrdering failureOrdering, std::optional<int64_t> alignment,
150 const mlir::DataLayout *dataLayout,
151 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
152 llvm_unreachable(
"isValidAtomicXchg for TargetAddressSpaceAttr NYI");
155bool TargetAddressSpaceAttr::isValidAddrSpaceCast(
156 mlir::Type tgt, mlir::Type src,
157 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
158 llvm_unreachable(
"isValidAddrSpaceCast for TargetAddressSpaceAttr NYI");
161bool TargetAddressSpaceAttr::isValidPtrIntCast(
162 mlir::Type intLikeTy, mlir::Type ptrLikeTy,
163 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
164 llvm_unreachable(
"isValidPtrIntCast for TargetAddressSpaceAttr NYI");
172 mlir::ArrayAttr members) {
174 llvm::interleaveComma(members, printer);
179 mlir::ArrayAttr &members) {
182 auto delimiter = AsmParser::Delimiter::Braces;
183 auto result = parser.parseCommaSeparatedList(delimiter, [&]() {
184 mlir::TypedAttr attr;
185 if (parser.parseAttribute(attr).failed())
186 return mlir::failure();
187 elts.push_back(attr);
188 return mlir::success();
192 return mlir::failure();
194 members = mlir::ArrayAttr::get(parser.getContext(), elts);
195 return mlir::success();
203ConstRecordAttr::verify(function_ref<InFlightDiagnostic()> emitError,
204 mlir::Type type, ArrayAttr members) {
205 auto sTy = mlir::dyn_cast_if_present<cir::RecordType>(type);
207 return emitError() <<
"expected !cir.record type";
209 if (sTy.getMembers().size() != members.size())
210 return emitError() <<
"number of elements must match";
212 unsigned attrIdx = 0;
213 for (
auto &member : sTy.getMembers()) {
214 auto m = mlir::cast<mlir::TypedAttr>(members[attrIdx]);
215 if (member != m.getType())
216 return emitError() <<
"element at index " << attrIdx <<
" has type "
218 <<
" but the expected type for this element is "
230LogicalResult OptInfoAttr::verify(function_ref<InFlightDiagnostic()> emitError,
231 unsigned level,
unsigned size) {
234 <<
"optimization level must be between 0 and 3 inclusive";
237 <<
"size optimization level must be between 0 and 2 inclusive";
247static ParseResult
parseConstPtr(AsmParser &parser, mlir::IntegerAttr &value) {
249 if (parser.parseOptionalKeyword(
"null").succeeded()) {
250 value = parser.getBuilder().getI64IntegerAttr(0);
254 return parser.parseAttribute(value);
268template <
typename IntT>
270 if constexpr (std::is_signed_v<IntT>) {
271 return value.getSExtValue() != expectedValue;
273 return value.getZExtValue() != expectedValue;
277template <
typename IntT>
280 cir::IntTypeInterface ty) {
282 const bool isSigned = ty.isSigned();
283 if (p.parseInteger(ivalue))
284 return p.emitError(p.getCurrentLocation(),
"expected integer value");
286 value = mlir::APInt(ty.getWidth(), ivalue, isSigned,
true);
288 return p.emitError(p.getCurrentLocation(),
289 "integer value too large for the given type");
295 cir::IntTypeInterface ty) {
302 cir::IntTypeInterface ty) {
304 p << value.getSExtValue();
306 p << value.getZExtValue();
309LogicalResult IntAttr::verify(function_ref<InFlightDiagnostic()> emitError,
310 cir::IntTypeInterface type, llvm::APInt value) {
311 if (value.getBitWidth() != type.getWidth())
312 return emitError() <<
"type and value bitwidth mismatch: "
313 << type.getWidth() <<
" != " << value.getBitWidth();
326 FailureOr<APFloat> &value,
327 cir::FPTypeInterface fpType) {
329 APFloat parsedValue(0.0);
330 if (parser.parseFloat(fpType.getFloatSemantics(), parsedValue))
333 value.emplace(parsedValue);
337FPAttr FPAttr::getZero(Type type) {
340 mlir::cast<cir::FPTypeInterface>(type).getFloatSemantics()));
343LogicalResult FPAttr::verify(function_ref<InFlightDiagnostic()> emitError,
344 cir::FPTypeInterface fpType, APFloat value) {
345 if (APFloat::SemanticsToEnum(fpType.getFloatSemantics()) !=
346 APFloat::SemanticsToEnum(value.getSemantics()))
347 return emitError() <<
"floating-point semantics mismatch";
356std::string CmpThreeWayInfoAttr::getAlias()
const {
357 std::string alias =
"cmpinfo";
359 switch (getOrdering()) {
360 case CmpOrdering::Strong:
361 alias.append(
"_strong_");
363 case CmpOrdering::Weak:
364 alias.append(
"_weak_");
366 case CmpOrdering::Partial:
367 alias.append(
"_partial_");
371 auto appendInt = [&](int64_t value) {
373 alias.push_back(
'n');
376 alias.append(std::to_string(value));
386 if (std::optional<int> unordered = getUnordered()) {
388 appendInt(unordered.value());
395CmpThreeWayInfoAttr::verify(function_ref<InFlightDiagnostic()> emitError,
396 CmpOrdering ordering, int64_t lt, int64_t eq,
397 int64_t gt, std::optional<int64_t> unordered) {
399 if ((ordering == CmpOrdering::Strong || ordering == CmpOrdering::Weak) &&
401 emitError() <<
"strong and weak ordering do not include unordered";
404 if (ordering == CmpOrdering::Partial && !unordered) {
405 emitError() <<
"partial ordering requires unordered value";
417ConstComplexAttr::verify(function_ref<InFlightDiagnostic()> emitError,
418 cir::ComplexType type, mlir::TypedAttr real,
419 mlir::TypedAttr imag) {
420 mlir::Type elemType = type.getElementType();
421 if (real.getType() != elemType)
423 <<
"type of the real part does not match the complex type";
425 if (imag.getType() != elemType)
427 <<
"type of the imaginary part does not match the complex type";
437DataMemberAttr::verify(function_ref<InFlightDiagnostic()> emitError,
438 cir::DataMemberType ty,
439 std::optional<unsigned> memberIndex) {
441 if (!memberIndex.has_value())
444 cir::RecordType recTy = ty.getClassTy();
445 if (recTy.isIncomplete())
447 <<
"incomplete 'cir.record' cannot be used to build a non-null "
448 "data member pointer";
450 unsigned memberIndexValue = memberIndex.value();
451 if (memberIndexValue >= recTy.getNumElements())
453 <<
"member index of a #cir.data_member attribute is out of range";
455 mlir::Type memberTy = recTy.getMembers()[memberIndexValue];
456 if (memberTy != ty.getMemberTy())
458 <<
"member type of a #cir.data_member attribute must match the "
468LogicalResult MethodAttr::verify(function_ref<InFlightDiagnostic()> emitError,
469 cir::MethodType type,
470 std::optional<FlatSymbolRefAttr> symbol,
471 std::optional<uint64_t> vtable_offset) {
472 if (symbol.has_value() && vtable_offset.has_value())
474 <<
"at most one of symbol and vtable_offset can be present "
480Attribute MethodAttr::parse(AsmParser &parser, Type odsType) {
481 auto ty = mlir::cast<cir::MethodType>(odsType);
483 if (parser.parseLess().failed())
487 if (parser.parseOptionalKeyword(
"null").succeeded()) {
488 if (parser.parseGreater().failed())
495 FlatSymbolRefAttr symbol;
496 mlir::OptionalParseResult parseSymbolRefResult =
497 parser.parseOptionalAttribute(symbol);
498 if (parseSymbolRefResult.has_value()) {
499 if (parseSymbolRefResult.value().failed())
501 if (parser.parseGreater().failed())
503 return get(ty, symbol);
507 std::uint64_t vtableOffset = 0;
508 if (parser.parseKeyword(
"vtable_offset"))
510 if (parser.parseEqual())
512 if (parser.parseInteger(vtableOffset))
515 if (parser.parseGreater())
518 return get(ty, vtableOffset);
521void MethodAttr::print(AsmPrinter &printer)
const {
522 auto symbol = getSymbol();
523 auto vtableOffset = getVtableOffset();
526 if (symbol.has_value()) {
528 }
else if (vtableOffset.has_value()) {
529 printer <<
"vtable_offset = " << *vtableOffset;
541ConstArrayAttr::verify(function_ref<InFlightDiagnostic()> emitError, Type type,
542 Attribute elts,
int trailingZerosNum) {
544 if (!(mlir::isa<ArrayAttr, StringAttr>(elts)))
545 return emitError() <<
"constant array expects ArrayAttr or StringAttr";
547 if (
auto strAttr = mlir::dyn_cast<StringAttr>(elts)) {
548 const auto arrayTy = mlir::cast<ArrayType>(type);
549 const auto intTy = mlir::dyn_cast<IntType>(arrayTy.getElementType());
552 if (!intTy || intTy.getWidth() != 8)
554 <<
"constant array element for string literals expects "
555 "!cir.int<u, 8> element type";
559 assert(mlir::isa<ArrayAttr>(elts));
560 const auto arrayAttr = mlir::cast<mlir::ArrayAttr>(elts);
561 const auto arrayTy = mlir::cast<ArrayType>(type);
564 if (arrayAttr.size() > arrayTy.getSize())
565 return emitError() <<
"constant array has " << arrayAttr.size()
566 <<
" values but array type has size "
567 << arrayTy.getSize();
568 if (arrayTy.getSize() != arrayAttr.size() + trailingZerosNum)
569 return emitError() <<
"constant array size should match type size";
573Attribute ConstArrayAttr::parse(AsmParser &parser, Type type) {
574 mlir::FailureOr<Type> resultTy;
575 mlir::FailureOr<Attribute> resultVal;
578 if (parser.parseLess())
582 resultVal = FieldParser<Attribute>::parse(parser);
583 if (failed(resultVal)) {
585 parser.getCurrentLocation(),
586 "failed to parse ConstArrayAttr parameter 'value' which is "
587 "to be a `Attribute`");
592 if (mlir::isa<ArrayAttr>(*resultVal)) {
594 if (parser.parseOptionalColon().failed()) {
597 resultTy = FieldParser<Type>::parse(parser);
598 if (failed(resultTy)) {
600 parser.getCurrentLocation(),
601 "failed to parse ConstArrayAttr parameter 'type' which is "
602 "to be a `::mlir::Type`");
607 auto ta = mlir::cast<TypedAttr>(*resultVal);
608 resultTy = ta.getType();
609 if (mlir::isa<mlir::NoneType>(*resultTy)) {
610 parser.emitError(parser.getCurrentLocation(),
611 "expected type declaration for string literal");
617 if (parser.parseOptionalComma().succeeded()) {
618 if (parser.parseOptionalKeyword(
"trailing_zeros").succeeded()) {
620 mlir::cast<cir::ArrayType>(resultTy.value()).getSize();
621 mlir::Attribute elts = resultVal.value();
622 if (
auto str = mlir::dyn_cast<mlir::StringAttr>(elts))
623 zeros = typeSize - str.size();
625 zeros = typeSize - mlir::cast<mlir::ArrayAttr>(elts).size();
632 if (parser.parseGreater())
635 return parser.getChecked<ConstArrayAttr>(
636 parser.getCurrentLocation(), parser.getContext(), resultTy.value(),
637 resultVal.value(), zeros);
640void ConstArrayAttr::print(AsmPrinter &printer)
const {
642 printer.printStrippedAttrOrType(getElts());
643 if (getTrailingZerosNum())
644 printer <<
", trailing_zeros";
653cir::ConstVectorAttr::verify(function_ref<InFlightDiagnostic()> emitError,
654 Type type, ArrayAttr elts) {
656 if (!mlir::isa<cir::VectorType>(type))
657 return emitError() <<
"type of cir::ConstVectorAttr is not a "
661 const auto vecType = mlir::cast<cir::VectorType>(type);
663 if (vecType.getSize() != elts.size())
665 <<
"number of constant elements should match vector size";
668 LogicalResult elementTypeCheck =
success();
669 elts.walkImmediateSubElements(
670 [&](Attribute element) {
671 if (elementTypeCheck.failed()) {
675 auto typedElement = mlir::dyn_cast<TypedAttr>(element);
677 typedElement.getType() != vecType.getElementType()) {
678 elementTypeCheck = failure();
679 emitError() <<
"constant type should match vector element type";
684 return elementTypeCheck;
691LogicalResult cir::VTableAttr::verify(
692 llvm::function_ref<mlir::InFlightDiagnostic()> emitError, mlir::Type type,
693 mlir::ArrayAttr data) {
694 auto sTy = mlir::dyn_cast_if_present<cir::RecordType>(type);
696 return emitError() <<
"expected !cir.record type result";
697 if (sTy.getMembers().empty() || data.empty())
698 return emitError() <<
"expected record type with one or more subtype";
700 if (cir::ConstRecordAttr::verify(emitError, type, data).failed())
703 for (
const auto &element : data.getAsRange<mlir::Attribute>()) {
704 const auto &constArrayAttr = mlir::dyn_cast<cir::ConstArrayAttr>(element);
706 return emitError() <<
"expected constant array subtype";
708 LogicalResult eltTypeCheck =
success();
709 auto arrayElts = mlir::cast<ArrayAttr>(constArrayAttr.getElts());
710 arrayElts.walkImmediateSubElements(
711 [&](mlir::Attribute attr) {
712 if (mlir::isa<ConstPtrAttr, GlobalViewAttr>(attr))
715 eltTypeCheck = emitError()
716 <<
"expected GlobalViewAttr or ConstPtrAttr";
718 [&](mlir::Type type) {});
719 if (eltTypeCheck.failed())
729std::string DynamicCastInfoAttr::getAlias()
const {
732 std::string alias =
"dyn_cast_info_";
734 alias.append(getSrcRtti().getSymbol().getValue());
735 alias.push_back(
'_');
736 alias.append(getDestRtti().getSymbol().getValue());
741LogicalResult DynamicCastInfoAttr::verify(
742 function_ref<InFlightDiagnostic()> emitError, cir::GlobalViewAttr srcRtti,
743 cir::GlobalViewAttr destRtti, mlir::FlatSymbolRefAttr runtimeFunc,
744 mlir::FlatSymbolRefAttr badCastFunc, cir::IntAttr offsetHint) {
745 auto isRttiPtr = [](mlir::Type ty) {
748 auto ptrTy = mlir::dyn_cast<cir::PointerType>(ty);
752 auto pointeeIntTy = mlir::dyn_cast<cir::IntType>(ptrTy.getPointee());
756 return pointeeIntTy.isUnsigned() && pointeeIntTy.getWidth() == 8;
759 if (!isRttiPtr(srcRtti.getType()))
760 return emitError() <<
"srcRtti must be an RTTI pointer";
762 if (!isRttiPtr(destRtti.getType()))
763 return emitError() <<
"destRtti must be an RTTI pointer";
772void CIRDialect::registerAttributes() {
774#define GET_ATTRDEF_LIST
775#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)
void printAddressSpaceValue(mlir::AsmPrinter &p, cir::LangAddressSpace addrSpace)
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)
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)
mlir::ParseResult parseAddressSpaceValue(mlir::AsmParser &p, cir::LangAddressSpace &addrSpace)