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";
196VectorSpecAttr::verify(function_ref<InFlightDiagnostic()> emitError,
200 if (maxAlign < 8 || !llvm::isPowerOf2_32(maxAlign))
201 return emitError() <<
"max_align must be a power of two of at least 8";
210BitFieldDeclAttr::verify(function_ref<InFlightDiagnostic()> emitError,
211 mlir::Type declaredType,
uint64_t width,
213 if (width == 0 && !isUnnamed)
214 return emitError() <<
"zero-width bit-field cannot be named";
223 mlir::ArrayAttr members) {
225 llvm::interleaveComma(members, printer);
230 mlir::ArrayAttr &members) {
233 auto delimiter = AsmParser::Delimiter::Braces;
234 auto result = parser.parseCommaSeparatedList(delimiter, [&]() {
235 mlir::TypedAttr attr;
236 if (parser.parseAttribute(attr).failed())
237 return mlir::failure();
238 elts.push_back(attr);
239 return mlir::success();
243 return mlir::failure();
245 members = mlir::ArrayAttr::get(parser.getContext(), elts);
246 return mlir::success();
254 mlir::Type initType) {
255 auto memberArrayType = mlir::dyn_cast<cir::ArrayType>(memberType);
256 auto initArrayType = mlir::dyn_cast<cir::ArrayType>(initType);
257 return memberArrayType && memberArrayType.getSize() == 0 && initArrayType &&
258 memberArrayType.getElementType() == initArrayType.getElementType();
262ConstRecordAttr::verify(function_ref<InFlightDiagnostic()> emitError,
263 mlir::Type type, ArrayAttr members) {
264 auto sTy = mlir::dyn_cast_if_present<cir::RecordType>(type);
266 return emitError() <<
"expected !cir.struct or !cir.union type";
271 if (members.size() != 1)
272 return emitError() <<
"union constant must have exactly one element, got "
274 auto m = mlir::cast<mlir::TypedAttr>(members[0]);
276 if (!llvm::any_of(sTy.getMembers(), [&](mlir::Type memberTy) {
277 mlir::Type storageType = cir::memberStorageType(memberTy);
278 return storageType == m.getType() ||
279 isValidFlexibleArrayInit(storageType, m.getType());
281 return emitError() <<
"union element type " << m.getType()
282 <<
" is not a member of " << sTy;
290 for (mlir::Type memberTy : sTy.getMembers())
294 if (storedMembers.size() != members.size())
295 return emitError() <<
"number of elements must match";
297 for (
const auto &[attrIdx, member] : llvm::enumerate(storedMembers)) {
298 auto m = mlir::cast<mlir::TypedAttr>(members[attrIdx]);
303 if (attrIdx == storedMembers.size() - 1) {
304 auto memArrayTy = dyn_cast<cir::ArrayType>(member);
305 if (memArrayTy && memArrayTy.getSize() == 0) {
308 if (!isa<cir::ArrayType>(m.getType()))
310 <<
"element at index " << attrIdx <<
" has type "
311 << m.getType() <<
" but the expected type for this element is "
314 cir::ArrayType initArrayTy = cast<cir::ArrayType>(m.getType());
316 if (initArrayTy.getElementType() != memArrayTy.getElementType())
318 <<
"flexible array member at index " << attrIdx <<
" has type "
320 <<
" which doesn't match the expected element type of member "
326 if (member != m.getType())
327 return emitError() <<
"element at index " << attrIdx <<
" has type "
329 <<
" but the expected type for this element is "
340LogicalResult OptInfoAttr::verify(function_ref<InFlightDiagnostic()> emitError,
341 unsigned level,
unsigned size) {
344 <<
"optimization level must be between 0 and 3 inclusive";
347 <<
"size optimization level must be between 0 and 2 inclusive";
357static ParseResult
parseConstPtr(AsmParser &parser, mlir::IntegerAttr &value) {
359 if (parser.parseOptionalKeyword(
"null").succeeded()) {
360 value = parser.getBuilder().getI64IntegerAttr(0);
364 return parser.parseAttribute(value);
375 mlir::DenseI32ArrayAttr &memberPath) {
376 if (parser.parseOptionalKeyword(
"null").succeeded())
379 auto parsed = mlir::FieldParser<mlir::DenseI32ArrayAttr>::parse(parser);
380 if (mlir::failed(parsed))
382 memberPath = *parsed;
387 mlir::DenseI32ArrayAttr memberPath) {
391 p.printStrippedAttrOrType(memberPath);
399 cir::IntTypeInterface ty) {
400 llvm::SMLoc loc = parser.getCurrentLocation();
402 mlir::OptionalParseResult result = parser.parseOptionalInteger(parsed);
403 if (!result.has_value() || failed(*result))
404 return parser.emitError(loc,
"expected integer value");
406 const unsigned width = ty.getWidth();
408 ty.isSigned() ? parsed.getSignificantBits() <= width
409 : !parsed.isNegative() && parsed.getActiveBits() <= width;
411 return parser.emitError(loc,
"integer value too large for the given type");
413 value = ty.isSigned() ? parsed.sextOrTrunc(width) : parsed.zextOrTrunc(width);
418 cir::IntTypeInterface ty) {
420 value.toString(str, 10, ty.isSigned());
424LogicalResult IntAttr::verify(function_ref<InFlightDiagnostic()> emitError,
425 cir::IntTypeInterface type, llvm::APInt value) {
426 if (value.getBitWidth() != type.getWidth())
427 return emitError() <<
"type and value bitwidth mismatch: "
428 << type.getWidth() <<
" != " << value.getBitWidth();
441 FailureOr<APFloat> &value,
442 cir::FPTypeInterface fpType) {
444 APFloat parsedValue(0.0);
445 if (parser.parseFloat(fpType.getFloatSemantics(), parsedValue))
448 value.emplace(parsedValue);
456MemoryEffectsAttr MemoryEffectsAttr::none(MLIRContext *ctx) {
457 return get(ctx, ModRefInfo::NoModRef);
460MemoryEffectsAttr MemoryEffectsAttr::readOnly(MLIRContext *ctx) {
461 return get(ctx, ModRefInfo::Ref);
464MemoryEffectsAttr MemoryEffectsAttr::writeOnly(MLIRContext *ctx) {
465 return get(ctx, ModRefInfo::Mod);
468MemoryEffectsAttr MemoryEffectsAttr::argMemOnly(MLIRContext *ctx,
470 return get(ctx, ModRefInfo::NoModRef, mr,
471 ModRefInfo::NoModRef,
472 ModRefInfo::NoModRef,
473 ModRefInfo::NoModRef,
474 ModRefInfo::NoModRef);
477MemoryEffectsAttr MemoryEffectsAttr::inaccessibleMemOnly(MLIRContext *ctx,
479 return get(ctx, ModRefInfo::NoModRef,
480 ModRefInfo::NoModRef, mr,
481 ModRefInfo::NoModRef,
482 ModRefInfo::NoModRef,
483 ModRefInfo::NoModRef);
486MemoryEffectsAttr MemoryEffectsAttr::errnoMemOnly(MLIRContext *ctx,
488 return get(ctx, ModRefInfo::NoModRef,
489 ModRefInfo::NoModRef,
490 ModRefInfo::NoModRef, mr,
491 ModRefInfo::NoModRef,
492 ModRefInfo::NoModRef);
495MemoryEffectsAttr MemoryEffectsAttr::otherMemOnly(MLIRContext *ctx,
497 return get(ctx, mr, ModRefInfo::NoModRef,
498 ModRefInfo::NoModRef,
499 ModRefInfo::NoModRef,
500 ModRefInfo::NoModRef,
501 ModRefInfo::NoModRef);
504MemoryEffectsAttr MemoryEffectsAttr::inaccessibleOrArgMemOnly(MLIRContext *ctx,
506 return get(ctx, ModRefInfo::NoModRef, mr,
507 mr, ModRefInfo::NoModRef,
508 ModRefInfo::NoModRef,
509 ModRefInfo::NoModRef);
512MemoryEffectsAttr MemoryEffectsAttr::inaccessibleOrErrnoMemOnly(
513 MLIRContext *ctx, ModRefInfo inaccessibleMr, ModRefInfo errnoMr) {
514 return get(ctx, ModRefInfo::NoModRef,
515 ModRefInfo::NoModRef,
516 inaccessibleMr, errnoMr,
517 ModRefInfo::NoModRef,
518 ModRefInfo::NoModRef);
521MemoryEffectsAttr MemoryEffectsAttr::inaccessibleOrArgOrErrnoMemOnly(
522 MLIRContext *ctx, ModRefInfo inaccessibleOrArgMr, ModRefInfo errnoMr) {
523 return get(ctx, ModRefInfo::NoModRef,
525 inaccessibleOrArgMr, errnoMr,
526 ModRefInfo::NoModRef,
527 ModRefInfo::NoModRef);
531MemoryEffectsAttr::argumentOrErrnoMemOnly(MLIRContext *ctx, ModRefInfo argMr,
532 ModRefInfo errnoMr) {
533 return get(ctx, ModRefInfo::NoModRef, argMr,
534 ModRefInfo::NoModRef, errnoMr,
535 ModRefInfo::NoModRef,
536 ModRefInfo::NoModRef);
539FPAttr FPAttr::getZero(Type type) {
542 mlir::cast<cir::FPTypeInterface>(type).getFloatSemantics()));
545LogicalResult FPAttr::verify(function_ref<InFlightDiagnostic()> emitError,
546 cir::FPTypeInterface fpType, APFloat value) {
547 if (APFloat::SemanticsToEnum(fpType.getFloatSemantics()) !=
548 APFloat::SemanticsToEnum(value.getSemantics()))
549 return emitError() <<
"floating-point semantics mismatch";
558std::string CmpThreeWayInfoAttr::getAlias()
const {
559 std::string alias =
"cmpinfo";
561 switch (getOrdering()) {
562 case CmpOrdering::Strong:
563 alias.append(
"_strong_");
565 case CmpOrdering::Weak:
566 alias.append(
"_weak_");
568 case CmpOrdering::Partial:
569 alias.append(
"_partial_");
573 auto appendInt = [&](
int64_t value) {
575 alias.push_back(
'n');
578 alias.append(std::to_string(value));
588 if (std::optional<int> unordered = getUnordered()) {
590 appendInt(unordered.value());
597CmpThreeWayInfoAttr::verify(function_ref<InFlightDiagnostic()> emitError,
599 int64_t gt, std::optional<int64_t> unordered) {
601 if ((ordering == CmpOrdering::Strong || ordering == CmpOrdering::Weak) &&
603 emitError() <<
"strong and weak ordering do not include unordered";
606 if (ordering == CmpOrdering::Partial && !unordered) {
607 emitError() <<
"partial ordering requires unordered value";
619ConstComplexAttr::verify(function_ref<InFlightDiagnostic()> emitError,
620 cir::ComplexType type, mlir::TypedAttr real,
621 mlir::TypedAttr imag) {
622 mlir::Type elemType = type.getElementType();
623 if (real.getType() != elemType)
625 <<
"type of the real part does not match the complex type";
627 if (imag.getType() != elemType)
629 <<
"type of the imaginary part does not match the complex type";
638void CUDAVarRegistrationInfoAttr::print(AsmPrinter &p)
const {
639 p <<
"<" << getDeviceSideName();
640 p <<
", " << stringifyEnum(
getKind());
647 if (
getKind() == CUDADeviceVarKind::Surface)
648 p <<
", surface_type = " << getSurfTexType();
649 else if (
getKind() == CUDADeviceVarKind::Texture) {
650 p <<
", texture_type = " << getSurfTexType();
657Attribute CUDAVarRegistrationInfoAttr::parse(AsmParser &parser, Type odsType) {
658 if (parser.parseLess())
661 std::string deviceSideName;
662 if (parser.parseKeywordOrString(&deviceSideName)) {
663 parser.emitError(parser.getCurrentLocation(),
664 "expected device variable name");
668 if (parser.parseComma())
673 if (parser.parseKeyword(&kindStr))
676 std::optional<CUDADeviceVarKind>
kind = symbolizeCUDADeviceVarKind(kindStr);
678 parser.emitError(parser.getCurrentLocation(),
679 "unknown device variable kind: ")
685 bool isExtern =
false;
686 bool isConstant =
false;
687 bool isManaged =
false;
689 bool normalized =
false;
691 while (parser.parseOptionalGreater().failed()) {
692 if (parser.parseComma())
696 if (parser.parseKeyword(&flag))
699 if (flag ==
"extern")
701 else if (flag ==
"constant")
703 else if (flag ==
"managed")
705 else if (flag ==
"normalized") {
706 if (*
kind != CUDADeviceVarKind::Texture) {
707 parser.emitError(parser.getCurrentLocation(),
708 "'normalized' is only valid for Texture");
712 }
else if (flag ==
"surface_type") {
713 if (*
kind != CUDADeviceVarKind::Surface) {
714 parser.emitError(parser.getCurrentLocation(),
715 "'surface_type' is only valid for Surface");
718 if (parser.parseEqual() || parser.parseInteger(surfTexType))
720 }
else if (flag ==
"texture_type") {
721 if (*
kind != CUDADeviceVarKind::Texture) {
722 parser.emitError(parser.getCurrentLocation(),
723 "'texture_type' is only valid for Texture");
726 if (parser.parseEqual() || parser.parseInteger(surfTexType))
729 parser.emitError(parser.getCurrentLocation(),
"unknown flag: ") << flag;
734 return get(parser.getContext(), deviceSideName, *
kind, isExtern, isConstant,
735 isManaged, surfTexType, normalized);
743DataMemberAttr::verify(function_ref<InFlightDiagnostic()> emitError,
744 cir::DataMemberType ty,
745 mlir::DenseI32ArrayAttr memberPath) {
749 if (memberPath.empty())
750 return emitError() <<
"#cir.data_member path must not be empty";
752 mlir::Type currentTy = ty.getClassTy();
753 for (
auto [
step, idx] : llvm::enumerate(memberPath.asArrayRef())) {
754 auto recTy = mlir::dyn_cast<cir::RecordType>(currentTy);
756 return emitError() <<
"#cir.data_member path step " <<
step
757 <<
" reaches a non-record type";
759 if (recTy.isIncomplete())
762 if (idx < 0 ||
static_cast<unsigned>(idx) >= recTy.getNumElements())
763 return emitError() <<
"#cir.data_member path index " << idx <<
" at step "
764 <<
step <<
" is out of range";
766 currentTy = recTy.getMembers()[idx];
769 if (currentTy != ty.getMemberTy())
771 <<
"member type of a #cir.data_member attribute must match "
772 "the attribute type";
781LogicalResult MethodAttr::verify(function_ref<InFlightDiagnostic()> emitError,
782 cir::MethodType type,
783 std::optional<FlatSymbolRefAttr> symbol,
784 std::optional<uint64_t> vtable_index) {
785 if (symbol.has_value() && vtable_index.has_value())
787 <<
"at most one of symbol and vtable_index can be present "
793Attribute MethodAttr::parse(AsmParser &parser, Type odsType) {
794 auto ty = mlir::cast<cir::MethodType>(odsType);
796 if (parser.parseLess().failed())
800 if (parser.parseOptionalKeyword(
"null").succeeded()) {
801 if (parser.parseGreater().failed())
808 FlatSymbolRefAttr symbol;
809 mlir::OptionalParseResult parseSymbolRefResult =
810 parser.parseOptionalAttribute(symbol);
811 if (parseSymbolRefResult.has_value()) {
812 if (parseSymbolRefResult.value().failed())
814 if (parser.parseGreater().failed())
816 return get(ty, symbol);
820 std::uint64_t vtableIndex = 0;
821 if (parser.parseKeyword(
"vtable_index"))
823 if (parser.parseEqual())
825 if (parser.parseInteger(vtableIndex))
828 if (parser.parseGreater())
831 return get(ty, vtableIndex);
834void MethodAttr::print(AsmPrinter &printer)
const {
835 auto symbol = getSymbol();
836 auto vtableIndex = getVtableIndex();
839 if (symbol.has_value()) {
841 }
else if (vtableIndex.has_value()) {
842 printer <<
"vtable_index = " << *vtableIndex;
854ConstArrayAttr::verify(function_ref<InFlightDiagnostic()> emitError, Type type,
855 Attribute elts,
int trailingZerosNum) {
857 if (!(mlir::isa<ArrayAttr, StringAttr>(elts)))
858 return emitError() <<
"constant array expects ArrayAttr or StringAttr";
860 if (
auto strAttr = mlir::dyn_cast<StringAttr>(elts)) {
861 const auto arrayTy = mlir::cast<ArrayType>(type);
862 const auto intTy = mlir::dyn_cast<IntType>(arrayTy.getElementType());
865 if (!intTy || intTy.getWidth() != 8)
867 <<
"constant array element for string literals expects "
868 "!cir.int<u, 8> element type";
872 assert(mlir::isa<ArrayAttr>(elts));
873 const auto arrayAttr = mlir::cast<mlir::ArrayAttr>(elts);
874 const auto arrayTy = mlir::cast<ArrayType>(type);
877 if (arrayAttr.size() > arrayTy.getSize())
878 return emitError() <<
"constant array has " << arrayAttr.size()
879 <<
" values but array type has size "
880 << arrayTy.getSize();
881 if (arrayTy.getSize() != arrayAttr.size() + trailingZerosNum)
882 return emitError() <<
"constant array size should match type size";
886Attribute ConstArrayAttr::parse(AsmParser &parser, Type type) {
887 mlir::FailureOr<Type> resultTy;
888 mlir::FailureOr<Attribute> resultVal;
891 if (parser.parseLess())
895 resultVal = FieldParser<Attribute>::parse(parser);
896 if (failed(resultVal)) {
898 parser.getCurrentLocation(),
899 "failed to parse ConstArrayAttr parameter 'value' which is "
900 "to be a `Attribute`");
905 if (mlir::isa<ArrayAttr>(*resultVal)) {
907 if (parser.parseOptionalColon().failed()) {
910 resultTy = FieldParser<Type>::parse(parser);
911 if (failed(resultTy)) {
913 parser.getCurrentLocation(),
914 "failed to parse ConstArrayAttr parameter 'type' which is "
915 "to be a `::mlir::Type`");
920 auto ta = mlir::cast<TypedAttr>(*resultVal);
921 resultTy = ta.getType();
922 if (mlir::isa<mlir::NoneType>(*resultTy)) {
923 parser.emitError(parser.getCurrentLocation(),
924 "expected type declaration for string literal");
930 if (parser.parseOptionalComma().succeeded()) {
931 if (parser.parseOptionalKeyword(
"trailing_zeros").succeeded()) {
932 unsigned totalSize = mlir::cast<cir::ArrayType>(type).getSize();
933 mlir::Attribute elts = resultVal.value();
934 if (
auto str = mlir::dyn_cast<mlir::StringAttr>(elts))
935 zeros = totalSize - str.size();
937 zeros = totalSize - mlir::cast<mlir::ArrayAttr>(elts).size();
944 if (parser.parseGreater())
947 return parser.getChecked<ConstArrayAttr>(parser.getCurrentLocation(),
948 parser.getContext(), type,
949 resultVal.value(), zeros);
952void ConstArrayAttr::print(AsmPrinter &printer)
const {
954 printer.printStrippedAttrOrType(getElts());
955 if (getTrailingZerosNum())
956 printer <<
", trailing_zeros";
965cir::ConstVectorAttr::verify(function_ref<InFlightDiagnostic()> emitError,
966 Type type, ArrayAttr elts) {
968 if (!mlir::isa<cir::VectorType>(type))
969 return emitError() <<
"type of cir::ConstVectorAttr is not a "
973 const auto vecType = mlir::cast<cir::VectorType>(type);
975 if (vecType.isScalable())
976 return emitError() <<
"scalable vector constants are not supported";
978 if (vecType.getFixedSize() != elts.size())
980 <<
"number of constant elements should match vector size";
983 LogicalResult elementTypeCheck =
success();
984 elts.walkImmediateSubElements(
985 [&](Attribute element) {
986 if (elementTypeCheck.failed()) {
990 auto typedElement = mlir::dyn_cast<TypedAttr>(element);
992 typedElement.getType() != vecType.getElementType()) {
993 elementTypeCheck = failure();
994 emitError() <<
"constant type should match vector element type";
999 return elementTypeCheck;
1006LogicalResult cir::VTableAttr::verify(
1007 llvm::function_ref<mlir::InFlightDiagnostic()> emitError, mlir::Type type,
1008 mlir::ArrayAttr data, mlir::ArrayAttr addressPoints) {
1009 auto sTy = mlir::dyn_cast_if_present<cir::RecordType>(type);
1011 return emitError() <<
"expected !cir.struct or !cir.union type result";
1012 if (sTy.getMembers().empty() || data.empty())
1013 return emitError() <<
"expected record type with one or more subtype";
1015 if (cir::ConstRecordAttr::verify(emitError, type, data).failed())
1018 if (addressPoints.size() != data.size())
1019 return emitError() <<
"expected one address point for each vtable, got "
1020 << addressPoints.size() <<
" for " << data.size()
1023 for (
const auto &[idx, element] :
1024 llvm::enumerate(data.getAsRange<mlir::Attribute>())) {
1025 const auto &constArrayAttr = mlir::dyn_cast<cir::ConstArrayAttr>(element);
1026 if (!constArrayAttr)
1027 return emitError() <<
"expected constant array subtype";
1029 auto arrayTy = mlir::cast<cir::ArrayType>(constArrayAttr.getType());
1031 mlir::dyn_cast<cir::AddressPointAttr>(addressPoints[idx]);
1033 return emitError() <<
"expected an address point for vtable " << idx;
1034 if (addressPoint.getIndex() !=
static_cast<int32_t>(idx))
1035 return emitError() <<
"address point " << idx <<
" refers to vtable "
1036 << addressPoint.getIndex();
1037 if (addressPoint.getOffset() < 0 ||
1038 static_cast<uint64_t>(addressPoint.getOffset()) > arrayTy.getSize())
1039 return emitError() <<
"address point offset " << addressPoint.getOffset()
1040 <<
" is out of range of vtable " << idx;
1042 LogicalResult eltTypeCheck =
success();
1043 auto arrayElts = mlir::cast<ArrayAttr>(constArrayAttr.getElts());
1044 arrayElts.walkImmediateSubElements(
1045 [&](mlir::Attribute attr) {
1046 if (mlir::isa<ConstPtrAttr, GlobalViewAttr>(attr))
1049 eltTypeCheck = emitError()
1050 <<
"expected GlobalViewAttr or ConstPtrAttr";
1052 [&](mlir::Type type) {});
1053 if (eltTypeCheck.failed())
1054 return eltTypeCheck;
1063std::string DynamicCastInfoAttr::getAlias()
const {
1066 std::string alias =
"dyn_cast_info_";
1068 alias.append(getSrcRtti().getSymbol().getValue());
1069 alias.push_back(
'_');
1070 alias.append(getDestRtti().getSymbol().getValue());
1078 auto ptrTy = mlir::dyn_cast<cir::PointerType>(ty);
1082 auto pointeeIntTy = mlir::dyn_cast<cir::IntType>(ptrTy.getPointee());
1086 return pointeeIntTy.isUnsigned() && pointeeIntTy.getWidth() == 8;
1089LogicalResult DynamicCastInfoAttr::verify(
1090 function_ref<InFlightDiagnostic()> emitError, cir::GlobalViewAttr srcRtti,
1091 cir::GlobalViewAttr destRtti, mlir::FlatSymbolRefAttr runtimeFunc,
1092 mlir::FlatSymbolRefAttr badCastFunc, cir::IntAttr offsetHint) {
1094 return emitError() <<
"srcRtti must be an RTTI pointer";
1097 return emitError() <<
"destRtti must be an RTTI pointer";
1106LogicalResult EhFilterAttr::verify(function_ref<InFlightDiagnostic()> emitError,
1107 mlir::ArrayAttr permittedTypes) {
1108 for (mlir::Attribute typeAttr : permittedTypes) {
1109 auto rtti = mlir::dyn_cast<cir::GlobalViewAttr>(typeAttr);
1110 if (!rtti || !
isRttiPtr(rtti.getType()))
1111 return emitError() <<
"permitted type list must contain only type info "
1122Attribute RecordLayoutAttr::parse(AsmParser &parser, Type odsType) {
1124 SMLoc loc = parser.getCurrentLocation();
1125 if (parser.parseLess() || parser.parseKeyword(
"arg_passing_kind") ||
1126 parser.parseEqual())
1129 FailureOr<ArgPassingKind>
kind = FieldParser<ArgPassingKind>::parse(parser);
1133 if (parser.parseComma() || parser.parseKeyword(
"has_trivial_dtor") ||
1134 parser.parseEqual())
1136 FailureOr<bool> hasTrivialDtor = FieldParser<bool>::parse(parser);
1137 if (failed(hasTrivialDtor))
1140 if (parser.parseComma() || parser.parseKeyword(
"record_align") ||
1141 parser.parseEqual())
1143 FailureOr<uint64_t> recordAlign = FieldParser<uint64_t>::parse(parser);
1144 if (failed(recordAlign))
1149 uint64_t unadjustedAlign = *recordAlign;
1150 bool transparentUnion =
false;
1151 bool flexibleArrayMember =
false;
1152 while (succeeded(parser.parseOptionalComma())) {
1153 if (succeeded(parser.parseOptionalKeyword(
"unadjusted_align"))) {
1154 if (parser.parseEqual())
1156 FailureOr<uint64_t> parsed = FieldParser<uint64_t>::parse(parser);
1159 unadjustedAlign = *parsed;
1162 if (succeeded(parser.parseOptionalKeyword(
"transparent_union"))) {
1163 if (parser.parseEqual())
1165 FailureOr<bool> parsed = FieldParser<bool>::parse(parser);
1168 transparentUnion = *parsed;
1171 if (succeeded(parser.parseOptionalKeyword(
"flexible_array_member"))) {
1172 if (parser.parseEqual())
1174 FailureOr<bool> parsed = FieldParser<bool>::parse(parser);
1177 flexibleArrayMember = *parsed;
1180 parser.emitError(parser.getCurrentLocation(),
1181 "expected unadjusted_align, transparent_union or "
1182 "flexible_array_member");
1186 if (parser.parseGreater())
1188 return parser.getChecked<RecordLayoutAttr>(
1189 loc, parser.getContext(), *
kind, *hasTrivialDtor, *recordAlign,
1190 unadjustedAlign, transparentUnion, flexibleArrayMember);
1193void RecordLayoutAttr::print(AsmPrinter &printer)
const {
1194 printer <<
"<arg_passing_kind = ";
1195 printer.printStrippedAttrOrType(getArgPassingKind());
1196 printer <<
", has_trivial_dtor = ";
1197 printer.printStrippedAttrOrType(getHasTrivialDtor());
1198 printer <<
", record_align = ";
1199 printer.printStrippedAttrOrType(getRecordAlign());
1201 if (getUnadjustedAlign() != getRecordAlign()) {
1202 printer <<
", unadjusted_align = ";
1203 printer.printStrippedAttrOrType(getUnadjustedAlign());
1205 if (getTransparentUnion()) {
1206 printer <<
", transparent_union = ";
1207 printer.printStrippedAttrOrType(getTransparentUnion());
1209 if (getFlexibleArrayMember()) {
1210 printer <<
", flexible_array_member = ";
1211 printer.printStrippedAttrOrType(getFlexibleArrayMember());
1221 mlir::StringAttr name) {
1224 auto dict = mod->getAttrOfType<mlir::DictionaryAttr>(
1225 CIRDialect::getRecordLayoutsAttrName());
1228 return dict.getAs<RecordLayoutAttr>(name);
1232 mlir::StringAttr name) {
1234 assert(attr &&
"record layout entry missing for named record");
1242void CIRDialect::registerAttributes() {
1244#define GET_ATTRDEF_LIST
1245#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_elementwise_add_sat __builtin_elementwise_sub_sat __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 __builtin_riscv_nclipu_u32 int64_t
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat int32_t