15#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
16#include "mlir/IR/BuiltinAttributes.h"
17#include "mlir/IR/DialectImplementation.h"
18#include "mlir/IR/MLIRContext.h"
19#include "mlir/Support/LLVM.h"
26#include "llvm/ADT/APFloat.h"
27#include "llvm/ADT/APInt.h"
28#include "llvm/ADT/APSInt.h"
29#include "llvm/Support/MathExtras.h"
37 if (
auto sizedTy = mlir::dyn_cast<cir::SizedTypeInterface>(ty))
38 return sizedTy.isSized();
44 mlir::MLIRContext *ctx) {
45 switch (llvm::APFloat::SemanticsToEnum(sem)) {
46 case llvm::APFloat::S_IEEEhalf:
47 return cir::FP16Type::get(ctx);
48 case llvm::APFloat::S_BFloat:
49 return cir::BF16Type::get(ctx);
50 case llvm::APFloat::S_IEEEsingle:
51 return cir::SingleType::get(ctx);
52 case llvm::APFloat::S_IEEEdouble:
53 return cir::DoubleType::get(ctx);
54 case llvm::APFloat::S_x87DoubleExtended:
55 return cir::FP80Type::get(ctx);
56 case llvm::APFloat::S_IEEEquad:
57 return cir::FP128Type::get(ctx);
69static mlir::ParseResult
73 mlir::ArrayRef<mlir::Type> params,
79static mlir::ParseResult
84 mlir::ArrayRef<mlir::Type> params,
94 mlir::ptr::MemorySpaceAttrInterface &attr);
97 mlir::ptr::MemorySpaceAttrInterface attr);
105#include "clang/CIR/Dialect/IR/CIRTypeConstraints.cpp.inc"
109#define GET_TYPEDEF_CLASSES
110#include "clang/CIR/Dialect/IR/CIROpsTypes.cpp.inc"
119Type CIRDialect::parseType(DialectAsmParser &parser)
const {
120 llvm::SMLoc typeLoc = parser.getCurrentLocation();
121 llvm::StringRef mnemonic;
125 OptionalParseResult parseResult =
126 generatedTypeParser(parser, &mnemonic, genType);
127 if (parseResult.has_value())
131 parser.emitError(typeLoc) <<
"unknown CIR type: " << mnemonic;
135void CIRDialect::printType(Type type, DialectAsmPrinter &os)
const {
137 if (generatedTypePrinter(type, os).succeeded())
141 llvm::report_fatal_error(
"printer is missing a handler for this type");
153 RecordMemberKind::Data);
158static mlir::LogicalResult
162 if (memberKinds.size() != numMembers)
163 return emitError() <<
"expected " << numMembers <<
" member kinds, got "
164 << memberKinds.size();
165 return mlir::success();
169static mlir::LogicalResult
172 for (
auto [idx, memberTy] : llvm::enumerate(members)) {
173 if (!cir::isCIRType(memberTy))
174 return emitError() <<
"record member at index " << idx
175 <<
" must be a CIR type, got " << memberTy;
176 if (mlir::isa<cir::VoidType>(memberTy))
177 return emitError() <<
"record member at index " << idx
178 <<
" cannot be void";
180 return mlir::success();
188static std::optional<RecordMemberKind>
190 llvm::StringRef keyword;
191 const llvm::SMLoc loc = parser.getCurrentLocation();
192 if (parser.parseKeyword(&keyword).failed())
194 std::optional<RecordMemberKind>
kind = symbolizeRecordMemberKind(keyword);
196 parser.emitError(loc,
"expected a record member kind");
203static mlir::ParseResult
207 assert(incomplete &&
"caller must pre-initialize incomplete to true");
208 if (parser.parseOptionalKeyword(
"incomplete").succeeded())
209 return mlir::success();
211 return parser.parseCommaSeparatedList(
212 AsmParser::Delimiter::Braces,
213 [&parser, &members, &memberKinds]() -> mlir::ParseResult {
216 return mlir::failure();
217 memberKinds.push_back(*
kind);
218 return parser.parseType(members.emplace_back());
226template <
typename RecordTy>
229 bool hasClassPrefix,
bool isPacked,
bool isIncomplete,
238 FailureOr<AsmPrinter::CyclicPrintReset> cyclicPrintGuard =
239 printer.tryStartCyclicPrint(self);
240 if (failed(cyclicPrintGuard)) {
245 if (hasClassPrefix || name)
248 printer <<
"packed ";
250 printer <<
"incomplete";
253 for (
auto [idx, member] : llvm::enumerate(members)) {
256 printer << stringifyRecordMemberKind(memberKinds[idx]) <<
' ';
257 printer.printType(member);
261 printer <<
", padding = {";
262 printer.printType(padding);
270Type StructType::parse(mlir::AsmParser &parser) {
271 FailureOr<AsmParser::CyclicParseReset> cyclicParseGuard;
272 const llvm::SMLoc loc = parser.getCurrentLocation();
273 const mlir::Location eLoc = parser.getEncodedSourceLoc(loc);
275 mlir::MLIRContext *context = parser.getContext();
277 if (parser.parseLess())
281 bool is_class = parser.parseOptionalKeyword(
"class").succeeded();
283 mlir::StringAttr
name;
284 parser.parseOptionalAttribute(name);
287 if (name && parser.parseOptionalGreater().succeeded()) {
288 StructType
type = StructType::getChecked(eLoc, context, name, is_class);
289 if (succeeded(parser.tryStartCyclicParse(type))) {
290 parser.emitError(loc,
"invalid self-reference within record");
298 StructType
type = StructType::getChecked(eLoc, context, name, is_class);
299 cyclicParseGuard = parser.tryStartCyclicParse(type);
300 if (failed(cyclicParseGuard)) {
301 parser.emitError(loc,
"record already defined");
306 if (parser.parseOptionalKeyword(
"packed").succeeded())
309 bool incomplete =
true;
310 llvm::SmallVector<mlir::Type> members;
311 llvm::SmallVector<RecordMemberKind> memberKinds;
312 if (
parseRecordBody(parser, incomplete, members, memberKinds).failed())
315 if (parser.parseGreater())
318 ArrayRef<mlir::Type> membersRef(members);
319 ArrayRef<RecordMemberKind> kindsRef(memberKinds);
320 mlir::Type
type = {};
321 if (name && incomplete) {
322 type = StructType::getChecked(eLoc, context, name, is_class);
323 }
else if (!name && !incomplete) {
324 type = StructType::getChecked(eLoc, context, membersRef, packed, is_class,
328 }
else if (!incomplete) {
329 type = StructType::getChecked(eLoc, context, membersRef, name, packed,
333 if (
auto structTy = mlir::dyn_cast<StructType>(type)) {
334 if (failed(structTy.mutate(membersRef, packed, kindsRef))) {
335 parser.emitError(loc,
"record '")
336 <<
name.getValue() <<
"' redefined with a different body";
342 parser.emitError(loc,
"anonymous records must be complete");
349void StructType::print(mlir::AsmPrinter &printer)
const {
351 isIncomplete(), getMembers(), {},
355mlir::LogicalResult StructType::verify(
356 function_ref<mlir::InFlightDiagnostic()> emitError,
357 llvm::ArrayRef<mlir::Type> members, mlir::StringAttr name,
bool incomplete,
358 bool packed, llvm::ArrayRef<RecordMemberKind> member_kinds,
bool is_class) {
359 if (name &&
name.getValue().empty())
360 return emitError() <<
"identified records cannot have an empty name";
362 return mlir::failure();
368llvm::ArrayRef<mlir::Type> StructType::getMembers()
const {
371mlir::StringAttr StructType::getName()
const {
return getImpl()->name; }
372bool StructType::isIncomplete()
const {
return getImpl()->incomplete; }
373bool StructType::getIncomplete()
const {
return getImpl()->incomplete; }
374bool StructType::getPacked()
const {
return getImpl()->packed; }
375llvm::ArrayRef<RecordMemberKind> StructType::getMemberKinds()
const {
376 return getImpl()->member_kinds;
378bool StructType::getIsClass()
const {
return getImpl()->is_class; }
380bool StructType::getPadded()
const {
381 return llvm::is_contained(getMemberKinds(), RecordMemberKind::Pad);
384bool StructType::isABIConvertedRecord()
const {
385 return getName() &&
getName().getValue().starts_with(abi_conversion_prefix);
388mlir::StringAttr StructType::getABIConvertedName()
const {
389 assert(!isABIConvertedRecord());
390 return StringAttr::get(getContext(),
391 abi_conversion_prefix +
getName().getValue());
394void StructType::removeABIConversionNamePrefix() {
395 mlir::StringAttr recordName =
getName();
396 if (recordName && recordName.getValue().starts_with(abi_conversion_prefix))
397 getImpl()->name = mlir::StringAttr::get(
398 recordName.getValue().drop_front(
sizeof(abi_conversion_prefix) - 1),
399 recordName.getType());
402void StructType::complete(ArrayRef<Type> members,
bool packed,
403 ArrayRef<RecordMemberKind> memberKinds) {
407 succeeded(verifyInvariants(
408 mlir::detail::getDefaultDiagnosticEmitFn(getContext()), members,
409 getName(),
false, packed, memberKinds, isClass())) &&
410 "invalid struct completion");
411 if (mutate(members, packed, memberKinds).failed())
412 llvm_unreachable(
"failed to complete struct");
415bool StructType::isLayoutIdentical(
const StructType &other) {
416 if (
getImpl() == other.getImpl())
418 if (getPacked() != other.getPacked())
420 return getMembers() == other.getMembers();
427Type UnionType::parse(mlir::AsmParser &parser) {
428 FailureOr<AsmParser::CyclicParseReset> cyclicParseGuard;
429 const llvm::SMLoc loc = parser.getCurrentLocation();
430 const mlir::Location eLoc = parser.getEncodedSourceLoc(loc);
433 mlir::MLIRContext *context = parser.getContext();
435 if (parser.parseLess())
438 mlir::StringAttr
name;
439 parser.parseOptionalAttribute(name);
442 if (name && parser.parseOptionalGreater().succeeded()) {
443 UnionType
type = UnionType::getChecked(eLoc, context, name);
444 if (succeeded(parser.tryStartCyclicParse(type))) {
445 parser.emitError(loc,
"invalid self-reference within record");
453 UnionType
type = UnionType::getChecked(eLoc, context, name);
454 cyclicParseGuard = parser.tryStartCyclicParse(type);
455 if (failed(cyclicParseGuard)) {
456 parser.emitError(loc,
"record already defined");
461 if (parser.parseOptionalKeyword(
"packed").succeeded())
464 bool incomplete =
true;
465 llvm::SmallVector<mlir::Type> members;
466 llvm::SmallVector<RecordMemberKind> memberKinds;
467 if (
parseRecordBody(parser, incomplete, members, memberKinds).failed())
472 if (!incomplete && parser.parseOptionalComma().succeeded()) {
473 if (parser.parseKeyword(
"padding").failed())
475 if (parser.parseEqual().failed())
477 if (parser.parseLBrace().failed())
479 const llvm::SMLoc paddingLoc = parser.getCurrentLocation();
480 llvm::StringRef paddingKeyword;
483 parser.emitError(paddingLoc,
"a union's tail padding takes no kind mark");
486 if (parser.parseType(padding).failed())
488 if (parser.parseRBrace().failed())
492 if (parser.parseGreater())
495 ArrayRef<mlir::Type> membersRef(members);
496 ArrayRef<RecordMemberKind> kindsRef(memberKinds);
497 mlir::Type
type = {};
498 if (name && incomplete) {
499 type = UnionType::getChecked(eLoc, context, name);
500 }
else if (!name && !incomplete) {
501 type = UnionType::getChecked(eLoc, context, membersRef, packed, padding,
505 }
else if (!incomplete) {
506 type = UnionType::getChecked(eLoc, context, membersRef, name, packed,
510 if (
auto unionTy = mlir::dyn_cast<UnionType>(type)) {
511 if (failed(unionTy.mutate(membersRef, packed, padding, kindsRef))) {
512 parser.emitError(loc,
"record '")
513 <<
name.getValue() <<
"' redefined with a different body";
519 parser.emitError(loc,
"anonymous records must be complete");
526void UnionType::print(mlir::AsmPrinter &printer)
const {
528 getPacked(), isIncomplete(), getMembers(), getPadding(),
533UnionType::verify(function_ref<mlir::InFlightDiagnostic()> emitError,
534 llvm::ArrayRef<mlir::Type> members, mlir::StringAttr name,
535 bool incomplete,
bool packed, mlir::Type padding,
536 llvm::ArrayRef<RecordMemberKind> member_kinds) {
537 if (name &&
name.getValue().empty())
538 return emitError() <<
"identified records cannot have an empty name";
542 if (llvm::is_contained(member_kinds, RecordMemberKind::Pad))
543 return emitError() <<
"a union member cannot be marked pad";
545 return mlir::failure();
548 mlir::Type charTy = padding;
549 if (
auto arrTy = mlir::dyn_cast<cir::ArrayType>(padding))
550 charTy = arrTy.getElementType();
551 auto intTy = mlir::dyn_cast<cir::IntType>(charTy);
552 if (!intTy || intTy.getWidth() != 8 || intTy.isSigned() || intTy.isBitInt())
554 <<
"union padding must be !u8i or an array of !u8i, got "
560 for (
auto [idx, memberTy] : llvm::enumerate(members))
561 if (
auto bfTy = mlir::dyn_cast<cir::BitFieldType>(memberTy);
562 bfTy && !bfTy.ownsBytes())
563 return emitError() <<
"union bit-field member at index " << idx
564 <<
" must own its access unit storage";
569llvm::ArrayRef<mlir::Type> UnionType::getMembers()
const {
572mlir::StringAttr UnionType::getName()
const {
return getImpl()->name; }
573bool UnionType::isIncomplete()
const {
return getImpl()->incomplete; }
574bool UnionType::getIncomplete()
const {
return getImpl()->incomplete; }
575bool UnionType::getPacked()
const {
return getImpl()->packed; }
576bool UnionType::getPadded()
const {
return getPadding() ?
true :
false; }
577mlir::Type UnionType::getPadding()
const {
return getImpl()->padding; }
578llvm::ArrayRef<RecordMemberKind> UnionType::getMemberKinds()
const {
579 return getImpl()->member_kinds;
582bool UnionType::isABIConvertedRecord()
const {
583 return getName() &&
getName().getValue().starts_with(abi_conversion_prefix);
586mlir::StringAttr UnionType::getABIConvertedName()
const {
587 assert(!isABIConvertedRecord());
588 return StringAttr::get(getContext(),
589 abi_conversion_prefix +
getName().getValue());
592void UnionType::removeABIConversionNamePrefix() {
593 mlir::StringAttr recordName =
getName();
594 if (recordName && recordName.getValue().starts_with(abi_conversion_prefix))
595 getImpl()->name = mlir::StringAttr::get(
596 recordName.getValue().drop_front(
sizeof(abi_conversion_prefix) - 1),
597 recordName.getType());
600void UnionType::complete(ArrayRef<Type> members,
bool packed,
602 ArrayRef<RecordMemberKind> memberKinds) {
605 assert(succeeded(verifyInvariants(
606 mlir::detail::getDefaultDiagnosticEmitFn(getContext()), members,
607 getName(),
false, packed, padding, memberKinds)) &&
608 "invalid union completion");
609 if (mutate(members, packed, padding, memberKinds).failed())
610 llvm_unreachable(
"failed to complete union");
614UnionType::getUnionStorageType(
const mlir::DataLayout &dataLayout)
const {
615 return getUnionStorageType(dataLayout, getMembers());
618mlir::Type UnionType::getUnionStorageType(
const mlir::DataLayout &dataLayout,
619 llvm::ArrayRef<mlir::Type> members) {
622 mlir::Type largest = *std::max_element(
623 members.begin(), members.end(), [&](mlir::Type lhs, mlir::Type rhs) {
624 return dataLayout.getTypeABIAlignment(lhs) <
625 dataLayout.getTypeABIAlignment(rhs) ||
626 (dataLayout.getTypeABIAlignment(lhs) ==
627 dataLayout.getTypeABIAlignment(rhs) &&
628 dataLayout.getTypeAllocSize(lhs) <
629 dataLayout.getTypeAllocSize(rhs));
637bool UnionType::isLayoutIdentical(
const UnionType &other) {
638 if (
getImpl() == other.getImpl())
640 return getMembers() == other.getMembers() &&
641 getPadding() == other.getPadding();
649 if (
auto s = mlir::dyn_cast<StructType>(*
this))
650 return s.getMembers();
651 return mlir::cast<UnionType>(*this).getMembers();
654 if (
auto s = mlir::dyn_cast<StructType>(*
this))
656 return mlir::cast<UnionType>(*this).getName();
659 if (
auto s = mlir::dyn_cast<StructType>(*
this))
660 return s.isIncomplete();
661 return mlir::cast<UnionType>(*this).isIncomplete();
664 if (
auto s = mlir::dyn_cast<StructType>(*
this))
665 return s.getPacked();
666 return mlir::cast<UnionType>(*this).getPacked();
669 if (
auto s = mlir::dyn_cast<StructType>(*
this))
670 return s.getPadded();
671 return mlir::cast<UnionType>(*this).getPadded();
674 if (
auto s = mlir::dyn_cast<StructType>(*
this))
675 return s.getMemberKinds();
676 return mlir::cast<UnionType>(*this).getMemberKinds();
679 if (
auto s = mlir::dyn_cast<StructType>(*
this))
684 if (
auto s = mlir::dyn_cast<StructType>(*
this))
689 if (mlir::isa<UnionType>(*
this))
691 return mlir::cast<StructType>(*this).getKindAsStr();
699 if (
auto s = mlir::dyn_cast<StructType>(*
this)) {
700 assert(!padding &&
"only a union takes a separate padding slot");
701 return s.complete(members, packed, memberKinds);
703 return mlir::cast<UnionType>(*this).complete(members, packed, padding,
707 unsigned idx)
const {
708 if (mlir::isa<UnionType>(*
this))
710 return mlir::cast<StructType>(*this).getElementOffset(dataLayout, idx);
713 if (
auto s = mlir::dyn_cast<StructType>(*
this)) {
714 if (
auto so = mlir::dyn_cast<StructType>(other))
715 return s.isLayoutIdentical(so);
718 if (
auto u = mlir::dyn_cast<UnionType>(*
this)) {
719 if (
auto uo = mlir::dyn_cast<UnionType>(other))
720 return u.isLayoutIdentical(uo);
726 if (
auto s = mlir::dyn_cast<StructType>(*
this))
727 return s.isABIConvertedRecord();
728 return mlir::cast<UnionType>(*this).isABIConvertedRecord();
731 if (
auto s = mlir::dyn_cast<StructType>(*
this))
732 return s.getABIConvertedName();
733 return mlir::cast<UnionType>(*this).getABIConvertedName();
736 if (
auto s = mlir::dyn_cast<StructType>(*
this))
737 return s.removeABIConversionNamePrefix();
738 return mlir::cast<UnionType>(*this).removeABIConversionNamePrefix();
752 return llvm::none_of(
getMembers(), [](mlir::Type memberTy) {
753 auto bfTy = mlir::dyn_cast<cir::BitFieldType>(memberTy);
754 return bfTy && bfTy.ownsBytes();
765constexpr static uint64_t kBitsInByte = 8;
768constexpr static uint64_t kDefaultPointerSizeBits = 64;
769constexpr static uint64_t kDefaultPointerAlignment = 8;
773cir::PtrSpecAttr getPointerSpec(mlir::DataLayoutEntryListRef params,
774 cir::PointerType type) {
777 for (mlir::DataLayoutEntryInterface entry : params) {
778 if (!entry.isTypeEntry())
781 mlir::cast<cir::PointerType>(mlir::cast<mlir::Type>(entry.getKey()));
782 if (key.getAddrSpace())
784 if (
auto spec = mlir::dyn_cast<cir::PtrSpecAttr>(entry.getValue()))
787 return cir::PtrSpecAttr::get(type.getContext(), kDefaultPointerSizeBits,
788 kDefaultPointerAlignment * kBitsInByte,
789 kDefaultPointerAlignment * kBitsInByte,
790 kDefaultPointerSizeBits);
795constexpr static uint64_t kDefaultBitIntMaxAlignBits = 64;
799uint64_t getBitIntMaxAlignBits(mlir::DataLayoutEntryListRef params) {
800 for (mlir::DataLayoutEntryInterface entry : params) {
801 if (!entry.isTypeEntry())
803 if (
auto spec = mlir::dyn_cast<cir::BitIntSpecAttr>(entry.getValue()))
804 return spec.getMaxAlign();
806 return kDefaultBitIntMaxAlignBits;
811PointerType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
812 ::mlir::DataLayoutEntryListRef params)
const {
813 return llvm::TypeSize::getFixed(getPointerSpec(params, *
this).getSize());
817PointerType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
818 ::mlir::DataLayoutEntryListRef params)
const {
819 return getPointerSpec(params, *
this).getAbi() / kBitsInByte;
822uint64_t PointerType::getPreferredAlignment(
823 const ::mlir::DataLayout &dataLayout,
824 ::mlir::DataLayoutEntryListRef params)
const {
825 return getPointerSpec(params, *
this).getPreferred() / kBitsInByte;
828std::optional<uint64_t>
829PointerType::getIndexBitwidth(const ::mlir::DataLayout &dataLayout,
830 ::mlir::DataLayoutEntryListRef params)
const {
831 cir::PtrSpecAttr spec = getPointerSpec(params, *
this);
832 if (spec.getIndex() == cir::PtrSpecAttr::kOptionalSpecValue)
833 return spec.getSize();
834 return spec.getIndex();
838PointerType::verifyEntries(mlir::DataLayoutEntryListRef entries,
839 mlir::Location loc)
const {
840 for (mlir::DataLayoutEntryInterface entry : entries) {
841 if (!entry.isTypeEntry())
843 auto key = mlir::cast<PointerType>(mlir::cast<mlir::Type>(entry.getKey()));
844 if (!mlir::isa<cir::PtrSpecAttr>(entry.getValue()))
845 return mlir::emitError(loc) <<
"expected layout attribute for " << key
846 <<
" to be a #cir.ptr_spec attribute";
847 if (!mlir::isa<cir::VoidType>(key.getPointee()))
848 return mlir::emitError(loc) <<
"expected !cir.ptr data layout entry for "
849 << key <<
" to use !cir.void as pointee";
851 if (key.getAddrSpace())
852 return mlir::emitError(loc)
853 <<
"!cir.ptr data layout entries are currently limited to the "
854 "default address space";
856 return mlir::success();
859bool PointerType::areCompatible(
860 mlir::DataLayoutEntryListRef oldLayout,
861 mlir::DataLayoutEntryListRef newLayout, mlir::DataLayoutSpecInterface,
862 const mlir::DataLayoutIdentifiedEntryMap &)
const {
865 cir::PtrSpecAttr oldSpec = getPointerSpec(oldLayout, *
this);
868 for (mlir::DataLayoutEntryInterface newEntry : newLayout) {
869 if (!newEntry.isTypeEntry())
871 auto newSpec = mlir::cast<cir::PtrSpecAttr>(newEntry.getValue());
872 if (size != newSpec.getSize() || abi < newSpec.getAbi() ||
873 abi % newSpec.getAbi() != 0)
880StructType::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
881 mlir::DataLayoutEntryListRef params)
const {
882 auto recordSize =
static_cast<uint64_t>(computeStructSize(dataLayout));
883 return llvm::TypeSize::getFixed(recordSize * 8);
887StructType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
888 ::mlir::DataLayoutEntryListRef params)
const {
892 return computeStructAlignment(dataLayout);
900UnionType::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
901 mlir::DataLayoutEntryListRef params)
const {
902 llvm::TypeSize size = llvm::TypeSize::getFixed(0);
903 if (mlir::Type storage = getUnionStorageType(dataLayout))
904 size += dataLayout.getTypeAllocSize(storage) * 8;
905 if (mlir::Type pad = getPadding())
906 size += dataLayout.getTypeSizeInBits(pad);
911UnionType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
912 ::mlir::DataLayoutEntryListRef params)
const {
915 mlir::Type storage = getUnionStorageType(dataLayout);
918 return dataLayout.getTypeABIAlignment(storage);
922StructType::computeStructSize(
const mlir::DataLayout &dataLayout)
const {
923 assert(isComplete() &&
"Cannot get layout of incomplete records");
928 unsigned recordSize = 0;
931 for (mlir::Type ty : getMembers()) {
935 (getPacked() ? 1 : dataLayout.getTypeABIAlignment(ty));
939 recordSize = llvm::alignTo(recordSize, tyAlign);
940 recordSize += dataLayout.getTypeAllocSize(ty);
944 recordAlignment = std::max(tyAlign, recordAlignment);
949 recordSize = llvm::alignTo(recordSize, recordAlignment);
954StructType::computeStructDataSize(
const mlir::DataLayout &dataLayout)
const {
955 assert(isComplete() &&
"Cannot get layout of incomplete records");
960 llvm::ArrayRef<mlir::Type> members = getMembers();
961 llvm::ArrayRef<RecordMemberKind> kinds = getMemberKinds();
962 assert(kinds.size() == members.size() &&
963 "the two drop_back calls below must stay in step");
964 while (!kinds.empty() && (kinds.back() == RecordMemberKind::Pad ||
966 kinds = kinds.drop_back();
967 members = members.drop_back();
970 unsigned recordSize = 0;
971 for (mlir::Type ty : members) {
973 (getPacked() ? 1 : dataLayout.getTypeABIAlignment(ty));
974 recordSize = llvm::alignTo(recordSize, tyAlign);
975 recordSize += dataLayout.getTypeAllocSize(ty);
985StructType::computeStructAlignment(
const mlir::DataLayout &dataLayout)
const {
986 assert(isComplete() &&
"Cannot get layout of incomplete records");
989 for (mlir::Type ty : getMembers())
991 std::max(dataLayout.getTypeABIAlignment(ty), recordAlignment);
992 return recordAlignment;
995unsigned StructType::getLLVMFieldIndex(
unsigned idx)
const {
996 llvm::ArrayRef<mlir::Type> members = getMembers();
997 assert(idx < members.size() &&
"access not valid");
999 "a member that owns no bytes has no LLVM field");
1001 unsigned llvmIdx = 0;
1002 for (
unsigned i = 0; i != idx; ++i)
1008uint64_t StructType::getElementOffset(const ::mlir::DataLayout &dataLayout,
1009 unsigned idx)
const {
1010 assert(idx < getMembers().size() &&
"access not valid");
1014 assert(isComplete() &&
"Cannot get layout of incomplete records");
1015 llvm::ArrayRef<mlir::Type> members = getMembers();
1020 unsigned offset = 0;
1021 for (
unsigned i = 0; i != idx; ++i) {
1022 const llvm::Align tyAlign = llvm::Align(
1023 getPacked() ? 1 : dataLayout.getTypeABIAlignment(members[i]));
1024 offset = llvm::alignTo(offset, tyAlign);
1025 offset += dataLayout.getTypeAllocSize(members[i]);
1028 const llvm::Align tyAlign = llvm::Align(
1029 getPacked() ? 1 : dataLayout.getTypeABIAlignment(members[idx]));
1030 return llvm::alignTo(offset, tyAlign);
1037Type IntType::parse(mlir::AsmParser &parser) {
1038 mlir::MLIRContext *context = parser.getBuilder().getContext();
1039 llvm::SMLoc loc = parser.getCurrentLocation();
1043 if (parser.parseLess())
1047 llvm::StringRef
sign;
1048 if (parser.parseKeyword(&
sign))
1052 else if (
sign ==
"u")
1055 parser.emitError(loc,
"expected 's' or 'u'");
1059 if (parser.parseComma())
1063 if (parser.parseInteger(width))
1065 if (width < IntType::minBitwidth() || width > IntType::maxBitwidth()) {
1066 parser.emitError(loc,
"expected integer width to be from ")
1067 << IntType::minBitwidth() <<
" up to " << IntType::maxBitwidth();
1071 bool isBitInt =
false;
1072 if (succeeded(parser.parseOptionalComma())) {
1074 if (parser.parseKeyword(&kw) || kw !=
"bitint") {
1075 parser.emitError(loc,
"expected 'bitint'");
1081 if (parser.parseGreater())
1084 return IntType::get(context, width, isSigned, isBitInt);
1087void IntType::print(mlir::AsmPrinter &printer)
const {
1088 char sign = isSigned() ?
's' :
'u';
1089 printer <<
'<' <<
sign <<
", " << getWidth();
1091 printer <<
", bitint";
1096IntType::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
1097 mlir::DataLayoutEntryListRef params)
const {
1098 return llvm::TypeSize::getFixed(getStorageTypeWidth(dataLayout));
1102IntType::getStorageTypeWidth(
const mlir::DataLayout &dataLayout)
const {
1105 uint64_t alignBits = dataLayout.getTypeABIAlignment(*
this) * 8;
1106 return static_cast<unsigned>(llvm::alignTo(getWidth(), alignBits));
1110IntType::getStorageTypeAlignment(
const mlir::DataLayout &dataLayout)
const {
1112 return getABIAlignment(dataLayout, {});
1114 mlir::IntegerType::get(getContext(), getStorageTypeWidth(dataLayout));
1115 return dataLayout.getTypeABIAlignment(storageTy);
1118uint64_t IntType::getABIAlignment(
const mlir::DataLayout &dataLayout,
1119 mlir::DataLayoutEntryListRef params)
const {
1120 unsigned width = getWidth();
1122 uint64_t maxAlignBits = getBitIntMaxAlignBits(params);
1124 static_cast<uint64_t>(llvm::PowerOf2Ceil(width)), maxAlignBits);
1125 return std::max(alignBits / 8,
static_cast<uint64_t>(1));
1132 uint64_t alignBits = llvm::PowerOf2Ceil(width);
1133 return std::max(alignBits / 8,
static_cast<uint64_t>(1));
1136bool IntType::areCompatible(mlir::DataLayoutEntryListRef oldLayout,
1137 mlir::DataLayoutEntryListRef newLayout,
1138 mlir::DataLayoutSpecInterface,
1139 const mlir::DataLayoutIdentifiedEntryMap &)
const {
1141 uint64_t oldMax = getBitIntMaxAlignBits(oldLayout);
1142 for (mlir::DataLayoutEntryInterface newEntry : newLayout) {
1143 if (!newEntry.isTypeEntry())
1145 auto newSpec = mlir::dyn_cast<cir::BitIntSpecAttr>(newEntry.getValue());
1146 if (newSpec && newSpec.getMaxAlign() != oldMax)
1152llvm::LogicalResult IntType::verifyEntries(mlir::DataLayoutEntryListRef entries,
1153 mlir::Location loc)
const {
1154 cir::IntType keyTy = getBitIntMaxAlignKey(getContext());
1155 for (mlir::DataLayoutEntryInterface entry : entries) {
1156 if (!entry.isTypeEntry())
1158 auto key = mlir::cast<IntType>(mlir::cast<mlir::Type>(entry.getKey()));
1160 return mlir::emitError(loc)
1161 <<
"expected _BitInt data layout entry to be keyed on " << keyTy
1163 if (!mlir::isa<cir::BitIntSpecAttr>(entry.getValue()))
1164 return mlir::emitError(loc) <<
"expected layout attribute for " << key
1165 <<
" to be a #cir.bitint_spec attribute";
1167 return mlir::success();
1171IntType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
1172 unsigned width,
bool isSigned,
bool isBitInt) {
1173 if (width < IntType::minBitwidth() || width > IntType::maxBitwidth())
1174 return emitError() <<
"IntType only supports widths from "
1175 << IntType::minBitwidth() <<
" up to "
1176 << IntType::maxBitwidth();
1177 return mlir::success();
1181 return width == 8 || width == 16 || width == 32 || width == 64;
1188const llvm::fltSemantics &SingleType::getFloatSemantics()
const {
1189 return llvm::APFloat::IEEEsingle();
1193SingleType::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
1194 mlir::DataLayoutEntryListRef params)
const {
1195 return llvm::TypeSize::getFixed(getWidth());
1199SingleType::getABIAlignment(
const mlir::DataLayout &dataLayout,
1200 mlir::DataLayoutEntryListRef params)
const {
1204const llvm::fltSemantics &DoubleType::getFloatSemantics()
const {
1205 return llvm::APFloat::IEEEdouble();
1209DoubleType::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
1210 mlir::DataLayoutEntryListRef params)
const {
1211 return llvm::TypeSize::getFixed(getWidth());
1215DoubleType::getABIAlignment(
const mlir::DataLayout &dataLayout,
1216 mlir::DataLayoutEntryListRef params)
const {
1220const llvm::fltSemantics &FP16Type::getFloatSemantics()
const {
1221 return llvm::APFloat::IEEEhalf();
1225FP16Type::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
1226 mlir::DataLayoutEntryListRef params)
const {
1227 return llvm::TypeSize::getFixed(getWidth());
1230uint64_t FP16Type::getABIAlignment(
const mlir::DataLayout &dataLayout,
1231 mlir::DataLayoutEntryListRef params)
const {
1235const llvm::fltSemantics &BF16Type::getFloatSemantics()
const {
1236 return llvm::APFloat::BFloat();
1240BF16Type::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
1241 mlir::DataLayoutEntryListRef params)
const {
1242 return llvm::TypeSize::getFixed(getWidth());
1245uint64_t BF16Type::getABIAlignment(
const mlir::DataLayout &dataLayout,
1246 mlir::DataLayoutEntryListRef params)
const {
1250const llvm::fltSemantics &FP80Type::getFloatSemantics()
const {
1251 return llvm::APFloat::x87DoubleExtended();
1255FP80Type::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
1256 mlir::DataLayoutEntryListRef params)
const {
1258 return llvm::TypeSize::getFixed(128);
1261uint64_t FP80Type::getABIAlignment(
const mlir::DataLayout &dataLayout,
1262 mlir::DataLayoutEntryListRef params)
const {
1266const llvm::fltSemantics &FP128Type::getFloatSemantics()
const {
1267 return llvm::APFloat::IEEEquad();
1271FP128Type::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
1272 mlir::DataLayoutEntryListRef params)
const {
1273 return llvm::TypeSize::getFixed(getWidth());
1276uint64_t FP128Type::getABIAlignment(
const mlir::DataLayout &dataLayout,
1277 mlir::DataLayoutEntryListRef params)
const {
1281const llvm::fltSemantics &LongDoubleType::getFloatSemantics()
const {
1282 return mlir::cast<cir::FPTypeInterface>(getUnderlying()).getFloatSemantics();
1286LongDoubleType::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
1287 mlir::DataLayoutEntryListRef params)
const {
1288 return mlir::cast<mlir::DataLayoutTypeInterface>(getUnderlying())
1289 .getTypeSizeInBits(dataLayout, params);
1293LongDoubleType::getABIAlignment(
const mlir::DataLayout &dataLayout,
1294 mlir::DataLayoutEntryListRef params)
const {
1295 return mlir::cast<mlir::DataLayoutTypeInterface>(getUnderlying())
1296 .getABIAlignment(dataLayout, params);
1304cir::ComplexType::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
1305 mlir::DataLayoutEntryListRef params)
const {
1311 return dataLayout.getTypeSizeInBits(getElementType()) * 2;
1315cir::ComplexType::getABIAlignment(
const mlir::DataLayout &dataLayout,
1316 mlir::DataLayoutEntryListRef params)
const {
1322 return dataLayout.getTypeABIAlignment(getElementType());
1325FuncType FuncType::clone(TypeRange inputs, TypeRange results)
const {
1326 assert(results.size() == 1 &&
"expected exactly one result type");
1327 return get(llvm::to_vector(inputs), results[0], isVarArg());
1331static mlir::ParseResult
1335 return p.parseCommaSeparatedList(
1336 AsmParser::Delimiter::Paren, [&]() -> mlir::ParseResult {
1338 return p.emitError(p.getCurrentLocation(),
1339 "variadic `...` must be the last parameter");
1340 if (succeeded(p.parseOptionalEllipsis())) {
1345 if (failed(p.parseType(type)))
1347 params.push_back(type);
1353 mlir::ArrayRef<mlir::Type> params,
1356 llvm::interleaveComma(params, p,
1357 [&p](mlir::Type type) { p.printType(type); });
1359 if (!params.empty())
1368mlir::Type FuncType::getReturnType()
const {
1369 if (hasVoidReturn())
1370 return cir::VoidType::get(getContext());
1371 return getOptionalReturnType();
1377llvm::ArrayRef<mlir::Type> FuncType::getReturnTypes()
const {
1378 if (hasVoidReturn())
1383 return getImpl()->optionalReturnType;
1387bool FuncType::hasVoidReturn()
const {
return !getOptionalReturnType(); }
1390FuncType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
1391 llvm::ArrayRef<mlir::Type> argTypes, mlir::Type returnType,
1393 if (mlir::isa_and_nonnull<cir::VoidType>(returnType))
1395 <<
"!cir.func cannot have an explicit 'void' return type";
1400 if (!llvm::all_of(argTypes, cir::isCIRType) ||
1401 (returnType && !cir::isCIRType(returnType)))
1403 <<
"expected all types in the function signature to be CIR types";
1405 return mlir::success();
1417 auto voidPtrTy = cir::PointerType::get(cir::VoidType::get(ctx));
1418 mlir::Type fields[2]{voidPtrTy, voidPtrTy};
1419 return cir::StructType::get(ctx, fields,
false,
1425MethodType::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
1426 mlir::DataLayoutEntryListRef params)
const {
1431MethodType::getABIAlignment(
const mlir::DataLayout &dataLayout,
1432 mlir::DataLayoutEntryListRef params)
const {
1434 .getABIAlignment(dataLayout, params);
1442BoolType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
1443 ::mlir::DataLayoutEntryListRef params)
const {
1444 return llvm::TypeSize::getFixed(8);
1448BoolType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
1449 ::mlir::DataLayoutEntryListRef params)
const {
1458 mlir::MLIRContext *ctx) {
1462 auto voidPtrTy = cir::PointerType::get(cir::VoidType::get(ctx));
1463 uint64_t width = dataLayout.getTypeIndexBitwidth(voidPtrTy).value_or(
1464 dataLayout.getTypeSizeInBits(voidPtrTy).getFixedValue());
1465 return cir::IntType::get(ctx, width,
true);
1469DataMemberType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
1470 ::mlir::DataLayoutEntryListRef params)
const {
1472 return dataLayout.getTypeSizeInBits(
1477DataMemberType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
1478 ::mlir::DataLayoutEntryListRef params)
const {
1480 return dataLayout.getTypeABIAlignment(
1489VPtrType::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
1490 mlir::DataLayoutEntryListRef params)
const {
1492 return dataLayout.getTypeSizeInBits(
1493 cir::PointerType::get(cir::VoidType::get(getContext())));
1496uint64_t VPtrType::getABIAlignment(
const mlir::DataLayout &dataLayout,
1497 mlir::DataLayoutEntryListRef params)
const {
1498 return dataLayout.getTypeABIAlignment(
1499 cir::PointerType::get(cir::VoidType::get(getContext())));
1507ArrayType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
1508 ::mlir::DataLayoutEntryListRef params)
const {
1509 return getSize() * dataLayout.getTypeAllocSize(getElementType()) * 8;
1513ArrayType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
1514 ::mlir::DataLayoutEntryListRef params)
const {
1515 return dataLayout.getTypeABIAlignment(getElementType());
1523BitFieldType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
1524 mlir::Type storageType,
1525 llvm::ArrayRef<cir::BitFieldDeclAttr> fields) {
1527 return emitError() <<
"bit-field member must hold at least one bit-field";
1532 if (fields.size() != 1 || fields.front().getWidth() != 0)
1533 return emitError() <<
"a bit-field member without storage must hold a "
1534 "single zero-width bit-field";
1535 return mlir::success();
1538 if (!cir::isCIRType(storageType))
1539 return emitError() <<
"bit-field access unit storage must be a CIR type, "
1543 if (mlir::isa<cir::BitFieldType>(storageType))
1544 return emitError() <<
"bit-field access unit storage cannot itself be a "
1548 return emitError() <<
"bit-field access unit storage must be sized, got "
1553 if (llvm::any_of(fields, [](cir::BitFieldDeclAttr decl) {
1554 return decl.getWidth() == 0;
1556 return emitError() <<
"a zero-width bit-field cannot occupy an access unit";
1558 return mlir::success();
1562BitFieldType::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
1563 mlir::DataLayoutEntryListRef params)
const {
1566 if (mlir::Type storage = getStorageType())
1567 return dataLayout.getTypeSizeInBits(storage);
1568 return llvm::TypeSize::getFixed(0);
1572BitFieldType::getABIAlignment(
const mlir::DataLayout &dataLayout,
1573 mlir::DataLayoutEntryListRef params)
const {
1576 if (mlir::Type storage = getStorageType())
1577 return dataLayout.getTypeABIAlignment(storage);
1585llvm::TypeSize cir::VectorType::getTypeSizeInBits(
1586 const ::mlir::DataLayout &dataLayout,
1587 ::mlir::DataLayoutEntryListRef params)
const {
1590 if (mlir::isa<cir::BoolType>(getElementType()) && getSize().isFixed()) {
1592 return llvm::TypeSize::getFixed(llvm::PowerOf2Ceil(getBoolStorageWidth()));
1595 llvm::TypeSize elementSize = dataLayout.getTypeSizeInBits(getElementType());
1596 return llvm::TypeSize::get(
1597 getSize().getKnownMinValue() * elementSize.getFixedValue(), isScalable());
1600uint64_t cir::VectorType::getBoolStorageWidth()
const {
1601 assert(mlir::isa<cir::BoolType>(getElementType()) &&
1602 "only a bool vector is stored as an integer");
1603 return std::max<uint64_t>(getFixedSize(), 8);
1607cir::VectorType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
1608 ::mlir::DataLayoutEntryListRef params)
const {
1610 return llvm::PowerOf2Ceil(llvm::divideCeil(
1611 dataLayout.getTypeSizeInBits(*this).getKnownMinValue(),
uint64_t{8}));
1614mlir::LogicalResult cir::VectorType::verify(
1615 llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
1616 mlir::Type elementType, llvm::ElementCount size) {
1618 return emitError() <<
"the number of vector elements must be non-zero";
1622mlir::Type cir::VectorType::parse(::mlir::AsmParser &odsParser) {
1624 llvm::SMLoc odsLoc = odsParser.getCurrentLocation();
1625 mlir::Builder odsBuilder(odsParser.getContext());
1626 mlir::FailureOr<::mlir::Type> elementType;
1627 mlir::FailureOr<uint64_t> size;
1628 bool isScalable =
false;
1631 if (odsParser.parseLess())
1635 if (odsParser.parseOptionalLSquare().succeeded())
1639 size = mlir::FieldParser<uint64_t>::parse(odsParser);
1640 if (mlir::failed(size)) {
1641 odsParser.emitError(odsParser.getCurrentLocation(),
1642 "failed to parse CIR_VectorType parameter 'size' which "
1643 "is to be a `uint64_t`");
1649 if (isScalable && odsParser.parseRSquare().failed()) {
1650 odsParser.emitError(odsParser.getCurrentLocation(),
1651 "missing closing `]` for scalable dim size");
1656 if (odsParser.parseKeyword(
"x"))
1660 elementType = mlir::FieldParser<::mlir::Type>::parse(odsParser);
1661 if (mlir::failed(elementType)) {
1662 odsParser.emitError(odsParser.getCurrentLocation(),
1663 "failed to parse CIR_VectorType parameter "
1664 "'elementType' which is to be a `mlir::Type`");
1669 if (odsParser.parseGreater())
1671 return odsParser.getChecked<VectorType>(
1672 odsLoc, odsParser.getContext(), mlir::Type((*elementType)),
1673 llvm::ElementCount::get(*size, isScalable));
1676void cir::VectorType::print(mlir::AsmPrinter &odsPrinter)
const {
1677 mlir::Builder odsBuilder(getContext());
1682 odsPrinter.printStrippedAttrOrType(getSize().getKnownMinValue());
1685 odsPrinter <<
' ' <<
"x";
1687 odsPrinter.printStrippedAttrOrType(getElementType());
1695llvm::TypeSize cir::MatrixType::getTypeSizeInBits(
1696 const ::mlir::DataLayout &dataLayout,
1697 ::mlir::DataLayoutEntryListRef params)
const {
1698 return llvm::TypeSize::getFixed(
1699 getNumRows() * getNumColumns() *
1700 dataLayout.getTypeSizeInBits(getElementType()));
1704cir::MatrixType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
1705 ::mlir::DataLayoutEntryListRef params)
const {
1706 return dataLayout.getTypeABIAlignment(getElementType());
1709mlir::LogicalResult cir::MatrixType::verify(
1710 llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
1713 return emitError() <<
"the number of matrix rows must be non-zero";
1715 return emitError() <<
"the number of matrix columns must be non-zero";
1724 mlir::ptr::MemorySpaceAttrInterface memorySpace) {
1725 return mlir::isa<cir::LangAddressSpaceAttr, cir::TargetAddressSpaceAttr>(
1732 case LangAS::Default:
1733 return LangAddressSpace::Default;
1734 case LangAS::opencl_global:
1735 case LangAS::sycl_global:
1736 return LangAddressSpace::OffloadGlobal;
1737 case LangAS::opencl_local:
1738 case LangAS::cuda_shared:
1739 case LangAS::sycl_local:
1742 return LangAddressSpace::OffloadLocal;
1743 case LangAS::cuda_device:
1744 return LangAddressSpace::OffloadGlobal;
1745 case LangAS::opencl_constant:
1746 case LangAS::cuda_constant:
1747 case LangAS::sycl_constant:
1748 return LangAddressSpace::OffloadConstant;
1749 case LangAS::opencl_private:
1750 case LangAS::sycl_private:
1751 return LangAddressSpace::OffloadPrivate;
1752 case LangAS::opencl_generic:
1753 case LangAS::sycl_generic:
1754 return LangAddressSpace::OffloadGeneric;
1755 case LangAS::opencl_global_device:
1756 case LangAS::sycl_global_device:
1757 return LangAddressSpace::OffloadGlobalDevice;
1758 case LangAS::opencl_global_host:
1759 case LangAS::sycl_global_host:
1760 return LangAddressSpace::OffloadGlobalHost;
1761 case LangAS::ptr32_sptr:
1762 case LangAS::ptr32_uptr:
1764 case LangAS::hlsl_groupshared:
1765 case LangAS::wasm_funcref:
1766 llvm_unreachable(
"NYI");
1768 llvm_unreachable(
"unknown/unsupported clang language address space");
1773 mlir::ptr::MemorySpaceAttrInterface &attr) {
1775 llvm::SMLoc loc = p.getCurrentLocation();
1779 if (p.parseOptionalKeyword(
"target_address_space").succeeded()) {
1781 if (p.parseLParen())
1782 return p.emitError(loc,
"expected '(' after 'target_address_space'");
1784 if (p.parseInteger(val))
1785 return p.emitError(loc,
"expected target address space value");
1787 if (p.parseRParen())
1788 return p.emitError(loc,
"expected ')'");
1790 attr = cir::TargetAddressSpaceAttr::get(p.getContext(), val);
1791 return mlir::success();
1795 if (p.parseOptionalKeyword(
"lang_address_space").succeeded()) {
1796 if (p.parseLParen())
1797 return p.emitError(loc,
"expected '(' after 'lang_address_space'");
1799 mlir::FailureOr<cir::LangAddressSpace> result =
1800 mlir::FieldParser<cir::LangAddressSpace>::parse(p);
1801 if (mlir::failed(result))
1802 return mlir::failure();
1804 if (p.parseRParen())
1805 return p.emitError(loc,
"expected ')'");
1807 attr = cir::LangAddressSpaceAttr::get(p.getContext(), result.value());
1808 return mlir::success();
1811 llvm::StringRef keyword;
1812 if (p.parseOptionalKeyword(&keyword).succeeded())
1813 return p.emitError(loc,
"unknown address space specifier '")
1814 << keyword <<
"'; expected 'target_address_space' or "
1815 <<
"'lang_address_space'";
1817 return mlir::success();
1821 mlir::ptr::MemorySpaceAttrInterface attr) {
1825 if (
auto language = dyn_cast<cir::LangAddressSpaceAttr>(attr)) {
1826 p <<
"lang_address_space("
1827 << cir::stringifyLangAddressSpace(language.getValue()) <<
')';
1831 if (
auto target = dyn_cast<cir::TargetAddressSpaceAttr>(attr)) {
1832 p <<
"target_address_space(" << target.getValue() <<
')';
1836 llvm_unreachable(
"unexpected address-space attribute kind");
1839mlir::OptionalParseResult
1841 mlir::ptr::MemorySpaceAttrInterface &attr) {
1843 mlir::SMLoc loc = p.getCurrentLocation();
1845 return p.emitError(loc,
"failed to parse Address Space Value for GlobalOp");
1846 return mlir::success();
1850 mlir::ptr::MemorySpaceAttrInterface attr) {
1855 mlir::ptr::MemorySpaceAttrInterface addrSpace) {
1857 mlir::dyn_cast_if_present<cir::LangAddressSpaceAttr>(addrSpace))
1858 if (langAS.getValue() == cir::LangAddressSpace::Default)
1863mlir::ptr::MemorySpaceAttrInterface
1867 if (langAS == LangAS::Default)
1868 return cir::LangAddressSpaceAttr::get(&ctx, cir::LangAddressSpace::Default);
1872 return cir::TargetAddressSpaceAttr::get(&ctx, targetAS);
1885 return expected == cirAS;
1892mlir::LogicalResult cir::PointerType::verify(
1893 llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
1894 mlir::Type pointee, mlir::ptr::MemorySpaceAttrInterface addrSpace) {
1897 return emitError() <<
"unsupported address space attribute; expected "
1898 "'target_address_space' or 'lang_address_space'";
1909void CIRDialect::registerTypes() {
1912#define GET_TYPEDEF_LIST
1913#include "clang/CIR/Dialect/IR/CIROpsTypes.cpp.inc"
Provides definitions for the various language-specific address spaces.
mlir::ParseResult parseMemorySpace(mlir::AsmParser &p, mlir::ptr::MemorySpaceAttrInterface &attr)
static mlir::LogicalResult verifyRecordMemberTypes(function_ref< mlir::InFlightDiagnostic()> emitError, llvm::ArrayRef< mlir::Type > members)
Members must be CIR types other than void, which has no layout.
static const llvm::StringRef memberKindMarks[]
The keywords that spell a member kind.
static void printRecordBody(mlir::AsmPrinter &printer, RecordTy self, mlir::StringAttr name, bool hasClassPrefix, bool isPacked, bool isIncomplete, llvm::ArrayRef< mlir::Type > members, mlir::Type padding, llvm::ArrayRef< RecordMemberKind > memberKinds)
Print a complete CIR record body: '<' ['class '] [name] ['packed '] body '>' where body is "incomplet...
static mlir::LogicalResult verifyRecordMemberKinds(function_ref< mlir::InFlightDiagnostic()> emitError, size_t numMembers, llvm::ArrayRef< RecordMemberKind > memberKinds)
An incomplete record has no members, so a kind for one is caught by the same check.
static mlir::ParseResult parseFuncTypeParams(mlir::AsmParser &p, llvm::SmallVector< mlir::Type > ¶ms, bool &isVarArg)
void printMemorySpace(mlir::AsmPrinter &printer, mlir::ptr::MemorySpaceAttrInterface attr)
static mlir::Type getMethodLayoutType(mlir::MLIRContext *ctx)
static void printFuncTypeParams(mlir::AsmPrinter &p, mlir::ArrayRef< mlir::Type > params, bool isVarArg)
static mlir::ParseResult parseRecordBody(mlir::AsmParser &parser, bool &incomplete, llvm::SmallVector< mlir::Type > &members, llvm::SmallVectorImpl< RecordMemberKind > &memberKinds)
Parse "incomplete" or "{mark type, mark type, ...}", writing results into incomplete,...
static std::optional< RecordMemberKind > parseMemberKind(mlir::AsmParser &parser)
static mlir::Type getDataMemberLayoutType(const mlir::DataLayout &dataLayout, mlir::MLIRContext *ctx)
static LiveVariablesImpl & getImpl(void *x)
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
bool isLayoutIdentical(const RecordType &other)
bool isABIConvertedRecord() const
bool isIncomplete() const
bool isEmptyForABI() const
Whether no member holds data.
std::string getPrefixedName() const
llvm::ArrayRef< mlir::Type > getMembers() const
void removeABIConversionNamePrefix()
static llvm::SmallVector< RecordMemberKind > getAllDataKinds(llvm::ArrayRef< mlir::Type > members)
One Data kind per member.
mlir::StringAttr getName() const
void complete(llvm::ArrayRef< mlir::Type > members, bool packed, mlir::Type padding, llvm::ArrayRef< RecordMemberKind > memberKinds)
padding is union-only.
mlir::StringAttr getABIConvertedName() const
std::string getKindAsStr() const
llvm::ArrayRef< RecordMemberKind > getMemberKinds() const
uint64_t getElementOffset(const mlir::DataLayout &dataLayout, unsigned idx) const
bool isMatchingAddressSpace(mlir::ptr::MemorySpaceAttrInterface cirAS, clang::LangAS as)
cir::LangAddressSpace toCIRLangAddressSpace(clang::LangAS langAS)
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.
bool isValidFundamentalIntWidth(unsigned width)
cir::FPTypeInterface getFloatingPointType(const llvm::fltSemantics &sem, mlir::MLIRContext *ctx)
Returns the CIR floating-point type for the given semantics, or a null type if CIR has no type for it...
mlir::ptr::MemorySpaceAttrInterface toCIRAddressSpaceAttr(mlir::MLIRContext &ctx, clang::LangAS langAS)
Convert an AST LangAS to the appropriate CIR address space attribute interface.
mlir::OptionalParseResult parseGlobalMemorySpace(mlir::AsmParser &p, mlir::ptr::MemorySpaceAttrInterface &attr)
bool anyMemberHoldsDataForABI(llvm::ArrayRef< RecordMemberKind > kinds)
Whether any member holds data for argument passing on its mark alone.
mlir::ptr::MemorySpaceAttrInterface normalizeDefaultAddressSpace(mlir::ptr::MemorySpaceAttrInterface addrSpace)
Normalize LangAddressSpace::Default to null (empty attribute).
bool isSized(mlir::Type ty)
Returns true if the type is a CIR sized type.
void printGlobalMemorySpace(mlir::AsmPrinter &printer, cir::GlobalOp op, mlir::ptr::MemorySpaceAttrInterface attr)
bool isSupportedCIRMemorySpaceAttr(mlir::ptr::MemorySpaceAttrInterface memorySpace)
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
StringRef getName(const HeaderType T)
bool isTargetAddressSpace(LangAS AS)
unsigned toTargetAddressSpace(LangAS AS)
LangAS
Defines the address space values used by the address space qualifier of QualType.
float __ovld __cnfn sign(float)
Returns 1.0 if x > 0, -0.0 if x = -0.0, +0.0 if x = +0.0, or -1.0 if x < 0.
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
static bool unsizedTypes()
static bool dataLayoutPtrHandlingBasedOnLangAS()
static bool astRecordDeclAttr()