-
Notifications
You must be signed in to change notification settings - Fork 85
/
CIRTypes.cpp
879 lines (732 loc) · 28.6 KB
/
CIRTypes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
//===- CIRTypes.cpp - MLIR CIR Types --------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines the types in the CIR dialect.
//
//===----------------------------------------------------------------------===//
#include "MissingFeatures.h"
#include "clang/CIR/Dialect/IR/CIRTypes.h"
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/IR/CIRTypesDetails.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Diagnostics.h"
#include "mlir/IR/DialectImplementation.h"
#include "mlir/Interfaces/DataLayoutInterfaces.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Support/LogicalResult.h"
#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Support/ErrorHandling.h"
#include <optional>
using cir::MissingFeatures;
//===----------------------------------------------------------------------===//
// CIR Custom Parser/Printer Signatures
//===----------------------------------------------------------------------===//
static mlir::ParseResult
parseFuncTypeArgs(mlir::AsmParser &p, llvm::SmallVector<mlir::Type> ¶ms,
bool &isVarArg);
static void printFuncTypeArgs(mlir::AsmPrinter &p,
mlir::ArrayRef<mlir::Type> params, bool isVarArg);
//===----------------------------------------------------------------------===//
// Get autogenerated stuff
//===----------------------------------------------------------------------===//
#define GET_TYPEDEF_CLASSES
#include "clang/CIR/Dialect/IR/CIROpsTypes.cpp.inc"
using namespace mlir;
using namespace mlir::cir;
//===----------------------------------------------------------------------===//
// General CIR parsing / printing
//===----------------------------------------------------------------------===//
Type CIRDialect::parseType(DialectAsmParser &parser) const {
llvm::SMLoc typeLoc = parser.getCurrentLocation();
StringRef mnemonic;
Type genType;
// Try to parse as a tablegen'd type.
OptionalParseResult parseResult =
generatedTypeParser(parser, &mnemonic, genType);
if (parseResult.has_value())
return genType;
// Type is not tablegen'd: try to parse as a raw C++ type.
return StringSwitch<function_ref<Type()>>(mnemonic)
.Case("struct", [&] { return StructType::parse(parser); })
.Default([&] {
parser.emitError(typeLoc) << "unknown CIR type: " << mnemonic;
return Type();
})();
}
void CIRDialect::printType(Type type, DialectAsmPrinter &os) const {
// Try to print as a tablegen'd type.
if (generatedTypePrinter(type, os).succeeded())
return;
// Type is not tablegen'd: try printing as a raw C++ type.
TypeSwitch<Type>(type)
.Case<StructType>([&](StructType type) {
os << type.getMnemonic();
type.print(os);
})
.Default([](Type) {
llvm::report_fatal_error("printer is missing a handler for this type");
});
}
Type BoolType::parse(mlir::AsmParser &parser) {
return get(parser.getContext());
}
void BoolType::print(mlir::AsmPrinter &printer) const {}
//===----------------------------------------------------------------------===//
// StructType Definitions
//===----------------------------------------------------------------------===//
/// Return the largest member of in the type.
///
/// Recurses into union members never returning a union as the largest member.
Type StructType::getLargestMember(const ::mlir::DataLayout &dataLayout) const {
if (!layoutInfo)
computeSizeAndAlignment(dataLayout);
return layoutInfo.cast<mlir::cir::StructLayoutAttr>().getLargestMember();
}
Type StructType::parse(mlir::AsmParser &parser) {
FailureOr<AsmParser::CyclicParseReset> cyclicParseGuard;
const auto loc = parser.getCurrentLocation();
const auto eLoc = parser.getEncodedSourceLoc(loc);
bool packed = false;
RecordKind kind;
auto *context = parser.getContext();
if (parser.parseLess())
return {};
// TODO(cir): in the future we should probably separate types for different
// source language declarations such as cir.class, cir.union, and cir.struct
if (parser.parseOptionalKeyword("struct").succeeded())
kind = RecordKind::Struct;
else if (parser.parseOptionalKeyword("union").succeeded())
kind = RecordKind::Union;
else if (parser.parseOptionalKeyword("class").succeeded())
kind = RecordKind::Class;
else {
parser.emitError(loc, "unknown struct type");
return {};
}
mlir::StringAttr name;
parser.parseOptionalAttribute(name);
// Is a self reference: ensure referenced type was parsed.
if (name && parser.parseOptionalGreater().succeeded()) {
auto type = getChecked(eLoc, context, name, kind);
if (succeeded(parser.tryStartCyclicParse(type))) {
parser.emitError(loc, "invalid self-reference within record");
return {};
}
return type;
}
// Is a named record definition: ensure name has not been parsed yet.
if (name) {
auto type = getChecked(eLoc, context, name, kind);
cyclicParseGuard = parser.tryStartCyclicParse(type);
if (failed(cyclicParseGuard)) {
parser.emitError(loc, "record already defined");
return {};
}
}
if (parser.parseOptionalKeyword("packed").succeeded())
packed = true;
// Parse record members or lack thereof.
bool incomplete = true;
llvm::SmallVector<mlir::Type> members;
if (parser.parseOptionalKeyword("incomplete").failed()) {
incomplete = false;
const auto delimiter = AsmParser::Delimiter::Braces;
const auto parseElementFn = [&parser, &members]() {
return parser.parseType(members.emplace_back());
};
if (parser.parseCommaSeparatedList(delimiter, parseElementFn).failed())
return {};
}
// Parse optional AST attribute. This is just a formality for now, since CIR
// cannot yet read serialized AST.
mlir::cir::ASTRecordDeclAttr ast = nullptr;
parser.parseOptionalAttribute(ast);
if (parser.parseGreater())
return {};
// Try to create the proper record type.
ArrayRef<mlir::Type> membersRef(members); // Needed for template deduction.
mlir::Type type = {};
if (name && incomplete) { // Identified & incomplete
type = getChecked(eLoc, context, name, kind);
} else if (name && !incomplete) { // Identified & complete
type = getChecked(eLoc, context, membersRef, name, packed, kind);
// If the record has a self-reference, its type already exists in a
// incomplete state. In this case, we must complete it.
if (type.cast<StructType>().isIncomplete())
type.cast<StructType>().complete(membersRef, packed, ast);
} else if (!name && !incomplete) { // anonymous & complete
type = getChecked(eLoc, context, membersRef, packed, kind);
} else { // anonymous & incomplete
parser.emitError(loc, "anonymous structs must be complete");
return {};
}
return type;
}
void StructType::print(mlir::AsmPrinter &printer) const {
FailureOr<AsmPrinter::CyclicPrintReset> cyclicPrintGuard;
printer << '<';
switch (getKind()) {
case RecordKind::Struct:
printer << "struct ";
break;
case RecordKind::Union:
printer << "union ";
break;
case RecordKind::Class:
printer << "class ";
break;
}
if (getName())
printer << getName();
// Current type has already been printed: print as self reference.
cyclicPrintGuard = printer.tryStartCyclicPrint(*this);
if (failed(cyclicPrintGuard)) {
printer << '>';
return;
}
// Type not yet printed: continue printing the entire record.
printer << ' ';
if (getPacked())
printer << "packed ";
if (isIncomplete()) {
printer << "incomplete";
} else {
printer << "{";
llvm::interleaveComma(getMembers(), printer);
printer << "}";
}
if (getAst()) {
printer << " ";
printer.printAttribute(getAst());
}
printer << '>';
}
mlir::LogicalResult
StructType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
llvm::ArrayRef<mlir::Type> members, mlir::StringAttr name,
bool incomplete, bool packed,
mlir::cir::StructType::RecordKind kind,
ASTRecordDeclInterface ast) {
if (name && name.getValue().empty()) {
emitError() << "identified structs cannot have an empty name";
return mlir::failure();
}
return mlir::success();
}
void StructType::dropAst() { getImpl()->ast = nullptr; }
StructType StructType::get(::mlir::MLIRContext *context, ArrayRef<Type> members,
StringAttr name, bool packed, RecordKind kind,
ASTRecordDeclInterface ast) {
return Base::get(context, members, name, /*incomplete=*/false, packed, kind,
ast);
}
StructType StructType::getChecked(
::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError,
::mlir::MLIRContext *context, ArrayRef<Type> members, StringAttr name,
bool packed, RecordKind kind, ASTRecordDeclInterface ast) {
return Base::getChecked(emitError, context, members, name,
/*incomplete=*/false, packed, kind, ast);
}
StructType StructType::get(::mlir::MLIRContext *context, StringAttr name,
RecordKind kind) {
return Base::get(context, /*members=*/ArrayRef<Type>{}, name,
/*incomplete=*/true, /*packed=*/false, kind,
/*ast=*/ASTRecordDeclInterface{});
}
StructType StructType::getChecked(
::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError,
::mlir::MLIRContext *context, StringAttr name, RecordKind kind) {
return Base::getChecked(emitError, context, ArrayRef<Type>{}, name,
/*incomplete=*/true, /*packed=*/false, kind,
ASTRecordDeclInterface{});
}
StructType StructType::get(::mlir::MLIRContext *context, ArrayRef<Type> members,
bool packed, RecordKind kind,
ASTRecordDeclInterface ast) {
return Base::get(context, members, StringAttr{}, /*incomplete=*/false, packed,
kind, ast);
}
StructType StructType::getChecked(
::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError,
::mlir::MLIRContext *context, ArrayRef<Type> members, bool packed,
RecordKind kind, ASTRecordDeclInterface ast) {
return Base::getChecked(emitError, context, members, StringAttr{},
/*incomplete=*/false, packed, kind, ast);
}
::llvm::ArrayRef<mlir::Type> StructType::getMembers() const {
return getImpl()->members;
}
bool StructType::isIncomplete() const { return getImpl()->incomplete; }
mlir::StringAttr StructType::getName() const { return getImpl()->name; }
bool StructType::getIncomplete() const { return getImpl()->incomplete; }
bool StructType::getPacked() const { return getImpl()->packed; }
mlir::cir::StructType::RecordKind StructType::getKind() const {
return getImpl()->kind;
}
ASTRecordDeclInterface StructType::getAst() const { return getImpl()->ast; }
void StructType::complete(ArrayRef<Type> members, bool packed,
ASTRecordDeclInterface ast) {
if (mutate(members, packed, ast).failed())
llvm_unreachable("failed to complete struct");
}
bool StructType::isLayoutIdentical(const StructType &other) {
if (getImpl() == other.getImpl())
return true;
if (getPacked() != other.getPacked())
return false;
return getMembers() == other.getMembers();
}
//===----------------------------------------------------------------------===//
// Data Layout information for types
//===----------------------------------------------------------------------===//
llvm::TypeSize
BoolType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
return llvm::TypeSize::getFixed(8);
}
uint64_t
BoolType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
return 1;
}
uint64_t
BoolType::getPreferredAlignment(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
return 1;
}
llvm::TypeSize
PointerType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
// FIXME: improve this in face of address spaces
return llvm::TypeSize::getFixed(64);
}
uint64_t
PointerType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
// FIXME: improve this in face of address spaces
return 8;
}
uint64_t PointerType::getPreferredAlignment(
const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
// FIXME: improve this in face of address spaces
return 8;
}
llvm::TypeSize
DataMemberType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
// FIXME: consider size differences under different ABIs
assert(!MissingFeatures::cxxABI());
return llvm::TypeSize::getFixed(64);
}
uint64_t
DataMemberType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
// FIXME: consider alignment differences under different ABIs
assert(!MissingFeatures::cxxABI());
return 8;
}
uint64_t DataMemberType::getPreferredAlignment(
const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
// FIXME: consider alignment differences under different ABIs
assert(!MissingFeatures::cxxABI());
return 8;
}
llvm::TypeSize
ArrayType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
return getSize() * dataLayout.getTypeSizeInBits(getEltType());
}
uint64_t
ArrayType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
return dataLayout.getTypeABIAlignment(getEltType());
}
uint64_t
ArrayType::getPreferredAlignment(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
return dataLayout.getTypePreferredAlignment(getEltType());
}
llvm::TypeSize mlir::cir::VectorType::getTypeSizeInBits(
const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
return llvm::TypeSize::getFixed(getSize() *
dataLayout.getTypeSizeInBits(getEltType()));
}
uint64_t mlir::cir::VectorType::getABIAlignment(
const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
return getSize() * dataLayout.getTypeABIAlignment(getEltType());
}
uint64_t mlir::cir::VectorType::getPreferredAlignment(
const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
return getSize() * dataLayout.getTypePreferredAlignment(getEltType());
}
llvm::TypeSize
StructType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
if (!layoutInfo)
computeSizeAndAlignment(dataLayout);
return llvm::TypeSize::getFixed(
layoutInfo.cast<mlir::cir::StructLayoutAttr>().getSize() * 8);
}
uint64_t
StructType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
if (!layoutInfo)
computeSizeAndAlignment(dataLayout);
return layoutInfo.cast<mlir::cir::StructLayoutAttr>().getAlignment();
}
uint64_t
StructType::getPreferredAlignment(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
llvm_unreachable("NYI");
}
bool StructType::isPadded(const ::mlir::DataLayout &dataLayout) const {
if (!layoutInfo)
computeSizeAndAlignment(dataLayout);
return layoutInfo.cast<mlir::cir::StructLayoutAttr>().getPadded();
}
uint64_t StructType::getElementOffset(const ::mlir::DataLayout &dataLayout,
unsigned idx) const {
assert(idx < getMembers().size() && "access not valid");
if (!layoutInfo)
computeSizeAndAlignment(dataLayout);
auto offsets = layoutInfo.cast<mlir::cir::StructLayoutAttr>().getOffsets();
auto intAttr = offsets[idx].cast<mlir::IntegerAttr>();
return intAttr.getInt();
}
void StructType::computeSizeAndAlignment(
const ::mlir::DataLayout &dataLayout) const {
assert(isComplete() && "Cannot get layout of incomplete structs");
// Do not recompute.
if (layoutInfo)
return;
// This is a similar algorithm to LLVM's StructLayout.
unsigned structSize = 0;
llvm::Align structAlignment{1};
bool isPadded = false;
unsigned numElements = getNumElements();
auto members = getMembers();
mlir::Type largestMember;
unsigned largestMemberSize = 0;
SmallVector<mlir::Attribute, 4> memberOffsets;
// Loop over each of the elements, placing them in memory.
memberOffsets.reserve(numElements);
for (unsigned i = 0, e = numElements; i != e; ++i) {
auto ty = members[i];
// Found a nested union: recurse into it to fetch its largest member.
auto structMember = ty.dyn_cast<StructType>();
if (structMember && structMember.isUnion()) {
auto candidate = structMember.getLargestMember(dataLayout);
if (dataLayout.getTypeSize(candidate) > largestMemberSize) {
largestMember = candidate;
largestMemberSize = dataLayout.getTypeSize(largestMember);
}
} else if (dataLayout.getTypeSize(ty) > largestMemberSize) {
largestMember = ty;
largestMemberSize = dataLayout.getTypeSize(largestMember);
}
// This matches LLVM since it uses the ABI instead of preferred alignment.
const llvm::Align tyAlign =
llvm::Align(getPacked() ? 1 : dataLayout.getTypeABIAlignment(ty));
// Add padding if necessary to align the data element properly.
if (!llvm::isAligned(tyAlign, structSize)) {
isPadded = true;
structSize = llvm::alignTo(structSize, tyAlign);
}
// Keep track of maximum alignment constraint.
structAlignment = std::max(tyAlign, structAlignment);
// Struct size up to each element is the element offset.
memberOffsets.push_back(mlir::IntegerAttr::get(
mlir::IntegerType::get(getContext(), 32), structSize));
// Consume space for this data item
structSize += dataLayout.getTypeSize(ty);
}
// For unions, the size and aligment is that of the largest element.
if (isUnion()) {
structSize = largestMemberSize;
isPadded = false;
} else {
// Add padding to the end of the struct so that it could be put in an array
// and all array elements would be aligned correctly.
if (!llvm::isAligned(structAlignment, structSize)) {
isPadded = true;
structSize = llvm::alignTo(structSize, structAlignment);
}
}
auto offsets = mlir::ArrayAttr::get(getContext(), memberOffsets);
layoutInfo = mlir::cir::StructLayoutAttr::get(
getContext(), structSize, structAlignment.value(), isPadded,
largestMember, offsets);
}
//===----------------------------------------------------------------------===//
// IntType Definitions
//===----------------------------------------------------------------------===//
Type IntType::parse(mlir::AsmParser &parser) {
auto *context = parser.getBuilder().getContext();
auto loc = parser.getCurrentLocation();
bool isSigned;
unsigned width;
if (parser.parseLess())
return {};
// Fetch integer sign.
llvm::StringRef sign;
if (parser.parseKeyword(&sign))
return {};
if (sign.equals("s"))
isSigned = true;
else if (sign.equals("u"))
isSigned = false;
else {
parser.emitError(loc, "expected 's' or 'u'");
return {};
}
if (parser.parseComma())
return {};
// Fetch integer size.
if (parser.parseInteger(width))
return {};
if (width < 1 || width > 64) {
parser.emitError(loc, "expected integer width to be from 1 up to 64");
return {};
}
if (parser.parseGreater())
return {};
return IntType::get(context, width, isSigned);
}
void IntType::print(mlir::AsmPrinter &printer) const {
auto sign = isSigned() ? 's' : 'u';
printer << '<' << sign << ", " << getWidth() << '>';
}
llvm::TypeSize
IntType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
mlir::DataLayoutEntryListRef params) const {
return llvm::TypeSize::getFixed(getWidth());
}
uint64_t IntType::getABIAlignment(const mlir::DataLayout &dataLayout,
mlir::DataLayoutEntryListRef params) const {
return (uint64_t)(getWidth() / 8);
}
uint64_t
IntType::getPreferredAlignment(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
return (uint64_t)(getWidth() / 8);
}
mlir::LogicalResult
IntType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
unsigned width, bool isSigned) {
if (width < IntType::minBitwidth() || width > IntType::maxBitwidth()) {
emitError() << "IntType only supports widths from "
<< IntType::minBitwidth() << "up to " << IntType::maxBitwidth();
return mlir::failure();
}
return mlir::success();
}
//===----------------------------------------------------------------------===//
// Floating-point type definitions
//===----------------------------------------------------------------------===//
const llvm::fltSemantics &SingleType::getFloatSemantics() const {
return llvm::APFloat::IEEEsingle();
}
llvm::TypeSize
SingleType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
mlir::DataLayoutEntryListRef params) const {
return llvm::TypeSize::getFixed(getWidth());
}
uint64_t
SingleType::getABIAlignment(const mlir::DataLayout &dataLayout,
mlir::DataLayoutEntryListRef params) const {
return (uint64_t)(getWidth() / 8);
}
uint64_t
SingleType::getPreferredAlignment(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
return (uint64_t)(getWidth() / 8);
}
const llvm::fltSemantics &DoubleType::getFloatSemantics() const {
return llvm::APFloat::IEEEdouble();
}
llvm::TypeSize
DoubleType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
mlir::DataLayoutEntryListRef params) const {
return llvm::TypeSize::getFixed(getWidth());
}
uint64_t
DoubleType::getABIAlignment(const mlir::DataLayout &dataLayout,
mlir::DataLayoutEntryListRef params) const {
return (uint64_t)(getWidth() / 8);
}
uint64_t
DoubleType::getPreferredAlignment(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
return (uint64_t)(getWidth() / 8);
}
const llvm::fltSemantics &FP16Type::getFloatSemantics() const {
return llvm::APFloat::IEEEhalf();
}
llvm::TypeSize
FP16Type::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
mlir::DataLayoutEntryListRef params) const {
return llvm::TypeSize::getFixed(getWidth());
}
uint64_t FP16Type::getABIAlignment(const mlir::DataLayout &dataLayout,
mlir::DataLayoutEntryListRef params) const {
return (uint64_t)(getWidth() / 8);
}
uint64_t
FP16Type::getPreferredAlignment(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
return (uint64_t)(getWidth() / 8);
}
const llvm::fltSemantics &BF16Type::getFloatSemantics() const {
return llvm::APFloat::BFloat();
}
llvm::TypeSize
BF16Type::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
mlir::DataLayoutEntryListRef params) const {
return llvm::TypeSize::getFixed(getWidth());
}
uint64_t BF16Type::getABIAlignment(const mlir::DataLayout &dataLayout,
mlir::DataLayoutEntryListRef params) const {
return (uint64_t)(getWidth() / 8);
}
uint64_t
BF16Type::getPreferredAlignment(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
return (uint64_t)(getWidth() / 8);
}
const llvm::fltSemantics &FP80Type::getFloatSemantics() const {
return llvm::APFloat::x87DoubleExtended();
}
llvm::TypeSize
FP80Type::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
mlir::DataLayoutEntryListRef params) const {
return llvm::TypeSize::getFixed(16);
}
uint64_t FP80Type::getABIAlignment(const mlir::DataLayout &dataLayout,
mlir::DataLayoutEntryListRef params) const {
return 16;
}
uint64_t
FP80Type::getPreferredAlignment(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
return 16;
}
const llvm::fltSemantics &LongDoubleType::getFloatSemantics() const {
return getUnderlying()
.cast<mlir::cir::CIRFPTypeInterface>()
.getFloatSemantics();
}
llvm::TypeSize
LongDoubleType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
mlir::DataLayoutEntryListRef params) const {
return getUnderlying()
.cast<mlir::DataLayoutTypeInterface>()
.getTypeSizeInBits(dataLayout, params);
}
uint64_t
LongDoubleType::getABIAlignment(const mlir::DataLayout &dataLayout,
mlir::DataLayoutEntryListRef params) const {
return getUnderlying().cast<mlir::DataLayoutTypeInterface>().getABIAlignment(
dataLayout, params);
}
uint64_t LongDoubleType::getPreferredAlignment(
const ::mlir::DataLayout &dataLayout,
mlir::DataLayoutEntryListRef params) const {
return getUnderlying()
.cast<mlir::DataLayoutTypeInterface>()
.getPreferredAlignment(dataLayout, params);
}
LogicalResult
LongDoubleType::verify(function_ref<InFlightDiagnostic()> emitError,
mlir::Type underlying) {
if (!underlying.isa<DoubleType, FP80Type>()) {
emitError() << "invalid underlying type for long double";
return failure();
}
return success();
}
//===----------------------------------------------------------------------===//
// Floating-point type helpers
//===----------------------------------------------------------------------===//
bool mlir::cir::isAnyFloatingPointType(mlir::Type t) {
return isa<mlir::cir::SingleType, mlir::cir::DoubleType,
mlir::cir::LongDoubleType, mlir::cir::FP80Type>(t);
}
//===----------------------------------------------------------------------===//
// FuncType Definitions
//===----------------------------------------------------------------------===//
FuncType FuncType::clone(TypeRange inputs, TypeRange results) const {
assert(results.size() == 1 && "expected exactly one result type");
return get(llvm::to_vector(inputs), results[0], isVarArg());
}
mlir::ParseResult parseFuncTypeArgs(mlir::AsmParser &p,
llvm::SmallVector<mlir::Type> ¶ms,
bool &isVarArg) {
isVarArg = false;
// `(` `)`
if (succeeded(p.parseOptionalRParen()))
return mlir::success();
// `(` `...` `)`
if (succeeded(p.parseOptionalEllipsis())) {
isVarArg = true;
return p.parseRParen();
}
// type (`,` type)* (`,` `...`)?
mlir::Type type;
if (p.parseType(type))
return mlir::failure();
params.push_back(type);
while (succeeded(p.parseOptionalComma())) {
if (succeeded(p.parseOptionalEllipsis())) {
isVarArg = true;
return p.parseRParen();
}
if (p.parseType(type))
return mlir::failure();
params.push_back(type);
}
return p.parseRParen();
}
void printFuncTypeArgs(mlir::AsmPrinter &p, mlir::ArrayRef<mlir::Type> params,
bool isVarArg) {
llvm::interleaveComma(params, p,
[&p](mlir::Type type) { p.printType(type); });
if (isVarArg) {
if (!params.empty())
p << ", ";
p << "...";
}
p << ')';
}
llvm::ArrayRef<mlir::Type> FuncType::getReturnTypes() const {
return static_cast<detail::FuncTypeStorage *>(getImpl())->returnType;
}
bool FuncType::isVoid() const { return getReturnType().isa<VoidType>(); }
//===----------------------------------------------------------------------===//
// CIR Dialect
//===----------------------------------------------------------------------===//
void CIRDialect::registerTypes() {
// Register tablegen'd types.
addTypes<
#define GET_TYPEDEF_LIST
#include "clang/CIR/Dialect/IR/CIROpsTypes.cpp.inc"
>();
// Register raw C++ types.
addTypes<StructType>();
}