13#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
16#include "mlir/IR/Attributes.h"
17#include "mlir/IR/DialectImplementation.h"
18#include "llvm/Support/MathExtras.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);
49 mlir::IntegerAttr &value);
53static mlir::ParseResult
55 mlir::DenseI32ArrayAttr &memberPath);
58 mlir::DenseI32ArrayAttr memberPath);
60#define GET_ATTRDEF_CLASSES
61#include "clang/CIR/Dialect/IR/CIROpsAttributes.cpp.inc"
71bool LangAddressSpaceAttr::isValidLoad(
72 mlir::Type type, mlir::ptr::AtomicOrdering ordering,
73 std::optional<int64_t> alignment,
const mlir::DataLayout *dataLayout,
74 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
75 llvm_unreachable(
"isValidLoad for LangAddressSpaceAttr NYI");
78bool LangAddressSpaceAttr::isValidStore(
79 mlir::Type type, mlir::ptr::AtomicOrdering ordering,
80 std::optional<int64_t> alignment,
const mlir::DataLayout *dataLayout,
81 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
82 llvm_unreachable(
"isValidStore for LangAddressSpaceAttr NYI");
85bool LangAddressSpaceAttr::isValidAtomicOp(
86 mlir::ptr::AtomicBinOp op, mlir::Type type,
87 mlir::ptr::AtomicOrdering ordering, std::optional<int64_t> alignment,
88 const mlir::DataLayout *dataLayout,
89 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
90 llvm_unreachable(
"isValidAtomicOp for LangAddressSpaceAttr NYI");
93bool LangAddressSpaceAttr::isValidAtomicXchg(
94 mlir::Type type, mlir::ptr::AtomicOrdering successOrdering,
95 mlir::ptr::AtomicOrdering failureOrdering, std::optional<int64_t> alignment,
96 const mlir::DataLayout *dataLayout,
97 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
98 llvm_unreachable(
"isValidAtomicXchg for LangAddressSpaceAttr NYI");
101bool LangAddressSpaceAttr::isValidAddrSpaceCast(
102 mlir::Type tgt, mlir::Type src,
103 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
104 llvm_unreachable(
"isValidAddrSpaceCast for LangAddressSpaceAttr NYI");
107bool LangAddressSpaceAttr::isValidPtrIntCast(
108 mlir::Type intLikeTy, mlir::Type ptrLikeTy,
109 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
110 llvm_unreachable(
"isValidPtrIntCast for LangAddressSpaceAttr NYI");
113bool TargetAddressSpaceAttr::isValidLoad(
114 mlir::Type type, mlir::ptr::AtomicOrdering ordering,
115 std::optional<int64_t> alignment,
const mlir::DataLayout *dataLayout,
116 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
117 llvm_unreachable(
"isValidLoad for TargetAddressSpaceAttr NYI");
120bool TargetAddressSpaceAttr::isValidStore(
121 mlir::Type type, mlir::ptr::AtomicOrdering ordering,
122 std::optional<int64_t> alignment,
const mlir::DataLayout *dataLayout,
123 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
124 llvm_unreachable(
"isValidStore for TargetAddressSpaceAttr NYI");
127bool TargetAddressSpaceAttr::isValidAtomicOp(
128 mlir::ptr::AtomicBinOp op, mlir::Type type,
129 mlir::ptr::AtomicOrdering ordering, std::optional<int64_t> alignment,
130 const mlir::DataLayout *dataLayout,
131 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
132 llvm_unreachable(
"isValidAtomicOp for TargetAddressSpaceAttr NYI");
135bool TargetAddressSpaceAttr::isValidAtomicXchg(
136 mlir::Type type, mlir::ptr::AtomicOrdering successOrdering,
137 mlir::ptr::AtomicOrdering failureOrdering, std::optional<int64_t> alignment,
138 const mlir::DataLayout *dataLayout,
139 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
140 llvm_unreachable(
"isValidAtomicXchg for TargetAddressSpaceAttr NYI");
143bool TargetAddressSpaceAttr::isValidAddrSpaceCast(
144 mlir::Type tgt, mlir::Type src,
145 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
146 llvm_unreachable(
"isValidAddrSpaceCast for TargetAddressSpaceAttr NYI");
149bool TargetAddressSpaceAttr::isValidPtrIntCast(
150 mlir::Type intLikeTy, mlir::Type ptrLikeTy,
151 llvm::function_ref<mlir::InFlightDiagnostic()> emitError)
const {
152 llvm_unreachable(
"isValidPtrIntCast for TargetAddressSpaceAttr NYI");
159LogicalResult PtrSpecAttr::verify(function_ref<InFlightDiagnostic()> emitError,
162 constexpr unsigned kBitsInByte = 8;
163 if (size % kBitsInByte != 0)
164 return emitError() <<
"size entry must be divisible by 8";
165 if (abi % kBitsInByte != 0)
166 return emitError() <<
"abi entry must be divisible by 8";
167 if (preferred % kBitsInByte != 0)
168 return emitError() <<
"preferred entry must be divisible by 8";
169 if (index != kOptionalSpecValue && index % kBitsInByte != 0)
170 return emitError() <<
"index entry must be divisible by 8";
172 return emitError() <<
"preferred alignment is expected to be at least "
173 "as large as ABI alignment";
182BitIntSpecAttr::verify(function_ref<InFlightDiagnostic()> emitError,
186 if (maxAlign < 8 || !llvm::isPowerOf2_32(maxAlign))
187 return emitError() <<
"max_align must be a power of two of at least 8";
196BitFieldDeclAttr::verify(function_ref<InFlightDiagnostic()> emitError,
197 mlir::Type declaredType, uint64_t width,
199 if (width == 0 && !isUnnamed)
200 return emitError() <<
"zero-width bit-field cannot be named";
209 mlir::ArrayAttr members) {
211 llvm::interleaveComma(members, printer);
216 mlir::ArrayAttr &members) {
219 auto delimiter = AsmParser::Delimiter::Braces;
220 auto result = parser.parseCommaSeparatedList(delimiter, [&]() {
221 mlir::TypedAttr attr;
222 if (parser.parseAttribute(attr).failed())
223 return mlir::failure();
224 elts.push_back(attr);
225 return mlir::success();
229 return mlir::failure();
231 members = mlir::ArrayAttr::get(parser.getContext(), elts);
232 return mlir::success();
240 mlir::Type initType) {
241 auto memberArrayType = mlir::dyn_cast<cir::ArrayType>(memberType);
242 auto initArrayType = mlir::dyn_cast<cir::ArrayType>(initType);
243 return memberArrayType && memberArrayType.getSize() == 0 && initArrayType &&
244 memberArrayType.getElementType() == initArrayType.getElementType();
248ConstRecordAttr::verify(function_ref<InFlightDiagnostic()> emitError,
249 mlir::Type type, ArrayAttr members) {
250 auto sTy = mlir::dyn_cast_if_present<cir::RecordType>(type);
252 return emitError() <<
"expected !cir.struct or !cir.union type";
257 if (members.size() != 1)
258 return emitError() <<
"union constant must have exactly one element, got "
260 auto m = mlir::cast<mlir::TypedAttr>(members[0]);
262 if (!llvm::any_of(sTy.getMembers(), [&](mlir::Type memberTy) {
263 mlir::Type storageType = cir::memberStorageType(memberTy);
264 return storageType == m.getType() ||
265 isValidFlexibleArrayInit(storageType, m.getType());
267 return emitError() <<
"union element type " << m.getType()
268 <<
" is not a member of " << sTy;
276 for (mlir::Type memberTy : sTy.getMembers())
280 if (storedMembers.size() != members.size())
281 return emitError() <<
"number of elements must match";
283 for (
const auto &[attrIdx, member] : llvm::enumerate(storedMembers)) {
284 auto m = mlir::cast<mlir::TypedAttr>(members[attrIdx]);
289 if (attrIdx == storedMembers.size() - 1) {
290 auto memArrayTy = dyn_cast<cir::ArrayType>(member);
291 if (memArrayTy && memArrayTy.getSize() == 0) {
294 if (!isa<cir::ArrayType>(m.getType()))
296 <<
"element at index " << attrIdx <<
" has type "
297 << m.getType() <<
" but the expected type for this element is "
300 cir::ArrayType initArrayTy = cast<cir::ArrayType>(m.getType());
302 if (initArrayTy.getElementType() != memArrayTy.getElementType())
304 <<
"flexible array member at index " << attrIdx <<
" has type "
306 <<
" which doesn't match the expected element type of member "
312 if (member != m.getType())
313 return emitError() <<
"element at index " << attrIdx <<
" has type "
315 <<
" but the expected type for this element is "
326LogicalResult OptInfoAttr::verify(function_ref<InFlightDiagnostic()> emitError,
327 unsigned level,
unsigned size) {
330 <<
"optimization level must be between 0 and 3 inclusive";
333 <<
"size optimization level must be between 0 and 2 inclusive";
343static ParseResult
parseConstPtr(AsmParser &parser, mlir::IntegerAttr &value) {
345 if (parser.parseOptionalKeyword(
"null").succeeded()) {
346 value = parser.getBuilder().getI64IntegerAttr(0);
350 return parser.parseAttribute(value);
361 mlir::DenseI32ArrayAttr &memberPath) {
362 if (parser.parseOptionalKeyword(
"null").succeeded())
365 auto parsed = mlir::FieldParser<mlir::DenseI32ArrayAttr>::parse(parser);
366 if (mlir::failed(parsed))
368 memberPath = *parsed;
373 mlir::DenseI32ArrayAttr memberPath) {
377 p.printStrippedAttrOrType(memberPath);
385 cir::IntTypeInterface ty) {
386 llvm::SMLoc loc = parser.getCurrentLocation();
388 mlir::OptionalParseResult result = parser.parseOptionalInteger(parsed);
389 if (!result.has_value() || failed(*result))
390 return parser.emitError(loc,
"expected integer value");
392 const unsigned width = ty.getWidth();
394 ty.isSigned() ? parsed.getSignificantBits() <= width
395 : !parsed.isNegative() && parsed.getActiveBits() <= width;
397 return parser.emitError(loc,
"integer value too large for the given type");
399 value = ty.isSigned() ? parsed.sextOrTrunc(width) : parsed.zextOrTrunc(width);
404 cir::IntTypeInterface ty) {
406 value.toString(str, 10, ty.isSigned());
410LogicalResult IntAttr::verify(function_ref<InFlightDiagnostic()> emitError,
411 cir::IntTypeInterface type, llvm::APInt value) {
412 if (value.getBitWidth() != type.getWidth())
413 return emitError() <<
"type and value bitwidth mismatch: "
414 << type.getWidth() <<
" != " << value.getBitWidth();
427 FailureOr<APFloat> &value,
428 cir::FPTypeInterface fpType) {
430 APFloat parsedValue(0.0);
431 if (parser.parseFloat(fpType.getFloatSemantics(), parsedValue))
434 value.emplace(parsedValue);
442MemoryEffectsAttr MemoryEffectsAttr::none(MLIRContext *ctx) {
443 return get(ctx, ModRefInfo::NoModRef);
446MemoryEffectsAttr MemoryEffectsAttr::readOnly(MLIRContext *ctx) {
447 return get(ctx, ModRefInfo::Ref);
450MemoryEffectsAttr MemoryEffectsAttr::writeOnly(MLIRContext *ctx) {
451 return get(ctx, ModRefInfo::Mod);
454MemoryEffectsAttr MemoryEffectsAttr::argMemOnly(MLIRContext *ctx,
456 return get(ctx, ModRefInfo::NoModRef, mr,
457 ModRefInfo::NoModRef,
458 ModRefInfo::NoModRef,
459 ModRefInfo::NoModRef,
460 ModRefInfo::NoModRef);
463MemoryEffectsAttr MemoryEffectsAttr::inaccessibleMemOnly(MLIRContext *ctx,
465 return get(ctx, ModRefInfo::NoModRef,
466 ModRefInfo::NoModRef, mr,
467 ModRefInfo::NoModRef,
468 ModRefInfo::NoModRef,
469 ModRefInfo::NoModRef);
472MemoryEffectsAttr MemoryEffectsAttr::errnoMemOnly(MLIRContext *ctx,
474 return get(ctx, ModRefInfo::NoModRef,
475 ModRefInfo::NoModRef,
476 ModRefInfo::NoModRef, mr,
477 ModRefInfo::NoModRef,
478 ModRefInfo::NoModRef);
481MemoryEffectsAttr MemoryEffectsAttr::otherMemOnly(MLIRContext *ctx,
483 return get(ctx, mr, ModRefInfo::NoModRef,
484 ModRefInfo::NoModRef,
485 ModRefInfo::NoModRef,
486 ModRefInfo::NoModRef,
487 ModRefInfo::NoModRef);
490MemoryEffectsAttr MemoryEffectsAttr::inaccessibleOrArgMemOnly(MLIRContext *ctx,
492 return get(ctx, ModRefInfo::NoModRef, mr,
493 mr, ModRefInfo::NoModRef,
494 ModRefInfo::NoModRef,
495 ModRefInfo::NoModRef);
498MemoryEffectsAttr MemoryEffectsAttr::inaccessibleOrErrnoMemOnly(
499 MLIRContext *ctx, ModRefInfo inaccessibleMr, ModRefInfo errnoMr) {
500 return get(ctx, ModRefInfo::NoModRef,
501 ModRefInfo::NoModRef,
502 inaccessibleMr, errnoMr,
503 ModRefInfo::NoModRef,
504 ModRefInfo::NoModRef);
507MemoryEffectsAttr MemoryEffectsAttr::inaccessibleOrArgOrErrnoMemOnly(
508 MLIRContext *ctx, ModRefInfo inaccessibleOrArgMr, ModRefInfo errnoMr) {
509 return get(ctx, ModRefInfo::NoModRef,
511 inaccessibleOrArgMr, errnoMr,
512 ModRefInfo::NoModRef,
513 ModRefInfo::NoModRef);
517MemoryEffectsAttr::argumentOrErrnoMemOnly(MLIRContext *ctx, ModRefInfo argMr,
518 ModRefInfo errnoMr) {
519 return get(ctx, ModRefInfo::NoModRef, argMr,
520 ModRefInfo::NoModRef, errnoMr,
521 ModRefInfo::NoModRef,
522 ModRefInfo::NoModRef);
525FPAttr FPAttr::getZero(Type type) {
528 mlir::cast<cir::FPTypeInterface>(type).getFloatSemantics()));
531LogicalResult FPAttr::verify(function_ref<InFlightDiagnostic()> emitError,
532 cir::FPTypeInterface fpType, APFloat value) {
533 if (APFloat::SemanticsToEnum(fpType.getFloatSemantics()) !=
534 APFloat::SemanticsToEnum(value.getSemantics()))
535 return emitError() <<
"floating-point semantics mismatch";
544std::string CmpThreeWayInfoAttr::getAlias()
const {
545 std::string alias =
"cmpinfo";
547 switch (getOrdering()) {
548 case CmpOrdering::Strong:
549 alias.append(
"_strong_");
551 case CmpOrdering::Weak:
552 alias.append(
"_weak_");
554 case CmpOrdering::Partial:
555 alias.append(
"_partial_");
559 auto appendInt = [&](
int64_t value) {
561 alias.push_back(
'n');
564 alias.append(std::to_string(value));
574 if (std::optional<int> unordered = getUnordered()) {
576 appendInt(unordered.value());
583CmpThreeWayInfoAttr::verify(function_ref<InFlightDiagnostic()> emitError,
585 int64_t gt, std::optional<int64_t> unordered) {
587 if ((ordering == CmpOrdering::Strong || ordering == CmpOrdering::Weak) &&
589 emitError() <<
"strong and weak ordering do not include unordered";
592 if (ordering == CmpOrdering::Partial && !unordered) {
593 emitError() <<
"partial ordering requires unordered value";
605ConstComplexAttr::verify(function_ref<InFlightDiagnostic()> emitError,
606 cir::ComplexType type, mlir::TypedAttr real,
607 mlir::TypedAttr imag) {
608 mlir::Type elemType = type.getElementType();
609 if (real.getType() != elemType)
611 <<
"type of the real part does not match the complex type";
613 if (imag.getType() != elemType)
615 <<
"type of the imaginary part does not match the complex type";
624void CUDAVarRegistrationInfoAttr::print(AsmPrinter &p)
const {
625 p <<
"<" << getDeviceSideName();
626 p <<
", " << stringifyEnum(
getKind());
633 if (
getKind() == CUDADeviceVarKind::Surface)
634 p <<
", surface_type = " << getSurfTexType();
635 else if (
getKind() == CUDADeviceVarKind::Texture) {
636 p <<
", texture_type = " << getSurfTexType();
643Attribute CUDAVarRegistrationInfoAttr::parse(AsmParser &parser, Type odsType) {
644 if (parser.parseLess())
647 std::string deviceSideName;
648 if (parser.parseKeywordOrString(&deviceSideName)) {
649 parser.emitError(parser.getCurrentLocation(),
650 "expected device variable name");
654 if (parser.parseComma())
659 if (parser.parseKeyword(&kindStr))
662 std::optional<CUDADeviceVarKind>
kind = symbolizeCUDADeviceVarKind(kindStr);
664 parser.emitError(parser.getCurrentLocation(),
665 "unknown device variable kind: ")
671 bool isExtern =
false;
672 bool isConstant =
false;
673 bool isManaged =
false;
675 bool normalized =
false;
677 while (parser.parseOptionalGreater().failed()) {
678 if (parser.parseComma())
682 if (parser.parseKeyword(&flag))
685 if (flag ==
"extern")
687 else if (flag ==
"constant")
689 else if (flag ==
"managed")
691 else if (flag ==
"normalized") {
692 if (*
kind != CUDADeviceVarKind::Texture) {
693 parser.emitError(parser.getCurrentLocation(),
694 "'normalized' is only valid for Texture");
698 }
else if (flag ==
"surface_type") {
699 if (*
kind != CUDADeviceVarKind::Surface) {
700 parser.emitError(parser.getCurrentLocation(),
701 "'surface_type' is only valid for Surface");
704 if (parser.parseEqual() || parser.parseInteger(surfTexType))
706 }
else if (flag ==
"texture_type") {
707 if (*
kind != CUDADeviceVarKind::Texture) {
708 parser.emitError(parser.getCurrentLocation(),
709 "'texture_type' is only valid for Texture");
712 if (parser.parseEqual() || parser.parseInteger(surfTexType))
715 parser.emitError(parser.getCurrentLocation(),
"unknown flag: ") << flag;
720 return get(parser.getContext(), deviceSideName, *
kind, isExtern, isConstant,
721 isManaged, surfTexType, normalized);
729DataMemberAttr::verify(function_ref<InFlightDiagnostic()> emitError,
730 cir::DataMemberType ty,
731 mlir::DenseI32ArrayAttr memberPath) {
735 if (memberPath.empty())
736 return emitError() <<
"#cir.data_member path must not be empty";
738 mlir::Type currentTy = ty.getClassTy();
739 for (
auto [
step, idx] : llvm::enumerate(memberPath.asArrayRef())) {
740 auto recTy = mlir::dyn_cast<cir::RecordType>(currentTy);
742 return emitError() <<
"#cir.data_member path step " <<
step
743 <<
" reaches a non-record type";
745 if (recTy.isIncomplete())
748 if (idx < 0 ||
static_cast<unsigned>(idx) >= recTy.getNumElements())
749 return emitError() <<
"#cir.data_member path index " << idx <<
" at step "
750 <<
step <<
" is out of range";
752 currentTy = recTy.getMembers()[idx];
755 if (currentTy != ty.getMemberTy())
757 <<
"member type of a #cir.data_member attribute must match "
758 "the attribute type";
767LogicalResult MethodAttr::verify(function_ref<InFlightDiagnostic()> emitError,
768 cir::MethodType type,
769 std::optional<FlatSymbolRefAttr> symbol,
770 std::optional<uint64_t> vtable_offset) {
771 if (symbol.has_value() && vtable_offset.has_value())
773 <<
"at most one of symbol and vtable_offset can be present "
779Attribute MethodAttr::parse(AsmParser &parser, Type odsType) {
780 auto ty = mlir::cast<cir::MethodType>(odsType);
782 if (parser.parseLess().failed())
786 if (parser.parseOptionalKeyword(
"null").succeeded()) {
787 if (parser.parseGreater().failed())
794 FlatSymbolRefAttr symbol;
795 mlir::OptionalParseResult parseSymbolRefResult =
796 parser.parseOptionalAttribute(symbol);
797 if (parseSymbolRefResult.has_value()) {
798 if (parseSymbolRefResult.value().failed())
800 if (parser.parseGreater().failed())
802 return get(ty, symbol);
806 std::uint64_t vtableOffset = 0;
807 if (parser.parseKeyword(
"vtable_offset"))
809 if (parser.parseEqual())
811 if (parser.parseInteger(vtableOffset))
814 if (parser.parseGreater())
817 return get(ty, vtableOffset);
820void MethodAttr::print(AsmPrinter &printer)
const {
821 auto symbol = getSymbol();
822 auto vtableOffset = getVtableOffset();
825 if (symbol.has_value()) {
827 }
else if (vtableOffset.has_value()) {
828 printer <<
"vtable_offset = " << *vtableOffset;
840ConstArrayAttr::verify(function_ref<InFlightDiagnostic()> emitError, Type type,
841 Attribute elts,
int trailingZerosNum) {
843 if (!(mlir::isa<ArrayAttr, StringAttr>(elts)))
844 return emitError() <<
"constant array expects ArrayAttr or StringAttr";
846 if (
auto strAttr = mlir::dyn_cast<StringAttr>(elts)) {
847 const auto arrayTy = mlir::cast<ArrayType>(type);
848 const auto intTy = mlir::dyn_cast<IntType>(arrayTy.getElementType());
851 if (!intTy || intTy.getWidth() != 8)
853 <<
"constant array element for string literals expects "
854 "!cir.int<u, 8> element type";
858 assert(mlir::isa<ArrayAttr>(elts));
859 const auto arrayAttr = mlir::cast<mlir::ArrayAttr>(elts);
860 const auto arrayTy = mlir::cast<ArrayType>(type);
863 if (arrayAttr.size() > arrayTy.getSize())
864 return emitError() <<
"constant array has " << arrayAttr.size()
865 <<
" values but array type has size "
866 << arrayTy.getSize();
867 if (arrayTy.getSize() != arrayAttr.size() + trailingZerosNum)
868 return emitError() <<
"constant array size should match type size";
872Attribute ConstArrayAttr::parse(AsmParser &parser, Type type) {
873 mlir::FailureOr<Type> resultTy;
874 mlir::FailureOr<Attribute> resultVal;
877 if (parser.parseLess())
881 resultVal = FieldParser<Attribute>::parse(parser);
882 if (failed(resultVal)) {
884 parser.getCurrentLocation(),
885 "failed to parse ConstArrayAttr parameter 'value' which is "
886 "to be a `Attribute`");
891 if (mlir::isa<ArrayAttr>(*resultVal)) {
893 if (parser.parseOptionalColon().failed()) {
896 resultTy = FieldParser<Type>::parse(parser);
897 if (failed(resultTy)) {
899 parser.getCurrentLocation(),
900 "failed to parse ConstArrayAttr parameter 'type' which is "
901 "to be a `::mlir::Type`");
906 auto ta = mlir::cast<TypedAttr>(*resultVal);
907 resultTy = ta.getType();
908 if (mlir::isa<mlir::NoneType>(*resultTy)) {
909 parser.emitError(parser.getCurrentLocation(),
910 "expected type declaration for string literal");
916 if (parser.parseOptionalComma().succeeded()) {
917 if (parser.parseOptionalKeyword(
"trailing_zeros").succeeded()) {
918 unsigned totalSize = mlir::cast<cir::ArrayType>(type).getSize();
919 mlir::Attribute elts = resultVal.value();
920 if (
auto str = mlir::dyn_cast<mlir::StringAttr>(elts))
921 zeros = totalSize - str.size();
923 zeros = totalSize - mlir::cast<mlir::ArrayAttr>(elts).size();
930 if (parser.parseGreater())
933 return parser.getChecked<ConstArrayAttr>(parser.getCurrentLocation(),
934 parser.getContext(), type,
935 resultVal.value(), zeros);
938void ConstArrayAttr::print(AsmPrinter &printer)
const {
940 printer.printStrippedAttrOrType(getElts());
941 if (getTrailingZerosNum())
942 printer <<
", trailing_zeros";
951cir::ConstVectorAttr::verify(function_ref<InFlightDiagnostic()> emitError,
952 Type type, ArrayAttr elts) {
954 if (!mlir::isa<cir::VectorType>(type))
955 return emitError() <<
"type of cir::ConstVectorAttr is not a "
959 const auto vecType = mlir::cast<cir::VectorType>(type);
961 if (vecType.isScalable())
962 return emitError() <<
"scalable vector constants are not supported";
964 if (vecType.getFixedSize() != elts.size())
966 <<
"number of constant elements should match vector size";
969 LogicalResult elementTypeCheck =
success();
970 elts.walkImmediateSubElements(
971 [&](Attribute element) {
972 if (elementTypeCheck.failed()) {
976 auto typedElement = mlir::dyn_cast<TypedAttr>(element);
978 typedElement.getType() != vecType.getElementType()) {
979 elementTypeCheck = failure();
980 emitError() <<
"constant type should match vector element type";
985 return elementTypeCheck;
992LogicalResult cir::VTableAttr::verify(
993 llvm::function_ref<mlir::InFlightDiagnostic()> emitError, mlir::Type type,
994 mlir::ArrayAttr data) {
995 auto sTy = mlir::dyn_cast_if_present<cir::RecordType>(type);
997 return emitError() <<
"expected !cir.struct or !cir.union type result";
998 if (sTy.getMembers().empty() || data.empty())
999 return emitError() <<
"expected record type with one or more subtype";
1001 if (cir::ConstRecordAttr::verify(emitError, type, data).failed())
1004 for (
const auto &element : data.getAsRange<mlir::Attribute>()) {
1005 const auto &constArrayAttr = mlir::dyn_cast<cir::ConstArrayAttr>(element);
1006 if (!constArrayAttr)
1007 return emitError() <<
"expected constant array subtype";
1009 LogicalResult eltTypeCheck =
success();
1010 auto arrayElts = mlir::cast<ArrayAttr>(constArrayAttr.getElts());
1011 arrayElts.walkImmediateSubElements(
1012 [&](mlir::Attribute attr) {
1013 if (mlir::isa<ConstPtrAttr, GlobalViewAttr>(attr))
1016 eltTypeCheck = emitError()
1017 <<
"expected GlobalViewAttr or ConstPtrAttr";
1019 [&](mlir::Type type) {});
1020 if (eltTypeCheck.failed())
1021 return eltTypeCheck;
1030std::string DynamicCastInfoAttr::getAlias()
const {
1033 std::string alias =
"dyn_cast_info_";
1035 alias.append(getSrcRtti().getSymbol().getValue());
1036 alias.push_back(
'_');
1037 alias.append(getDestRtti().getSymbol().getValue());
1045 auto ptrTy = mlir::dyn_cast<cir::PointerType>(ty);
1049 auto pointeeIntTy = mlir::dyn_cast<cir::IntType>(ptrTy.getPointee());
1053 return pointeeIntTy.isUnsigned() && pointeeIntTy.getWidth() == 8;
1056LogicalResult DynamicCastInfoAttr::verify(
1057 function_ref<InFlightDiagnostic()> emitError, cir::GlobalViewAttr srcRtti,
1058 cir::GlobalViewAttr destRtti, mlir::FlatSymbolRefAttr runtimeFunc,
1059 mlir::FlatSymbolRefAttr badCastFunc, cir::IntAttr offsetHint) {
1061 return emitError() <<
"srcRtti must be an RTTI pointer";
1064 return emitError() <<
"destRtti must be an RTTI pointer";
1073LogicalResult EhFilterAttr::verify(function_ref<InFlightDiagnostic()> emitError,
1074 mlir::ArrayAttr permittedTypes) {
1075 for (mlir::Attribute typeAttr : permittedTypes) {
1076 auto rtti = mlir::dyn_cast<cir::GlobalViewAttr>(typeAttr);
1077 if (!rtti || !
isRttiPtr(rtti.getType()))
1078 return emitError() <<
"permitted type list must contain only type info "
1089Attribute RecordLayoutAttr::parse(AsmParser &parser, Type odsType) {
1091 SMLoc loc = parser.getCurrentLocation();
1092 if (parser.parseLess() || parser.parseKeyword(
"arg_passing_kind") ||
1093 parser.parseEqual())
1096 FailureOr<ArgPassingKind>
kind = FieldParser<ArgPassingKind>::parse(parser);
1100 if (parser.parseComma() || parser.parseKeyword(
"has_trivial_dtor") ||
1101 parser.parseEqual())
1103 FailureOr<bool> hasTrivialDtor = FieldParser<bool>::parse(parser);
1104 if (failed(hasTrivialDtor))
1107 if (parser.parseComma() || parser.parseKeyword(
"record_align") ||
1108 parser.parseEqual())
1110 FailureOr<uint64_t> recordAlign = FieldParser<uint64_t>::parse(parser);
1111 if (failed(recordAlign))
1116 uint64_t unadjustedAlign = *recordAlign;
1117 bool transparentUnion =
false;
1118 bool flexibleArrayMember =
false;
1119 while (succeeded(parser.parseOptionalComma())) {
1120 if (succeeded(parser.parseOptionalKeyword(
"unadjusted_align"))) {
1121 if (parser.parseEqual())
1123 FailureOr<uint64_t> parsed = FieldParser<uint64_t>::parse(parser);
1126 unadjustedAlign = *parsed;
1129 if (succeeded(parser.parseOptionalKeyword(
"transparent_union"))) {
1130 if (parser.parseEqual())
1132 FailureOr<bool> parsed = FieldParser<bool>::parse(parser);
1135 transparentUnion = *parsed;
1138 if (succeeded(parser.parseOptionalKeyword(
"flexible_array_member"))) {
1139 if (parser.parseEqual())
1141 FailureOr<bool> parsed = FieldParser<bool>::parse(parser);
1144 flexibleArrayMember = *parsed;
1147 parser.emitError(parser.getCurrentLocation(),
1148 "expected unadjusted_align, transparent_union or "
1149 "flexible_array_member");
1153 if (parser.parseGreater())
1155 return parser.getChecked<RecordLayoutAttr>(
1156 loc, parser.getContext(), *
kind, *hasTrivialDtor, *recordAlign,
1157 unadjustedAlign, transparentUnion, flexibleArrayMember);
1160void RecordLayoutAttr::print(AsmPrinter &printer)
const {
1161 printer <<
"<arg_passing_kind = ";
1162 printer.printStrippedAttrOrType(getArgPassingKind());
1163 printer <<
", has_trivial_dtor = ";
1164 printer.printStrippedAttrOrType(getHasTrivialDtor());
1165 printer <<
", record_align = ";
1166 printer.printStrippedAttrOrType(getRecordAlign());
1168 if (getUnadjustedAlign() != getRecordAlign()) {
1169 printer <<
", unadjusted_align = ";
1170 printer.printStrippedAttrOrType(getUnadjustedAlign());
1172 if (getTransparentUnion()) {
1173 printer <<
", transparent_union = ";
1174 printer.printStrippedAttrOrType(getTransparentUnion());
1176 if (getFlexibleArrayMember()) {
1177 printer <<
", flexible_array_member = ";
1178 printer.printStrippedAttrOrType(getFlexibleArrayMember());
1188 mlir::StringAttr name) {
1191 auto dict = mod->getAttrOfType<mlir::DictionaryAttr>(
1192 CIRDialect::getRecordLayoutsAttrName());
1195 return dict.getAs<RecordLayoutAttr>(name);
1199 mlir::StringAttr name) {
1201 assert(attr &&
"record layout entry missing for named record");
1209void CIRDialect::registerAttributes() {
1211#define GET_ATTRDEF_LIST
1212#include "clang/CIR/Dialect/IR/CIROpsAttributes.cpp.inc"
static mlir::ParseResult parseFloatLiteral(mlir::AsmParser &parser, mlir::FailureOr< llvm::APFloat > &value, cir::FPTypeInterface fpType)
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 void printFloatLiteral(mlir::AsmPrinter &p, llvm::APFloat value, mlir::Type ty)
static mlir::ParseResult parseDataMemberPath(mlir::AsmParser &parser, mlir::DenseI32ArrayAttr &memberPath)
static bool isRttiPtr(mlir::Type ty)
static void printDataMemberPath(mlir::AsmPrinter &p, mlir::DenseI32ArrayAttr memberPath)
static mlir::ParseResult parseRecordMembers(mlir::AsmParser &parser, mlir::ArrayAttr &members)
static bool isValidFlexibleArrayInit(mlir::Type memberType, mlir::Type initType)
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
RecordLayoutAttr tryGetRecordLayout(mlir::ModuleOp mod, mlir::StringAttr name)
Same lookup as getRecordLayout, but returns a null attribute instead of asserting when the record has...
RecordLayoutAttr getRecordLayout(mlir::ModuleOp mod, mlir::StringAttr name)
Look up the RecordLayoutAttr for a named record in the module's cir.record_layouts dictionary.
mlir::Type memberStorageType(mlir::Type memberTy)
The storage a member is stored as: the access unit for a bit-field member, and the member type itself...
bool memberOwnsBytes(mlir::Type memberTy)
Whether a record member occupies bytes of its record.
float __ovld __cnfn step(float, float)
Returns 0.0 if x < edge, otherwise it returns 1.0.
__builtin_bitreverse32 uint32_t __builtin_riscv_aaddu_u32 __builtin_riscv_asubu_u32 int32_t
int32_t uint32_t uint32_t __builtin_riscv_mhracc_i32 __builtin_riscv_mhraccu_u32 __builtin_riscv_mhraccsu_i32 __builtin_riscv_mulqr_i32 int64_t