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 .Default([](mlir::Type otherTy) {
152 llvm_unreachable("unexpected type");
153 return otherTy; // Even though this is unreachable, we need to
154 // return a type to satisfy the return type of the
155 // lambda.
156 });
157
158 assert(subType);
159 computeGlobalViewIndicesFromFlatOffset(offset, subType, layout, indices);
160}
161
163 const cir::CIRDataLayout &layout, mlir::Type ty,
164 llvm::ArrayRef<int64_t> indices) {
165 int64_t offset = 0;
166 for (int64_t idx : indices) {
167 if (auto recordTy = dyn_cast<cir::RecordType>(ty)) {
168 offset += recordTy.getElementOffset(layout.layout, idx);
169 const llvm::Align tyAlign = llvm::Align(
170 recordTy.getPacked() ? 1 : layout.layout.getTypeABIAlignment(ty));
171 offset = llvm::alignTo(offset, tyAlign);
172 assert(idx < (int64_t)recordTy.getMembers().size());
173 ty = recordTy.getMembers()[idx];
174 } else if (auto arrayTy = dyn_cast<cir::ArrayType>(ty)) {
175 ty = arrayTy.getElementType();
176 offset += layout.getTypeAllocSize(ty) * idx;
177 } else {
178 llvm_unreachable("unexpected type");
179 }
180 }
181 return offset;
182}
183
185 mlir::ArrayAttr fields, bool packed, bool padded, llvm::StringRef name) {
188 members.reserve(fields.size());
189 llvm::transform(fields, std::back_inserter(members),
190 [](mlir::Attribute attr) {
191 return mlir::cast<mlir::TypedAttr>(attr).getType();
192 });
193
194 if (name.empty())
195 return getAnonRecordTy(members, packed, padded);
196
197 return getCompleteNamedRecordType(members, packed, padded, name);
198}
199
201 mlir::ArrayAttr arrayAttr, bool packed, bool padded, mlir::Type type) {
202 auto recordTy = mlir::cast_or_null<cir::RecordType>(type);
203
204 // Record type not specified: create anon record type from members.
205 if (!recordTy) {
206 recordTy = getCompleteRecordType(arrayAttr, packed, padded);
207 }
208
209 // Return zero or anonymous constant record.
210 const bool isZero = llvm::all_of(
211 arrayAttr, [&](mlir::Attribute a) { return isNullValue(a); });
212 if (isZero)
213 return cir::ZeroAttr::get(recordTy);
214 return cir::ConstRecordAttr::get(recordTy, arrayAttr);
215}
216
217// This can't be defined in Address.h because that file is included by
218// CIRGenBuilder.h
220 mlir::Type elemTy) const {
224
225 return Address(builder.createPtrBitcast(getBasePointer(), elemTy), elemTy,
226 getAlignment());
227}
static bool isUnsigned(SValBuilder &SVB, NonLoc Value)
__device__ __2f16 float c
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
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:136
mlir::Value getBasePointer() const
Definition Address.h:101
Address(std::nullptr_t)
Definition Address.h:44
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={})
cir::RecordType getAnonRecordTy(llvm::ArrayRef< mlir::Type > members, bool packed=false, bool padded=false)
Get a CIR anonymous record 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)
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:601
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
static bool addressPointerAuthInfo()
static bool addressOffset()
static bool astRecordDeclAttr()
static bool addressIsKnownNonNull()