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 LoopHintAttr::verify(
341 function_ref<InFlightDiagnostic()> emitError, mlir::UnitAttr mustProgress,
342 mlir::BoolAttr vectorizeEnable,
uint32_t vectorizeWidth,
343 mlir::BoolAttr vectorizeScalable, mlir::BoolAttr vectorizePredicate,
344 uint32_t interleaveCount, std::optional<cir::LoopUnrollMode> unroll,
345 uint32_t unrollCount, std::optional<cir::LoopUnrollMode> unrollAndJam,
346 uint32_t unrollAndJamCount, mlir::BoolAttr distribute,
347 mlir::UnitAttr pipelineDisabled,
uint32_t pipelineInitiationInterval,
348 mlir::UnitAttr licmDisabled, mlir::UnitAttr parallel,
uint32_t codeAlign,
349 mlir::UnitAttr vectorized, mlir::Attribute unrollAndJamInner) {
350 if (!unrollAndJamInner)
353 auto inner = mlir::dyn_cast<LoopHintAttr>(unrollAndJamInner);
355 return emitError() <<
"'unroll_and_jam_inner' must be a '#cir.loop_hint'";
357 if (!unrollAndJam && !unrollAndJamCount)
358 return emitError() <<
"'unroll_and_jam_inner' requires the loop to be "
359 "unrolled and jammed";
361 if (inner.getUnrollAndJamInner())
362 return emitError() <<
"'unroll_and_jam_inner' can't have its own "
363 "'unroll_and_jam_inner'";
371LogicalResult OptInfoAttr::verify(function_ref<InFlightDiagnostic()> emitError,
372 unsigned level,
unsigned size) {
375 <<
"optimization level must be between 0 and 3 inclusive";
378 <<
"size optimization level must be between 0 and 2 inclusive";
388static ParseResult
parseConstPtr(AsmParser &parser, mlir::IntegerAttr &value) {
390 if (parser.parseOptionalKeyword(
"null").succeeded()) {
391 value = parser.getBuilder().getI64IntegerAttr(0);
395 return parser.parseAttribute(value);
406 mlir::DenseI32ArrayAttr &memberPath) {
407 if (parser.parseOptionalKeyword(
"null").succeeded())
410 auto parsed = mlir::FieldParser<mlir::DenseI32ArrayAttr>::parse(parser);
411 if (mlir::failed(parsed))
413 memberPath = *parsed;
418 mlir::DenseI32ArrayAttr memberPath) {
422 p.printStrippedAttrOrType(memberPath);
430 cir::IntTypeInterface ty) {
431 llvm::SMLoc loc = parser.getCurrentLocation();
433 mlir::OptionalParseResult result = parser.parseOptionalInteger(parsed);
434 if (!result.has_value() || failed(*result))
435 return parser.emitError(loc,
"expected integer value");
437 const unsigned width = ty.getWidth();
439 ty.isSigned() ? parsed.getSignificantBits() <= width
440 : !parsed.isNegative() && parsed.getActiveBits() <= width;
442 return parser.emitError(loc,
"integer value too large for the given type");
444 value = ty.isSigned() ? parsed.sextOrTrunc(width) : parsed.zextOrTrunc(width);
449 cir::IntTypeInterface ty) {
451 value.toString(str, 10, ty.isSigned());
455LogicalResult IntAttr::verify(function_ref<InFlightDiagnostic()> emitError,
456 cir::IntTypeInterface type, llvm::APInt value) {
457 if (value.getBitWidth() !=
type.getWidth())
458 return emitError() <<
"type and value bitwidth mismatch: "
459 <<
type.getWidth() <<
" != " << value.getBitWidth();
472 FailureOr<APFloat> &value,
473 cir::FPTypeInterface fpType) {
475 APFloat parsedValue(0.0);
476 if (parser.parseFloat(fpType.getFloatSemantics(), parsedValue))
479 value.emplace(parsedValue);
487MemoryEffectsAttr MemoryEffectsAttr::none(MLIRContext *ctx) {
488 return get(ctx, ModRefInfo::NoModRef);
491MemoryEffectsAttr MemoryEffectsAttr::readOnly(MLIRContext *ctx) {
492 return get(ctx, ModRefInfo::Ref);
495MemoryEffectsAttr MemoryEffectsAttr::writeOnly(MLIRContext *ctx) {
496 return get(ctx, ModRefInfo::Mod);
499MemoryEffectsAttr MemoryEffectsAttr::argMemOnly(MLIRContext *ctx,
501 return get(ctx, ModRefInfo::NoModRef, mr,
502 ModRefInfo::NoModRef,
503 ModRefInfo::NoModRef,
504 ModRefInfo::NoModRef,
505 ModRefInfo::NoModRef);
508MemoryEffectsAttr MemoryEffectsAttr::inaccessibleMemOnly(MLIRContext *ctx,
510 return get(ctx, ModRefInfo::NoModRef,
511 ModRefInfo::NoModRef, mr,
512 ModRefInfo::NoModRef,
513 ModRefInfo::NoModRef,
514 ModRefInfo::NoModRef);
517MemoryEffectsAttr MemoryEffectsAttr::errnoMemOnly(MLIRContext *ctx,
519 return get(ctx, ModRefInfo::NoModRef,
520 ModRefInfo::NoModRef,
521 ModRefInfo::NoModRef, mr,
522 ModRefInfo::NoModRef,
523 ModRefInfo::NoModRef);
526MemoryEffectsAttr MemoryEffectsAttr::otherMemOnly(MLIRContext *ctx,
528 return get(ctx, mr, ModRefInfo::NoModRef,
529 ModRefInfo::NoModRef,
530 ModRefInfo::NoModRef,
531 ModRefInfo::NoModRef,
532 ModRefInfo::NoModRef);
535MemoryEffectsAttr MemoryEffectsAttr::inaccessibleOrArgMemOnly(MLIRContext *ctx,
537 return get(ctx, ModRefInfo::NoModRef, mr,
538 mr, ModRefInfo::NoModRef,
539 ModRefInfo::NoModRef,
540 ModRefInfo::NoModRef);
543MemoryEffectsAttr MemoryEffectsAttr::inaccessibleOrErrnoMemOnly(
544 MLIRContext *ctx, ModRefInfo inaccessibleMr, ModRefInfo errnoMr) {
545 return get(ctx, ModRefInfo::NoModRef,
546 ModRefInfo::NoModRef,
547 inaccessibleMr, errnoMr,
548 ModRefInfo::NoModRef,
549 ModRefInfo::NoModRef);
552MemoryEffectsAttr MemoryEffectsAttr::inaccessibleOrArgOrErrnoMemOnly(
553 MLIRContext *ctx, ModRefInfo inaccessibleOrArgMr, ModRefInfo errnoMr) {
554 return get(ctx, ModRefInfo::NoModRef,
556 inaccessibleOrArgMr, errnoMr,
557 ModRefInfo::NoModRef,
558 ModRefInfo::NoModRef);
562MemoryEffectsAttr::argumentOrErrnoMemOnly(MLIRContext *ctx, ModRefInfo argMr,
563 ModRefInfo errnoMr) {
564 return get(ctx, ModRefInfo::NoModRef, argMr,
565 ModRefInfo::NoModRef, errnoMr,
566 ModRefInfo::NoModRef,
567 ModRefInfo::NoModRef);
570FPAttr FPAttr::getZero(Type type) {
573 mlir::cast<cir::FPTypeInterface>(type).getFloatSemantics()));
576LogicalResult FPAttr::verify(function_ref<InFlightDiagnostic()> emitError,
577 cir::FPTypeInterface fpType, APFloat value) {
578 if (APFloat::SemanticsToEnum(fpType.getFloatSemantics()) !=
579 APFloat::SemanticsToEnum(value.getSemantics()))
580 return emitError() <<
"floating-point semantics mismatch";
589std::string CmpThreeWayInfoAttr::getAlias()
const {
590 std::string alias =
"cmpinfo";
592 switch (getOrdering()) {
593 case CmpOrdering::Strong:
594 alias.append(
"_strong_");
596 case CmpOrdering::Weak:
597 alias.append(
"_weak_");
599 case CmpOrdering::Partial:
600 alias.append(
"_partial_");
604 auto appendInt = [&](
int64_t value) {
606 alias.push_back(
'n');
609 alias.append(std::to_string(value));
619 if (std::optional<int> unordered = getUnordered()) {
621 appendInt(unordered.value());
628CmpThreeWayInfoAttr::verify(function_ref<InFlightDiagnostic()> emitError,
630 int64_t gt, std::optional<int64_t> unordered) {
632 if ((ordering == CmpOrdering::Strong || ordering == CmpOrdering::Weak) &&
634 emitError() <<
"strong and weak ordering do not include unordered";
637 if (ordering == CmpOrdering::Partial && !unordered) {
638 emitError() <<
"partial ordering requires unordered value";
650ConstComplexAttr::verify(function_ref<InFlightDiagnostic()> emitError,
651 cir::ComplexType type, mlir::TypedAttr real,
652 mlir::TypedAttr imag) {
653 mlir::Type elemType =
type.getElementType();
654 if (real.getType() != elemType)
656 <<
"type of the real part does not match the complex type";
658 if (imag.getType() != elemType)
660 <<
"type of the imaginary part does not match the complex type";
669void CUDAVarRegistrationInfoAttr::print(AsmPrinter &p)
const {
670 p <<
"<" << getDeviceSideName();
671 p <<
", " << stringifyEnum(
getKind());
678 if (
getKind() == CUDADeviceVarKind::Surface)
679 p <<
", surface_type = " << getSurfTexType();
680 else if (
getKind() == CUDADeviceVarKind::Texture) {
681 p <<
", texture_type = " << getSurfTexType();
688Attribute CUDAVarRegistrationInfoAttr::parse(AsmParser &parser, Type odsType) {
689 if (parser.parseLess())
692 std::string deviceSideName;
693 if (parser.parseKeywordOrString(&deviceSideName)) {
694 parser.emitError(parser.getCurrentLocation(),
695 "expected device variable name");
699 if (parser.parseComma())
704 if (parser.parseKeyword(&kindStr))
707 std::optional<CUDADeviceVarKind>
kind = symbolizeCUDADeviceVarKind(kindStr);
709 parser.emitError(parser.getCurrentLocation(),
710 "unknown device variable kind: ")
716 bool isExtern =
false;
717 bool isConstant =
false;
718 bool isManaged =
false;
720 bool normalized =
false;
722 while (parser.parseOptionalGreater().failed()) {
723 if (parser.parseComma())
727 if (parser.parseKeyword(&flag))
730 if (flag ==
"extern")
732 else if (flag ==
"constant")
734 else if (flag ==
"managed")
736 else if (flag ==
"normalized") {
737 if (*
kind != CUDADeviceVarKind::Texture) {
738 parser.emitError(parser.getCurrentLocation(),
739 "'normalized' is only valid for Texture");
743 }
else if (flag ==
"surface_type") {
744 if (*
kind != CUDADeviceVarKind::Surface) {
745 parser.emitError(parser.getCurrentLocation(),
746 "'surface_type' is only valid for Surface");
749 if (parser.parseEqual() || parser.parseInteger(surfTexType))
751 }
else if (flag ==
"texture_type") {
752 if (*
kind != CUDADeviceVarKind::Texture) {
753 parser.emitError(parser.getCurrentLocation(),
754 "'texture_type' is only valid for Texture");
757 if (parser.parseEqual() || parser.parseInteger(surfTexType))
760 parser.emitError(parser.getCurrentLocation(),
"unknown flag: ") << flag;
765 return get(parser.getContext(), deviceSideName, *
kind, isExtern, isConstant,
766 isManaged, surfTexType, normalized);
774DataMemberAttr::verify(function_ref<InFlightDiagnostic()> emitError,
775 cir::DataMemberType ty,
776 mlir::DenseI32ArrayAttr memberPath) {
780 if (memberPath.empty())
781 return emitError() <<
"#cir.data_member path must not be empty";
783 mlir::Type currentTy = ty.getClassTy();
784 for (
auto [
step, idx] : llvm::enumerate(memberPath.asArrayRef())) {
785 auto recTy = mlir::dyn_cast<cir::RecordType>(currentTy);
787 return emitError() <<
"#cir.data_member path step " <<
step
788 <<
" reaches a non-record type";
790 if (recTy.isIncomplete())
793 if (idx < 0 ||
static_cast<unsigned>(idx) >= recTy.getNumElements())
794 return emitError() <<
"#cir.data_member path index " << idx <<
" at step "
795 <<
step <<
" is out of range";
797 currentTy = recTy.getMembers()[idx];
800 if (currentTy != ty.getMemberTy())
802 <<
"member type of a #cir.data_member attribute must match "
803 "the attribute type";
812LogicalResult MethodAttr::verify(function_ref<InFlightDiagnostic()> emitError,
813 cir::MethodType type,
814 std::optional<FlatSymbolRefAttr> symbol,
815 std::optional<uint64_t> vtable_index) {
816 if (symbol.has_value() && vtable_index.has_value())
818 <<
"at most one of symbol and vtable_index can be present "
824Attribute MethodAttr::parse(AsmParser &parser, Type odsType) {
825 auto ty = mlir::cast<cir::MethodType>(odsType);
827 if (parser.parseLess().failed())
831 if (parser.parseOptionalKeyword(
"null").succeeded()) {
832 if (parser.parseGreater().failed())
839 FlatSymbolRefAttr symbol;
840 mlir::OptionalParseResult parseSymbolRefResult =
841 parser.parseOptionalAttribute(symbol);
842 if (parseSymbolRefResult.has_value()) {
843 if (parseSymbolRefResult.value().failed())
845 if (parser.parseGreater().failed())
847 return get(ty, symbol);
851 std::uint64_t vtableIndex = 0;
852 if (parser.parseKeyword(
"vtable_index"))
854 if (parser.parseEqual())
856 if (parser.parseInteger(vtableIndex))
859 if (parser.parseGreater())
862 return get(ty, vtableIndex);
865void MethodAttr::print(AsmPrinter &printer)
const {
866 auto symbol = getSymbol();
867 auto vtableIndex = getVtableIndex();
870 if (symbol.has_value()) {
872 }
else if (vtableIndex.has_value()) {
873 printer <<
"vtable_index = " << *vtableIndex;
885ConstArrayAttr::verify(function_ref<InFlightDiagnostic()> emitError, Type type,
886 Attribute elts,
int trailingZerosNum) {
888 if (!(mlir::isa<ArrayAttr, StringAttr>(elts)))
889 return emitError() <<
"constant array expects ArrayAttr or StringAttr";
891 if (
auto strAttr = mlir::dyn_cast<StringAttr>(elts)) {
892 const auto arrayTy = mlir::cast<ArrayType>(type);
893 const auto intTy = mlir::dyn_cast<IntType>(arrayTy.getElementType());
896 if (!intTy || intTy.getWidth() != 8)
898 <<
"constant array element for string literals expects "
899 "!cir.int<u, 8> element type";
903 assert(mlir::isa<ArrayAttr>(elts));
904 const auto arrayAttr = mlir::cast<mlir::ArrayAttr>(elts);
905 const auto arrayTy = mlir::cast<ArrayType>(type);
908 if (arrayAttr.size() > arrayTy.getSize())
909 return emitError() <<
"constant array has " << arrayAttr.size()
910 <<
" values but array type has size "
911 << arrayTy.getSize();
912 if (arrayTy.getSize() != arrayAttr.size() + trailingZerosNum)
913 return emitError() <<
"constant array size should match type size";
917Attribute ConstArrayAttr::parse(AsmParser &parser, Type type) {
918 mlir::FailureOr<Type> resultTy;
919 mlir::FailureOr<Attribute> resultVal;
922 if (parser.parseLess())
926 resultVal = FieldParser<Attribute>::parse(parser);
927 if (failed(resultVal)) {
929 parser.getCurrentLocation(),
930 "failed to parse ConstArrayAttr parameter 'value' which is "
931 "to be a `Attribute`");
936 if (mlir::isa<ArrayAttr>(*resultVal)) {
938 if (parser.parseOptionalColon().failed()) {
941 resultTy = FieldParser<Type>::parse(parser);
942 if (failed(resultTy)) {
944 parser.getCurrentLocation(),
945 "failed to parse ConstArrayAttr parameter 'type' which is "
946 "to be a `::mlir::Type`");
951 auto ta = mlir::cast<TypedAttr>(*resultVal);
952 resultTy = ta.getType();
953 if (mlir::isa<mlir::NoneType>(*resultTy)) {
954 parser.emitError(parser.getCurrentLocation(),
955 "expected type declaration for string literal");
961 if (parser.parseOptionalComma().succeeded()) {
962 if (parser.parseOptionalKeyword(
"trailing_zeros").succeeded()) {
963 unsigned totalSize = mlir::cast<cir::ArrayType>(type).getSize();
964 mlir::Attribute elts = resultVal.value();
965 if (
auto str = mlir::dyn_cast<mlir::StringAttr>(elts))
966 zeros = totalSize - str.size();
968 zeros = totalSize - mlir::cast<mlir::ArrayAttr>(elts).size();
975 if (parser.parseGreater())
978 return parser.getChecked<ConstArrayAttr>(parser.getCurrentLocation(),
979 parser.getContext(), type,
980 resultVal.value(), zeros);
983void ConstArrayAttr::print(AsmPrinter &printer)
const {
985 printer.printStrippedAttrOrType(getElts());
986 if (getTrailingZerosNum())
987 printer <<
", trailing_zeros";
996cir::ConstVectorAttr::verify(function_ref<InFlightDiagnostic()> emitError,
997 Type type, ArrayAttr elts) {
999 if (!mlir::isa<cir::VectorType>(type))
1000 return emitError() <<
"type of cir::ConstVectorAttr is not a "
1004 const auto vecType = mlir::cast<cir::VectorType>(type);
1006 if (vecType.isScalable())
1007 return emitError() <<
"scalable vector constants are not supported";
1009 if (vecType.getFixedSize() != elts.size())
1011 <<
"number of constant elements should match vector size";
1014 LogicalResult elementTypeCheck =
success();
1015 elts.walkImmediateSubElements(
1016 [&](Attribute element) {
1017 if (elementTypeCheck.failed()) {
1021 auto typedElement = mlir::dyn_cast<TypedAttr>(element);
1022 if (!typedElement ||
1023 typedElement.getType() != vecType.getElementType()) {
1024 elementTypeCheck = failure();
1025 emitError() <<
"constant type should match vector element type";
1030 return elementTypeCheck;
1037LogicalResult cir::VTableAttr::verify(
1038 llvm::function_ref<mlir::InFlightDiagnostic()> emitError, mlir::Type type,
1039 mlir::ArrayAttr data, mlir::ArrayAttr addressPoints) {
1040 auto sTy = mlir::dyn_cast_if_present<cir::RecordType>(type);
1042 return emitError() <<
"expected !cir.struct or !cir.union type result";
1043 if (sTy.getMembers().empty() || data.empty())
1044 return emitError() <<
"expected record type with one or more subtype";
1046 if (cir::ConstRecordAttr::verify(emitError, type, data).failed())
1049 if (addressPoints.size() != data.size())
1050 return emitError() <<
"expected one address point for each vtable, got "
1051 << addressPoints.size() <<
" for " << data.size()
1054 for (
const auto &[idx, element] :
1055 llvm::enumerate(data.getAsRange<mlir::Attribute>())) {
1056 const auto &constArrayAttr = mlir::dyn_cast<cir::ConstArrayAttr>(element);
1057 if (!constArrayAttr)
1058 return emitError() <<
"expected constant array subtype";
1060 auto arrayTy = mlir::cast<cir::ArrayType>(constArrayAttr.getType());
1062 mlir::dyn_cast<cir::AddressPointAttr>(addressPoints[idx]);
1064 return emitError() <<
"expected an address point for vtable " << idx;
1065 if (addressPoint.getIndex() !=
static_cast<int32_t>(idx))
1066 return emitError() <<
"address point " << idx <<
" refers to vtable "
1067 << addressPoint.getIndex();
1068 if (addressPoint.getOffset() < 0 ||
1069 static_cast<uint64_t>(addressPoint.getOffset()) > arrayTy.getSize())
1070 return emitError() <<
"address point offset " << addressPoint.getOffset()
1071 <<
" is out of range of vtable " << idx;
1073 LogicalResult eltTypeCheck =
success();
1074 auto arrayElts = mlir::cast<ArrayAttr>(constArrayAttr.getElts());
1075 arrayElts.walkImmediateSubElements(
1076 [&](mlir::Attribute attr) {
1077 if (mlir::isa<ConstPtrAttr, GlobalViewAttr>(attr))
1080 eltTypeCheck = emitError()
1081 <<
"expected GlobalViewAttr or ConstPtrAttr";
1083 [&](mlir::Type
type) {});
1084 if (eltTypeCheck.failed())
1085 return eltTypeCheck;
1094std::string DynamicCastInfoAttr::getAlias()
const {
1097 std::string alias =
"dyn_cast_info_";
1099 alias.append(getSrcRtti().getSymbol().getValue());
1100 alias.push_back(
'_');
1101 alias.append(getDestRtti().getSymbol().getValue());
1109 auto ptrTy = mlir::dyn_cast<cir::PointerType>(ty);
1113 auto pointeeIntTy = mlir::dyn_cast<cir::IntType>(ptrTy.getPointee());
1117 return pointeeIntTy.isUnsigned() && pointeeIntTy.getWidth() == 8;
1120LogicalResult DynamicCastInfoAttr::verify(
1121 function_ref<InFlightDiagnostic()> emitError, cir::GlobalViewAttr srcRtti,
1122 cir::GlobalViewAttr destRtti, mlir::FlatSymbolRefAttr runtimeFunc,
1123 mlir::FlatSymbolRefAttr badCastFunc, cir::IntAttr offsetHint) {
1125 return emitError() <<
"srcRtti must be an RTTI pointer";
1128 return emitError() <<
"destRtti must be an RTTI pointer";
1137LogicalResult EhFilterAttr::verify(function_ref<InFlightDiagnostic()> emitError,
1138 mlir::ArrayAttr permittedTypes) {
1139 for (mlir::Attribute typeAttr : permittedTypes) {
1140 auto rtti = mlir::dyn_cast<cir::GlobalViewAttr>(typeAttr);
1141 if (!rtti || !
isRttiPtr(rtti.getType()))
1142 return emitError() <<
"permitted type list must contain only type info "
1153Attribute RecordLayoutAttr::parse(AsmParser &parser, Type odsType) {
1155 SMLoc loc = parser.getCurrentLocation();
1156 if (parser.parseLess() || parser.parseKeyword(
"arg_passing_kind") ||
1157 parser.parseEqual())
1160 FailureOr<ArgPassingKind>
kind = FieldParser<ArgPassingKind>::parse(parser);
1164 if (parser.parseComma() || parser.parseKeyword(
"has_trivial_dtor") ||
1165 parser.parseEqual())
1167 FailureOr<bool> hasTrivialDtor = FieldParser<bool>::parse(parser);
1168 if (failed(hasTrivialDtor))
1171 if (parser.parseComma() || parser.parseKeyword(
"record_align") ||
1172 parser.parseEqual())
1174 FailureOr<uint64_t> recordAlign = FieldParser<uint64_t>::parse(parser);
1175 if (failed(recordAlign))
1180 uint64_t unadjustedAlign = *recordAlign;
1181 bool transparentUnion =
false;
1182 bool flexibleArrayMember =
false;
1183 while (succeeded(parser.parseOptionalComma())) {
1184 if (succeeded(parser.parseOptionalKeyword(
"unadjusted_align"))) {
1185 if (parser.parseEqual())
1187 FailureOr<uint64_t> parsed = FieldParser<uint64_t>::parse(parser);
1190 unadjustedAlign = *parsed;
1193 if (succeeded(parser.parseOptionalKeyword(
"transparent_union"))) {
1194 if (parser.parseEqual())
1196 FailureOr<bool> parsed = FieldParser<bool>::parse(parser);
1199 transparentUnion = *parsed;
1202 if (succeeded(parser.parseOptionalKeyword(
"flexible_array_member"))) {
1203 if (parser.parseEqual())
1205 FailureOr<bool> parsed = FieldParser<bool>::parse(parser);
1208 flexibleArrayMember = *parsed;
1211 parser.emitError(parser.getCurrentLocation(),
1212 "expected unadjusted_align, transparent_union or "
1213 "flexible_array_member");
1217 if (parser.parseGreater())
1219 return parser.getChecked<RecordLayoutAttr>(
1220 loc, parser.getContext(), *
kind, *hasTrivialDtor, *recordAlign,
1221 unadjustedAlign, transparentUnion, flexibleArrayMember);
1224void RecordLayoutAttr::print(AsmPrinter &printer)
const {
1225 printer <<
"<arg_passing_kind = ";
1226 printer.printStrippedAttrOrType(getArgPassingKind());
1227 printer <<
", has_trivial_dtor = ";
1228 printer.printStrippedAttrOrType(getHasTrivialDtor());
1229 printer <<
", record_align = ";
1230 printer.printStrippedAttrOrType(getRecordAlign());
1232 if (getUnadjustedAlign() != getRecordAlign()) {
1233 printer <<
", unadjusted_align = ";
1234 printer.printStrippedAttrOrType(getUnadjustedAlign());
1236 if (getTransparentUnion()) {
1237 printer <<
", transparent_union = ";
1238 printer.printStrippedAttrOrType(getTransparentUnion());
1240 if (getFlexibleArrayMember()) {
1241 printer <<
", flexible_array_member = ";
1242 printer.printStrippedAttrOrType(getFlexibleArrayMember());
1252 mlir::StringAttr name) {
1255 auto dict = mod->getAttrOfType<mlir::DictionaryAttr>(
1256 CIRDialect::getRecordLayoutsAttrName());
1259 return dict.getAs<RecordLayoutAttr>(name);
1263 mlir::StringAttr name) {
1265 assert(attr &&
"record layout entry missing for named record");
1274 return mlir::isa<TBAAOmnipotentCharAttr, TBAAScalarAttr, TBAAStructAttr>(
1279TBAAStructAttr::verify(function_ref<InFlightDiagnostic()> emitError,
1281 if (!llvm::is_sorted(members, [](TBAAMemberAttr a, TBAAMemberAttr b) {
1282 return a.getOffset() < b.getOffset();
1284 return emitError() <<
"expected the member offsets of TBAA struct '" <<
id
1285 <<
"' to be non-decreasing";
1289LogicalResult TBAATagAttr::verify(function_ref<InFlightDiagnostic()> emitError,
1293 if (!mlir::isa<TBAAOmnipotentCharAttr, TBAAScalarAttr>(access))
1294 return emitError() <<
"expected the access type to be a scalar TBAA type "
1296 if (!mlir::isa<TBAAStructAttr>(base) && (base != access || offset != 0))
1297 return emitError() <<
"expected a non-struct base to match the access "
1298 "type at offset zero";
1304 while (
auto record = mlir::dyn_cast<TBAAStructAttr>(node)) {
1305 ArrayRef<TBAAMemberAttr> members = record.getMembers();
1306 auto it = llvm::upper_bound(members, remaining,
1307 [](
uint64_t off, TBAAMemberAttr member) {
1308 return off <
member.getOffset();
1310 if (it == members.begin())
1311 return emitError() <<
"TBAA struct '" << record.getId()
1312 <<
"' has no member at offset " << remaining;
1314 remaining -= it->getOffset();
1315 node = it->getTypeDesc();
1318 return emitError() <<
"TBAA access offset " << offset
1319 <<
" does not point at the start of a scalar member";
1320 while (node != access) {
1321 auto scalar = mlir::dyn_cast<TBAAScalarAttr>(node);
1323 return emitError() <<
"TBAA access type is not on the access path of "
1324 "the base type at offset "
1326 node = scalar.getParent()
1328 : TBAAOmnipotentCharAttr::get(scalar.getContext());
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
Base class of the TBAA type descriptor attributes.
static bool classof(mlir::Attribute attr)
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.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
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 uint32_t int64_t
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat int32_t