clang 24.0.0git
CIRTypes.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 declares the types in the CIR dialect.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef CLANG_CIR_DIALECT_IR_CIRTYPES_H
14#define CLANG_CIR_DIALECT_IR_CIRTYPES_H
15
16#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
17#include "mlir/IR/Attributes.h"
18#include "mlir/IR/BuiltinAttributes.h"
19#include "mlir/IR/MLIRContext.h"
20#include "mlir/IR/Types.h"
21#include "mlir/Interfaces/DataLayoutInterfaces.h"
26#include "llvm/ADT/ArrayRef.h"
27#include "llvm/ADT/STLExtras.h"
28#include "llvm/ADT/SmallVector.h"
29#include "llvm/Support/TypeSize.h"
30
31namespace llvm {
32struct fltSemantics;
33} // namespace llvm
34
35namespace cir {
36
37namespace detail {
39struct UnionTypeStorage;
40} // namespace detail
41
42bool isValidFundamentalIntWidth(unsigned width);
43
44/// Whether a member of this kind holds data for argument passing. An `empty`
45/// member can hold data all the same, since an access unit of unnamed
46/// bit-fields takes that mark and is storage the classifier reads; use
47/// RecordType::isEmptyForABI to ask about a whole record.
48inline bool holdsDataForABI(RecordMemberKind kind) {
49 return kind == RecordMemberKind::Data || kind == RecordMemberKind::BitField;
50}
51
52/// Whether any member holds data for argument passing on its mark alone.
54 return llvm::any_of(
55 kinds, [](RecordMemberKind kind) { return holdsDataForABI(kind); });
56}
57
58/// Whether a member of this kind is an access unit the source can read a
59/// bit-field of. A unit can be narrower than the type a bit-field in it was
60/// declared with, so its extent does not answer what the source declared;
61/// `!cir.bitfield` carries that. A true answer does not mean the member is an
62/// access unit: a union's base subobject takes this mark when any variant is
63/// one, whatever its own storage type came from. A unit of nothing but
64/// unnamed bit-fields is `empty` instead.
65inline bool isNamedBitField(RecordMemberKind kind) {
66 return kind == RecordMemberKind::BitField;
67}
68
69/// Returns true if the type is a CIR sized type.
70///
71/// Types are sized if they implement SizedTypeInterface and
72/// return true from its method isSized.
73///
74/// Unsized types are those that do not have a size, such as
75/// void, or abstract types.
76bool isSized(mlir::Type ty);
77
78/// Returns true if the type is a `cir::IntType` representing a _BitInt.
79bool isBitIntType(mlir::Type ty);
80
81/// Returns the CIR floating-point type for the given semantics, or a null
82/// type if CIR has no type for it (e.g. PPCDoubleDouble or a Float8 format).
83/// Mirrors llvm::Type::getFloatingPointTy.
84cir::FPTypeInterface getFloatingPointType(const llvm::fltSemantics &sem,
85 mlir::MLIRContext *ctx);
86
87//===----------------------------------------------------------------------===//
88// AddressSpace helpers
89//===----------------------------------------------------------------------===//
90
91cir::LangAddressSpace toCIRLangAddressSpace(clang::LangAS langAS);
92
93// Compare a CIR memory space attribute with a Clang LangAS.
94bool isMatchingAddressSpace(mlir::ptr::MemorySpaceAttrInterface cirAS,
95 clang::LangAS as);
96
97/// Convert an AST LangAS to the appropriate CIR address space attribute
98/// interface.
99mlir::ptr::MemorySpaceAttrInterface
100toCIRAddressSpaceAttr(mlir::MLIRContext &ctx, clang::LangAS langAS);
101
102/// Normalize LangAddressSpace::Default to null (empty attribute).
103mlir::ptr::MemorySpaceAttrInterface
104normalizeDefaultAddressSpace(mlir::ptr::MemorySpaceAttrInterface addrSpace);
105
107 mlir::ptr::MemorySpaceAttrInterface memorySpace);
108
109} // namespace cir
110
111//===----------------------------------------------------------------------===//
112// CIR Dialect Tablegen'd Types
113//===----------------------------------------------------------------------===//
114
115namespace cir {
116
117#include "clang/CIR/Dialect/IR/CIRTypeConstraints.h.inc"
118
119} // namespace cir
120
121#define GET_TYPEDEF_CLASSES
122#include "clang/CIR/Dialect/IR/CIROpsTypes.h.inc"
123
124namespace cir {
125
126/// Whether a record member occupies bytes of its record. Every member does
127/// except a zero-width bit-field, which contributes neither size nor alignment
128/// to the record and is left out of the lowered LLVM struct body.
129inline bool memberOwnsBytes(mlir::Type memberTy) {
130 if (auto bfTy = mlir::dyn_cast<cir::BitFieldType>(memberTy))
131 return bfTy.ownsBytes();
132 return true;
133}
134
135/// The storage a member is stored as: the access unit for a bit-field member,
136/// and the member type itself for anything else. Null for a zero-width
137/// bit-field, which has no storage.
138inline mlir::Type memberStorageType(mlir::Type memberTy) {
139 if (auto bfTy = mlir::dyn_cast<cir::BitFieldType>(memberTy))
140 return bfTy.getStorageType();
141 return memberTy;
142}
143
144/// C++ view class that accepts both !cir.struct and !cir.union types.
145///
146/// Follows the MLIR BaseMemRefType pattern: StructType and UnionType are the
147/// concrete tablegen types; RecordType is a hand-written view class that
148/// covers both. Use it when code must handle either kind generically.
149///
150/// Methods that are common to both types are forwarded through dyn_cast
151/// dispatch. Type-specific methods (getPadding, getUnionStorageType) are only
152/// available on the concrete type.
153class RecordType : public mlir::Type {
154public:
155 using mlir::Type::Type;
156
157 // Allow implicit construction from concrete record types so that
158 // functions returning cir::RecordType can return StructType/UnionType
159 // values without an explicit cast.
160 // NOLINTNEXTLINE(google-explicit-constructor)
161 RecordType(StructType t) : mlir::Type(t) {}
162 // NOLINTNEXTLINE(google-explicit-constructor)
163 RecordType(UnionType t) : mlir::Type(t) {}
164
165 static bool classof(mlir::Type t) {
166 return mlir::isa<StructType>(t) || mlir::isa<UnionType>(t);
167 }
168
170 mlir::StringAttr getName() const;
171 bool isIncomplete() const;
172 bool isComplete() const { return !isIncomplete(); }
173 bool getPacked() const;
174 bool getPadded() const;
176
177 bool isClass() const;
178 bool isStruct() const;
179 bool isUnion() const { return mlir::isa<UnionType>(*this); }
180
181 /// Whether no member holds data. Vacuously true for a complete record with
182 /// no members, and false for an incomplete one, whose members are not known
183 /// yet. A union's tail-padding slot is not a member and does not count.
184 bool isEmptyForABI() const;
185
186 /// One `Data` kind per member. Takes the member list rather than a count so
187 /// the length cannot drift from the record it describes.
190
191 size_t getNumElements() const { return getMembers().size(); }
192 mlir::Type getElementType(size_t idx) const { return getMembers()[idx]; }
193 std::string getKindAsStr() const;
194 std::string getPrefixedName() const;
195
196 /// \p padding is union-only. A struct carries its padding as a member
197 /// marked pad.
198 void complete(llvm::ArrayRef<mlir::Type> members, bool packed,
199 mlir::Type padding,
201 uint64_t getElementOffset(const mlir::DataLayout &dataLayout,
202 unsigned idx) const;
203 bool isLayoutIdentical(const RecordType &other);
204
205 bool isABIConvertedRecord() const;
206 mlir::StringAttr getABIConvertedName() const;
208};
209
210} // namespace cir
211
212#endif // CLANG_CIR_DIALECT_IR_CIRTYPES_H
Provides definitions for the various language-specific address spaces.
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
C++ view class that accepts both !cir.struct and !cir.union types.
Definition CIRTypes.h:153
bool isUnion() const
Definition CIRTypes.h:179
bool isLayoutIdentical(const RecordType &other)
Definition CIRTypes.cpp:717
mlir::Type getElementType(size_t idx) const
Definition CIRTypes.h:192
bool isComplete() const
Definition CIRTypes.h:172
bool isABIConvertedRecord() const
Definition CIRTypes.cpp:730
bool isIncomplete() const
Definition CIRTypes.cpp:663
bool isEmptyForABI() const
Whether no member holds data.
Definition CIRTypes.cpp:746
std::string getPrefixedName() const
Definition CIRTypes.cpp:698
llvm::ArrayRef< mlir::Type > getMembers() const
Definition CIRTypes.cpp:653
RecordType(UnionType t)
Definition CIRTypes.h:163
bool isClass() const
Definition CIRTypes.cpp:683
void removeABIConversionNamePrefix()
Definition CIRTypes.cpp:740
static bool classof(mlir::Type t)
Definition CIRTypes.h:165
bool getPacked() const
Definition CIRTypes.cpp:668
static llvm::SmallVector< RecordMemberKind > getAllDataKinds(llvm::ArrayRef< mlir::Type > members)
One Data kind per member.
Definition CIRTypes.cpp:156
RecordType(StructType t)
Definition CIRTypes.h:161
mlir::StringAttr getName() const
Definition CIRTypes.cpp:658
void complete(llvm::ArrayRef< mlir::Type > members, bool packed, mlir::Type padding, llvm::ArrayRef< RecordMemberKind > memberKinds)
padding is union-only.
Definition CIRTypes.cpp:701
mlir::StringAttr getABIConvertedName() const
Definition CIRTypes.cpp:735
std::string getKindAsStr() const
Definition CIRTypes.cpp:693
bool isStruct() const
Definition CIRTypes.cpp:688
bool getPadded() const
Definition CIRTypes.cpp:673
llvm::ArrayRef< RecordMemberKind > getMemberKinds() const
Definition CIRTypes.cpp:678
size_t getNumElements() const
Definition CIRTypes.h:191
uint64_t getElementOffset(const mlir::DataLayout &dataLayout, unsigned idx) const
Definition CIRTypes.cpp:711
bool isMatchingAddressSpace(mlir::ptr::MemorySpaceAttrInterface cirAS, clang::LangAS as)
cir::LangAddressSpace toCIRLangAddressSpace(clang::LangAS langAS)
mlir::Type memberStorageType(mlir::Type memberTy)
The storage a member is stored as: the access unit for a bit-field member, and the member type itself...
Definition CIRTypes.h:138
bool memberOwnsBytes(mlir::Type memberTy)
Whether a record member occupies bytes of its record.
Definition CIRTypes.h:129
bool isValidFundamentalIntWidth(unsigned width)
bool isBitIntType(mlir::Type ty)
Returns true if the type is a cir::IntType representing a _BitInt.
Definition CIRTypes.cpp:43
cir::FPTypeInterface getFloatingPointType(const llvm::fltSemantics &sem, mlir::MLIRContext *ctx)
Returns the CIR floating-point type for the given semantics, or a null type if CIR has no type for it...
Definition CIRTypes.cpp:48
mlir::ptr::MemorySpaceAttrInterface toCIRAddressSpaceAttr(mlir::MLIRContext &ctx, clang::LangAS langAS)
Convert an AST LangAS to the appropriate CIR address space attribute interface.
bool anyMemberHoldsDataForABI(llvm::ArrayRef< RecordMemberKind > kinds)
Whether any member holds data for argument passing on its mark alone.
Definition CIRTypes.h:53
bool isNamedBitField(RecordMemberKind kind)
Whether a member of this kind is an access unit the source can read a bit-field of.
Definition CIRTypes.h:65
mlir::ptr::MemorySpaceAttrInterface normalizeDefaultAddressSpace(mlir::ptr::MemorySpaceAttrInterface addrSpace)
Normalize LangAddressSpace::Default to null (empty attribute).
bool isSized(mlir::Type ty)
Returns true if the type is a CIR sized type.
Definition CIRTypes.cpp:36
bool holdsDataForABI(RecordMemberKind kind)
Whether a member of this kind holds data for argument passing.
Definition CIRTypes.h:48
bool isSupportedCIRMemorySpaceAttr(mlir::ptr::MemorySpaceAttrInterface memorySpace)
LangAS
Defines the address space values used by the address space qualifier of QualType.
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
Type storage for CIR struct/class types.
Type storage for CIR union types.