10#include "mlir/IR/BuiltinAttributes.h"
12#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/TypeSwitch.h"
20 const auto arrayPtrTy = mlir::cast<cir::PointerType>(arrayPtr.getType());
21 const auto arrayTy = mlir::dyn_cast<cir::ArrayType>(arrayPtrTy.getPointee());
24 const cir::PointerType flatPtrTy =
getPointerTo(arrayTy.getElementType());
25 return cir::CastOp::create(*
this, loc, flatPtrTy,
26 cir::CastKind::array_to_ptrdecay, arrayPtr);
29 assert(arrayPtrTy.getPointee() == eltTy &&
30 "flat pointee type must match original array element type");
35 mlir::Location arrayLocEnd,
37 mlir::Type eltTy, mlir::Value idx,
39 auto arrayPtrTy = mlir::dyn_cast<cir::PointerType>(arrayPtr.getType());
40 assert(arrayPtrTy &&
"expected pointer type");
42 auto arrayTy = mlir::dyn_cast<cir::ArrayType>(arrayPtrTy.getPointee());
44 if (shouldDecay && arrayTy && arrayTy == eltTy) {
46 getPointerTo(arrayTy.getElementType(), arrayPtrTy.getAddrSpace());
47 return cir::GetElementOp::create(*
this, arrayLocEnd, eltPtrTy, arrayPtr,
52 mlir::Value basePtr = arrayPtr;
55 const mlir::Type flatPtrTy = basePtr.getType();
56 return cir::PtrStrideOp::create(*
this, arrayLocEnd, flatPtrTy, basePtr, idx);
60 llvm::APSInt intVal) {
61 bool isSigned = intVal.isSigned();
62 unsigned width = intVal.getBitWidth();
65 isSigned ? intVal.getSExtValue() : intVal.getZExtValue());
76 assert(mlir::isa<cir::IntType>(t) &&
"expected cir::IntType");
77 return cir::ConstantOp::create(*
this, loc, cir::IntAttr::get(t,
c));
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));
93 auto getIndexAndNewOffset =
94 [](int64_t offset, int64_t eltSize) -> std::pair<int64_t, int64_t> {
95 int64_t divRet = offset / eltSize;
98 int64_t modRet = offset - (divRet * eltSize);
99 return {divRet, modRet};
103 llvm::TypeSwitch<mlir::Type, mlir::Type>(ty)
104 .Case<cir::ArrayType>([&](
auto arrayTy) {
106 const auto [
index, newOffset] =
107 getIndexAndNewOffset(offset, eltSize);
108 indices.push_back(
index);
110 return arrayTy.getElementType();
112 .Case<cir::RecordType>([&](
auto recordTy) {
115 for (
size_t i = 0; i < elts.size(); ++i) {
119 if (recordTy.getPacked())
125 if (!recordTy.isUnion())
126 pos = (pos + alignMask) & ~alignMask;
128 if (offset < pos + eltSize) {
129 indices.push_back(i);
134 if (!recordTy.isUnion())
137 llvm_unreachable(
"offset was not found within the record");
139 .
Default([](mlir::Type otherTy) {
140 llvm_unreachable(
"unexpected type");
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();
167 mlir::ArrayAttr arrayAttr,
bool packed,
bool padded, mlir::Type
type) {
168 auto recordTy = mlir::cast_or_null<cir::RecordType>(
type);
176 const bool isZero = llvm::all_of(
177 arrayAttr, [&](mlir::Attribute a) {
return isNullValue(a); });
179 return cir::ZeroAttr::get(recordTy);
180 return cir::ConstRecordAttr::get(recordTy, arrayAttr);
186 mlir::Type elemTy)
const {
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
mlir::Value getBasePointer() const
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()