14#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
17mlir::DenseElementsAttr
21 const auto stringAttr = mlir::cast<mlir::StringAttr>(attr.getElts());
23 for (
const char element : stringAttr)
24 values.push_back({8, (uint64_t)element});
26 const auto arrayTy = mlir::cast<cir::ArrayType>(attr.getType());
27 if (arrayTy.getSize() != stringAttr.size())
30 return mlir::DenseElementsAttr::get(
31 mlir::RankedTensorType::get({(int64_t)values.size()}, type),
36 assert(mlir::isa<cir::IntType>(ty) &&
"expected int type");
37 const auto intTy = mlir::cast<cir::IntType>(ty);
38 return mlir::APInt::getZero(intTy.getWidth());
42 assert((mlir::isa<cir::SingleType, cir::DoubleType>(ty)) &&
43 "only float and double supported");
45 if (ty.isF32() || mlir::isa<cir::SingleType>(ty))
46 return mlir::APFloat(0.f);
48 if (ty.isF64() || mlir::isa<cir::DoubleType>(ty))
49 return mlir::APFloat(0.0);
51 llvm_unreachable(
"NYI");
59template <
typename AttrTy,
typename StorageTy>
63 int64_t currentIndex) {
64 if (
auto stringAttr = mlir::dyn_cast<mlir::StringAttr>(attr.getElts())) {
65 if (
auto arrayType = mlir::dyn_cast<cir::ArrayType>(attr.getType())) {
66 for (
auto element : stringAttr) {
67 auto intAttr = cir::IntAttr::get(arrayType.getElementType(), element);
68 values[currentIndex++] = mlir::dyn_cast<AttrTy>(intAttr).getValue();
75 std::size_t elementsSizeInCurrentDim = 1;
76 for (std::size_t i = dimIndex; i < currentDims.size(); i++)
77 elementsSizeInCurrentDim *= currentDims[i];
79 auto arrayAttr = mlir::cast<mlir::ArrayAttr>(attr.getElts());
80 for (
auto eltAttr : arrayAttr) {
81 if (
auto valueAttr = mlir::dyn_cast<AttrTy>(eltAttr)) {
82 values[currentIndex++] = valueAttr.getValue();
86 if (
auto subArrayAttr = mlir::dyn_cast<cir::ConstArrayAttr>(eltAttr)) {
88 dimIndex, currentIndex);
89 currentIndex += elementsSizeInCurrentDim;
93 if (mlir::isa<cir::ZeroAttr, cir::UndefAttr>(eltAttr)) {
94 currentIndex += elementsSizeInCurrentDim;
98 llvm_unreachable(
"unknown element in ConstArrayAttr");
102template <
typename AttrTy,
typename StorageTy>
105 mlir::Type elementType, mlir::Type convertedElementType) {
106 unsigned vectorSize = 1;
107 for (
auto dim : dims)
113 return mlir::DenseElementsAttr::get(
114 mlir::RankedTensorType::get(dims, convertedElementType),
118std::optional<mlir::Attribute>
120 const mlir::TypeConverter *converter) {
122 const auto typedConstArr = mlir::cast<mlir::TypedAttr>(constArr);
125 const auto cirArrayType = mlir::cast<cir::ArrayType>(typedConstArr.getType());
128 mlir::Type type = cirArrayType;
130 while (
auto arrayType = mlir::dyn_cast<cir::ArrayType>(type)) {
131 dims.push_back(arrayType.getSize());
132 type = arrayType.getElementType();
135 if (mlir::isa<mlir::StringAttr>(constArr.getElts()))
137 converter->convertType(type));
138 if (mlir::isa<cir::IntType>(type))
140 constArr, dims, type, converter->convertType(type));
142 if (mlir::isa<cir::FPTypeInterface>(type))
144 constArr, dims, type, converter->convertType(type));
150 mlir::Type typ,
const llvm::APInt &val) {
151 return bld.create<mlir::LLVM::ConstantOp>(loc, typ, val);
154mlir::Value
getConst(mlir::OpBuilder &bld, mlir::Location loc, mlir::Type typ,
156 return bld.create<mlir::LLVM::ConstantOp>(loc, typ, val);
159mlir::Value
createShL(mlir::OpBuilder &bld, mlir::Value lhs,
unsigned rhs) {
162 mlir::Value rhsVal =
getConst(bld, lhs.getLoc(), lhs.getType(), rhs);
163 return bld.create<mlir::LLVM::ShlOp>(lhs.getLoc(), lhs, rhsVal);
166mlir::Value
createAShR(mlir::OpBuilder &bld, mlir::Value lhs,
unsigned rhs) {
169 mlir::Value rhsVal =
getConst(bld, lhs.getLoc(), lhs.getType(), rhs);
170 return bld.create<mlir::LLVM::AShrOp>(lhs.getLoc(), lhs, rhsVal);
173mlir::Value
createAnd(mlir::OpBuilder &bld, mlir::Value lhs,
174 const llvm::APInt &rhs) {
175 mlir::Value rhsVal =
getConstAPInt(bld, lhs.getLoc(), lhs.getType(), rhs);
176 return bld.create<mlir::LLVM::AndOp>(lhs.getLoc(), lhs, rhsVal);
179mlir::Value
createLShR(mlir::OpBuilder &bld, mlir::Value lhs,
unsigned rhs) {
182 mlir::Value rhsVal =
getConst(bld, lhs.getLoc(), lhs.getType(), rhs);
183 return bld.create<mlir::LLVM::LShrOp>(lhs.getLoc(), lhs, rhsVal);
mlir::DenseElementsAttr convertStringAttrToDenseElementsAttr(cir::ConstArrayAttr attr, mlir::Type type)
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)
std::optional< mlir::Attribute > lowerConstArrayAttr(cir::ConstArrayAttr constArr, const mlir::TypeConverter *converter)
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)
StorageTy getZeroInitFromType(mlir::Type ty)
static bool stringTypeWithDifferentArraySize()