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/SmallVector.h"
27
28namespace llvm {
29struct fltSemantics;
30} // namespace llvm
31
32namespace cir {
33
34namespace detail {
36struct UnionTypeStorage;
37} // namespace detail
38
39bool isValidFundamentalIntWidth(unsigned width);
40
41/// Returns true if the type is a CIR sized type.
42///
43/// Types are sized if they implement SizedTypeInterface and
44/// return true from its method isSized.
45///
46/// Unsized types are those that do not have a size, such as
47/// void, or abstract types.
48bool isSized(mlir::Type ty);
49
50/// Returns the CIR floating-point type for the given semantics, or a null
51/// type if CIR has no type for it (e.g. PPCDoubleDouble or a Float8 format).
52/// Mirrors llvm::Type::getFloatingPointTy.
53cir::FPTypeInterface getFloatingPointType(const llvm::fltSemantics &sem,
54 mlir::MLIRContext *ctx);
55
56//===----------------------------------------------------------------------===//
57// AddressSpace helpers
58//===----------------------------------------------------------------------===//
59
60cir::LangAddressSpace toCIRLangAddressSpace(clang::LangAS langAS);
61
62// Compare a CIR memory space attribute with a Clang LangAS.
63bool isMatchingAddressSpace(mlir::ptr::MemorySpaceAttrInterface cirAS,
64 clang::LangAS as);
65
66/// Convert an AST LangAS to the appropriate CIR address space attribute
67/// interface.
68mlir::ptr::MemorySpaceAttrInterface
69toCIRAddressSpaceAttr(mlir::MLIRContext &ctx, clang::LangAS langAS);
70
71/// Normalize LangAddressSpace::Default to null (empty attribute).
72mlir::ptr::MemorySpaceAttrInterface
73normalizeDefaultAddressSpace(mlir::ptr::MemorySpaceAttrInterface addrSpace);
74
76 mlir::ptr::MemorySpaceAttrInterface memorySpace);
77
78} // namespace cir
79
80//===----------------------------------------------------------------------===//
81// CIR Dialect Tablegen'd Types
82//===----------------------------------------------------------------------===//
83
84namespace cir {
85
86#include "clang/CIR/Dialect/IR/CIRTypeConstraints.h.inc"
87
88} // namespace cir
89
90#define GET_TYPEDEF_CLASSES
91#include "clang/CIR/Dialect/IR/CIROpsTypes.h.inc"
92
93namespace cir {
94
95/// C++ view class that accepts both !cir.struct and !cir.union types.
96///
97/// Follows the MLIR BaseMemRefType pattern: StructType and UnionType are the
98/// concrete tablegen types; RecordType is a hand-written view class that
99/// covers both. Use it when code must handle either kind generically.
100///
101/// Methods that are common to both types are forwarded through dyn_cast
102/// dispatch. Type-specific methods (getPadding, getUnionStorageType) are only
103/// available on the concrete type.
104class RecordType : public mlir::Type {
105public:
106 using mlir::Type::Type;
107
108 // Allow implicit construction from concrete record types so that
109 // functions returning cir::RecordType can return StructType/UnionType
110 // values without an explicit cast.
111 // NOLINTNEXTLINE(google-explicit-constructor)
112 RecordType(StructType t) : mlir::Type(t) {}
113 // NOLINTNEXTLINE(google-explicit-constructor)
114 RecordType(UnionType t) : mlir::Type(t) {}
115
116 static bool classof(mlir::Type t) {
117 return mlir::isa<StructType>(t) || mlir::isa<UnionType>(t);
118 }
119
121 mlir::StringAttr getName() const;
122 bool isIncomplete() const;
123 bool isComplete() const { return !isIncomplete(); }
124 bool getPacked() const;
125 bool getPadded() const;
127
128 bool isClass() const;
129 bool isStruct() const;
130 bool isUnion() const { return mlir::isa<UnionType>(*this); }
131
132 /// Whether no member holds data. Vacuously true for a complete record with
133 /// no members, and false for an incomplete one, whose members are not known
134 /// yet. A union's tail-padding slot is not a member and does not count.
135 bool isEmptyForABI() const;
136
137 /// One `Data` kind per member. Takes the member list rather than a count so
138 /// the length cannot drift from the record it describes.
141
142 size_t getNumElements() const { return getMembers().size(); }
143 mlir::Type getElementType(size_t idx) const { return getMembers()[idx]; }
144 std::string getKindAsStr() const;
145 std::string getPrefixedName() const;
146
147 /// \p padding is union-only. A struct carries its padding as a member
148 /// marked pad.
149 void complete(llvm::ArrayRef<mlir::Type> members, bool packed,
150 mlir::Type padding,
152 uint64_t getElementOffset(const mlir::DataLayout &dataLayout,
153 unsigned idx) const;
154 bool isLayoutIdentical(const RecordType &other);
155
156 bool isABIConvertedRecord() const;
157 mlir::StringAttr getABIConvertedName() const;
159};
160
161} // namespace cir
162
163#endif // CLANG_CIR_DIALECT_IR_CIRTYPES_H
Provides definitions for the various language-specific address spaces.
C++ view class that accepts both !cir.struct and !cir.union types.
Definition CIRTypes.h:104
bool isUnion() const
Definition CIRTypes.h:130
bool isLayoutIdentical(const RecordType &other)
Definition CIRTypes.cpp:654
mlir::Type getElementType(size_t idx) const
Definition CIRTypes.h:143
bool isComplete() const
Definition CIRTypes.h:123
bool isABIConvertedRecord() const
Definition CIRTypes.cpp:667
bool isIncomplete() const
Definition CIRTypes.cpp:600
bool isEmptyForABI() const
Whether no member holds data.
Definition CIRTypes.cpp:683
std::string getPrefixedName() const
Definition CIRTypes.cpp:635
llvm::ArrayRef< mlir::Type > getMembers() const
Definition CIRTypes.cpp:590
RecordType(UnionType t)
Definition CIRTypes.h:114
bool isClass() const
Definition CIRTypes.cpp:620
void removeABIConversionNamePrefix()
Definition CIRTypes.cpp:677
static bool classof(mlir::Type t)
Definition CIRTypes.h:116
bool getPacked() const
Definition CIRTypes.cpp:605
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:112
mlir::StringAttr getName() const
Definition CIRTypes.cpp:595
void complete(llvm::ArrayRef< mlir::Type > members, bool packed, mlir::Type padding, llvm::ArrayRef< RecordMemberKind > memberKinds)
padding is union-only.
Definition CIRTypes.cpp:638
mlir::StringAttr getABIConvertedName() const
Definition CIRTypes.cpp:672
std::string getKindAsStr() const
Definition CIRTypes.cpp:630
bool isStruct() const
Definition CIRTypes.cpp:625
bool getPadded() const
Definition CIRTypes.cpp:610
llvm::ArrayRef< RecordMemberKind > getMemberKinds() const
Definition CIRTypes.cpp:615
size_t getNumElements() const
Definition CIRTypes.h:142
uint64_t getElementOffset(const mlir::DataLayout &dataLayout, unsigned idx) const
Definition CIRTypes.cpp:648
bool isMatchingAddressSpace(mlir::ptr::MemorySpaceAttrInterface cirAS, clang::LangAS as)
cir::LangAddressSpace toCIRLangAddressSpace(clang::LangAS langAS)
bool isValidFundamentalIntWidth(unsigned width)
cir::FPTypeInterface getFloatingPointType(const llvm::fltSemantics &sem, mlir::MLIRContext *ctx)
Returns the CIR floating-point type for the given semantics, or a null type if CIR has no type for it...
Definition CIRTypes.cpp:42
mlir::ptr::MemorySpaceAttrInterface toCIRAddressSpaceAttr(mlir::MLIRContext &ctx, clang::LangAS langAS)
Convert an AST LangAS to the appropriate CIR address space attribute interface.
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:35
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
Type storage for CIR struct/class types.
Type storage for CIR union types.