14#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
15#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
16#include "mlir/IR/SymbolTable.h"
19 if (
auto intTy = mlir::dyn_cast<cir::IntType>(ty))
20 return intTy.getWidth();
21 assert(mlir::isa<cir::BoolType>(ty) &&
22 "expected CIR integer or bool element type");
26mlir::DenseElementsAttr
29 const auto stringAttr = mlir::cast<mlir::StringAttr>(attr.getElts());
30 const auto arrayTy = mlir::cast<cir::ArrayType>(attr.getType());
31 const unsigned totalSize = arrayTy.getSize();
32 const unsigned trailingZeros = attr.getTrailingZerosNum();
33 assert(stringAttr.size() + trailingZeros == totalSize &&
34 "string const_array size must match explicit elements plus "
39 values.reserve(totalSize);
44 for (
const char element : stringAttr)
45 values.emplace_back(bitWidth,
static_cast<unsigned char>(element));
47 values.insert(values.end(), trailingZeros, mlir::APInt::getZero(bitWidth));
49 return mlir::DenseElementsAttr::get(
50 mlir::RankedTensorType::get({totalSize}, type),
llvm::ArrayRef(values));
54 if (mlir::isa<cir::BoolType>(ty))
55 return mlir::APInt::getZero(1);
56 const auto intTy = mlir::cast<cir::IntType>(ty);
57 return mlir::APInt::getZero(intTy.getWidth());
61 assert((mlir::isa<cir::SingleType, cir::DoubleType>(ty)) &&
62 "only float and double supported");
64 if (ty.isF32() || mlir::isa<cir::SingleType>(ty))
65 return mlir::APFloat(0.f);
67 if (ty.isF64() || mlir::isa<cir::DoubleType>(ty))
68 return mlir::APFloat(0.0);
70 llvm_unreachable(
"NYI");
78template <
typename AttrTy,
typename StorageTy>
82 int64_t currentIndex) {
83 if (
auto stringAttr = mlir::dyn_cast<mlir::StringAttr>(attr.getElts())) {
84 if (
auto arrayType = mlir::dyn_cast<cir::ArrayType>(attr.getType())) {
85 for (
auto element : stringAttr) {
86 auto intAttr = cir::IntAttr::get(arrayType.getElementType(), element);
87 values[currentIndex++] = mlir::dyn_cast<AttrTy>(intAttr).getValue();
90 currentIndex += attr.getTrailingZerosNum();
96 std::size_t elementsSizeInCurrentDim = 1;
97 for (std::size_t i = dimIndex; i < currentDims.size(); i++)
98 elementsSizeInCurrentDim *= currentDims[i];
100 auto arrayAttr = mlir::cast<mlir::ArrayAttr>(attr.getElts());
101 for (
auto eltAttr : arrayAttr) {
102 if constexpr (std::is_same_v<StorageTy, mlir::APInt>) {
103 if (
auto boolAttr = mlir::dyn_cast<cir::BoolAttr>(eltAttr)) {
104 values[currentIndex++] =
105 llvm::APInt(1,
static_cast<uint64_t
>(boolAttr.getValue()));
109 if (
auto valueAttr = mlir::dyn_cast<AttrTy>(eltAttr)) {
110 values[currentIndex++] = valueAttr.getValue();
114 if (
auto subArrayAttr = mlir::dyn_cast<cir::ConstArrayAttr>(eltAttr)) {
116 dimIndex, currentIndex);
117 currentIndex += elementsSizeInCurrentDim;
121 if (mlir::isa<cir::ZeroAttr, cir::UndefAttr>(eltAttr)) {
122 currentIndex += elementsSizeInCurrentDim;
126 llvm_unreachable(
"unknown element in ConstArrayAttr");
130template <
typename AttrTy,
typename StorageTy>
133 mlir::Type elementType, mlir::Type convertedElementType) {
134 unsigned vectorSize = 1;
135 for (
auto dim : dims)
141 return mlir::DenseElementsAttr::get(
142 mlir::RankedTensorType::get(dims, convertedElementType),
149 mlir::ModuleOp moduleOp,
150 const mlir::TypeConverter *converter) {
151 if (gv.getIndices() || mlir::isa<cir::IntType, cir::VPtrType>(gv.getType()))
154 auto ptrTy = mlir::dyn_cast<cir::PointerType>(gv.getType());
158 unsigned sourceAddrSpace = 0;
159 mlir::Type sourceType;
161 mlir::SymbolTable::lookupSymbolIn(moduleOp, gv.getSymbol());
162 if (
auto llvmSymbol = mlir::dyn_cast<mlir::LLVM::GlobalOp>(sourceSymbol)) {
163 sourceType = llvmSymbol.getType();
164 sourceAddrSpace = llvmSymbol.getAddrSpace();
165 }
else if (
auto cirSymbol = mlir::dyn_cast<cir::GlobalOp>(sourceSymbol)) {
166 sourceType = converter->convertType(cirSymbol.getSymType());
167 if (
auto targetAS = mlir::dyn_cast_if_present<cir::TargetAddressSpaceAttr>(
168 cirSymbol.getAddrSpaceAttr()))
169 sourceAddrSpace = targetAS.getValue();
176 auto llvmDstTy = converter->convertType<mlir::LLVM::LLVMPointerType>(ptrTy);
177 if (llvmDstTy.getAddressSpace() != sourceAddrSpace)
180 mlir::Type llvmEltTy = converter->convertType(ptrTy.getPointee());
181 if (llvmEltTy == sourceType)
183 if (
auto arrTy = mlir::dyn_cast<mlir::LLVM::LLVMArrayType>(sourceType))
184 return llvmEltTy == arrTy.getElementType();
192static std::optional<mlir::Attribute>
194 mlir::ModuleOp moduleOp,
195 const mlir::TypeConverter *converter) {
196 if (
auto gv = mlir::dyn_cast<cir::GlobalViewAttr>(elt)) {
199 return gv.getSymbol();
201 if (
auto nullPtr = mlir::dyn_cast<cir::ConstPtrAttr>(elt)) {
202 if (nullPtr.isNullValue())
203 return mlir::LLVM::ZeroAttr::get(ctx);
210 if (mlir::isa<cir::PoisonAttr>(attr))
212 if (
auto elts = mlir::dyn_cast<mlir::ArrayAttr>(attr))
214 if (
auto constArr = mlir::dyn_cast<cir::ConstArrayAttr>(attr)) {
215 if (mlir::isa<mlir::StringAttr>(constArr.getElts()))
217 if (
auto elts = mlir::dyn_cast<mlir::ArrayAttr>(constArr.getElts()))
223std::optional<mlir::Attribute>
225 const mlir::TypeConverter *converter,
226 mlir::ModuleOp moduleOp) {
228 const auto typedConstArr = mlir::cast<mlir::TypedAttr>(constArr);
231 const auto cirArrayType = mlir::cast<cir::ArrayType>(typedConstArr.getType());
234 mlir::Type type = cirArrayType;
236 while (
auto arrayType = mlir::dyn_cast<cir::ArrayType>(type)) {
237 dims.push_back(arrayType.getSize());
238 type = arrayType.getElementType();
244 if (mlir::isa<mlir::StringAttr>(constArr.getElts()))
246 converter->convertType(type));
247 if (mlir::isa<cir::IntType>(type))
249 constArr, dims, type, converter->convertType(type));
251 if (mlir::isa<cir::BoolType>(type))
253 constArr, dims, type, converter->convertType(type));
255 if (mlir::isa<cir::FPTypeInterface>(type))
257 constArr, dims, type, converter->convertType(type));
259 if (mlir::isa<cir::PointerType>(type)) {
262 if (constArr.getTrailingZerosNum() > 0)
264 auto eltsArr = mlir::dyn_cast<mlir::ArrayAttr>(constArr.getElts());
268 lowered.reserve(eltsArr.size());
269 mlir::MLIRContext *ctx = constArr.getContext();
270 for (mlir::Attribute elt : eltsArr) {
271 std::optional<mlir::Attribute> llvmElt =
275 lowered.push_back(*llvmElt);
277 return mlir::ArrayAttr::get(ctx, lowered);
284 mlir::Type typ,
const llvm::APInt &val) {
285 return mlir::LLVM::ConstantOp::create(bld, loc, typ, val);
288mlir::Value
getConst(mlir::OpBuilder &bld, mlir::Location loc, mlir::Type typ,
290 return mlir::LLVM::ConstantOp::create(bld, loc, typ, val);
293mlir::Value
createShL(mlir::OpBuilder &bld, mlir::Value lhs,
unsigned rhs) {
296 mlir::Value rhsVal =
getConst(bld, lhs.getLoc(), lhs.getType(), rhs);
297 return mlir::LLVM::ShlOp::create(bld, lhs.getLoc(), lhs, rhsVal);
300mlir::Value
createAShR(mlir::OpBuilder &bld, mlir::Value lhs,
unsigned rhs) {
303 mlir::Value rhsVal =
getConst(bld, lhs.getLoc(), lhs.getType(), rhs);
304 return mlir::LLVM::AShrOp::create(bld, lhs.getLoc(), lhs, rhsVal);
307mlir::Value
createAnd(mlir::OpBuilder &bld, mlir::Value lhs,
308 const llvm::APInt &rhs) {
309 mlir::Value rhsVal =
getConstAPInt(bld, lhs.getLoc(), lhs.getType(), rhs);
310 return mlir::LLVM::AndOp::create(bld, lhs.getLoc(), lhs, rhsVal);
313mlir::Value
createLShR(mlir::OpBuilder &bld, mlir::Value lhs,
unsigned rhs) {
316 mlir::Value rhsVal =
getConst(bld, lhs.getLoc(), lhs.getType(), rhs);
317 return mlir::LLVM::LShrOp::create(bld, lhs.getLoc(), lhs, rhsVal);
mlir::DenseElementsAttr convertStringAttrToDenseElementsAttr(cir::ConstArrayAttr attr, mlir::Type type)
std::optional< mlir::Attribute > lowerConstArrayAttr(cir::ConstArrayAttr constArr, const mlir::TypeConverter *converter, mlir::ModuleOp moduleOp)
mlir::Value createLShR(mlir::OpBuilder &bld, mlir::Value lhs, unsigned rhs)
mlir::Value createShL(mlir::OpBuilder &bld, mlir::Value lhs, unsigned rhs)
mlir::Value getConst(mlir::OpBuilder &bld, mlir::Location loc, mlir::Type typ, unsigned val)
mlir::Value getConstAPInt(mlir::OpBuilder &bld, mlir::Location loc, mlir::Type typ, const llvm::APInt &val)
void convertToDenseElementsAttrImpl(cir::ConstArrayAttr attr, llvm::SmallVectorImpl< StorageTy > &values, const llvm::SmallVectorImpl< int64_t > ¤tDims, int64_t dimIndex, int64_t currentIndex)
static bool containsPoison(mlir::Attribute attr)
static bool globalViewMatchesPointerLeaf(cir::GlobalViewAttr gv, mlir::ModuleOp moduleOp, const mlir::TypeConverter *converter)
Return true when gv can be lowered to a FlatSymbolRefAttr leaf without addrspacecast or bitcast (mirr...
static std::optional< mlir::Attribute > lowerPointerElementAttr(mlir::Attribute elt, mlir::MLIRContext *ctx, mlir::ModuleOp moduleOp, const mlir::TypeConverter *converter)
Lower a single pointer-element of a cir.const_array to an LLVM-dialect constant leaf suitable for a b...
mlir::Value createAShR(mlir::OpBuilder &bld, mlir::Value lhs, unsigned rhs)
mlir::Value createAnd(mlir::OpBuilder &bld, mlir::Value lhs, const llvm::APInt &rhs)
mlir::DenseElementsAttr convertToDenseElementsAttr(cir::ConstArrayAttr attr, const llvm::SmallVectorImpl< int64_t > &dims, mlir::Type elementType, mlir::Type convertedElementType)
mlir::APInt getZeroInitFromType(mlir::Type ty)
static unsigned getIntOrBoolBitWidth(mlir::Type ty)
StorageTy getZeroInitFromType(mlir::Type ty)