13#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
16#include "mlir/IR/Attributes.h"
17#include "mlir/IR/DialectImplementation.h"
18#include "llvm/ADT/TypeSwitch.h"
26 mlir::ArrayAttr &members);
33 cir::IntTypeInterface ty);
36 cir::IntTypeInterface ty);
43static mlir::ParseResult
45 mlir::FailureOr<llvm::APFloat> &value,
46 cir::FPTypeInterface fpType);
53 cir::LangAddressSpace &addrSpace) {
54 llvm::SMLoc loc = p.getCurrentLocation();
55 mlir::FailureOr<cir::LangAddressSpace> result =
56 mlir::FieldParser<cir::LangAddressSpace>::parse(p);
57 if (mlir::failed(result))
58 return p.emitError(loc,
"expected address space keyword");
59 addrSpace = result.value();
60 return mlir::success();
64 cir::LangAddressSpace addrSpace) {
65 p << cir::stringifyEnum(addrSpace);
69 mlir::IntegerAttr &value);
73#define GET_ATTRDEF_CLASSES
74#include "clang/CIR/Dialect/IR/CIROpsAttributes.cpp.inc"
84bool LangAddressSpaceAttr::isValidLoad(
85 mlir::Type type, mlir::ptr::AtomicOrdering ordering,
86 std::optional<int64_t> alignment,
const mlir::DataLayout *dataLayout,
87 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
88 llvm_unreachable(
"isValidLoad for LangAddressSpaceAttr NYI");
91bool LangAddressSpaceAttr::isValidStore(
92 mlir::Type type, mlir::ptr::AtomicOrdering ordering,
93 std::optional<int64_t> alignment,
const mlir::DataLayout *dataLayout,
94 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
95 llvm_unreachable(
"isValidStore for LangAddressSpaceAttr NYI");
98bool LangAddressSpaceAttr::isValidAtomicOp(
99 mlir::ptr::AtomicBinOp op, mlir::Type type,
100 mlir::ptr::AtomicOrdering ordering, std::optional<int64_t> alignment,
101 const mlir::DataLayout *dataLayout,
102 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
103 llvm_unreachable(
"isValidAtomicOp for LangAddressSpaceAttr NYI");
106bool LangAddressSpaceAttr::isValidAtomicXchg(
107 mlir::Type type, mlir::ptr::AtomicOrdering successOrdering,
108 mlir::ptr::AtomicOrdering failureOrdering, std::optional<int64_t> alignment,
109 const mlir::DataLayout *dataLayout,
110 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
111 llvm_unreachable(
"isValidAtomicXchg for LangAddressSpaceAttr NYI");
114bool LangAddressSpaceAttr::isValidAddrSpaceCast(
115 mlir::Type tgt, mlir::Type src,
116 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
117 llvm_unreachable(
"isValidAddrSpaceCast for LangAddressSpaceAttr NYI");
120bool LangAddressSpaceAttr::isValidPtrIntCast(
121 mlir::Type intLikeTy, mlir::Type ptrLikeTy,
122 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
123 llvm_unreachable(
"isValidPtrIntCast for LangAddressSpaceAttr NYI");
126bool TargetAddressSpaceAttr::isValidLoad(
127 mlir::Type type, mlir::ptr::AtomicOrdering ordering,
128 std::optional<int64_t> alignment,
const mlir::DataLayout *dataLayout,
129 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
130 llvm_unreachable(
"isValidLoad for TargetAddressSpaceAttr NYI");
133bool TargetAddressSpaceAttr::isValidStore(
134 mlir::Type type, mlir::ptr::AtomicOrdering ordering,
135 std::optional<int64_t> alignment,
const mlir::DataLayout *dataLayout,
136 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
137 llvm_unreachable(
"isValidStore for TargetAddressSpaceAttr NYI");
140bool TargetAddressSpaceAttr::isValidAtomicOp(
141 mlir::ptr::AtomicBinOp op, mlir::Type type,
142 mlir::ptr::AtomicOrdering ordering, std::optional<int64_t> alignment,
143 const mlir::DataLayout *dataLayout,
144 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
145 llvm_unreachable(
"isValidAtomicOp for TargetAddressSpaceAttr NYI");
148bool TargetAddressSpaceAttr::isValidAtomicXchg(
149 mlir::Type type, mlir::ptr::AtomicOrdering successOrdering,
150 mlir::ptr::AtomicOrdering failureOrdering, std::optional<int64_t> alignment,
151 const mlir::DataLayout *dataLayout,
152 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
153 llvm_unreachable(
"isValidAtomicXchg for TargetAddressSpaceAttr NYI");
156bool TargetAddressSpaceAttr::isValidAddrSpaceCast(
157 mlir::Type tgt, mlir::Type src,
158 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
159 llvm_unreachable(
"isValidAddrSpaceCast for TargetAddressSpaceAttr NYI");
162bool TargetAddressSpaceAttr::isValidPtrIntCast(
163 mlir::Type intLikeTy, mlir::Type ptrLikeTy,
164 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
165 llvm_unreachable(
"isValidPtrIntCast for TargetAddressSpaceAttr NYI");
173 mlir::ArrayAttr members) {
175 llvm::interleaveComma(members, printer);
180 mlir::ArrayAttr &members) {
183 auto delimiter = AsmParser::Delimiter::Braces;
184 auto result = parser.parseCommaSeparatedList(delimiter, [&]() {
185 mlir::TypedAttr attr;
186 if (parser.parseAttribute(attr).failed())
187 return mlir::failure();
188 elts.push_back(attr);
189 return mlir::success();
193 return mlir::failure();
195 members = mlir::ArrayAttr::get(parser.getContext(), elts);
196 return mlir::success();
204ConstRecordAttr::verify(function_ref<InFlightDiagnostic()> emitError,
205 mlir::Type type, ArrayAttr members) {
206 auto sTy = mlir::dyn_cast_if_present<cir::RecordType>(type);
208 return emitError() <<
"expected !cir.struct or !cir.union type";
210 if (sTy.getMembers().size() != members.size())
211 return emitError() <<
"number of elements must match";
213 unsigned attrIdx = 0;
214 for (
auto &member : sTy.getMembers()) {
215 auto m = mlir::cast<mlir::TypedAttr>(members[attrIdx]);
216 if (member != m.getType())
217 return emitError() <<
"element at index " << attrIdx <<
" has type "
219 <<
" but the expected type for this element is "
231LogicalResult OptInfoAttr::verify(function_ref<InFlightDiagnostic()> emitError,
232 unsigned level,
unsigned size) {
235 <<
"optimization level must be between 0 and 3 inclusive";
238 <<
"size optimization level must be between 0 and 2 inclusive";
248static ParseResult
parseConstPtr(AsmParser &parser, mlir::IntegerAttr &value) {
250 if (parser.parseOptionalKeyword(
"null").succeeded()) {
251 value = parser.getBuilder().getI64IntegerAttr(0);
255 return parser.parseAttribute(value);
269template <
typename IntT>
271 if constexpr (std::is_signed_v<IntT>) {
272 return value.getSExtValue() != expectedValue;
274 return value.getZExtValue() != expectedValue;
278template <
typename IntT>
281 cir::IntTypeInterface ty) {
283 const bool isSigned = ty.isSigned();
284 if (p.parseInteger(ivalue))
285 return p.emitError(p.getCurrentLocation(),
"expected integer value");
287 value = mlir::APInt(ty.getWidth(), ivalue, isSigned,
true);
289 return p.emitError(p.getCurrentLocation(),
290 "integer value too large for the given type");
296 cir::IntTypeInterface ty) {
303 cir::IntTypeInterface ty) {
305 p << value.getSExtValue();
307 p << value.getZExtValue();
310LogicalResult IntAttr::verify(function_ref<InFlightDiagnostic()> emitError,
311 cir::IntTypeInterface type, llvm::APInt value) {
312 if (value.getBitWidth() != type.getWidth())
313 return emitError() <<
"type and value bitwidth mismatch: "
314 << type.getWidth() <<
" != " << value.getBitWidth();
327 FailureOr<APFloat> &value,
328 cir::FPTypeInterface fpType) {
330 APFloat parsedValue(0.0);
331 if (parser.parseFloat(fpType.getFloatSemantics(), parsedValue))
334 value.emplace(parsedValue);
338FPAttr FPAttr::getZero(Type type) {
341 mlir::cast<cir::FPTypeInterface>(type).getFloatSemantics()));
344LogicalResult FPAttr::verify(function_ref<InFlightDiagnostic()> emitError,
345 cir::FPTypeInterface fpType, APFloat value) {
346 if (APFloat::SemanticsToEnum(fpType.getFloatSemantics()) !=
347 APFloat::SemanticsToEnum(value.getSemantics()))
348 return emitError() <<
"floating-point semantics mismatch";
357std::string CmpThreeWayInfoAttr::getAlias()
const {
358 std::string alias =
"cmpinfo";
360 switch (getOrdering()) {
361 case CmpOrdering::Strong:
362 alias.append(
"_strong_");
364 case CmpOrdering::Weak:
365 alias.append(
"_weak_");
367 case CmpOrdering::Partial:
368 alias.append(
"_partial_");
372 auto appendInt = [&](int64_t value) {
374 alias.push_back(
'n');
377 alias.append(std::to_string(value));
387 if (std::optional<int> unordered = getUnordered()) {
389 appendInt(unordered.value());
396CmpThreeWayInfoAttr::verify(function_ref<InFlightDiagnostic()> emitError,
397 CmpOrdering ordering, int64_t lt, int64_t eq,
398 int64_t gt, std::optional<int64_t> unordered) {
400 if ((ordering == CmpOrdering::Strong || ordering == CmpOrdering::Weak) &&
402 emitError() <<
"strong and weak ordering do not include unordered";
405 if (ordering == CmpOrdering::Partial && !unordered) {
406 emitError() <<
"partial ordering requires unordered value";
418ConstComplexAttr::verify(function_ref<InFlightDiagnostic()> emitError,
419 cir::ComplexType type, mlir::TypedAttr real,
420 mlir::TypedAttr imag) {
421 mlir::Type elemType = type.getElementType();
422 if (real.getType() != elemType)
424 <<
"type of the real part does not match the complex type";
426 if (imag.getType() != elemType)
428 <<
"type of the imaginary part does not match the complex type";
437void CUDAVarRegistrationInfoAttr::print(AsmPrinter &p)
const {
438 p <<
"<" << getDeviceSideName();
439 p <<
", " << stringifyEnum(
getKind());
449Attribute CUDAVarRegistrationInfoAttr::parse(AsmParser &parser, Type odsType) {
450 if (parser.parseLess())
453 std::string deviceSideName;
454 if (parser.parseKeywordOrString(&deviceSideName)) {
455 parser.emitError(parser.getCurrentLocation(),
456 "expected device variable name");
460 if (parser.parseComma())
465 if (parser.parseKeyword(&kindStr))
468 std::optional<CUDADeviceVarKind>
kind = symbolizeCUDADeviceVarKind(kindStr);
470 parser.emitError(parser.getCurrentLocation(),
471 "unknown device variable kind: ")
477 bool isExtern =
false;
478 bool isConstant =
false;
479 bool isManaged =
false;
481 while (parser.parseOptionalGreater().failed()) {
482 if (parser.parseComma())
486 if (parser.parseKeyword(&flag))
489 if (flag ==
"extern")
491 else if (flag ==
"constant")
493 else if (flag ==
"managed")
496 parser.emitError(parser.getCurrentLocation(),
"unknown flag: ") << flag;
501 return get(parser.getContext(), deviceSideName, *
kind, isExtern, isConstant,
510DataMemberAttr::verify(function_ref<InFlightDiagnostic()> emitError,
511 cir::DataMemberType ty,
512 std::optional<unsigned> memberIndex) {
514 if (!memberIndex.has_value())
520 <<
"incomplete 'cir.record' cannot be used to build a non-null "
521 "data member pointer";
523 unsigned memberIndexValue = memberIndex.value();
526 <<
"member index of a #cir.data_member attribute is out of range";
528 mlir::Type memberTy = recTy.
getMembers()[memberIndexValue];
529 if (memberTy != ty.getMemberTy())
531 <<
"member type of a #cir.data_member attribute must match the "
541LogicalResult MethodAttr::verify(function_ref<InFlightDiagnostic()> emitError,
542 cir::MethodType type,
543 std::optional<FlatSymbolRefAttr> symbol,
544 std::optional<uint64_t> vtable_offset) {
545 if (symbol.has_value() && vtable_offset.has_value())
547 <<
"at most one of symbol and vtable_offset can be present "
553Attribute MethodAttr::parse(AsmParser &parser, Type odsType) {
554 auto ty = mlir::cast<cir::MethodType>(odsType);
556 if (parser.parseLess().failed())
560 if (parser.parseOptionalKeyword(
"null").succeeded()) {
561 if (parser.parseGreater().failed())
568 FlatSymbolRefAttr symbol;
569 mlir::OptionalParseResult parseSymbolRefResult =
570 parser.parseOptionalAttribute(symbol);
571 if (parseSymbolRefResult.has_value()) {
572 if (parseSymbolRefResult.value().failed())
574 if (parser.parseGreater().failed())
576 return get(ty, symbol);
580 std::uint64_t vtableOffset = 0;
581 if (parser.parseKeyword(
"vtable_offset"))
583 if (parser.parseEqual())
585 if (parser.parseInteger(vtableOffset))
588 if (parser.parseGreater())
591 return get(ty, vtableOffset);
594void MethodAttr::print(AsmPrinter &printer)
const {
595 auto symbol = getSymbol();
596 auto vtableOffset = getVtableOffset();
599 if (symbol.has_value()) {
601 }
else if (vtableOffset.has_value()) {
602 printer <<
"vtable_offset = " << *vtableOffset;
614ConstArrayAttr::verify(function_ref<InFlightDiagnostic()> emitError, Type type,
615 Attribute elts,
int trailingZerosNum) {
617 if (!(mlir::isa<ArrayAttr, StringAttr>(elts)))
618 return emitError() <<
"constant array expects ArrayAttr or StringAttr";
620 if (
auto strAttr = mlir::dyn_cast<StringAttr>(elts)) {
621 const auto arrayTy = mlir::cast<ArrayType>(type);
622 const auto intTy = mlir::dyn_cast<IntType>(arrayTy.getElementType());
625 if (!intTy || intTy.getWidth() != 8)
627 <<
"constant array element for string literals expects "
628 "!cir.int<u, 8> element type";
632 assert(mlir::isa<ArrayAttr>(elts));
633 const auto arrayAttr = mlir::cast<mlir::ArrayAttr>(elts);
634 const auto arrayTy = mlir::cast<ArrayType>(type);
637 if (arrayAttr.size() > arrayTy.getSize())
638 return emitError() <<
"constant array has " << arrayAttr.size()
639 <<
" values but array type has size "
640 << arrayTy.getSize();
641 if (arrayTy.getSize() != arrayAttr.size() + trailingZerosNum)
642 return emitError() <<
"constant array size should match type size";
646Attribute ConstArrayAttr::parse(AsmParser &parser, Type type) {
647 mlir::FailureOr<Type> resultTy;
648 mlir::FailureOr<Attribute> resultVal;
651 if (parser.parseLess())
655 resultVal = FieldParser<Attribute>::parse(parser);
656 if (failed(resultVal)) {
658 parser.getCurrentLocation(),
659 "failed to parse ConstArrayAttr parameter 'value' which is "
660 "to be a `Attribute`");
665 if (mlir::isa<ArrayAttr>(*resultVal)) {
667 if (parser.parseOptionalColon().failed()) {
670 resultTy = FieldParser<Type>::parse(parser);
671 if (failed(resultTy)) {
673 parser.getCurrentLocation(),
674 "failed to parse ConstArrayAttr parameter 'type' which is "
675 "to be a `::mlir::Type`");
680 auto ta = mlir::cast<TypedAttr>(*resultVal);
681 resultTy = ta.getType();
682 if (mlir::isa<mlir::NoneType>(*resultTy)) {
683 parser.emitError(parser.getCurrentLocation(),
684 "expected type declaration for string literal");
690 if (parser.parseOptionalComma().succeeded()) {
691 if (parser.parseOptionalKeyword(
"trailing_zeros").succeeded()) {
692 unsigned totalSize = mlir::cast<cir::ArrayType>(type).getSize();
693 mlir::Attribute elts = resultVal.value();
694 if (
auto str = mlir::dyn_cast<mlir::StringAttr>(elts))
695 zeros = totalSize - str.size();
697 zeros = totalSize - mlir::cast<mlir::ArrayAttr>(elts).size();
704 if (parser.parseGreater())
707 return parser.getChecked<ConstArrayAttr>(parser.getCurrentLocation(),
708 parser.getContext(), type,
709 resultVal.value(), zeros);
712void ConstArrayAttr::print(AsmPrinter &printer)
const {
714 printer.printStrippedAttrOrType(getElts());
715 if (getTrailingZerosNum())
716 printer <<
", trailing_zeros";
725cir::ConstVectorAttr::verify(function_ref<InFlightDiagnostic()> emitError,
726 Type type, ArrayAttr elts) {
728 if (!mlir::isa<cir::VectorType>(type))
729 return emitError() <<
"type of cir::ConstVectorAttr is not a "
733 const auto vecType = mlir::cast<cir::VectorType>(type);
735 if (vecType.getSize() != elts.size())
737 <<
"number of constant elements should match vector size";
740 LogicalResult elementTypeCheck =
success();
741 elts.walkImmediateSubElements(
742 [&](Attribute element) {
743 if (elementTypeCheck.failed()) {
747 auto typedElement = mlir::dyn_cast<TypedAttr>(element);
749 typedElement.getType() != vecType.getElementType()) {
750 elementTypeCheck = failure();
751 emitError() <<
"constant type should match vector element type";
756 return elementTypeCheck;
763LogicalResult cir::VTableAttr::verify(
764 llvm::function_ref<mlir::InFlightDiagnostic()> emitError, mlir::Type type,
765 mlir::ArrayAttr data) {
766 auto sTy = mlir::dyn_cast_if_present<cir::RecordType>(type);
768 return emitError() <<
"expected !cir.struct or !cir.union type result";
769 if (sTy.getMembers().empty() || data.empty())
770 return emitError() <<
"expected record type with one or more subtype";
772 if (cir::ConstRecordAttr::verify(emitError, type, data).failed())
775 for (
const auto &element : data.getAsRange<mlir::Attribute>()) {
776 const auto &constArrayAttr = mlir::dyn_cast<cir::ConstArrayAttr>(element);
778 return emitError() <<
"expected constant array subtype";
780 LogicalResult eltTypeCheck =
success();
781 auto arrayElts = mlir::cast<ArrayAttr>(constArrayAttr.getElts());
782 arrayElts.walkImmediateSubElements(
783 [&](mlir::Attribute attr) {
784 if (mlir::isa<ConstPtrAttr, GlobalViewAttr>(attr))
787 eltTypeCheck = emitError()
788 <<
"expected GlobalViewAttr or ConstPtrAttr";
790 [&](mlir::Type type) {});
791 if (eltTypeCheck.failed())
801std::string DynamicCastInfoAttr::getAlias()
const {
804 std::string alias =
"dyn_cast_info_";
806 alias.append(getSrcRtti().getSymbol().getValue());
807 alias.push_back(
'_');
808 alias.append(getDestRtti().getSymbol().getValue());
813LogicalResult DynamicCastInfoAttr::verify(
814 function_ref<InFlightDiagnostic()> emitError, cir::GlobalViewAttr srcRtti,
815 cir::GlobalViewAttr destRtti, mlir::FlatSymbolRefAttr runtimeFunc,
816 mlir::FlatSymbolRefAttr badCastFunc, cir::IntAttr offsetHint) {
817 auto isRttiPtr = [](mlir::Type ty) {
820 auto ptrTy = mlir::dyn_cast<cir::PointerType>(ty);
824 auto pointeeIntTy = mlir::dyn_cast<cir::IntType>(ptrTy.getPointee());
828 return pointeeIntTy.isUnsigned() && pointeeIntTy.getWidth() == 8;
831 if (!isRttiPtr(srcRtti.getType()))
832 return emitError() <<
"srcRtti must be an RTTI pointer";
834 if (!isRttiPtr(destRtti.getType()))
835 return emitError() <<
"destRtti must be an RTTI pointer";
845 mlir::StringAttr name) {
846 auto dict =
module->getAttrOfType<mlir::DictionaryAttr>(
847 CIRDialect::getRecordLayoutsAttrName());
848 assert(dict &&
"module missing cir.record_layouts attribute");
849 auto attr = dict.getAs<RecordLayoutAttr>(name);
850 assert(attr &&
"record layout entry missing for named record");
858void CIRDialect::registerAttributes() {
860#define GET_ATTRDEF_LIST
861#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)
static Decl::Kind getKind(const Decl *D)
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
C++ view class that accepts both !cir.struct and !cir.union types.
bool isIncomplete() const
llvm::ArrayRef< mlir::Type > getMembers() const
size_t getNumElements() const
RecordLayoutAttr getRecordLayout(mlir::ModuleOp module, mlir::StringAttr name)
Look up the RecordLayoutAttr for a named record in the module's cir.record_layouts dictionary.