clang 23.0.0git
CIRGenBuilder.cpp
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#include "CIRGenBuilder.h"
10#include "mlir/IR/BuiltinAttributes.h"
12#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/TypeSwitch.h"
14
15using namespace clang::CIRGen;
16
17mlir::Value CIRGenBuilderTy::maybeBuildArrayDecay(mlir::Location loc,
18 mlir::Value arrayPtr,
19 mlir::Type eltTy) {
20 const auto arrayPtrTy = mlir::cast<cir::PointerType>(arrayPtr.getType());
21 const auto arrayTy = mlir::dyn_cast<cir::ArrayType>(arrayPtrTy.getPointee());
22
23 if (arrayTy) {
24 const cir::PointerType flatPtrTy =
25 getPointerTo(arrayTy.getElementType(), arrayPtrTy.getAddrSpace());
26 return cir::CastOp::create(*this, loc, flatPtrTy,
27 cir::CastKind::array_to_ptrdecay, arrayPtr);
28 }
29
30 assert(arrayPtrTy.getPointee() == eltTy &&
31 "flat pointee type must match original array element type");
32 return arrayPtr;
33}
34
35mlir::Value CIRGenBuilderTy::getArrayElement(mlir::Location arrayLocBegin,
36 mlir::Location arrayLocEnd,
37 mlir::Value arrayPtr,
38 mlir::Type eltTy, mlir::Value idx,
39 bool shouldDecay) {
40 auto arrayPtrTy = mlir::dyn_cast<cir::PointerType>(arrayPtr.getType());
41 assert(arrayPtrTy && "expected pointer type");
42 // If the array pointer is not decayed, emit a GetElementOp.
43 auto arrayTy = mlir::dyn_cast<cir::ArrayType>(arrayPtrTy.getPointee());
44
45 assert(mlir::isa<cir::IntType>(idx.getType()) &&
47 mlir::cast<cir::IntType>(idx.getType()).getWidth()));
48
49 if (shouldDecay && arrayTy && arrayTy == eltTy) {
50 auto eltPtrTy =
51 getPointerTo(arrayTy.getElementType(), arrayPtrTy.getAddrSpace());
52 return cir::GetElementOp::create(*this, arrayLocEnd, eltPtrTy, arrayPtr,
53 idx);
54 }
55
56 // If we don't have sufficient type information, emit a PtrStrideOp.
57 mlir::Value basePtr = arrayPtr;
58 if (shouldDecay)
59 basePtr = maybeBuildArrayDecay(arrayLocBegin, arrayPtr, eltTy);
60 const mlir::Type flatPtrTy = basePtr.getType();
61 return cir::PtrStrideOp::create(*this, arrayLocEnd, flatPtrTy, basePtr, idx);
62}
63
64cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc,
65 llvm::APSInt intVal) {
66 bool isSigned = intVal.isSigned();
67 unsigned width = intVal.getBitWidth();
68 cir::IntType t = isSigned ? getSIntNTy(width) : getUIntNTy(width);
69 return getConstInt(loc, t,
70 isSigned ? intVal.getSExtValue() : intVal.getZExtValue());
71}
72
73cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc,
74 llvm::APInt intVal,
75 bool isUnsigned) {
76 return getConstInt(loc, llvm::APSInt(intVal, isUnsigned));
77}
78
79cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc, mlir::Type t,
80 uint64_t c) {
81 assert(mlir::isa<cir::IntType>(t) && "expected cir::IntType");
82 return cir::ConstantOp::create(*this, loc, cir::IntAttr::get(t, c));
83}
84
85cir::ConstantOp
86clang::CIRGen::CIRGenBuilderTy::getConstFP(mlir::Location loc, mlir::Type t,
87 llvm::APFloat fpVal) {
88 assert(mlir::isa<cir::FPTypeInterface>(t) && "expected floating point type");
89 return cir::ConstantOp::create(*this, loc, cir::FPAttr::get(t, fpVal));
90}
91
93 int64_t offset, mlir::Type ty, cir::CIRDataLayout layout,
95 if (!offset)
96 return;
97
98 // Compute floor-division and a non-negative remainder. A negative flat
99 // offset (e.g. from a pointer one element before the start of an array)
100 // must translate to a negative array index with a non-negative remainder
101 // so that the recursive call can descend into the element type without
102 // a negative offset flowing into the record case below.
103 auto getIndexAndNewOffset =
104 [](int64_t offset, int64_t eltSize) -> std::pair<int64_t, int64_t> {
105 int64_t divRet = offset / eltSize;
106 int64_t modRet = offset % eltSize;
107 if (modRet < 0) {
108 divRet -= 1;
109 modRet += eltSize;
110 }
111 return {divRet, modRet};
112 };
113
114 mlir::Type subType =
115 llvm::TypeSwitch<mlir::Type, mlir::Type>(ty)
116 .Case<cir::ArrayType>([&](auto arrayTy) {
117 int64_t eltSize = layout.getTypeAllocSize(arrayTy.getElementType());
118 const auto [index, newOffset] =
119 getIndexAndNewOffset(offset, eltSize);
120 indices.push_back(index);
121 offset = newOffset;
122 return arrayTy.getElementType();
123 })
124 .Case<cir::RecordType>([&](auto recordTy) {
125 ArrayRef<mlir::Type> elts = recordTy.getMembers();
126 int64_t pos = 0;
127 for (size_t i = 0; i < elts.size(); ++i) {
128 int64_t eltSize =
129 (int64_t)layout.getTypeAllocSize(elts[i]).getFixedValue();
130 unsigned alignMask = layout.getABITypeAlign(elts[i]).value() - 1;
131 if (recordTy.getPacked())
132 alignMask = 0;
133 // Union's fields have the same offset, so no need to change pos
134 // here, we just need to find eltSize that is greater then the
135 // required offset. The same is true for the similar union type
136 // check below
137 if (!recordTy.isUnion())
138 pos = (pos + alignMask) & ~alignMask;
139 assert(offset >= 0);
140 if (offset < pos + eltSize) {
141 indices.push_back(i);
142 offset -= pos;
143 return elts[i];
144 }
145 // No need to update pos here, see the comment above.
146 if (!recordTy.isUnion())
147 pos += eltSize;
148 }
149 llvm_unreachable("offset was not found within the record");
150 })
151 .Case<cir::IntType>([&](cir::IntType intTy) -> mlir::Type {
152 // Integer element type: the offset is a flat element count.
153 // This covers pointer arithmetic through a plain integer base,
154 // e.g. a char* GlobalViewAttr whose pointee type is !s8i rather
155 // than an array — the GEP is getelementptr i8, ptr @sym, i64 N.
156 int64_t eltSize =
157 (int64_t)layout.getTypeAllocSize(intTy).getFixedValue();
158 assert(eltSize > 0 && "element size must be positive");
159 const auto [index, newOffset] =
160 getIndexAndNewOffset(offset, eltSize);
161 indices.push_back(index);
162 offset = newOffset;
163 return intTy;
164 })
165 .Default([](mlir::Type) -> mlir::Type {
166 llvm_unreachable("unexpected type");
167 });
168
169 assert(subType);
170 computeGlobalViewIndicesFromFlatOffset(offset, subType, layout, indices);
171}
172
174 const cir::CIRDataLayout &layout, mlir::Type ty,
175 llvm::ArrayRef<int64_t> indices) {
176 int64_t offset = 0;
177 for (int64_t idx : indices) {
178 if (auto recordTy = dyn_cast<cir::RecordType>(ty)) {
179 offset += recordTy.getElementOffset(layout.layout, idx);
180 const llvm::Align tyAlign = llvm::Align(
181 recordTy.getPacked() ? 1 : layout.layout.getTypeABIAlignment(ty));
182 offset = llvm::alignTo(offset, tyAlign);
183 assert(idx < (int64_t)recordTy.getMembers().size());
184 ty = recordTy.getMembers()[idx];
185 } else if (auto arrayTy = dyn_cast<cir::ArrayType>(ty)) {
186 ty = arrayTy.getElementType();
187 offset += layout.getTypeAllocSize(ty) * idx;
188 } else if (mlir::isa<cir::IntType>(ty)) {
189 // Integer element type: the index is a flat element count.
190 offset += (int64_t)layout.getTypeAllocSize(ty).getFixedValue() * idx;
191 } else {
192 llvm_unreachable("unexpected type");
193 }
194 }
195 return offset;
196}
197
199 mlir::ArrayAttr fields, bool packed, bool padded, llvm::StringRef name) {
202 members.reserve(fields.size());
203 llvm::transform(fields, std::back_inserter(members),
204 [](mlir::Attribute attr) {
205 return mlir::cast<mlir::TypedAttr>(attr).getType();
206 });
207
208 if (name.empty())
209 return getAnonRecordTy(members, packed, padded);
210
211 return getCompleteNamedRecordType(members, packed, padded, name);
212}
213
215 mlir::ArrayAttr arrayAttr, bool packed, bool padded, mlir::Type type) {
216 auto recordTy = mlir::cast_or_null<cir::RecordType>(type);
217
218 // Record type not specified: create anon record type from members.
219 if (!recordTy) {
220 recordTy = getCompleteRecordType(arrayAttr, packed, padded);
221 }
222
223 // Return zero or anonymous constant record.
224 const bool isZero = llvm::all_of(
225 arrayAttr, [&](mlir::Attribute a) { return isNullValue(a); });
226 if (isZero)
227 return cir::ZeroAttr::get(recordTy);
228 return cir::ConstRecordAttr::get(recordTy, arrayAttr);
229}
230
231// This can't be defined in Address.h because that file is included by
232// CIRGenBuilder.h
234 mlir::Type elemTy) const {
238
239 return Address(builder.createPtrBitcast(getBasePointer(), elemTy), elemTy,
240 getAlignment());
241}
static bool isUnsigned(SValBuilder &SVB, NonLoc Value)
cir::PointerType getPointerTo(mlir::Type ty)
mlir::Value createPtrBitcast(mlir::Value src, mlir::Type newPointeeTy)
mlir::DataLayout layout
llvm::TypeSize getTypeAllocSize(mlir::Type ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
llvm::Align getABITypeAlign(mlir::Type ty) const
C++ view class that accepts both !cir.struct and !cir.union types.
Definition CIRTypes.h:93
Address withElementType(CIRGenBuilderTy &builder, mlir::Type ElemTy) const
Return address with different element type, a bitcast pointer, and the same alignment.
clang::CharUnits getAlignment() const
Definition Address.h:138
mlir::Value getBasePointer() const
Definition Address.h:103
Address(std::nullptr_t)
Definition Address.h:46
cir::RecordType getCompleteNamedRecordType(llvm::ArrayRef< mlir::Type > members, bool packed, bool padded, llvm::StringRef name)
Get a CIR named record type.
cir::IntType getSIntNTy(int n)
mlir::Attribute getConstRecordOrZeroAttr(mlir::ArrayAttr arrayAttr, bool packed=false, bool padded=false, mlir::Type type={})
mlir::Value maybeBuildArrayDecay(mlir::Location loc, mlir::Value arrayPtr, mlir::Type eltTy)
Returns a decayed pointer to the first element of the array pointed to by arrayPtr.
cir::ConstantOp getConstFP(mlir::Location loc, mlir::Type t, llvm::APFloat fpVal)
uint64_t computeOffsetFromGlobalViewIndices(const cir::CIRDataLayout &layout, mlir::Type ty, llvm::ArrayRef< int64_t > indices)
cir::StructType getAnonRecordTy(llvm::ArrayRef< mlir::Type > members, bool packed=false, bool padded=false)
Get a CIR anonymous struct type.
bool isNullValue(mlir::Attribute attr) const
cir::RecordType getCompleteRecordType(mlir::ArrayAttr fields, bool packed=false, bool padded=false, llvm::StringRef name="")
cir::ConstantOp getConstInt(mlir::Location loc, llvm::APSInt intVal)
void computeGlobalViewIndicesFromFlatOffset(int64_t offset, mlir::Type ty, cir::CIRDataLayout layout, llvm::SmallVectorImpl< int64_t > &indices)
cir::IntType getUIntNTy(int n)
mlir::Value getArrayElement(mlir::Location arrayLocBegin, mlir::Location arrayLocEnd, mlir::Value arrayPtr, mlir::Type eltTy, mlir::Value idx, bool shouldDecay)
Create a cir.ptr_stride operation to get access to an array element.
bool isValidFundamentalIntWidth(unsigned width)
Definition CIRTypes.cpp:825
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
@ Default
Set to the current date and time.
static bool addressPointerAuthInfo()
static bool addressOffset()
static bool astRecordDeclAttr()
static bool addressIsKnownNonNull()