clang 22.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 = getPointerTo(arrayTy.getElementType());
25 return cir::CastOp::create(*this, loc, flatPtrTy,
26 cir::CastKind::array_to_ptrdecay, arrayPtr);
27 }
28
29 assert(arrayPtrTy.getPointee() == eltTy &&
30 "flat pointee type must match original array element type");
31 return arrayPtr;
32}
33
34mlir::Value CIRGenBuilderTy::getArrayElement(mlir::Location arrayLocBegin,
35 mlir::Location arrayLocEnd,
36 mlir::Value arrayPtr,
37 mlir::Type eltTy, mlir::Value idx,
38 bool shouldDecay) {
39 auto arrayPtrTy = mlir::dyn_cast<cir::PointerType>(arrayPtr.getType());
40 assert(arrayPtrTy && "expected pointer type");
41 // If the array pointer is not decayed, emit a GetElementOp.
42 auto arrayTy = mlir::dyn_cast<cir::ArrayType>(arrayPtrTy.getPointee());
43
44 if (shouldDecay && arrayTy && arrayTy == eltTy) {
45 auto eltPtrTy =
46 getPointerTo(arrayTy.getElementType(), arrayPtrTy.getAddrSpace());
47 return cir::GetElementOp::create(*this, arrayLocEnd, eltPtrTy, arrayPtr,
48 idx);
49 }
50
51 // If we don't have sufficient type information, emit a PtrStrideOp.
52 mlir::Value basePtr = arrayPtr;
53 if (shouldDecay)
54 basePtr = maybeBuildArrayDecay(arrayLocBegin, arrayPtr, eltTy);
55 const mlir::Type flatPtrTy = basePtr.getType();
56 return cir::PtrStrideOp::create(*this, arrayLocEnd, flatPtrTy, basePtr, idx);
57}
58
59cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc,
60 llvm::APSInt intVal) {
61 bool isSigned = intVal.isSigned();
62 unsigned width = intVal.getBitWidth();
63 cir::IntType t = isSigned ? getSIntNTy(width) : getUIntNTy(width);
64 return getConstInt(loc, t,
65 isSigned ? intVal.getSExtValue() : intVal.getZExtValue());
66}
67
68cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc,
69 llvm::APInt intVal,
70 bool isUnsigned) {
71 return getConstInt(loc, llvm::APSInt(intVal, isUnsigned));
72}
73
74cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc, mlir::Type t,
75 uint64_t c) {
76 assert(mlir::isa<cir::IntType>(t) && "expected cir::IntType");
77 return cir::ConstantOp::create(*this, loc, cir::IntAttr::get(t, c));
78}
79
80cir::ConstantOp
81clang::CIRGen::CIRGenBuilderTy::getConstFP(mlir::Location loc, mlir::Type t,
82 llvm::APFloat fpVal) {
83 assert(mlir::isa<cir::FPTypeInterface>(t) && "expected floating point type");
84 return cir::ConstantOp::create(*this, loc, cir::FPAttr::get(t, fpVal));
85}
86
88 int64_t offset, mlir::Type ty, cir::CIRDataLayout layout,
90 if (!offset)
91 return;
92
93 auto getIndexAndNewOffset =
94 [](int64_t offset, int64_t eltSize) -> std::pair<int64_t, int64_t> {
95 int64_t divRet = offset / eltSize;
96 if (divRet < 0)
97 divRet -= 1; // make sure offset is positive
98 int64_t modRet = offset - (divRet * eltSize);
99 return {divRet, modRet};
100 };
101
102 mlir::Type subType =
103 llvm::TypeSwitch<mlir::Type, mlir::Type>(ty)
104 .Case<cir::ArrayType>([&](auto arrayTy) {
105 int64_t eltSize = layout.getTypeAllocSize(arrayTy.getElementType());
106 const auto [index, newOffset] =
107 getIndexAndNewOffset(offset, eltSize);
108 indices.push_back(index);
109 offset = newOffset;
110 return arrayTy.getElementType();
111 })
112 .Case<cir::RecordType>([&](auto recordTy) {
113 ArrayRef<mlir::Type> elts = recordTy.getMembers();
114 int64_t pos = 0;
115 for (size_t i = 0; i < elts.size(); ++i) {
116 int64_t eltSize =
117 (int64_t)layout.getTypeAllocSize(elts[i]).getFixedValue();
118 unsigned alignMask = layout.getABITypeAlign(elts[i]).value() - 1;
119 if (recordTy.getPacked())
120 alignMask = 0;
121 // Union's fields have the same offset, so no need to change pos
122 // here, we just need to find eltSize that is greater then the
123 // required offset. The same is true for the similar union type
124 // check below
125 if (!recordTy.isUnion())
126 pos = (pos + alignMask) & ~alignMask;
127 assert(offset >= 0);
128 if (offset < pos + eltSize) {
129 indices.push_back(i);
130 offset -= pos;
131 return elts[i];
132 }
133 // No need to update pos here, see the comment above.
134 if (!recordTy.isUnion())
135 pos += eltSize;
136 }
137 llvm_unreachable("offset was not found within the record");
138 })
139 .Default([](mlir::Type otherTy) {
140 llvm_unreachable("unexpected type");
141 return otherTy; // Even though this is unreachable, we need to
142 // return a type to satisfy the return type of the
143 // lambda.
144 });
145
146 assert(subType);
147 computeGlobalViewIndicesFromFlatOffset(offset, subType, layout, indices);
148}
149
151 mlir::ArrayAttr fields, bool packed, bool padded, llvm::StringRef name) {
154 members.reserve(fields.size());
155 llvm::transform(fields, std::back_inserter(members),
156 [](mlir::Attribute attr) {
157 return mlir::cast<mlir::TypedAttr>(attr).getType();
158 });
159
160 if (name.empty())
161 return getAnonRecordTy(members, packed, padded);
162
163 return getCompleteNamedRecordType(members, packed, padded, name);
164}
165
167 mlir::ArrayAttr arrayAttr, bool packed, bool padded, mlir::Type type) {
168 auto recordTy = mlir::cast_or_null<cir::RecordType>(type);
169
170 // Record type not specified: create anon record type from members.
171 if (!recordTy) {
172 recordTy = getCompleteRecordType(arrayAttr, packed, padded);
173 }
174
175 // Return zero or anonymous constant record.
176 const bool isZero = llvm::all_of(
177 arrayAttr, [&](mlir::Attribute a) { return isNullValue(a); });
178 if (isZero)
179 return cir::ZeroAttr::get(recordTy);
180 return cir::ConstRecordAttr::get(recordTy, arrayAttr);
181}
182
183// This can't be defined in Address.h because that file is included by
184// CIRGenBuilder.h
186 mlir::Type elemTy) const {
190
191 return Address(builder.createPtrBitcast(getBasePointer(), elemTy), elemTy,
192 getAlignment());
193}
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)
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:130
mlir::Value getBasePointer() const
Definition Address.h:95
Address(std::nullptr_t)
Definition Address.h:43
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)
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.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
static bool addressPointerAuthInfo()
static bool addressOffset()
static bool astRecordDeclAttr()
static bool addressIsKnownNonNull()