15#include "mlir/IR/DialectImplementation.h"
19#include "llvm/ADT/TypeSwitch.h"
25 if (
auto sizedTy = mlir::dyn_cast<cir::SizedTypeInterface>(ty))
26 return sizedTy.isSized();
35static mlir::ParseResult
39 mlir::ArrayRef<mlir::Type> params,
48#include "clang/CIR/Dialect/IR/CIRTypeConstraints.cpp.inc"
52#define GET_TYPEDEF_CLASSES
53#include "clang/CIR/Dialect/IR/CIROpsTypes.cpp.inc"
62Type CIRDialect::parseType(DialectAsmParser &parser)
const {
63 llvm::SMLoc typeLoc = parser.getCurrentLocation();
64 llvm::StringRef mnemonic;
68 OptionalParseResult parseResult =
69 generatedTypeParser(parser, &mnemonic, genType);
70 if (parseResult.has_value())
74 return StringSwitch<function_ref<
Type()>>(mnemonic)
75 .Case(
"record", [&] {
return RecordType::parse(parser); })
77 parser.emitError(typeLoc) <<
"unknown CIR type: " << mnemonic;
82void CIRDialect::printType(Type type, DialectAsmPrinter &os)
const {
84 if (generatedTypePrinter(type, os).succeeded())
88 llvm::report_fatal_error(
"printer is missing a handler for this type");
95Type RecordType::parse(mlir::AsmParser &parser) {
96 FailureOr<AsmParser::CyclicParseReset> cyclicParseGuard;
97 const llvm::SMLoc loc = parser.getCurrentLocation();
98 const mlir::Location eLoc = parser.getEncodedSourceLoc(loc);
102 mlir::MLIRContext *context = parser.getContext();
104 if (parser.parseLess())
109 if (parser.parseOptionalKeyword(
"struct").succeeded())
110 kind = RecordKind::Struct;
111 else if (parser.parseOptionalKeyword(
"union").succeeded())
112 kind = RecordKind::Union;
113 else if (parser.parseOptionalKeyword(
"class").succeeded())
114 kind = RecordKind::Class;
116 parser.emitError(loc,
"unknown record type");
120 mlir::StringAttr
name;
121 parser.parseOptionalAttribute(name);
124 if (name && parser.parseOptionalGreater().succeeded()) {
125 RecordType
type = getChecked(eLoc, context, name, kind);
126 if (succeeded(parser.tryStartCyclicParse(type))) {
127 parser.emitError(loc,
"invalid self-reference within record");
135 RecordType
type = getChecked(eLoc, context, name, kind);
136 cyclicParseGuard = parser.tryStartCyclicParse(type);
137 if (failed(cyclicParseGuard)) {
138 parser.emitError(loc,
"record already defined");
143 if (parser.parseOptionalKeyword(
"packed").succeeded())
146 if (parser.parseOptionalKeyword(
"padded").succeeded())
150 bool incomplete =
true;
152 if (parser.parseOptionalKeyword(
"incomplete").failed()) {
154 const auto delimiter = AsmParser::Delimiter::Braces;
155 const auto parseElementFn = [&parser, &members]() {
156 return parser.parseType(members.emplace_back());
158 if (parser.parseCommaSeparatedList(delimiter, parseElementFn).failed())
162 if (parser.parseGreater())
166 ArrayRef<mlir::Type> membersRef(members);
167 mlir::Type
type = {};
168 if (name && incomplete) {
169 type = getChecked(eLoc, context, name, kind);
170 }
else if (!name && !incomplete) {
171 type = getChecked(eLoc, context, membersRef, packed, padded, kind);
172 }
else if (!incomplete) {
173 type = getChecked(eLoc, context, membersRef, name, packed, padded, kind);
176 if (mlir::cast<RecordType>(type).isIncomplete())
177 mlir::cast<RecordType>(type).complete(membersRef, packed, padded);
180 parser.emitError(loc,
"anonymous records must be complete");
187void RecordType::print(mlir::AsmPrinter &printer)
const {
188 FailureOr<AsmPrinter::CyclicPrintReset> cyclicPrintGuard;
192 case RecordKind::Struct:
193 printer <<
"struct ";
195 case RecordKind::Union:
198 case RecordKind::Class:
207 cyclicPrintGuard = printer.tryStartCyclicPrint(*
this);
208 if (failed(cyclicPrintGuard)) {
217 printer <<
"packed ";
220 printer <<
"padded ";
222 if (isIncomplete()) {
223 printer <<
"incomplete";
226 llvm::interleaveComma(getMembers(), printer);
234RecordType::verify(function_ref<mlir::InFlightDiagnostic()> emitError,
236 bool incomplete,
bool packed,
bool padded,
237 RecordType::RecordKind kind) {
238 if (name &&
name.getValue().empty())
239 return emitError() <<
"identified records cannot have an empty name";
240 return mlir::success();
247bool RecordType::isIncomplete()
const {
return getImpl()->incomplete; }
249mlir::StringAttr RecordType::getName()
const {
return getImpl()->name; }
251bool RecordType::getIncomplete()
const {
return getImpl()->incomplete; }
253bool RecordType::getPacked()
const {
return getImpl()->packed; }
255bool RecordType::getPadded()
const {
return getImpl()->padded; }
257cir::RecordType::RecordKind RecordType::getKind()
const {
261void RecordType::complete(ArrayRef<Type> members,
bool packed,
bool padded) {
263 if (mutate(members, packed, padded).failed())
264 llvm_unreachable(
"failed to complete record");
270Type RecordType::getLargestMember(const ::mlir::DataLayout &dataLayout)
const {
271 assert(isUnion() &&
"Only call getLargestMember on unions");
275 return *std::max_element(
276 members.begin(), getPadded() ? members.end() - 1 : members.end(),
277 [&](Type lhs, Type rhs) {
278 return dataLayout.getTypeABIAlignment(lhs) <
279 dataLayout.getTypeABIAlignment(rhs) ||
280 (dataLayout.getTypeABIAlignment(lhs) ==
281 dataLayout.getTypeABIAlignment(rhs) &&
282 dataLayout.getTypeSize(lhs) < dataLayout.getTypeSize(rhs));
291RecordType::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
292 mlir::DataLayoutEntryListRef params)
const {
294 return dataLayout.getTypeSize(getLargestMember(dataLayout));
296 unsigned recordSize = computeStructSize(dataLayout);
297 return llvm::TypeSize::getFixed(recordSize * 8);
301RecordType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
302 ::mlir::DataLayoutEntryListRef params)
const {
304 return dataLayout.getTypeABIAlignment(getLargestMember(dataLayout));
309 return computeStructAlignment(dataLayout);
313RecordType::computeStructSize(
const mlir::DataLayout &dataLayout)
const {
314 assert(isComplete() &&
"Cannot get layout of incomplete records");
317 unsigned recordSize = 0;
320 for (mlir::Type ty : getMembers()) {
324 (getPacked() ? 1 : dataLayout.getTypeABIAlignment(ty));
328 recordSize = llvm::alignTo(recordSize, tyAlign);
329 recordSize += dataLayout.getTypeSize(ty);
333 recordAlignment = std::max(tyAlign, recordAlignment);
338 recordSize = llvm::alignTo(recordSize, recordAlignment);
347RecordType::computeStructAlignment(
const mlir::DataLayout &dataLayout)
const {
348 assert(isComplete() &&
"Cannot get layout of incomplete records");
352 for (mlir::Type ty : getMembers())
354 std::max(dataLayout.getTypeABIAlignment(ty), recordAlignment);
356 return recordAlignment;
359uint64_t RecordType::getElementOffset(const ::mlir::DataLayout &dataLayout,
360 unsigned idx)
const {
361 assert(idx < getMembers().size() &&
"access not valid");
364 if (isUnion() || idx == 0)
367 assert(isComplete() &&
"Cannot get layout of incomplete records");
368 assert(idx < getNumElements());
374 llvm::make_range(members.begin(), std::next(members.begin(), idx))) {
376 const llvm::Align tyAlign =
377 llvm::Align(getPacked() ? 1 : dataLayout.getTypeABIAlignment(ty));
380 offset = llvm::alignTo(offset, tyAlign);
383 offset += dataLayout.getTypeSize(ty);
388 const llvm::Align tyAlign = llvm::Align(
389 getPacked() ? 1 : dataLayout.getTypeABIAlignment(members[idx]));
390 offset = llvm::alignTo(offset, tyAlign);
399Type IntType::parse(mlir::AsmParser &parser) {
400 mlir::MLIRContext *context = parser.getBuilder().getContext();
401 llvm::SMLoc loc = parser.getCurrentLocation();
405 if (parser.parseLess())
409 llvm::StringRef
sign;
410 if (parser.parseKeyword(&
sign))
414 else if (
sign ==
"u")
417 parser.emitError(loc,
"expected 's' or 'u'");
421 if (parser.parseComma())
425 if (parser.parseInteger(width))
427 if (width < IntType::minBitwidth() || width > IntType::maxBitwidth()) {
428 parser.emitError(loc,
"expected integer width to be from ")
429 << IntType::minBitwidth() <<
" up to " << IntType::maxBitwidth();
433 if (parser.parseGreater())
436 return IntType::get(context, width, isSigned);
439void IntType::print(mlir::AsmPrinter &printer)
const {
440 char sign = isSigned() ?
's' :
'u';
441 printer <<
'<' <<
sign <<
", " << getWidth() <<
'>';
445IntType::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
446 mlir::DataLayoutEntryListRef params)
const {
447 return llvm::TypeSize::getFixed(getWidth());
450uint64_t IntType::getABIAlignment(
const mlir::DataLayout &dataLayout,
451 mlir::DataLayoutEntryListRef params)
const {
452 return (uint64_t)(getWidth() / 8);
456IntType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
457 unsigned width,
bool isSigned) {
458 if (width < IntType::minBitwidth() || width > IntType::maxBitwidth())
459 return emitError() <<
"IntType only supports widths from "
460 << IntType::minBitwidth() <<
" up to "
461 << IntType::maxBitwidth();
462 return mlir::success();
466 return width == 8 || width == 16 || width == 32 || width == 64;
473const llvm::fltSemantics &SingleType::getFloatSemantics()
const {
474 return llvm::APFloat::IEEEsingle();
478SingleType::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
479 mlir::DataLayoutEntryListRef params)
const {
480 return llvm::TypeSize::getFixed(getWidth());
484SingleType::getABIAlignment(
const mlir::DataLayout &dataLayout,
485 mlir::DataLayoutEntryListRef params)
const {
486 return (uint64_t)(getWidth() / 8);
489const llvm::fltSemantics &DoubleType::getFloatSemantics()
const {
490 return llvm::APFloat::IEEEdouble();
494DoubleType::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
495 mlir::DataLayoutEntryListRef params)
const {
496 return llvm::TypeSize::getFixed(getWidth());
500DoubleType::getABIAlignment(
const mlir::DataLayout &dataLayout,
501 mlir::DataLayoutEntryListRef params)
const {
502 return (uint64_t)(getWidth() / 8);
505const llvm::fltSemantics &FP16Type::getFloatSemantics()
const {
506 return llvm::APFloat::IEEEhalf();
510FP16Type::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
511 mlir::DataLayoutEntryListRef params)
const {
512 return llvm::TypeSize::getFixed(getWidth());
515uint64_t FP16Type::getABIAlignment(
const mlir::DataLayout &dataLayout,
516 mlir::DataLayoutEntryListRef params)
const {
517 return (uint64_t)(getWidth() / 8);
520const llvm::fltSemantics &BF16Type::getFloatSemantics()
const {
521 return llvm::APFloat::BFloat();
525BF16Type::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
526 mlir::DataLayoutEntryListRef params)
const {
527 return llvm::TypeSize::getFixed(getWidth());
530uint64_t BF16Type::getABIAlignment(
const mlir::DataLayout &dataLayout,
531 mlir::DataLayoutEntryListRef params)
const {
532 return (uint64_t)(getWidth() / 8);
535const llvm::fltSemantics &FP80Type::getFloatSemantics()
const {
536 return llvm::APFloat::x87DoubleExtended();
540FP80Type::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
541 mlir::DataLayoutEntryListRef params)
const {
543 return llvm::TypeSize::getFixed(128);
546uint64_t FP80Type::getABIAlignment(
const mlir::DataLayout &dataLayout,
547 mlir::DataLayoutEntryListRef params)
const {
551const llvm::fltSemantics &FP128Type::getFloatSemantics()
const {
552 return llvm::APFloat::IEEEquad();
556FP128Type::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
557 mlir::DataLayoutEntryListRef params)
const {
558 return llvm::TypeSize::getFixed(getWidth());
561uint64_t FP128Type::getABIAlignment(
const mlir::DataLayout &dataLayout,
562 mlir::DataLayoutEntryListRef params)
const {
566const llvm::fltSemantics &LongDoubleType::getFloatSemantics()
const {
567 return mlir::cast<cir::FPTypeInterface>(getUnderlying()).getFloatSemantics();
571LongDoubleType::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
572 mlir::DataLayoutEntryListRef params)
const {
573 return mlir::cast<mlir::DataLayoutTypeInterface>(getUnderlying())
574 .getTypeSizeInBits(dataLayout, params);
578LongDoubleType::getABIAlignment(
const mlir::DataLayout &dataLayout,
579 mlir::DataLayoutEntryListRef params)
const {
580 return mlir::cast<mlir::DataLayoutTypeInterface>(getUnderlying())
581 .getABIAlignment(dataLayout, params);
589cir::ComplexType::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
590 mlir::DataLayoutEntryListRef params)
const {
596 return dataLayout.getTypeSizeInBits(getElementType()) * 2;
600cir::ComplexType::getABIAlignment(
const mlir::DataLayout &dataLayout,
601 mlir::DataLayoutEntryListRef params)
const {
607 return dataLayout.getTypeABIAlignment(getElementType());
610FuncType FuncType::clone(TypeRange inputs, TypeRange results)
const {
611 assert(results.size() == 1 &&
"expected exactly one result type");
612 return get(llvm::to_vector(inputs), results[0], isVarArg());
616static mlir::ParseResult
620 return p.parseCommaSeparatedList(
621 AsmParser::Delimiter::Paren, [&]() -> mlir::ParseResult {
623 return p.emitError(p.getCurrentLocation(),
624 "variadic `...` must be the last parameter");
625 if (succeeded(p.parseOptionalEllipsis())) {
630 if (failed(p.parseType(type)))
632 params.push_back(type);
638 mlir::ArrayRef<mlir::Type> params,
641 llvm::interleaveComma(params, p,
642 [&p](mlir::Type type) { p.printType(type); });
653mlir::Type FuncType::getReturnType()
const {
655 return cir::VoidType::get(getContext());
656 return getOptionalReturnType();
668 return getImpl()->optionalReturnType;
672bool FuncType::hasVoidReturn()
const {
return !getOptionalReturnType(); }
675FuncType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
678 if (mlir::isa_and_nonnull<cir::VoidType>(returnType))
680 <<
"!cir.func cannot have an explicit 'void' return type";
681 return mlir::success();
689BoolType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
690 ::mlir::DataLayoutEntryListRef params)
const {
691 return llvm::TypeSize::getFixed(8);
695BoolType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
696 ::mlir::DataLayoutEntryListRef params)
const {
705VPtrType::getTypeSizeInBits(
const mlir::DataLayout &dataLayout,
706 mlir::DataLayoutEntryListRef params)
const {
708 return llvm::TypeSize::getFixed(64);
711uint64_t VPtrType::getABIAlignment(
const mlir::DataLayout &dataLayout,
712 mlir::DataLayoutEntryListRef params)
const {
722ArrayType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
723 ::mlir::DataLayoutEntryListRef params)
const {
724 return getSize() * dataLayout.getTypeSizeInBits(getElementType());
728ArrayType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
729 ::mlir::DataLayoutEntryListRef params)
const {
730 return dataLayout.getTypeABIAlignment(getElementType());
737llvm::TypeSize cir::VectorType::getTypeSizeInBits(
738 const ::mlir::DataLayout &dataLayout,
739 ::mlir::DataLayoutEntryListRef params)
const {
740 return llvm::TypeSize::getFixed(
741 getSize() * dataLayout.getTypeSizeInBits(getElementType()));
745cir::VectorType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
746 ::mlir::DataLayoutEntryListRef params)
const {
747 return llvm::NextPowerOf2(dataLayout.getTypeSizeInBits(*
this));
750mlir::LogicalResult cir::VectorType::verify(
751 llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
752 mlir::Type elementType, uint64_t size) {
754 return emitError() <<
"the number of vector elements must be non-zero";
763PointerType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
764 ::mlir::DataLayoutEntryListRef params)
const {
766 return llvm::TypeSize::getFixed(64);
770PointerType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
771 ::mlir::DataLayoutEntryListRef params)
const {
777PointerType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
778 mlir::Type pointee) {
780 return mlir::success();
787void CIRDialect::registerTypes() {
790#define GET_TYPEDEF_LIST
791#include "clang/CIR/Dialect/IR/CIROpsTypes.cpp.inc"
static mlir::ParseResult parseFuncTypeParams(mlir::AsmParser &p, llvm::SmallVector< mlir::Type > ¶ms, bool &isVarArg)
static void printFuncTypeParams(mlir::AsmPrinter &p, mlir::ArrayRef< mlir::Type > params, bool isVarArg)
static Decl::Kind getKind(const Decl *D)
static LiveVariablesImpl & getImpl(void *x)
bool isValidFundamentalIntWidth(unsigned width)
bool isSized(mlir::Type ty)
Returns true if the type is a CIR sized type.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
unsigned kind
All of the diagnostics that can be emitted by the frontend.
StringRef getName(const HeaderType T)
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.
static bool unsizedTypes()
static bool astRecordDeclAttr()