1//===- CIRTypes.cpp - MLIR CIR Types --------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the types in the CIR dialect.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/CIR/Dialect/IR/CIRTypes.h"
14
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"
20#include "clang/Basic/AddressSpaces.h"
21#include "clang/CIR/Dialect/IR/CIRAttrs.h"
22#include "clang/CIR/Dialect/IR/CIRDialect.h"
23#include "clang/CIR/Dialect/IR/CIROpsEnums.h"
24#include "clang/CIR/Dialect/IR/CIRTypesDetails.h"
25#include "clang/CIR/MissingFeatures.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"
30
31#include "CIROpDefinitions.h"
32
33//===----------------------------------------------------------------------===//
34// CIR Helpers
35//===----------------------------------------------------------------------===//
36bool cir::isSized(mlir::Type ty) {
37 if (auto sizedTy = mlir::dyn_cast<cir::SizedTypeInterface>(Val&: ty))
38 return sizedTy.isSized();
39 assert(!cir::MissingFeatures::unsizedTypes());
40 return false;
41}
42
43cir::FPTypeInterface cir::getFloatingPointType(const llvm::fltSemantics &sem,
44 mlir::MLIRContext *ctx) {
45 switch (llvm::APFloat::SemanticsToEnum(Sem: 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);
58 default:
59 // CIR has no type for the remaining semantics (PPCDoubleDouble, the
60 // Float8 formats). Return null and let the caller report it.
61 return {};
62 }
63}
64
65//===----------------------------------------------------------------------===//
66// CIR Custom Parser/Printer Signatures
67//===----------------------------------------------------------------------===//
68
69static mlir::ParseResult
70parseFuncTypeParams(mlir::AsmParser &p, llvm::SmallVector<mlir::Type> &params,
71 bool &isVarArg);
72static void printFuncTypeParams(mlir::AsmPrinter &p,
73 mlir::ArrayRef<mlir::Type> params,
74 bool isVarArg);
75//===----------------------------------------------------------------------===//
76// CIR Custom Parser/Printer Signatures
77//===----------------------------------------------------------------------===//
78
79static mlir::ParseResult
80parseFuncTypeParams(mlir::AsmParser &p, llvm::SmallVector<mlir::Type> &params,
81 bool &isVarArg);
82
83static void printFuncTypeParams(mlir::AsmPrinter &p,
84 mlir::ArrayRef<mlir::Type> params,
85 bool isVarArg);
86
87//===----------------------------------------------------------------------===//
88// AddressSpace
89//===----------------------------------------------------------------------===//
90
91// Spells the address space of `!cir.ptr` and `cir.global` as either
92// `lang_address_space(x)` or `target_address_space(n)`.
93mlir::ParseResult parseMemorySpace(mlir::AsmParser &p,
94 mlir::ptr::MemorySpaceAttrInterface &attr);
95
96void printMemorySpace(mlir::AsmPrinter &printer,
97 mlir::ptr::MemorySpaceAttrInterface attr);
98
99//===----------------------------------------------------------------------===//
100// Get autogenerated stuff
101//===----------------------------------------------------------------------===//
102
103namespace cir {
104
105#include "clang/CIR/Dialect/IR/CIRTypeConstraints.cpp.inc"
106
107} // namespace cir
108
109#define GET_TYPEDEF_CLASSES
110#include "clang/CIR/Dialect/IR/CIROpsTypes.cpp.inc"
111
112using namespace mlir;
113using namespace cir;
114
115//===----------------------------------------------------------------------===//
116// General CIR parsing / printing
117//===----------------------------------------------------------------------===//
118
119Type CIRDialect::parseType(DialectAsmParser &parser) const {
120 llvm::SMLoc typeLoc = parser.getCurrentLocation();
121 llvm::StringRef mnemonic;
122 Type genType;
123
124 // Try to parse as a tablegen'd type.
125 OptionalParseResult parseResult =
126 generatedTypeParser(parser, mnemonic: &mnemonic, value&: genType);
127 if (parseResult.has_value())
128 return genType;
129
130 // All CIR types are now tablegen'd; nothing left to dispatch here.
131 parser.emitError(loc: typeLoc) << "unknown CIR type: " << mnemonic;
132 return Type();
133}
134
135void CIRDialect::printType(Type type, DialectAsmPrinter &os) const {
136 // Try to print as a tablegen'd type.
137 if (generatedTypePrinter(def: type, printer&: os).succeeded())
138 return;
139
140 // TODO(CIR) Attempt to print as a raw C++ type.
141 llvm::report_fatal_error(reason: "printer is missing a handler for this type");
142}
143
144//===----------------------------------------------------------------------===//
145// StructType
146//===----------------------------------------------------------------------===//
147
148// Shared helpers for StructType and UnionType parse/print.
149
150llvm::SmallVector<RecordMemberKind>
151RecordType::getAllDataKinds(llvm::ArrayRef<mlir::Type> members) {
152 return llvm::SmallVector<RecordMemberKind>(members.size(),
153 RecordMemberKind::Data);
154}
155
156/// An incomplete record has no members, so a kind for one is caught by the
157/// same check.
158static mlir::LogicalResult
159verifyRecordMemberKinds(function_ref<mlir::InFlightDiagnostic()> emitError,
160 size_t numMembers,
161 llvm::ArrayRef<RecordMemberKind> memberKinds) {
162 if (memberKinds.size() != numMembers)
163 return emitError() << "expected " << numMembers << " member kinds, got "
164 << memberKinds.size();
165 return mlir::success();
166}
167
168/// Members must be CIR types other than void, which has no layout.
169static mlir::LogicalResult
170verifyRecordMemberTypes(function_ref<mlir::InFlightDiagnostic()> emitError,
171 llvm::ArrayRef<mlir::Type> members) {
172 for (auto [idx, memberTy] : llvm::enumerate(First&: members)) {
173 if (!cir::isCIRType(type: memberTy))
174 return emitError() << "record member at index " << idx
175 << " must be a CIR type, got " << memberTy;
176 if (mlir::isa<cir::VoidType>(Val: memberTy))
177 return emitError() << "record member at index " << idx
178 << " cannot be void";
179 }
180 return mlir::success();
181}
182
183/// The keywords that spell a member kind. A union's tail-padding slot probes
184/// for one of these to reject it, since that slot is not a member.
185static const llvm::StringRef memberKindMarks[] = {"data", "pad", "empty",
186 "bitfield"};
187
188static std::optional<RecordMemberKind>
189parseMemberKind(mlir::AsmParser &parser) {
190 llvm::StringRef keyword;
191 const llvm::SMLoc loc = parser.getCurrentLocation();
192 if (parser.parseKeyword(keyword: &keyword).failed())
193 return std::nullopt;
194 std::optional<RecordMemberKind> kind = symbolizeRecordMemberKind(keyword);
195 if (!kind)
196 parser.emitError(loc, message: "expected a record member kind");
197 return kind;
198}
199
200/// Parse "incomplete" or "{mark type, mark type, ...}", writing results into
201/// \p incomplete, \p members and \p memberKinds. Returns failure if member
202/// parsing fails.
203static mlir::ParseResult
204parseRecordBody(mlir::AsmParser &parser, bool &incomplete,
205 llvm::SmallVector<mlir::Type> &members,
206 llvm::SmallVectorImpl<RecordMemberKind> &memberKinds) {
207 assert(incomplete && "caller must pre-initialize incomplete to true");
208 if (parser.parseOptionalKeyword(keyword: "incomplete").succeeded())
209 return mlir::success();
210 incomplete = false;
211 return parser.parseCommaSeparatedList(
212 delimiter: AsmParser::Delimiter::Braces,
213 parseElementFn: [&parser, &members, &memberKinds]() -> mlir::ParseResult {
214 std::optional<RecordMemberKind> kind = parseMemberKind(parser);
215 if (!kind)
216 return mlir::failure();
217 memberKinds.push_back(Elt: *kind);
218 return parser.parseType(result&: members.emplace_back());
219 });
220}
221
222/// Print a complete CIR record body:
223/// '<' ['class '] [name] ['packed '] body '>'
224/// where body is "incomplete" or "{[mark] members}[, padding = {type}]".
225/// RecordTy must be a mutable MLIR type (StructType or UnionType).
226template <typename RecordTy>
227static void
228printRecordBody(mlir::AsmPrinter &printer, RecordTy self, mlir::StringAttr name,
229 bool hasClassPrefix, bool isPacked, bool isIncomplete,
230 llvm::ArrayRef<mlir::Type> members, mlir::Type padding,
231 llvm::ArrayRef<RecordMemberKind> memberKinds) {
232 printer << '<';
233 if (hasClassPrefix)
234 printer << "class ";
235 if (name)
236 printer << name;
237
238 FailureOr<AsmPrinter::CyclicPrintReset> cyclicPrintGuard =
239 printer.tryStartCyclicPrint(self);
240 if (failed(Result: cyclicPrintGuard)) {
241 printer << '>';
242 return;
243 }
244
245 if (hasClassPrefix || name)
246 printer << ' ';
247 if (isPacked)
248 printer << "packed ";
249 if (isIncomplete) {
250 printer << "incomplete";
251 } else {
252 printer << "{";
253 for (auto [idx, member] : llvm::enumerate(First&: members)) {
254 if (idx)
255 printer << ", ";
256 printer << stringifyRecordMemberKind(memberKinds[idx]) << ' ';
257 printer.printType(type: member);
258 }
259 printer << "}";
260 if (padding) {
261 printer << ", padding = {";
262 printer.printType(type: padding);
263 printer << '}';
264 }
265 }
266 printer << '>';
267}
268
269/// Parse the body of a !cir.struct<...> type.
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);
274 bool packed = false;
275 mlir::MLIRContext *context = parser.getContext();
276
277 if (parser.parseLess())
278 return {};
279
280 // An optional "class" keyword distinguishes class from struct.
281 bool is_class = parser.parseOptionalKeyword(keyword: "class").succeeded();
282
283 mlir::StringAttr name;
284 parser.parseOptionalAttribute(result&: name);
285
286 // Self-reference: ensure the referenced type was already parsed.
287 if (name && parser.parseOptionalGreater().succeeded()) {
288 StructType type = StructType::getChecked(loc: eLoc, args&: context, args&: name, args&: is_class);
289 if (succeeded(Result: parser.tryStartCyclicParse(attrOrType: type))) {
290 parser.emitError(loc, message: "invalid self-reference within record");
291 return {};
292 }
293 return type;
294 }
295
296 // Named definition: ensure name has not been parsed yet.
297 if (name) {
298 StructType type = StructType::getChecked(loc: eLoc, args&: context, args&: name, args&: is_class);
299 cyclicParseGuard = parser.tryStartCyclicParse(attrOrType: type);
300 if (failed(Result: cyclicParseGuard)) {
301 parser.emitError(loc, message: "record already defined");
302 return {};
303 }
304 }
305
306 if (parser.parseOptionalKeyword(keyword: "packed").succeeded())
307 packed = true;
308
309 bool incomplete = true;
310 llvm::SmallVector<mlir::Type> members;
311 llvm::SmallVector<RecordMemberKind> memberKinds;
312 if (parseRecordBody(parser, incomplete, members, memberKinds).failed())
313 return {};
314
315 if (parser.parseGreater())
316 return {};
317
318 ArrayRef<mlir::Type> membersRef(members);
319 ArrayRef<RecordMemberKind> kindsRef(memberKinds);
320 mlir::Type type = {};
321 if (name && incomplete) {
322 type = StructType::getChecked(loc: eLoc, args&: context, args&: name, args&: is_class);
323 } else if (!name && !incomplete) {
324 type = StructType::getChecked(loc: eLoc, args&: context, args&: membersRef, args&: packed, args&: is_class,
325 args&: kindsRef);
326 if (!type)
327 return {};
328 } else if (!incomplete) {
329 type = StructType::getChecked(loc: eLoc, args&: context, args&: membersRef, args&: name, args&: packed,
330 args&: is_class, args&: kindsRef);
331 if (!type)
332 return {};
333 if (auto structTy = mlir::dyn_cast<StructType>(Val&: type)) {
334 if (failed(Result: structTy.mutate(args&: membersRef, args&: packed, args&: kindsRef))) {
335 parser.emitError(loc, message: "record '")
336 << name.getValue() << "' redefined with a different body";
337 return {};
338 }
339 }
340 assert(!cir::MissingFeatures::astRecordDeclAttr());
341 } else {
342 parser.emitError(loc, message: "anonymous records must be complete");
343 return {};
344 }
345
346 return type;
347}
348
349void StructType::print(mlir::AsmPrinter &printer) const {
350 printRecordBody(printer, self: *this, name: getName(), hasClassPrefix: isClass(), isPacked: getPacked(),
351 isIncomplete: isIncomplete(), members: getMembers(), /*padding=*/{},
352 memberKinds: getMemberKinds());
353}
354
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";
361 if (failed(Result: verifyRecordMemberTypes(emitError, members)))
362 return mlir::failure();
363 return verifyRecordMemberKinds(emitError, numMembers: members.size(), memberKinds: member_kinds);
364}
365
366// Accessors are hand-written because genStorageClass = 0 suppresses generated
367// implementations.
368llvm::ArrayRef<mlir::Type> StructType::getMembers() const {
369 return getImpl()->members;
370}
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;
377}
378bool StructType::getIsClass() const { return getImpl()->is_class; }
379
380bool StructType::getPadded() const {
381 return llvm::is_contained(Range: getMemberKinds(), Element: RecordMemberKind::Pad);
382}
383
384bool StructType::isABIConvertedRecord() const {
385 return getName() && getName().getValue().starts_with(Prefix: abi_conversion_prefix);
386}
387
388mlir::StringAttr StructType::getABIConvertedName() const {
389 assert(!isABIConvertedRecord());
390 return StringAttr::get(context: getContext(),
391 bytes: abi_conversion_prefix + getName().getValue());
392}
393
394void StructType::removeABIConversionNamePrefix() {
395 mlir::StringAttr recordName = getName();
396 if (recordName && recordName.getValue().starts_with(Prefix: abi_conversion_prefix))
397 getImpl()->name = mlir::StringAttr::get(
398 bytes: recordName.getValue().drop_front(N: sizeof(abi_conversion_prefix) - 1),
399 type: recordName.getType());
400}
401
402void StructType::complete(ArrayRef<Type> members, bool packed,
403 ArrayRef<RecordMemberKind> memberKinds) {
404 assert(!cir::MissingFeatures::astRecordDeclAttr());
405 // mutate() bypasses the verifier, so check what get() would have checked.
406 assert(
407 succeeded(verifyInvariants(
408 mlir::detail::getDefaultDiagnosticEmitFn(getContext()), members,
409 getName(), /*incomplete=*/false, packed, memberKinds, isClass())) &&
410 "invalid struct completion");
411 if (mutate(args&: members, args&: packed, args&: memberKinds).failed())
412 llvm_unreachable("failed to complete struct");
413}
414
415bool StructType::isLayoutIdentical(const StructType &other) {
416 if (getImpl() == other.getImpl())
417 return true;
418 if (getPacked() != other.getPacked())
419 return false;
420 return getMembers() == other.getMembers();
421}
422
423//===----------------------------------------------------------------------===//
424// UnionType
425//===----------------------------------------------------------------------===//
426
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);
431 bool packed = false;
432 mlir::Type padding;
433 mlir::MLIRContext *context = parser.getContext();
434
435 if (parser.parseLess())
436 return {};
437
438 mlir::StringAttr name;
439 parser.parseOptionalAttribute(result&: name);
440
441 // Self-reference.
442 if (name && parser.parseOptionalGreater().succeeded()) {
443 UnionType type = UnionType::getChecked(loc: eLoc, args&: context, args&: name);
444 if (succeeded(Result: parser.tryStartCyclicParse(attrOrType: type))) {
445 parser.emitError(loc, message: "invalid self-reference within record");
446 return {};
447 }
448 return type;
449 }
450
451 // Named definition.
452 if (name) {
453 UnionType type = UnionType::getChecked(loc: eLoc, args&: context, args&: name);
454 cyclicParseGuard = parser.tryStartCyclicParse(attrOrType: type);
455 if (failed(Result: cyclicParseGuard)) {
456 parser.emitError(loc, message: "record already defined");
457 return {};
458 }
459 }
460
461 if (parser.parseOptionalKeyword(keyword: "packed").succeeded())
462 packed = true;
463
464 bool incomplete = true;
465 llvm::SmallVector<mlir::Type> members;
466 llvm::SmallVector<RecordMemberKind> memberKinds;
467 if (parseRecordBody(parser, incomplete, members, memberKinds).failed())
468 return {};
469
470 // Optional tail-padding slot: ", padding = { <type> }". It is not a variant
471 // and so takes no mark.
472 if (!incomplete && parser.parseOptionalComma().succeeded()) {
473 if (parser.parseKeyword(keyword: "padding").failed())
474 return {};
475 if (parser.parseEqual().failed())
476 return {};
477 if (parser.parseLBrace().failed())
478 return {};
479 const llvm::SMLoc paddingLoc = parser.getCurrentLocation();
480 llvm::StringRef paddingKeyword;
481 if (parser.parseOptionalKeyword(keyword: &paddingKeyword, allowedValues: memberKindMarks)
482 .succeeded()) {
483 parser.emitError(loc: paddingLoc, message: "a union's tail padding takes no kind mark");
484 return {};
485 }
486 if (parser.parseType(result&: padding).failed())
487 return {};
488 if (parser.parseRBrace().failed())
489 return {};
490 }
491
492 if (parser.parseGreater())
493 return {};
494
495 ArrayRef<mlir::Type> membersRef(members);
496 ArrayRef<RecordMemberKind> kindsRef(memberKinds);
497 mlir::Type type = {};
498 if (name && incomplete) {
499 type = UnionType::getChecked(loc: eLoc, args&: context, args&: name);
500 } else if (!name && !incomplete) {
501 type = UnionType::getChecked(loc: eLoc, args&: context, args&: membersRef, args&: packed, args&: padding,
502 args&: kindsRef);
503 if (!type)
504 return {};
505 } else if (!incomplete) {
506 type = UnionType::getChecked(loc: eLoc, args&: context, args&: membersRef, args&: name, args&: packed,
507 args&: padding, args&: kindsRef);
508 if (!type)
509 return {};
510 if (auto unionTy = mlir::dyn_cast<UnionType>(Val&: type)) {
511 if (failed(Result: unionTy.mutate(args&: membersRef, args&: packed, args&: padding, args&: kindsRef))) {
512 parser.emitError(loc, message: "record '")
513 << name.getValue() << "' redefined with a different body";
514 return {};
515 }
516 }
517 assert(!cir::MissingFeatures::astRecordDeclAttr());
518 } else {
519 parser.emitError(loc, message: "anonymous records must be complete");
520 return {};
521 }
522
523 return type;
524}
525
526void UnionType::print(mlir::AsmPrinter &printer) const {
527 printRecordBody(printer, self: *this, name: getName(), /*hasClassPrefix=*/false,
528 isPacked: getPacked(), isIncomplete: isIncomplete(), members: getMembers(), padding: getPadding(),
529 memberKinds: getMemberKinds());
530}
531
532mlir::LogicalResult
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";
539 // A union's variants all start at offset zero, so there is no inter-member
540 // padding for a pad mark to describe. Its tail padding lives in the separate
541 // padding slot.
542 if (llvm::is_contained(Range&: member_kinds, Element: RecordMemberKind::Pad))
543 return emitError() << "a union member cannot be marked pad";
544 if (failed(Result: verifyRecordMemberTypes(emitError, members)))
545 return mlir::failure();
546 // CIRGen pads a union's tail with a char or an array of chars.
547 if (padding) {
548 mlir::Type charTy = padding;
549 if (auto arrTy = mlir::dyn_cast<cir::ArrayType>(Val&: padding))
550 charTy = arrTy.getElementType();
551 auto intTy = mlir::dyn_cast<cir::IntType>(Val&: charTy);
552 if (!intTy || intTy.getWidth() != 8 || intTy.isSigned() || intTy.isBitInt())
553 return emitError()
554 << "union padding must be !u8i or an array of !u8i, got "
555 << padding;
556 }
557 // Every variant starts at offset zero, so a bit-field variant is an access
558 // unit of its own. There is no run for a zero-width bit-field to end, so a
559 // variant always has storage.
560 for (auto [idx, memberTy] : llvm::enumerate(First&: members))
561 if (auto bfTy = mlir::dyn_cast<cir::BitFieldType>(Val: memberTy);
562 bfTy && !bfTy.ownsBytes())
563 return emitError() << "union bit-field member at index " << idx
564 << " must own its access unit storage";
565 return verifyRecordMemberKinds(emitError, numMembers: members.size(), memberKinds: member_kinds);
566}
567
568// Accessors.
569llvm::ArrayRef<mlir::Type> UnionType::getMembers() const {
570 return getImpl()->members;
571}
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;
580}
581
582bool UnionType::isABIConvertedRecord() const {
583 return getName() && getName().getValue().starts_with(Prefix: abi_conversion_prefix);
584}
585
586mlir::StringAttr UnionType::getABIConvertedName() const {
587 assert(!isABIConvertedRecord());
588 return StringAttr::get(context: getContext(),
589 bytes: abi_conversion_prefix + getName().getValue());
590}
591
592void UnionType::removeABIConversionNamePrefix() {
593 mlir::StringAttr recordName = getName();
594 if (recordName && recordName.getValue().starts_with(Prefix: abi_conversion_prefix))
595 getImpl()->name = mlir::StringAttr::get(
596 bytes: recordName.getValue().drop_front(N: sizeof(abi_conversion_prefix) - 1),
597 type: recordName.getType());
598}
599
600void UnionType::complete(ArrayRef<Type> members, bool packed,
601 mlir::Type padding,
602 ArrayRef<RecordMemberKind> memberKinds) {
603 assert(!cir::MissingFeatures::astRecordDeclAttr());
604 // mutate() bypasses the verifier, so check what get() would have checked.
605 assert(succeeded(verifyInvariants(
606 mlir::detail::getDefaultDiagnosticEmitFn(getContext()), members,
607 getName(), /*incomplete=*/false, packed, padding, memberKinds)) &&
608 "invalid union completion");
609 if (mutate(args&: members, args&: packed, args&: padding, args&: memberKinds).failed())
610 llvm_unreachable("failed to complete union");
611}
612
613mlir::Type
614UnionType::getUnionStorageType(const mlir::DataLayout &dataLayout) const {
615 return getUnionStorageType(dataLayout, members: getMembers());
616}
617
618mlir::Type UnionType::getUnionStorageType(const mlir::DataLayout &dataLayout,
619 llvm::ArrayRef<mlir::Type> members) {
620 if (members.empty())
621 return {};
622 mlir::Type largest = *std::max_element(
623 first: members.begin(), last: members.end(), comp: [&](mlir::Type lhs, mlir::Type rhs) {
624 return dataLayout.getTypeABIAlignment(t: lhs) <
625 dataLayout.getTypeABIAlignment(t: rhs) ||
626 (dataLayout.getTypeABIAlignment(t: lhs) ==
627 dataLayout.getTypeABIAlignment(t: rhs) &&
628 dataLayout.getTypeAllocSize(t: lhs) <
629 dataLayout.getTypeAllocSize(t: rhs));
630 });
631 // A union's storage is bytes, and stands in for every variant rather than
632 // for the one bit-field that happened to be widest, so a bit-field variant
633 // contributes its access unit rather than itself.
634 return memberStorageType(memberTy: largest);
635}
636
637bool UnionType::isLayoutIdentical(const UnionType &other) {
638 if (getImpl() == other.getImpl())
639 return true;
640 return getMembers() == other.getMembers() &&
641 getPadding() == other.getPadding();
642}
643
644//===----------------------------------------------------------------------===//
645// RecordType view-class method implementations
646//===----------------------------------------------------------------------===//
647
648llvm::ArrayRef<mlir::Type> RecordType::getMembers() const {
649 if (auto s = mlir::dyn_cast<StructType>(Val: *this))
650 return s.getMembers();
651 return mlir::cast<UnionType>(Val: *this).getMembers();
652}
653mlir::StringAttr RecordType::getName() const {
654 if (auto s = mlir::dyn_cast<StructType>(Val: *this))
655 return s.getName();
656 return mlir::cast<UnionType>(Val: *this).getName();
657}
658bool RecordType::isIncomplete() const {
659 if (auto s = mlir::dyn_cast<StructType>(Val: *this))
660 return s.isIncomplete();
661 return mlir::cast<UnionType>(Val: *this).isIncomplete();
662}
663bool RecordType::getPacked() const {
664 if (auto s = mlir::dyn_cast<StructType>(Val: *this))
665 return s.getPacked();
666 return mlir::cast<UnionType>(Val: *this).getPacked();
667}
668bool RecordType::getPadded() const {
669 if (auto s = mlir::dyn_cast<StructType>(Val: *this))
670 return s.getPadded();
671 return mlir::cast<UnionType>(Val: *this).getPadded();
672}
673llvm::ArrayRef<RecordMemberKind> RecordType::getMemberKinds() const {
674 if (auto s = mlir::dyn_cast<StructType>(Val: *this))
675 return s.getMemberKinds();
676 return mlir::cast<UnionType>(Val: *this).getMemberKinds();
677}
678bool RecordType::isClass() const {
679 if (auto s = mlir::dyn_cast<StructType>(Val: *this))
680 return s.isClass();
681 return false;
682}
683bool RecordType::isStruct() const {
684 if (auto s = mlir::dyn_cast<StructType>(Val: *this))
685 return s.isStruct();
686 return false;
687}
688std::string RecordType::getKindAsStr() const {
689 if (mlir::isa<UnionType>(Val: *this))
690 return "union";
691 return mlir::cast<StructType>(Val: *this).getKindAsStr();
692}
693std::string RecordType::getPrefixedName() const {
694 return getKindAsStr() + "." + getName().getValue().str();
695}
696void RecordType::complete(ArrayRef<Type> members, bool packed,
697 mlir::Type padding,
698 ArrayRef<RecordMemberKind> memberKinds) {
699 if (auto s = mlir::dyn_cast<StructType>(Val&: *this)) {
700 assert(!padding && "only a union takes a separate padding slot");
701 return s.complete(members, packed, memberKinds);
702 }
703 return mlir::cast<UnionType>(Val&: *this).complete(members, packed, padding,
704 memberKinds);
705}
706uint64_t RecordType::getElementOffset(const mlir::DataLayout &dataLayout,
707 unsigned idx) const {
708 if (mlir::isa<UnionType>(Val: *this))
709 return 0;
710 return mlir::cast<StructType>(Val: *this).getElementOffset(dataLayout, idx);
711}
712bool RecordType::isLayoutIdentical(const RecordType &other) {
713 if (auto s = mlir::dyn_cast<StructType>(Val&: *this)) {
714 if (auto so = mlir::dyn_cast<StructType>(Val: other))
715 return s.isLayoutIdentical(other: so);
716 return false;
717 }
718 if (auto u = mlir::dyn_cast<UnionType>(Val&: *this)) {
719 if (auto uo = mlir::dyn_cast<UnionType>(Val: other))
720 return u.isLayoutIdentical(other: uo);
721 return false;
722 }
723 return false;
724}
725bool RecordType::isABIConvertedRecord() const {
726 if (auto s = mlir::dyn_cast<StructType>(Val: *this))
727 return s.isABIConvertedRecord();
728 return mlir::cast<UnionType>(Val: *this).isABIConvertedRecord();
729}
730mlir::StringAttr RecordType::getABIConvertedName() const {
731 if (auto s = mlir::dyn_cast<StructType>(Val: *this))
732 return s.getABIConvertedName();
733 return mlir::cast<UnionType>(Val: *this).getABIConvertedName();
734}
735void RecordType::removeABIConversionNamePrefix() {
736 if (auto s = mlir::dyn_cast<StructType>(Val&: *this))
737 return s.removeABIConversionNamePrefix();
738 return mlir::cast<UnionType>(Val&: *this).removeABIConversionNamePrefix();
739}
740
741bool RecordType::isEmptyForABI() const {
742 // An incomplete record has no members yet, which must not read as vacuously
743 // holding no data.
744 if (isIncomplete())
745 return false;
746 if (anyMemberHoldsDataForABI(kinds: getMemberKinds()))
747 return false;
748 // An access unit of nothing but unnamed bit-fields is marked `empty`, since
749 // no field of the source reads it, but it is still storage that holds data
750 // for the ABI. This has to answer the way CIRGen's isEmptyFieldForABI does,
751 // which is where that rule lives.
752 return llvm::none_of(Range: getMembers(), P: [](mlir::Type memberTy) {
753 auto bfTy = mlir::dyn_cast<cir::BitFieldType>(Val&: memberTy);
754 return bfTy && bfTy.ownsBytes();
755 });
756}
757
758//===----------------------------------------------------------------------===//
759// Data Layout information for types
760//===----------------------------------------------------------------------===//
761
762// The cir.ptr data-layout entry holds a #cir.ptr_spec attribute (see
763// cir::setMLIRDataLayout).
764namespace {
765constexpr static uint64_t kBitsInByte = 8;
766
767// Defaults used when the module carries no cir.ptr data-layout entry.
768constexpr static uint64_t kDefaultPointerSizeBits = 64;
769constexpr static uint64_t kDefaultPointerAlignment = 8;
770
771/// Returns the default-address-space #cir.ptr_spec entry, or a synthesized
772/// 64-bit default when there is none. Per-AS entries are not modeled yet.
773cir::PtrSpecAttr getPointerSpec(mlir::DataLayoutEntryListRef params,
774 cir::PointerType type) {
775 // FIXME: improve this in face of address spaces
776 assert(!cir::MissingFeatures::dataLayoutPtrHandlingBasedOnLangAS());
777 for (mlir::DataLayoutEntryInterface entry : params) {
778 if (!entry.isTypeEntry())
779 continue;
780 auto key =
781 mlir::cast<cir::PointerType>(Val: mlir::cast<mlir::Type>(Val: entry.getKey()));
782 if (key.getAddrSpace())
783 continue;
784 if (auto spec = mlir::dyn_cast<cir::PtrSpecAttr>(Val: entry.getValue()))
785 return spec;
786 }
787 return cir::PtrSpecAttr::get(context: type.getContext(), size: kDefaultPointerSizeBits,
788 abi: kDefaultPointerAlignment * kBitsInByte,
789 preferred: kDefaultPointerAlignment * kBitsInByte,
790 index: kDefaultPointerSizeBits);
791}
792
793// TargetInfo::getBitIntMaxAlign() when the module has no #cir.bitint_spec
794// entry. That is LongLongAlign's initial value.
795constexpr static uint64_t kDefaultBitIntMaxAlignBits = 64;
796
797} // namespace
798
799/// The #cir.bitint_spec entry holds TargetInfo::getBitIntMaxAlign().
800uint64_t IntType::getBitIntMaxAlignBits(mlir::DataLayoutEntryListRef params) {
801 for (mlir::DataLayoutEntryInterface entry : params) {
802 if (!entry.isTypeEntry())
803 continue;
804 if (auto spec = mlir::dyn_cast<cir::BitIntSpecAttr>(Val: entry.getValue()))
805 return spec.getMaxAlign();
806 }
807 return kDefaultBitIntMaxAlignBits;
808}
809
810llvm::TypeSize
811PointerType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
812 ::mlir::DataLayoutEntryListRef params) const {
813 return llvm::TypeSize::getFixed(ExactSize: getPointerSpec(params, type: *this).getSize());
814}
815
816uint64_t
817PointerType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
818 ::mlir::DataLayoutEntryListRef params) const {
819 return getPointerSpec(params, type: *this).getAbi() / kBitsInByte;
820}
821
822uint64_t PointerType::getPreferredAlignment(
823 const ::mlir::DataLayout &dataLayout,
824 ::mlir::DataLayoutEntryListRef params) const {
825 return getPointerSpec(params, type: *this).getPreferred() / kBitsInByte;
826}
827
828std::optional<uint64_t>
829PointerType::getIndexBitwidth(const ::mlir::DataLayout &dataLayout,
830 ::mlir::DataLayoutEntryListRef params) const {
831 cir::PtrSpecAttr spec = getPointerSpec(params, type: *this);
832 if (spec.getIndex() == cir::PtrSpecAttr::kOptionalSpecValue)
833 return spec.getSize();
834 return spec.getIndex();
835}
836
837llvm::LogicalResult
838PointerType::verifyEntries(mlir::DataLayoutEntryListRef entries,
839 mlir::Location loc) const {
840 for (mlir::DataLayoutEntryInterface entry : entries) {
841 if (!entry.isTypeEntry())
842 continue;
843 auto key = mlir::cast<PointerType>(Val: mlir::cast<mlir::Type>(Val: entry.getKey()));
844 if (!mlir::isa<cir::PtrSpecAttr>(Val: 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>(Val: key.getPointee()))
848 return mlir::emitError(loc) << "expected !cir.ptr data layout entry for "
849 << key << " to use !cir.void as pointee";
850 // Per-address-space pointer layouts are not supported yet.
851 if (key.getAddrSpace())
852 return mlir::emitError(loc)
853 << "!cir.ptr data layout entries are currently limited to the "
854 "default address space";
855 }
856 return mlir::success();
857}
858
859bool PointerType::areCompatible(
860 mlir::DataLayoutEntryListRef oldLayout,
861 mlir::DataLayoutEntryListRef newLayout, mlir::DataLayoutSpecInterface,
862 const mlir::DataLayoutIdentifiedEntryMap &) const {
863 // A nested spec may only override with the same size and a compatible ABI
864 // alignment. TODO(cir): match by address space once per-AS specs exist.
865 cir::PtrSpecAttr oldSpec = getPointerSpec(params: oldLayout, type: *this);
866 uint64_t size = oldSpec.getSize();
867 uint64_t abi = oldSpec.getAbi();
868 for (mlir::DataLayoutEntryInterface newEntry : newLayout) {
869 if (!newEntry.isTypeEntry())
870 continue;
871 auto newSpec = mlir::cast<cir::PtrSpecAttr>(Val: newEntry.getValue());
872 if (size != newSpec.getSize() || abi < newSpec.getAbi() ||
873 abi % newSpec.getAbi() != 0)
874 return false;
875 }
876 return true;
877}
878
879llvm::TypeSize
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(ExactSize: recordSize * 8);
884}
885
886uint64_t
887StructType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
888 ::mlir::DataLayoutEntryListRef params) const {
889 // Packed structures always have an ABI alignment of 1.
890 if (getPacked())
891 return 1;
892 return computeStructAlignment(dataLayout);
893}
894
895// Sums the storage member's alloc size (if present) with the padding field's
896// size (if present).
897// A union whose member list came out empty has no storage type, so its whole
898// size lives in the padding, which lowerUnion sizes from the ASTRecordLayout.
899llvm::TypeSize
900UnionType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
901 mlir::DataLayoutEntryListRef params) const {
902 llvm::TypeSize size = llvm::TypeSize::getFixed(ExactSize: 0);
903 if (mlir::Type storage = getUnionStorageType(dataLayout))
904 size += dataLayout.getTypeAllocSize(t: storage) * 8;
905 if (mlir::Type pad = getPadding())
906 size += dataLayout.getTypeSizeInBits(t: pad);
907 return size;
908}
909
910uint64_t
911UnionType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
912 ::mlir::DataLayoutEntryListRef params) const {
913 if (getPacked())
914 return 1;
915 mlir::Type storage = getUnionStorageType(dataLayout);
916 if (!storage)
917 return 1;
918 return dataLayout.getTypeABIAlignment(t: storage);
919}
920
921unsigned
922StructType::computeStructSize(const mlir::DataLayout &dataLayout) const {
923 assert(isComplete() && "Cannot get layout of incomplete records");
924
925 // This is a similar algorithm to LLVM's StructLayout. A member that owns no
926 // bytes needs no special case: it reports size 0 and alignment 1, so the
927 // running size passes over it unchanged.
928 unsigned recordSize = 0;
929 uint64_t recordAlignment = 1;
930
931 for (mlir::Type ty : getMembers()) {
932 // This assumes that we're calculating size based on the ABI alignment, not
933 // the preferred alignment for each type.
934 const uint64_t tyAlign =
935 (getPacked() ? 1 : dataLayout.getTypeABIAlignment(t: ty));
936
937 // Add padding to the struct size to align it to the abi alignment of the
938 // element type before adding the element's alloc size.
939 recordSize = llvm::alignTo(Value: recordSize, Align: tyAlign);
940 recordSize += dataLayout.getTypeAllocSize(t: ty);
941
942 // The alignment requirement of a struct is equal to the strictest
943 // alignment requirement of its elements.
944 recordAlignment = std::max(a: tyAlign, b: recordAlignment);
945 }
946
947 // At the end, add padding to the struct to satisfy its own alignment
948 // requirement. Otherwise structs inside of arrays would be misaligned.
949 recordSize = llvm::alignTo(Value: recordSize, Align: recordAlignment);
950 return recordSize;
951}
952
953unsigned
954StructType::computeStructDataSize(const mlir::DataLayout &dataLayout) const {
955 assert(isComplete() && "Cannot get layout of incomplete records");
956
957 // Tail padding is the trailing run of pad members, which is what a derived
958 // class may reuse. A member that owns no bytes holds no storage, so it does
959 // not end that run. A member of any other kind stays inside the data size.
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 ||
965 !memberOwnsBytes(memberTy: members.back()))) {
966 kinds = kinds.drop_back();
967 members = members.drop_back();
968 }
969
970 unsigned recordSize = 0;
971 for (mlir::Type ty : members) {
972 const uint64_t tyAlign =
973 (getPacked() ? 1 : dataLayout.getTypeABIAlignment(t: ty));
974 recordSize = llvm::alignTo(Value: recordSize, Align: tyAlign);
975 recordSize += dataLayout.getTypeAllocSize(t: ty);
976 }
977 return recordSize;
978}
979
980// We also compute the alignment as part of computeStructSize, but this is more
981// efficient. Ideally, we'd like to compute both at once and cache the result,
982// but that's not implemented yet.
983// TODO(CIR): Implement a way to cache the result.
984uint64_t
985StructType::computeStructAlignment(const mlir::DataLayout &dataLayout) const {
986 assert(isComplete() && "Cannot get layout of incomplete records");
987
988 uint64_t recordAlignment = 1;
989 for (mlir::Type ty : getMembers())
990 recordAlignment =
991 std::max(a: dataLayout.getTypeABIAlignment(t: ty), b: recordAlignment);
992 return recordAlignment;
993}
994
995unsigned StructType::getLLVMFieldIndex(unsigned idx) const {
996 llvm::ArrayRef<mlir::Type> members = getMembers();
997 assert(idx < members.size() && "access not valid");
998 assert(memberOwnsBytes(members[idx]) &&
999 "a member that owns no bytes has no LLVM field");
1000
1001 unsigned llvmIdx = 0;
1002 for (unsigned i = 0; i != idx; ++i)
1003 if (memberOwnsBytes(memberTy: members[i]))
1004 ++llvmIdx;
1005 return llvmIdx;
1006}
1007
1008uint64_t StructType::getElementOffset(const ::mlir::DataLayout &dataLayout,
1009 unsigned idx) const {
1010 assert(idx < getMembers().size() && "access not valid");
1011 if (idx == 0)
1012 return 0;
1013
1014 assert(isComplete() && "Cannot get layout of incomplete records");
1015 llvm::ArrayRef<mlir::Type> members = getMembers();
1016
1017 // A zero-width bit-field reports alignment 1 and size 0, so the running
1018 // offset passes over it unchanged and lands on where the storage ahead of it
1019 // ends, which is the offset it was declared at.
1020 unsigned offset = 0;
1021 for (unsigned i = 0; i != idx; ++i) {
1022 const llvm::Align tyAlign = llvm::Align(
1023 getPacked() ? 1 : dataLayout.getTypeABIAlignment(t: members[i]));
1024 offset = llvm::alignTo(Size: offset, A: tyAlign);
1025 offset += dataLayout.getTypeAllocSize(t: members[i]);
1026 }
1027
1028 const llvm::Align tyAlign = llvm::Align(
1029 getPacked() ? 1 : dataLayout.getTypeABIAlignment(t: members[idx]));
1030 return llvm::alignTo(Size: offset, A: tyAlign);
1031}
1032
1033//===----------------------------------------------------------------------===//
1034// IntType Definitions
1035//===----------------------------------------------------------------------===//
1036
1037Type IntType::parse(mlir::AsmParser &parser) {
1038 mlir::MLIRContext *context = parser.getBuilder().getContext();
1039 llvm::SMLoc loc = parser.getCurrentLocation();
1040 bool isSigned;
1041 unsigned width;
1042
1043 if (parser.parseLess())
1044 return {};
1045
1046 // Fetch integer sign.
1047 llvm::StringRef sign;
1048 if (parser.parseKeyword(keyword: &sign))
1049 return {};
1050 if (sign == "s")
1051 isSigned = true;
1052 else if (sign == "u")
1053 isSigned = false;
1054 else {
1055 parser.emitError(loc, message: "expected 's' or 'u'");
1056 return {};
1057 }
1058
1059 if (parser.parseComma())
1060 return {};
1061
1062 // Fetch integer size.
1063 if (parser.parseInteger(result&: width))
1064 return {};
1065 if (width < IntType::minBitwidth() || width > IntType::maxBitwidth()) {
1066 parser.emitError(loc, message: "expected integer width to be from ")
1067 << IntType::minBitwidth() << " up to " << IntType::maxBitwidth();
1068 return {};
1069 }
1070
1071 bool isBitInt = false;
1072 if (succeeded(Result: parser.parseOptionalComma())) {
1073 llvm::StringRef kw;
1074 if (parser.parseKeyword(keyword: &kw) || kw != "bitint") {
1075 parser.emitError(loc, message: "expected 'bitint'");
1076 return {};
1077 }
1078 isBitInt = true;
1079 }
1080
1081 if (parser.parseGreater())
1082 return {};
1083
1084 return IntType::get(context, width, is_signed: isSigned, is_bit_int: isBitInt);
1085}
1086
1087void IntType::print(mlir::AsmPrinter &printer) const {
1088 char sign = isSigned() ? 's' : 'u';
1089 printer << '<' << sign << ", " << getWidth();
1090 if (isBitInt())
1091 printer << ", bitint";
1092 printer << '>';
1093}
1094
1095llvm::TypeSize
1096IntType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
1097 mlir::DataLayoutEntryListRef params) const {
1098 if (!isBitInt())
1099 return llvm::TypeSize::getFixed(ExactSize: getWidth());
1100 uint64_t alignBits =
1101 getBitIntAlignRequirement(maxAlignBits: getBitIntMaxAlignBits(params)) * 8;
1102 return llvm::TypeSize::getFixed(ExactSize: llvm::alignTo(Value: getWidth(), Align: alignBits));
1103}
1104
1105unsigned
1106IntType::getStorageTypeWidth(const mlir::DataLayout &dataLayout) const {
1107 return static_cast<unsigned>(
1108 dataLayout.getTypeSizeInBits(t: *this).getFixedValue());
1109}
1110
1111uint64_t
1112IntType::getStorageTypeAlignment(const mlir::DataLayout &dataLayout) const {
1113 if (!isBitInt())
1114 return getABIAlignment(dataLayout, params: {});
1115 auto storageTy =
1116 mlir::IntegerType::get(context: getContext(), width: getStorageTypeWidth(dataLayout));
1117 return dataLayout.getTypeABIAlignment(t: storageTy);
1118}
1119
1120uint64_t IntType::getBitIntAlignRequirement(uint64_t maxAlignBits) const {
1121 assert(isBitInt() && "only a _BitInt has a target alignment cap");
1122 uint64_t alignBits = std::min(
1123 a: static_cast<uint64_t>(llvm::PowerOf2Ceil(A: getWidth())), b: maxAlignBits);
1124 return std::max(a: alignBits / 8, b: static_cast<uint64_t>(1));
1125}
1126
1127uint64_t IntType::getABIAlignment(const mlir::DataLayout &dataLayout,
1128 mlir::DataLayoutEntryListRef params) const {
1129 unsigned width = getWidth();
1130 if (isBitInt()) {
1131 uint64_t storageBytes = getStorageTypeWidth(dataLayout) / 8;
1132 uint64_t storageAlign = getStorageTypeAlignment(dataLayout);
1133 if (llvm::alignTo(Value: storageBytes, Align: storageAlign) == storageBytes)
1134 return storageAlign;
1135 return 1;
1136 }
1137 // Round up to a power-of-two byte alignment. DataLayout consumers such as
1138 // llvm::Align require power-of-two alignments, and width / 8 is not a power
1139 // of two for non-fundamental widths (e.g. i24 -> 3). This leaves the
1140 // fundamental widths unchanged (i8 -> 1, i16 -> 2, i32 -> 4, i64 -> 8) and
1141 // keeps __int128 at 16.
1142 uint64_t alignBits = llvm::PowerOf2Ceil(A: width);
1143 return std::max(a: alignBits / 8, b: static_cast<uint64_t>(1));
1144}
1145
1146bool IntType::areCompatible(mlir::DataLayoutEntryListRef oldLayout,
1147 mlir::DataLayoutEntryListRef newLayout,
1148 mlir::DataLayoutSpecInterface,
1149 const mlir::DataLayoutIdentifiedEntryMap &) const {
1150 // A nested spec may only repeat the same _BitInt max alignment.
1151 uint64_t oldMax = getBitIntMaxAlignBits(params: oldLayout);
1152 for (mlir::DataLayoutEntryInterface newEntry : newLayout) {
1153 if (!newEntry.isTypeEntry())
1154 continue;
1155 auto newSpec = mlir::dyn_cast<cir::BitIntSpecAttr>(Val: newEntry.getValue());
1156 if (newSpec && newSpec.getMaxAlign() != oldMax)
1157 return false;
1158 }
1159 return true;
1160}
1161
1162llvm::LogicalResult IntType::verifyEntries(mlir::DataLayoutEntryListRef entries,
1163 mlir::Location loc) const {
1164 cir::IntType keyTy = getBitIntMaxAlignKey(ctx: getContext());
1165 for (mlir::DataLayoutEntryInterface entry : entries) {
1166 if (!entry.isTypeEntry())
1167 continue;
1168 auto key = mlir::cast<IntType>(Val: mlir::cast<mlir::Type>(Val: entry.getKey()));
1169 if (key != keyTy)
1170 return mlir::emitError(loc)
1171 << "expected _BitInt data layout entry to be keyed on " << keyTy
1172 << ", got " << key;
1173 if (!mlir::isa<cir::BitIntSpecAttr>(Val: entry.getValue()))
1174 return mlir::emitError(loc) << "expected layout attribute for " << key
1175 << " to be a #cir.bitint_spec attribute";
1176 }
1177 return mlir::success();
1178}
1179
1180mlir::LogicalResult
1181IntType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
1182 unsigned width, bool isSigned, bool isBitInt) {
1183 if (width < IntType::minBitwidth() || width > IntType::maxBitwidth())
1184 return emitError() << "IntType only supports widths from "
1185 << IntType::minBitwidth() << " up to "
1186 << IntType::maxBitwidth();
1187 return mlir::success();
1188}
1189
1190bool cir::isValidFundamentalIntWidth(unsigned width) {
1191 return width == 8 || width == 16 || width == 32 || width == 64;
1192}
1193
1194//===----------------------------------------------------------------------===//
1195// Floating-point type definitions
1196//===----------------------------------------------------------------------===//
1197
1198const llvm::fltSemantics &SingleType::getFloatSemantics() const {
1199 return llvm::APFloat::IEEEsingle();
1200}
1201
1202llvm::TypeSize
1203SingleType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
1204 mlir::DataLayoutEntryListRef params) const {
1205 return llvm::TypeSize::getFixed(ExactSize: getWidth());
1206}
1207
1208uint64_t
1209SingleType::getABIAlignment(const mlir::DataLayout &dataLayout,
1210 mlir::DataLayoutEntryListRef params) const {
1211 return (uint64_t)(getWidth() / 8);
1212}
1213
1214const llvm::fltSemantics &DoubleType::getFloatSemantics() const {
1215 return llvm::APFloat::IEEEdouble();
1216}
1217
1218llvm::TypeSize
1219DoubleType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
1220 mlir::DataLayoutEntryListRef params) const {
1221 return llvm::TypeSize::getFixed(ExactSize: getWidth());
1222}
1223
1224uint64_t
1225DoubleType::getABIAlignment(const mlir::DataLayout &dataLayout,
1226 mlir::DataLayoutEntryListRef params) const {
1227 return (uint64_t)(getWidth() / 8);
1228}
1229
1230const llvm::fltSemantics &FP16Type::getFloatSemantics() const {
1231 return llvm::APFloat::IEEEhalf();
1232}
1233
1234llvm::TypeSize
1235FP16Type::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
1236 mlir::DataLayoutEntryListRef params) const {
1237 return llvm::TypeSize::getFixed(ExactSize: getWidth());
1238}
1239
1240uint64_t FP16Type::getABIAlignment(const mlir::DataLayout &dataLayout,
1241 mlir::DataLayoutEntryListRef params) const {
1242 return (uint64_t)(getWidth() / 8);
1243}
1244
1245const llvm::fltSemantics &BF16Type::getFloatSemantics() const {
1246 return llvm::APFloat::BFloat();
1247}
1248
1249llvm::TypeSize
1250BF16Type::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
1251 mlir::DataLayoutEntryListRef params) const {
1252 return llvm::TypeSize::getFixed(ExactSize: getWidth());
1253}
1254
1255uint64_t BF16Type::getABIAlignment(const mlir::DataLayout &dataLayout,
1256 mlir::DataLayoutEntryListRef params) const {
1257 return (uint64_t)(getWidth() / 8);
1258}
1259
1260const llvm::fltSemantics &FP80Type::getFloatSemantics() const {
1261 return llvm::APFloat::x87DoubleExtended();
1262}
1263
1264llvm::TypeSize
1265FP80Type::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
1266 mlir::DataLayoutEntryListRef params) const {
1267 // Though only 80 bits are used for the value, the type is 128 bits in size.
1268 return llvm::TypeSize::getFixed(ExactSize: 128);
1269}
1270
1271uint64_t FP80Type::getABIAlignment(const mlir::DataLayout &dataLayout,
1272 mlir::DataLayoutEntryListRef params) const {
1273 return 16;
1274}
1275
1276const llvm::fltSemantics &FP128Type::getFloatSemantics() const {
1277 return llvm::APFloat::IEEEquad();
1278}
1279
1280llvm::TypeSize
1281FP128Type::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
1282 mlir::DataLayoutEntryListRef params) const {
1283 return llvm::TypeSize::getFixed(ExactSize: getWidth());
1284}
1285
1286uint64_t FP128Type::getABIAlignment(const mlir::DataLayout &dataLayout,
1287 mlir::DataLayoutEntryListRef params) const {
1288 return 16;
1289}
1290
1291const llvm::fltSemantics &LongDoubleType::getFloatSemantics() const {
1292 return mlir::cast<cir::FPTypeInterface>(Val: getUnderlying()).getFloatSemantics();
1293}
1294
1295llvm::TypeSize
1296LongDoubleType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
1297 mlir::DataLayoutEntryListRef params) const {
1298 return mlir::cast<mlir::DataLayoutTypeInterface>(Val: getUnderlying())
1299 .getTypeSizeInBits(dataLayout, params);
1300}
1301
1302uint64_t
1303LongDoubleType::getABIAlignment(const mlir::DataLayout &dataLayout,
1304 mlir::DataLayoutEntryListRef params) const {
1305 return mlir::cast<mlir::DataLayoutTypeInterface>(Val: getUnderlying())
1306 .getABIAlignment(dataLayout, params);
1307}
1308
1309//===----------------------------------------------------------------------===//
1310// ComplexType Definitions
1311//===----------------------------------------------------------------------===//
1312
1313llvm::TypeSize
1314cir::ComplexType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
1315 mlir::DataLayoutEntryListRef params) const {
1316 // C17 6.2.5p13:
1317 // Each complex type has the same representation and alignment requirements
1318 // as an array type containing exactly two elements of the corresponding
1319 // real type.
1320
1321 return dataLayout.getTypeSizeInBits(t: getElementType()) * 2;
1322}
1323
1324uint64_t
1325cir::ComplexType::getABIAlignment(const mlir::DataLayout &dataLayout,
1326 mlir::DataLayoutEntryListRef params) const {
1327 // C17 6.2.5p13:
1328 // Each complex type has the same representation and alignment requirements
1329 // as an array type containing exactly two elements of the corresponding
1330 // real type.
1331
1332 return dataLayout.getTypeABIAlignment(t: getElementType());
1333}
1334
1335FuncType FuncType::clone(TypeRange inputs, TypeRange results) const {
1336 assert(results.size() == 1 && "expected exactly one result type");
1337 return get(inputs: llvm::to_vector(Range&: inputs), returnType: results[0], isVarArg: isVarArg());
1338}
1339
1340// Custom parser that parses function parameters of form `(<type>*, ...)`.
1341static mlir::ParseResult
1342parseFuncTypeParams(mlir::AsmParser &p, llvm::SmallVector<mlir::Type> &params,
1343 bool &isVarArg) {
1344 isVarArg = false;
1345 return p.parseCommaSeparatedList(
1346 delimiter: AsmParser::Delimiter::Paren, parseElementFn: [&]() -> mlir::ParseResult {
1347 if (isVarArg)
1348 return p.emitError(loc: p.getCurrentLocation(),
1349 message: "variadic `...` must be the last parameter");
1350 if (succeeded(Result: p.parseOptionalEllipsis())) {
1351 isVarArg = true;
1352 return success();
1353 }
1354 mlir::Type type;
1355 if (failed(Result: p.parseType(result&: type)))
1356 return failure();
1357 params.push_back(Elt: type);
1358 return success();
1359 });
1360}
1361
1362static void printFuncTypeParams(mlir::AsmPrinter &p,
1363 mlir::ArrayRef<mlir::Type> params,
1364 bool isVarArg) {
1365 p << '(';
1366 llvm::interleaveComma(c: params, os&: p,
1367 each_fn: [&p](mlir::Type type) { p.printType(type); });
1368 if (isVarArg) {
1369 if (!params.empty())
1370 p << ", ";
1371 p << "...";
1372 }
1373 p << ')';
1374}
1375
1376/// Get the C-style return type of the function, which is !cir.void if the
1377/// function returns nothing and the actual return type otherwise.
1378mlir::Type FuncType::getReturnType() const {
1379 if (hasVoidReturn())
1380 return cir::VoidType::get(ctx: getContext());
1381 return getOptionalReturnType();
1382}
1383
1384/// Get the MLIR-style return type of the function, which is an empty
1385/// ArrayRef if the function returns nothing and a single-element ArrayRef
1386/// with the actual return type otherwise.
1387llvm::ArrayRef<mlir::Type> FuncType::getReturnTypes() const {
1388 if (hasVoidReturn())
1389 return {};
1390 // Can't use getOptionalReturnType() here because llvm::ArrayRef hold a
1391 // pointer to its elements and doesn't do lifetime extension. That would
1392 // result in returning a pointer to a temporary that has gone out of scope.
1393 return getImpl()->optionalReturnType;
1394}
1395
1396// Does the fuction type return nothing?
1397bool FuncType::hasVoidReturn() const { return !getOptionalReturnType(); }
1398
1399mlir::LogicalResult
1400FuncType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
1401 llvm::ArrayRef<mlir::Type> argTypes, mlir::Type returnType,
1402 bool isVarArg) {
1403 if (mlir::isa_and_nonnull<cir::VoidType>(Val: returnType))
1404 return emitError()
1405 << "!cir.func cannot have an explicit 'void' return type";
1406
1407 // The calling convention lowering pass expects all types in a function
1408 // signature to be CIR types. CIR types only nest CIR types, so checking the
1409 // signature's own types covers everything reachable from them.
1410 if (!llvm::all_of(Range&: argTypes, P: cir::isCIRType) ||
1411 (returnType && !cir::isCIRType(type: returnType)))
1412 return emitError()
1413 << "expected all types in the function signature to be CIR types";
1414
1415 return mlir::success();
1416}
1417
1418//===----------------------------------------------------------------------===//
1419// MethodType Definitions
1420//===----------------------------------------------------------------------===//
1421
1422static mlir::Type getMethodLayoutType(mlir::MLIRContext *ctx) {
1423 // With Itanium ABI, member function pointers have the same layout as the
1424 // following struct: struct { fnptr_t, ptrdiff_t }, where fnptr_t is a
1425 // function pointer type.
1426 // TODO: consider member function pointer layout in other ABIs
1427 auto voidPtrTy = cir::PointerType::get(pointee: cir::VoidType::get(ctx));
1428 mlir::Type fields[2]{voidPtrTy, voidPtrTy};
1429 return cir::StructType::get(context: ctx, members: fields, /*packed=*/false,
1430 /*is_class=*/false,
1431 member_kinds: cir::RecordType::getAllDataKinds(members: fields));
1432}
1433
1434llvm::TypeSize
1435MethodType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
1436 mlir::DataLayoutEntryListRef params) const {
1437 return dataLayout.getTypeSizeInBits(t: getMethodLayoutType(ctx: getContext()));
1438}
1439
1440uint64_t
1441MethodType::getABIAlignment(const mlir::DataLayout &dataLayout,
1442 mlir::DataLayoutEntryListRef params) const {
1443 return cast<cir::StructType>(Val: getMethodLayoutType(ctx: getContext()))
1444 .getABIAlignment(dataLayout, params);
1445}
1446
1447//===----------------------------------------------------------------------===//
1448// BoolType
1449//===----------------------------------------------------------------------===//
1450
1451llvm::TypeSize
1452BoolType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
1453 ::mlir::DataLayoutEntryListRef params) const {
1454 return llvm::TypeSize::getFixed(ExactSize: 8);
1455}
1456
1457uint64_t
1458BoolType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
1459 ::mlir::DataLayoutEntryListRef params) const {
1460 return 1;
1461}
1462
1463//===----------------------------------------------------------------------===//
1464// DataMemberType Definitions
1465//===----------------------------------------------------------------------===//
1466
1467static mlir::Type getDataMemberLayoutType(const mlir::DataLayout &dataLayout,
1468 mlir::MLIRContext *ctx) {
1469 // Itanium ABI: a data member pointer is a ptrdiff_t, an integer of the
1470 // pointer index width.
1471 // TODO: consider data member pointer layout in other ABIs
1472 auto voidPtrTy = cir::PointerType::get(pointee: cir::VoidType::get(ctx));
1473 uint64_t width = dataLayout.getTypeIndexBitwidth(t: voidPtrTy).value_or(
1474 u: dataLayout.getTypeSizeInBits(t: voidPtrTy).getFixedValue());
1475 return cir::IntType::get(context: ctx, width, /*is_signed=*/true);
1476}
1477
1478llvm::TypeSize
1479DataMemberType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
1480 ::mlir::DataLayoutEntryListRef params) const {
1481 assert(!MissingFeatures::cxxABI());
1482 return dataLayout.getTypeSizeInBits(
1483 t: getDataMemberLayoutType(dataLayout, ctx: getContext()));
1484}
1485
1486uint64_t
1487DataMemberType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
1488 ::mlir::DataLayoutEntryListRef params) const {
1489 assert(!MissingFeatures::cxxABI());
1490 return dataLayout.getTypeABIAlignment(
1491 t: getDataMemberLayoutType(dataLayout, ctx: getContext()));
1492}
1493
1494//===----------------------------------------------------------------------===//
1495// VPtrType Definitions
1496//===----------------------------------------------------------------------===//
1497
1498llvm::TypeSize
1499VPtrType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
1500 mlir::DataLayoutEntryListRef params) const {
1501 // A vtable pointer is an ordinary data pointer; size it as a cir.ptr.
1502 return dataLayout.getTypeSizeInBits(
1503 t: cir::PointerType::get(pointee: cir::VoidType::get(ctx: getContext())));
1504}
1505
1506uint64_t VPtrType::getABIAlignment(const mlir::DataLayout &dataLayout,
1507 mlir::DataLayoutEntryListRef params) const {
1508 return dataLayout.getTypeABIAlignment(
1509 t: cir::PointerType::get(pointee: cir::VoidType::get(ctx: getContext())));
1510}
1511
1512//===----------------------------------------------------------------------===//
1513// ArrayType Definitions
1514//===----------------------------------------------------------------------===//
1515
1516llvm::TypeSize
1517ArrayType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
1518 ::mlir::DataLayoutEntryListRef params) const {
1519 return getSize() * dataLayout.getTypeAllocSize(t: getElementType()) * 8;
1520}
1521
1522uint64_t
1523ArrayType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
1524 ::mlir::DataLayoutEntryListRef params) const {
1525 return dataLayout.getTypeABIAlignment(t: getElementType());
1526}
1527
1528//===----------------------------------------------------------------------===//
1529// BitFieldType Definitions
1530//===----------------------------------------------------------------------===//
1531
1532mlir::LogicalResult
1533BitFieldType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
1534 mlir::Type storageType,
1535 llvm::ArrayRef<cir::BitFieldDeclAttr> fields) {
1536 if (fields.empty())
1537 return emitError() << "bit-field member must hold at least one bit-field";
1538
1539 if (!storageType) {
1540 // Absent storage is the zero-width bit-field, which belongs to no access
1541 // unit and so keeps company with nothing.
1542 if (fields.size() != 1 || fields.front().getWidth() != 0)
1543 return emitError() << "a bit-field member without storage must hold a "
1544 "single zero-width bit-field";
1545 return mlir::success();
1546 }
1547
1548 if (!cir::isCIRType(type: storageType))
1549 return emitError() << "bit-field access unit storage must be a CIR type, "
1550 "got "
1551 << storageType;
1552
1553 if (mlir::isa<cir::BitFieldType>(Val: storageType))
1554 return emitError() << "bit-field access unit storage cannot itself be a "
1555 "bit-field type";
1556
1557 if (!cir::isSized(ty: storageType))
1558 return emitError() << "bit-field access unit storage must be sized, got "
1559 << storageType;
1560
1561 // A zero-width bit-field ends the run before it rather than joining a unit,
1562 // so it never appears alongside the fields that occupy one.
1563 if (llvm::any_of(Range&: fields, P: [](cir::BitFieldDeclAttr decl) {
1564 return decl.getWidth() == 0;
1565 }))
1566 return emitError() << "a zero-width bit-field cannot occupy an access unit";
1567
1568 return mlir::success();
1569}
1570
1571llvm::TypeSize
1572BitFieldType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
1573 mlir::DataLayoutEntryListRef params) const {
1574 // A unit is stored as its storage type. A zero-width bit-field has none,
1575 // and holds no bits of its own.
1576 if (mlir::Type storage = getStorageType())
1577 return dataLayout.getTypeSizeInBits(t: storage);
1578 return llvm::TypeSize::getFixed(ExactSize: 0);
1579}
1580
1581uint64_t
1582BitFieldType::getABIAlignment(const mlir::DataLayout &dataLayout,
1583 mlir::DataLayoutEntryListRef params) const {
1584 // A member that occupies no bytes imposes no alignment, which is what lets
1585 // the record layout walk a zero-width bit-field without skipping it.
1586 if (mlir::Type storage = getStorageType())
1587 return dataLayout.getTypeABIAlignment(t: storage);
1588 return 1;
1589}
1590
1591//===----------------------------------------------------------------------===//
1592// VectorType Definitions
1593//===----------------------------------------------------------------------===//
1594
1595llvm::TypeSize cir::VectorType::getTypeSizeInBits(
1596 const ::mlir::DataLayout &dataLayout,
1597 ::mlir::DataLayoutEntryListRef params) const {
1598 // Clang packs a fixed-length bool vector one bit per element and rounds the
1599 // vector's size up to a power of two of at least a byte.
1600 if (mlir::isa<cir::BoolType>(Val: getElementType()) && getSize().isFixed()) {
1601 assert(!cir::MissingFeatures::hlsl());
1602 return llvm::TypeSize::getFixed(ExactSize: llvm::PowerOf2Ceil(A: getBoolStorageWidth()));
1603 }
1604
1605 llvm::TypeSize elementSize = dataLayout.getTypeSizeInBits(t: getElementType());
1606 return llvm::TypeSize::get(
1607 Quantity: getSize().getKnownMinValue() * elementSize.getFixedValue(), Scalable: isScalable());
1608}
1609
1610uint64_t cir::VectorType::getBoolStorageWidth() const {
1611 assert(mlir::isa<cir::BoolType>(getElementType()) &&
1612 "only a bool vector is stored as an integer");
1613 return std::max<uint64_t>(a: getFixedSize(), b: 8);
1614}
1615
1616uint64_t
1617cir::VectorType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
1618 ::mlir::DataLayoutEntryListRef params) const {
1619 // This hook answers in bytes, not bits.
1620 return llvm::PowerOf2Ceil(A: llvm::divideCeil(
1621 Numerator: dataLayout.getTypeSizeInBits(t: *this).getKnownMinValue(), Denominator: uint64_t{8}));
1622}
1623
1624/// The #cir.vector_spec entry holds TargetInfo::getMaxVectorAlign().
1625uint64_t cir::VectorType::getMaxAlignBits(mlir::DataLayoutEntryListRef params) {
1626 for (mlir::DataLayoutEntryInterface entry : params) {
1627 if (!entry.isTypeEntry())
1628 continue;
1629 if (auto spec = mlir::dyn_cast<cir::VectorSpecAttr>(Val: entry.getValue()))
1630 return spec.getMaxAlign();
1631 }
1632 return 0;
1633}
1634
1635bool cir::VectorType::areCompatible(
1636 mlir::DataLayoutEntryListRef oldLayout,
1637 mlir::DataLayoutEntryListRef newLayout, mlir::DataLayoutSpecInterface,
1638 const mlir::DataLayoutIdentifiedEntryMap &) const {
1639 // A nested spec may only repeat the vector alignment cap already in
1640 // effect. No entry means the target imposes no limit.
1641 uint64_t oldMax = getMaxAlignBits(params: oldLayout);
1642 for (mlir::DataLayoutEntryInterface newEntry : newLayout) {
1643 if (!newEntry.isTypeEntry())
1644 continue;
1645 auto newSpec = mlir::dyn_cast<cir::VectorSpecAttr>(Val: newEntry.getValue());
1646 if (newSpec && newSpec.getMaxAlign() != oldMax)
1647 return false;
1648 }
1649 return true;
1650}
1651
1652llvm::LogicalResult
1653cir::VectorType::verifyEntries(mlir::DataLayoutEntryListRef entries,
1654 mlir::Location loc) const {
1655 cir::VectorType keyTy = getMaxAlignKey(ctx: getContext());
1656 for (mlir::DataLayoutEntryInterface entry : entries) {
1657 if (!entry.isTypeEntry())
1658 continue;
1659 auto key = mlir::cast<VectorType>(Val: mlir::cast<mlir::Type>(Val: entry.getKey()));
1660 if (key != keyTy)
1661 return mlir::emitError(loc)
1662 << "expected vector data layout entry to be keyed on " << keyTy
1663 << ", got " << key;
1664 if (!mlir::isa<cir::VectorSpecAttr>(Val: entry.getValue()))
1665 return mlir::emitError(loc) << "expected layout attribute for " << key
1666 << " to be a #cir.vector_spec attribute";
1667 }
1668 return mlir::success();
1669}
1670
1671mlir::LogicalResult cir::VectorType::verify(
1672 llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
1673 mlir::Type elementType, llvm::ElementCount size) {
1674 if (size.isZero())
1675 return emitError() << "the number of vector elements must be non-zero";
1676 return success();
1677}
1678
1679mlir::Type cir::VectorType::parse(::mlir::AsmParser &odsParser) {
1680
1681 llvm::SMLoc odsLoc = odsParser.getCurrentLocation();
1682 mlir::Builder odsBuilder(odsParser.getContext());
1683 mlir::FailureOr<::mlir::Type> elementType;
1684 mlir::FailureOr<uint64_t> size;
1685 bool isScalable = false;
1686
1687 // Parse literal '<'
1688 if (odsParser.parseLess())
1689 return {};
1690
1691 // Parse literal '[', if present, and set the scalability flag accordingly
1692 if (odsParser.parseOptionalLSquare().succeeded())
1693 isScalable = true;
1694
1695 // Parse variable 'size'
1696 size = mlir::FieldParser<uint64_t>::parse(parser&: odsParser);
1697 if (mlir::failed(Result: size)) {
1698 odsParser.emitError(loc: odsParser.getCurrentLocation(),
1699 message: "failed to parse CIR_VectorType parameter 'size' which "
1700 "is to be a `uint64_t`");
1701 return {};
1702 }
1703
1704 // Parse literal ']', which is expected when dealing with scalable
1705 // dim sizes
1706 if (isScalable && odsParser.parseRSquare().failed()) {
1707 odsParser.emitError(loc: odsParser.getCurrentLocation(),
1708 message: "missing closing `]` for scalable dim size");
1709 return {};
1710 }
1711
1712 // Parse literal 'x'
1713 if (odsParser.parseKeyword(keyword: "x"))
1714 return {};
1715
1716 // Parse variable 'elementType'
1717 elementType = mlir::FieldParser<::mlir::Type>::parse(parser&: odsParser);
1718 if (mlir::failed(Result: elementType)) {
1719 odsParser.emitError(loc: odsParser.getCurrentLocation(),
1720 message: "failed to parse CIR_VectorType parameter "
1721 "'elementType' which is to be a `mlir::Type`");
1722 return {};
1723 }
1724
1725 // Parse literal '>'
1726 if (odsParser.parseGreater())
1727 return {};
1728 return odsParser.getChecked<VectorType>(
1729 loc: odsLoc, params: odsParser.getContext(), params: mlir::Type((*elementType)),
1730 params: llvm::ElementCount::get(MinVal: *size, Scalable: isScalable));
1731}
1732
1733void cir::VectorType::print(mlir::AsmPrinter &odsPrinter) const {
1734 mlir::Builder odsBuilder(getContext());
1735 odsPrinter << "<";
1736 if (isScalable())
1737 odsPrinter << "[";
1738
1739 odsPrinter.printStrippedAttrOrType(attrOrType: getSize().getKnownMinValue());
1740 if (isScalable())
1741 odsPrinter << "]";
1742 odsPrinter << ' ' << "x";
1743 odsPrinter << ' ';
1744 odsPrinter.printStrippedAttrOrType(attrOrType: getElementType());
1745 odsPrinter << ">";
1746}
1747
1748//===----------------------------------------------------------------------===//
1749// MatrixType Definitions
1750//===----------------------------------------------------------------------===//
1751
1752llvm::TypeSize cir::MatrixType::getTypeSizeInBits(
1753 const ::mlir::DataLayout &dataLayout,
1754 ::mlir::DataLayoutEntryListRef params) const {
1755 return llvm::TypeSize::getFixed(
1756 ExactSize: getNumRows() * getNumColumns() *
1757 dataLayout.getTypeSizeInBits(t: getElementType()));
1758}
1759
1760uint64_t
1761cir::MatrixType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
1762 ::mlir::DataLayoutEntryListRef params) const {
1763 return dataLayout.getTypeABIAlignment(t: getElementType());
1764}
1765
1766mlir::LogicalResult cir::MatrixType::verify(
1767 llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
1768 mlir::Type elementType, uint64_t row, uint64_t column) {
1769 if (row == 0)
1770 return emitError() << "the number of matrix rows must be non-zero";
1771 if (column == 0)
1772 return emitError() << "the number of matrix columns must be non-zero";
1773 return success();
1774}
1775
1776//===----------------------------------------------------------------------===//
1777// AddressSpace definitions
1778//===----------------------------------------------------------------------===//
1779
1780bool cir::isSupportedCIRMemorySpaceAttr(
1781 mlir::ptr::MemorySpaceAttrInterface memorySpace) {
1782 return mlir::isa<cir::LangAddressSpaceAttr, cir::TargetAddressSpaceAttr>(
1783 Val: memorySpace);
1784}
1785
1786cir::LangAddressSpace cir::toCIRLangAddressSpace(clang::LangAS langAS) {
1787 using clang::LangAS;
1788 switch (langAS) {
1789 case LangAS::Default:
1790 return LangAddressSpace::Default;
1791 case LangAS::opencl_global:
1792 case LangAS::sycl_global:
1793 return LangAddressSpace::OffloadGlobal;
1794 case LangAS::opencl_local:
1795 case LangAS::cuda_shared:
1796 case LangAS::sycl_local:
1797 // Local means local among the work-group (OpenCL, SYCL) or block (CUDA).
1798 // All threads inside the kernel can access local memory.
1799 return LangAddressSpace::OffloadLocal;
1800 case LangAS::cuda_device:
1801 return LangAddressSpace::OffloadGlobal;
1802 case LangAS::opencl_constant:
1803 case LangAS::cuda_constant:
1804 case LangAS::sycl_constant:
1805 return LangAddressSpace::OffloadConstant;
1806 case LangAS::opencl_private:
1807 case LangAS::sycl_private:
1808 return LangAddressSpace::OffloadPrivate;
1809 case LangAS::opencl_generic:
1810 case LangAS::sycl_generic:
1811 return LangAddressSpace::OffloadGeneric;
1812 case LangAS::opencl_global_device:
1813 case LangAS::sycl_global_device:
1814 return LangAddressSpace::OffloadGlobalDevice;
1815 case LangAS::opencl_global_host:
1816 case LangAS::sycl_global_host:
1817 return LangAddressSpace::OffloadGlobalHost;
1818 case LangAS::ptr32_sptr:
1819 case LangAS::ptr32_uptr:
1820 case LangAS::ptr64:
1821 case LangAS::hlsl_groupshared:
1822 case LangAS::wasm_funcref:
1823 llvm_unreachable("NYI");
1824 default:
1825 llvm_unreachable("unknown/unsupported clang language address space");
1826 }
1827}
1828
1829mlir::ParseResult parseMemorySpace(mlir::AsmParser &p,
1830 mlir::ptr::MemorySpaceAttrInterface &attr) {
1831
1832 llvm::SMLoc loc = p.getCurrentLocation();
1833
1834 // Try to parse target address space first.
1835 attr = nullptr;
1836 if (p.parseOptionalKeyword(keyword: "target_address_space").succeeded()) {
1837 unsigned val;
1838 if (p.parseLParen())
1839 return p.emitError(loc, message: "expected '(' after 'target_address_space'");
1840
1841 if (p.parseInteger(result&: val))
1842 return p.emitError(loc, message: "expected target address space value");
1843
1844 if (p.parseRParen())
1845 return p.emitError(loc, message: "expected ')'");
1846
1847 attr = cir::TargetAddressSpaceAttr::get(context: p.getContext(), value: val);
1848 return mlir::success();
1849 }
1850
1851 // Try to parse language specific address space.
1852 if (p.parseOptionalKeyword(keyword: "lang_address_space").succeeded()) {
1853 if (p.parseLParen())
1854 return p.emitError(loc, message: "expected '(' after 'lang_address_space'");
1855
1856 mlir::FailureOr<cir::LangAddressSpace> result =
1857 mlir::FieldParser<cir::LangAddressSpace>::parse(parser&: p);
1858 if (mlir::failed(Result: result))
1859 return mlir::failure();
1860
1861 if (p.parseRParen())
1862 return p.emitError(loc, message: "expected ')'");
1863
1864 attr = cir::LangAddressSpaceAttr::get(context: p.getContext(), value: result.value());
1865 return mlir::success();
1866 }
1867
1868 llvm::StringRef keyword;
1869 if (p.parseOptionalKeyword(keyword: &keyword).succeeded())
1870 return p.emitError(loc, message: "unknown address space specifier '")
1871 << keyword << "'; expected 'target_address_space' or "
1872 << "'lang_address_space'";
1873
1874 return mlir::success();
1875}
1876
1877void printMemorySpace(mlir::AsmPrinter &p,
1878 mlir::ptr::MemorySpaceAttrInterface attr) {
1879 if (!attr)
1880 return;
1881
1882 if (auto language = dyn_cast<cir::LangAddressSpaceAttr>(Val&: attr)) {
1883 p << "lang_address_space("
1884 << cir::stringifyLangAddressSpace(language.getValue()) << ')';
1885 return;
1886 }
1887
1888 if (auto target = dyn_cast<cir::TargetAddressSpaceAttr>(Val&: attr)) {
1889 p << "target_address_space(" << target.getValue() << ')';
1890 return;
1891 }
1892
1893 llvm_unreachable("unexpected address-space attribute kind");
1894}
1895
1896mlir::OptionalParseResult
1897cir::parseGlobalMemorySpace(mlir::AsmParser &p,
1898 mlir::ptr::MemorySpaceAttrInterface &attr) {
1899
1900 mlir::SMLoc loc = p.getCurrentLocation();
1901 if (parseMemorySpace(p, attr).failed())
1902 return p.emitError(loc, message: "failed to parse Address Space Value for GlobalOp");
1903 return mlir::success();
1904}
1905
1906void cir::printGlobalMemorySpace(mlir::AsmPrinter &printer, cir::GlobalOp,
1907 mlir::ptr::MemorySpaceAttrInterface attr) {
1908 printMemorySpace(p&: printer, attr);
1909}
1910
1911mlir::ptr::MemorySpaceAttrInterface cir::normalizeDefaultAddressSpace(
1912 mlir::ptr::MemorySpaceAttrInterface addrSpace) {
1913 if (auto langAS =
1914 mlir::dyn_cast_if_present<cir::LangAddressSpaceAttr>(Val&: addrSpace))
1915 if (langAS.getValue() == cir::LangAddressSpace::Default)
1916 return {};
1917 return addrSpace;
1918}
1919
1920mlir::ptr::MemorySpaceAttrInterface
1921cir::toCIRAddressSpaceAttr(mlir::MLIRContext &ctx, clang::LangAS langAS) {
1922 using clang::LangAS;
1923
1924 if (langAS == LangAS::Default)
1925 return cir::LangAddressSpaceAttr::get(context: &ctx, value: cir::LangAddressSpace::Default);
1926
1927 if (clang::isTargetAddressSpace(AS: langAS)) {
1928 unsigned targetAS = clang::toTargetAddressSpace(AS: langAS);
1929 return cir::TargetAddressSpaceAttr::get(context: &ctx, value: targetAS);
1930 }
1931
1932 return cir::LangAddressSpaceAttr::get(context: &ctx, value: toCIRLangAddressSpace(langAS));
1933}
1934
1935bool cir::isMatchingAddressSpace(mlir::ptr::MemorySpaceAttrInterface cirAS,
1936 clang::LangAS as) {
1937 cirAS = normalizeDefaultAddressSpace(addrSpace: cirAS);
1938 if (!cirAS)
1939 return as == clang::LangAS::Default;
1940 mlir::ptr::MemorySpaceAttrInterface expected = normalizeDefaultAddressSpace(
1941 addrSpace: toCIRAddressSpaceAttr(ctx&: *cirAS.getContext(), langAS: as));
1942 return expected == cirAS;
1943}
1944
1945//===----------------------------------------------------------------------===//
1946// PointerType Definitions
1947//===----------------------------------------------------------------------===//
1948
1949mlir::LogicalResult cir::PointerType::verify(
1950 llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
1951 mlir::Type pointee, mlir::ptr::MemorySpaceAttrInterface addrSpace) {
1952 if (addrSpace) {
1953 if (!isSupportedCIRMemorySpaceAttr(memorySpace: addrSpace)) {
1954 return emitError() << "unsupported address space attribute; expected "
1955 "'target_address_space' or 'lang_address_space'";
1956 }
1957 }
1958
1959 return success();
1960}
1961
1962//===----------------------------------------------------------------------===//
1963// CIR Dialect
1964//===----------------------------------------------------------------------===//
1965
1966void CIRDialect::registerTypes() {
1967 // Register tablegen'd types.
1968 addTypes<
1969#define GET_TYPEDEF_LIST
1970#include "clang/CIR/Dialect/IR/CIROpsTypes.cpp.inc"
1971 >();
1972
1973 // Register raw C++ types.
1974 // TODO(CIR) addTypes<RecordType>();
1975}
1976