14#include "mlir/Dialect/LLVMIR/LLVMArithOps.h"
15#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
16#include "mlir/Dialect/LLVMIR/LLVMConstantOps.h"
17#include "mlir/Dialect/LLVMIR/LLVMModuleOps.h"
18#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
19#include "mlir/IR/BuiltinTypes.h"
20#include "mlir/IR/SymbolTable.h"
21#include "mlir/Interfaces/DataLayoutInterfaces.h"
30bool isSplitStorageBitInt(cir::IntType ty,
const mlir::DataLayout &dataLayout) {
33 unsigned storageBits = ty.getStorageTypeWidth(dataLayout);
34 auto storageTy = mlir::IntegerType::get(ty.getContext(), storageBits);
35 uint64_t storeSize = storageBits / 8;
37 llvm::alignTo(storeSize, dataLayout.getTypeABIAlignment(storageTy));
38 return allocSize != storeSize;
42bool isBigEndian(
const mlir::DataLayout &dataLayout) {
44 mlir::dyn_cast_or_null<mlir::StringAttr>(dataLayout.getEndianness());
45 return endiannessStr && endiannessStr ==
"big";
51 const mlir::DataLayout &dataLayout) {
52 auto intTy = mlir::cast<cir::IntType>(attr.getType());
53 unsigned storageBits = intTy.getStorageTypeWidth(dataLayout);
54 llvm::APInt val = attr.getValue();
55 val = intTy.isSigned() ? val.sext(storageBits) : val.zext(storageBits);
57 if (!isSplitStorageBitInt(intTy, dataLayout))
58 return rewriter.getIntegerAttr(
59 mlir::IntegerType::get(intTy.getContext(), storageBits), val);
61 if (isBigEndian(dataLayout))
66 unsigned numBytes = storageBits / 8;
68 bytes.reserve(numBytes);
69 for (
unsigned i = 0; i != numBytes; ++i)
70 bytes.emplace_back(8, val.extractBitsAsZExtValue(8, i * 8));
72 auto i8Ty = mlir::IntegerType::get(intTy.getContext(), 8);
73 return mlir::DenseElementsAttr::get(
74 mlir::RankedTensorType::get({numBytes}, i8Ty),
bytes);
78 mlir::DataLayout
const &dataLayout,
79 mlir::Type type,
bool isForHLSL) {
82 if (mlir::isa<cir::BoolType>(type)) {
83 return mlir::IntegerType::get(type.getContext(),
84 dataLayout.getTypeSizeInBits(type));
87 if (
auto matrixTy = mlir::dyn_cast<cir::MatrixType>(type)) {
89 unsigned vecLen = matrixTy.getNumRows();
90 unsigned arrayLen = matrixTy.getNumColumns();
91 mlir::Type elementType = converter.convertType(matrixTy.getElementType());
92 auto vecTy = mlir::VectorType::get(vecLen, elementType);
93 return mlir::LLVM::LLVMArrayType::get(vecTy, arrayLen);
96 uint64_t size = matrixTy.getNumRows() * matrixTy.getNumColumns();
97 mlir::Type elementType = converter.convertType(matrixTy.getElementType());
98 return mlir::LLVM::LLVMArrayType::get(elementType, size);
101 if (
auto vecTy = mlir::dyn_cast<cir::VectorType>(type)) {
102 if (mlir::isa<cir::BoolType>(vecTy.getElementType())) {
104 return mlir::IntegerType::get(type.getContext(),
105 vecTy.getBoolStorageWidth());
114 if (
auto intTy = mlir::dyn_cast<cir::IntType>(type);
115 intTy && intTy.isBitInt()) {
116 if (isSplitStorageBitInt(intTy, dataLayout))
117 return mlir::LLVM::LLVMArrayType::get(
118 mlir::IntegerType::get(type.getContext(), 8),
119 intTy.getStorageTypeWidth(dataLayout) / 8);
120 return mlir::IntegerType::get(type.getContext(),
121 intTy.getStorageTypeWidth(dataLayout));
124 return converter.convertType(type);
128 mlir::DataLayout
const &dataLayout,
133 if (
auto intTy = mlir::dyn_cast<cir::IntType>(type);
134 intTy && intTy.isBitInt())
135 return mlir::IntegerType::get(type.getContext(),
136 intTy.getStorageTypeWidth(dataLayout));
140 if (
auto matrixTy = mlir::dyn_cast<cir::MatrixType>(type)) {
141 uint64_t size = matrixTy.getNumRows() * matrixTy.getNumColumns();
142 mlir::Type elemTy = converter.convertType(matrixTy.getElementType());
143 return mlir::VectorType::get(size, elemTy);
150 if (
auto intTy = mlir::dyn_cast<cir::IntType>(ty))
151 return intTy.getWidth();
152 assert(mlir::isa<cir::BoolType>(ty) &&
153 "expected CIR integer or bool element type");
157mlir::DenseElementsAttr
160 const auto stringAttr = mlir::cast<mlir::StringAttr>(attr.getElts());
161 const auto arrayTy = mlir::cast<cir::ArrayType>(attr.getType());
162 const unsigned totalSize = arrayTy.getSize();
163 const unsigned trailingZeros = attr.getTrailingZerosNum();
164 assert(stringAttr.size() + trailingZeros == totalSize &&
165 "string const_array size must match explicit elements plus "
170 values.reserve(totalSize);
175 for (
const char element : stringAttr)
176 values.emplace_back(bitWidth,
static_cast<unsigned char>(element));
178 values.insert(values.end(), trailingZeros, mlir::APInt::getZero(bitWidth));
180 return mlir::DenseElementsAttr::get(
181 mlir::RankedTensorType::get({totalSize}, type),
llvm::ArrayRef(values));
185 const auto intTy = mlir::cast<mlir::IntegerType>(ty);
186 return mlir::APInt::getZero(intTy.getWidth());
190 auto fpTy = mlir::cast<mlir::FloatType>(ty);
191 return mlir::APFloat::getZero(fpTy.getFloatSemantics());
200template <
typename AttrTy,
typename StorageTy>
204 int64_t currentIndex, mlir::Type convertedElementType) {
205 if (
auto stringAttr = mlir::dyn_cast<mlir::StringAttr>(attr.getElts())) {
206 if (
auto arrayType = mlir::dyn_cast<cir::ArrayType>(attr.getType())) {
207 for (
auto element : stringAttr) {
208 auto intAttr = cir::IntAttr::get(arrayType.getElementType(), element);
209 values[currentIndex++] = mlir::dyn_cast<AttrTy>(intAttr).getValue();
212 currentIndex += attr.getTrailingZerosNum();
218 std::size_t elementsSizeInCurrentDim = 1;
219 for (std::size_t i = dimIndex; i < currentDims.size(); i++)
220 elementsSizeInCurrentDim *= currentDims[i];
222 auto arrayAttr = mlir::cast<mlir::ArrayAttr>(attr.getElts());
223 for (
auto eltAttr : arrayAttr) {
224 if constexpr (std::is_same_v<StorageTy, mlir::APInt>) {
225 if (
auto boolAttr = mlir::dyn_cast<cir::BoolAttr>(eltAttr)) {
227 mlir::cast<mlir::IntegerType>(convertedElementType).getWidth();
228 values[currentIndex++] =
229 llvm::APInt(bitWidth,
static_cast<uint64_t>(boolAttr.getValue()));
233 if (
auto valueAttr = mlir::dyn_cast<AttrTy>(eltAttr)) {
234 values[currentIndex++] = valueAttr.getValue();
238 if (
auto subArrayAttr = mlir::dyn_cast<cir::ConstArrayAttr>(eltAttr)) {
240 dimIndex, currentIndex,
241 convertedElementType);
242 currentIndex += elementsSizeInCurrentDim;
246 if (mlir::isa<cir::ZeroAttr, cir::UndefAttr>(eltAttr)) {
247 currentIndex += elementsSizeInCurrentDim;
253 if (
auto global = mlir::dyn_cast<cir::GlobalViewAttr>(eltAttr))
256 llvm_unreachable(
"unknown element in ConstArrayAttr");
261template <
typename AttrTy,
typename StorageTy>
262std::optional<mlir::DenseElementsAttr>
265 mlir::Type convertedElementType) {
266 unsigned vectorSize = 1;
267 for (
auto dim : dims)
275 convertedElementType))
278 return mlir::DenseElementsAttr::get(
279 mlir::RankedTensorType::get(dims, convertedElementType),
286 cir::GlobalViewAttr gv, mlir::ModuleOp moduleOp,
287 mlir::SymbolTableCollection &symbolTables,
288 const mlir::TypeConverter *converter,
290 if (gv.getIndices() || mlir::isa<cir::IntType, cir::VPtrType>(gv.getType()))
293 auto ptrTy = mlir::dyn_cast<cir::PointerType>(gv.getType());
297 unsigned sourceAddrSpace = 0;
298 mlir::Type sourceType;
299 auto sourceSymbol = symbolTables.lookupSymbolIn(moduleOp, gv.getSymbol());
300 if (
auto llvmSymbol = mlir::dyn_cast<mlir::LLVM::GlobalOp>(sourceSymbol)) {
301 if (mlir::Type preType = preConversionTypes.lookup(llvmSymbol.getSymName()))
302 sourceType = converter->convertType(preType);
304 sourceType = llvmSymbol.getType();
305 sourceAddrSpace = llvmSymbol.getAddrSpace();
306 }
else if (
auto cirSymbol = mlir::dyn_cast<cir::GlobalOp>(sourceSymbol)) {
307 sourceType = converter->convertType(cirSymbol.getSymType());
308 if (
auto targetAS = mlir::dyn_cast_if_present<cir::TargetAddressSpaceAttr>(
309 cirSymbol.getAddrSpaceAttr()))
310 sourceAddrSpace = targetAS.getValue();
317 auto llvmDstTy = converter->convertType<mlir::LLVM::LLVMPointerType>(ptrTy);
318 if (llvmDstTy.getAddressSpace() != sourceAddrSpace)
321 mlir::Type llvmEltTy = converter->convertType(ptrTy.getPointee());
322 if (llvmEltTy == sourceType)
324 if (
auto arrTy = mlir::dyn_cast<mlir::LLVM::LLVMArrayType>(sourceType))
325 return llvmEltTy == arrTy.getElementType();
333static std::optional<mlir::Attribute>
335 mlir::ModuleOp moduleOp,
336 mlir::SymbolTableCollection &symbolTables,
337 const mlir::TypeConverter *converter,
339 if (
auto gv = mlir::dyn_cast<cir::GlobalViewAttr>(elt)) {
344 return gv.getSymbol();
346 if (
auto nullPtr = mlir::dyn_cast<cir::ConstPtrAttr>(elt)) {
347 if (nullPtr.isNullValue())
348 return mlir::LLVM::ZeroAttr::get(ctx);
355 if (mlir::isa<cir::PoisonAttr>(attr))
357 if (
auto elts = mlir::dyn_cast<mlir::ArrayAttr>(attr))
359 if (
auto constArr = mlir::dyn_cast<cir::ConstArrayAttr>(attr)) {
360 if (mlir::isa<mlir::StringAttr>(constArr.getElts()))
362 if (
auto elts = mlir::dyn_cast<mlir::ArrayAttr>(constArr.getElts()))
374 if (mlir::isa<cir::BlockAddrInfoAttr, cir::BlockAddrDiffAttr>(attr))
376 if (
auto elts = mlir::dyn_cast<mlir::ArrayAttr>(attr))
378 if (
auto constArr = mlir::dyn_cast<cir::ConstArrayAttr>(attr)) {
379 if (mlir::isa<mlir::StringAttr>(constArr.getElts()))
381 if (
auto elts = mlir::dyn_cast<mlir::ArrayAttr>(constArr.getElts()))
392 auto elts = mlir::dyn_cast<mlir::ArrayAttr>(attr.getElts());
393 return elts && !elts.empty() &&
398 if (
auto record = mlir::dyn_cast<cir::ConstRecordAttr>(attr))
400 auto array = mlir::dyn_cast<cir::ConstArrayAttr>(attr);
405 auto elts = mlir::dyn_cast<mlir::ArrayAttr>(array.getElts());
413 if (
auto record = mlir::dyn_cast<cir::ConstRecordAttr>(attr)) {
416 return cir::ConstRecordAttr::get(
417 mlir::cast<cir::RecordType>(record.getType()),
418 mlir::ArrayAttr::get(attr.getContext(), members));
421 auto array = mlir::cast<cir::ConstArrayAttr>(attr);
422 auto arrayTy = mlir::cast<cir::ArrayType>(array.getType());
423 auto elts = llvm::map_to_vector(mlir::cast<mlir::ArrayAttr>(array.getElts()),
426 return cir::ConstArrayAttr::get(
427 arrayTy, mlir::ArrayAttr::get(attr.getContext(), elts),
428 array.getTrailingZerosNum());
429 elts.append(array.getTrailingZerosNum(),
430 cir::ZeroAttr::get(arrayTy.getElementType()));
431 return cir::ConstArrayAttr::get(arrayTy,
432 mlir::ArrayAttr::get(attr.getContext(), elts),
437 mlir::Attribute attr, mlir::SymbolTableCollection &symbolTables,
438 const mlir::TypeConverter *converter,
444static std::optional<mlir::Attribute>
446 mlir::SymbolTableCollection &symbolTables,
447 const mlir::TypeConverter *converter,
449 mlir::ModuleOp moduleOp) {
450 mlir::MLIRContext *ctx = constArr.getContext();
451 auto arrayTy = mlir::cast<cir::ArrayType>(constArr.getType());
452 auto elts = mlir::cast<mlir::ArrayAttr>(constArr.getElts());
453 mlir::Type adjustedTy =
455 *converter, mlir::DataLayout(moduleOp));
459 auto structTy = mlir::dyn_cast<mlir::LLVM::LLVMStructType>(adjustedTy);
460 if (structTy && structTy.getBody().size() < elts.size()) {
461 auto prefix = cir::ConstArrayAttr::get(
462 cir::ArrayType::get(arrayTy.getElementType(), elts.size()), elts);
464 prefix, symbolTables, converter, preConversionTypes, moduleOp);
467 lowered.push_back(*loweredPrefix);
469 for (mlir::Attribute elt : elts) {
471 elt, symbolTables, converter, preConversionTypes, moduleOp);
474 lowered.push_back(*loweredElt);
482 mlir::LLVM::ZeroAttr::get(ctx));
483 return mlir::ArrayAttr::get(ctx, lowered);
486std::optional<mlir::Attribute>
488 mlir::SymbolTableCollection &symbolTables,
489 const mlir::TypeConverter *converter,
491 mlir::ModuleOp moduleOp) {
493 const auto typedConstArr = mlir::cast<mlir::TypedAttr>(constArr);
496 const auto cirArrayType = mlir::cast<cir::ArrayType>(typedConstArr.getType());
499 mlir::Type type = cirArrayType;
501 while (
auto arrayType = mlir::dyn_cast<cir::ArrayType>(type)) {
502 dims.push_back(arrayType.getSize());
503 type = arrayType.getElementType();
516 preConversionTypes, moduleOp);
518 if (mlir::isa<mlir::StringAttr>(constArr.getElts()))
520 converter->convertType(type));
521 if (mlir::isa<cir::IntType>(type)) {
525 if (mlir::cast<cir::IntType>(type).isBitInt())
528 constArr, dims, converter->convertType(type));
531 if (mlir::isa<cir::BoolType>(type)) {
532 mlir::Type storageType =
538 if (mlir::isa<cir::FPTypeInterface>(type))
540 constArr, dims, converter->convertType(type));
542 if (mlir::isa<cir::PointerType>(type)) {
545 if (constArr.getTrailingZerosNum() > 0)
547 auto eltsArr = mlir::dyn_cast<mlir::ArrayAttr>(constArr.getElts());
551 lowered.reserve(eltsArr.size());
552 mlir::MLIRContext *ctx = constArr.getContext();
553 for (mlir::Attribute elt : eltsArr) {
555 elt, ctx, moduleOp, symbolTables, converter, preConversionTypes);
558 lowered.push_back(*llvmElt);
560 return mlir::ArrayAttr::get(ctx, lowered);
563 if (mlir::isa<cir::RecordType>(type)) {
565 auto eltsArr = mlir::dyn_cast<mlir::ArrayAttr>(constArr.getElts());
570 loweredElts.reserve(cirArrayType.getSize());
572 for (mlir::Attribute elt : eltsArr) {
574 elt, symbolTables, converter, preConversionTypes, moduleOp);
577 loweredElts.push_back(*llvmElt);
583 while (loweredElts.size() < cirArrayType.getSize())
584 loweredElts.push_back(mlir::LLVM::ZeroAttr::get(constArr.getContext()));
585 return mlir::ArrayAttr::get(constArr.getContext(), loweredElts);
598static std::optional<mlir::Attribute>
600 mlir::SymbolTableCollection &symbolTables,
601 const mlir::TypeConverter *converter,
603 mlir::ModuleOp moduleOp) {
604 mlir::MLIRContext *ctx = attr.getContext();
606 if (
auto arrayAttr = mlir::dyn_cast<cir::ConstArrayAttr>(attr))
608 preConversionTypes, moduleOp);
610 if (
auto recordAttr = mlir::dyn_cast<cir::ConstRecordAttr>(attr))
612 preConversionTypes, moduleOp);
614 if (mlir::isa<cir::ZeroAttr>(attr))
615 return mlir::LLVM::ZeroAttr::get(ctx);
617 if (mlir::isa<cir::UndefAttr>(attr))
618 return mlir::LLVM::UndefAttr::get(ctx);
620 if (
auto intAttr = mlir::dyn_cast<cir::IntAttr>(attr)) {
623 if (mlir::cast<cir::IntType>(intAttr.getType()).isBitInt())
625 return mlir::IntegerAttr::get(converter->convertType(intAttr.getType()),
629 if (
auto boolAttr = mlir::dyn_cast<cir::BoolAttr>(attr)) {
631 *converter, mlir::DataLayout(moduleOp), boolAttr.getType());
632 return mlir::IntegerAttr::get(storageType, boolAttr.getValue() ? 1 : 0);
635 if (
auto fpAttr = mlir::dyn_cast<cir::FPAttr>(attr))
636 return mlir::FloatAttr::get(converter->convertType(fpAttr.getType()),
658 for (mlir::Type member : members) {
659 uint64_t align = dataLayout.getTypeABIAlignment(member);
660 maxAlign = std::max(maxAlign, align);
661 uint64_t size = dataLayout.getTypeSize(member).getFixedValue();
663 if (llvm::alignTo(totalSize, align) != totalSize)
668 return llvm::alignTo(totalSize, maxAlign) != totalSize;
687 mlir::LLVM::LLVMStructType structTy, cir::ConstRecordAttr constRecord,
688 const mlir::TypeConverter &converter,
const mlir::DataLayout &dataLayout,
690 mlir::Type *failedType) {
691 assert(paddingAddedIndexes.empty() &&
692 "Not for accumulation, just single depth");
694 constRecord.getMembers().getValue();
697 bool packed = structTy.isPacked();
699 bool changed =
false;
705 for (
auto [idx, member] : llvm::enumerate(initMembers)) {
706 if (idx >= origBody.size())
709 origBody[idx], member, converter, dataLayout, failedType);
712 unsigned adjustedAlign = dataLayout.getTypeABIAlignment(adjusted);
714 if (adjusted != origBody[idx]) {
718 unsigned origAlign = dataLayout.getTypeABIAlignment(origBody[idx]);
721 packed ? curOffset : llvm::alignTo(curOffset, origAlign);
723 packed ? curOffset : llvm::alignTo(curOffset, adjustedAlign);
725 if (adjustedOffset != origOffset) {
729 assert(adjustedOffset < origOffset);
733 uint64_t difference = origOffset - curOffset;
734 newBody.push_back(mlir::LLVM::LLVMArrayType::get(
735 mlir::IntegerType::get(structTy.getContext(), 8), difference));
736 paddingAddedIndexes.push_back(idx);
737 curOffset = origOffset;
741 newBody.push_back(adjusted);
744 curOffset = llvm::alignTo(curOffset, adjustedAlign);
745 curOffset += dataLayout.getTypeSize(adjusted).getFixedValue();
754 bool widenedFAM =
false;
756 mlir::dyn_cast<mlir::LLVM::LLVMArrayType>(structTy.getBody().back());
757 fam && fam.getNumElements() == 0) {
758 mlir::Type lastInitType =
759 mlir::cast<mlir::TypedAttr>(initMembers.back()).getType();
760 if (mlir::cast<cir::ArrayType>(lastInitType).getSize() != 0) {
762 converter.convertType(lastInitType), initMembers.back(), converter,
763 dataLayout, failedType);
780 uint64_t declaredSize = dataLayout.getTypeSize(structTy).getFixedValue();
781 assert(curOffset <= declaredSize &&
"body bigger than type?");
782 if (curOffset < declaredSize)
783 newBody.push_back(mlir::LLVM::LLVMArrayType::get(
784 mlir::IntegerType::get(structTy.getContext(), 8),
785 declaredSize - curOffset));
788 return mlir::LLVM::LLVMStructType::getLiteral(structTy.getContext(), newBody,
801 mlir::LLVM::LLVMStructType structTy, cir::ConstRecordAttr constRecord,
802 const mlir::TypeConverter &converter,
const mlir::DataLayout &dataLayout,
803 mlir::Type *failedType) {
805 auto unionTy = mlir::cast<cir::UnionType>(constRecord.getType());
808 assert(constRecord.getMembers().size() == 1);
809 mlir::Attribute member = constRecord.getMembers()[0];
811 converter, dataLayout, mlir::cast<mlir::TypedAttr>(member).
getType());
824 if (memberTy == structTy.getBody().front())
827 uint64_t unionSize = dataLayout.getTypeSize(unionTy).getFixedValue();
828 uint64_t initSize = dataLayout.getTypeSize(memberTy).getFixedValue();
830 newBody.push_back(memberTy);
833 if (initSize < unionSize)
834 newBody.push_back(mlir::LLVM::LLVMArrayType::get(
835 mlir::IntegerType::get(structTy.getContext(), 8),
836 unionSize - initSize));
838 return mlir::LLVM::LLVMStructType::getLiteral(structTy.getContext(), newBody,
839 unionTy.getPacked());
853 mlir::LLVM::LLVMArrayType arrayTy, cir::ConstArrayAttr arrayInit,
854 const mlir::TypeConverter &converter,
const mlir::DataLayout &dataLayout,
855 mlir::Type *failedType) {
856 auto elts = mlir::dyn_cast<mlir::ArrayAttr>(arrayInit.getElts());
861 mlir::Type origEltTy = arrayTy.getElementType();
863 adjustedElts.reserve(elts.size());
864 bool changed = elts.size() > arrayTy.getNumElements();
866 for (mlir::Attribute elt : elts) {
868 dataLayout, failedType);
871 changed |= adjusted != origEltTy;
872 adjustedElts.push_back(adjusted);
876 mlir::Type commonEltTy =
877 llvm::all_equal(adjustedElts) ? adjustedElts.front() : mlir::Type{};
881 mlir::LLVM::LLVMArrayType::get(commonEltTy, adjustedElts.size()));
883 body.append(adjustedElts);
885 mlir::LLVM::LLVMArrayType::get(commonEltTy ? commonEltTy : origEltTy,
886 arrayInit.getTrailingZerosNum()));
887 return mlir::LLVM::LLVMStructType::getLiteral(arrayTy.getContext(), body,
895 std::max<size_t>(adjustedElts.size(), arrayTy.getNumElements()),
898 if (llvm::all_equal(adjustedElts))
899 return mlir::LLVM::LLVMArrayType::get(adjustedElts.front(),
900 adjustedElts.size());
903 return mlir::LLVM::LLVMStructType::getLiteral(
904 arrayTy.getContext(), adjustedElts,
true);
910 const mlir::TypeConverter &converter,
911 const mlir::DataLayout &dataLayout,
913 mlir::Type *failedType) {
916 if (
auto typedInit = mlir::dyn_cast_if_present<mlir::TypedAttr>(init))
917 *failedType = typedInit.getType();
920 if (
auto arrayInit = mlir::dyn_cast_if_present<cir::ConstArrayAttr>(init)) {
921 auto arrayTy = mlir::dyn_cast<mlir::LLVM::LLVMArrayType>(llvmType);
925 dataLayout, failedType);
929 auto constRecord = mlir::dyn_cast_if_present<cir::ConstRecordAttr>(init);
935 auto structTy = mlir::dyn_cast<mlir::LLVM::LLVMStructType>(llvmType);
936 if (!structTy || structTy.getBody().empty())
940 if (mlir::isa<cir::StructType>(constRecord.getType()))
942 dataLayout, paddingAddedIndexes,
944 if (mlir::isa<cir::UnionType>(constRecord.getType()))
946 dataLayout, failedType);
951 const mlir::TypeConverter &converter,
952 const mlir::DataLayout &dataLayout,
953 mlir::Type *failedType) {
956 ignoredAddedIndexes, failedType);
959std::optional<mlir::Attribute>
961 mlir::SymbolTableCollection &symbolTables,
962 const mlir::TypeConverter *converter,
964 mlir::ModuleOp moduleOp) {
970 mlir::ArrayAttr memberAttrs = constRecord.getMembers();
972 loweredMembers.reserve(memberAttrs.size());
973 for (mlir::Attribute member : memberAttrs) {
975 member, symbolTables, converter, preConversionTypes, moduleOp);
978 loweredMembers.push_back(*lowered);
996 converter->convertType(constRecord.getType()), constRecord, *converter,
997 mlir::DataLayout(moduleOp), paddingAddedIndexes);
1004 for (
unsigned paddedElt : llvm::reverse(paddingAddedIndexes))
1005 loweredMembers.insert(loweredMembers.begin() + paddedElt,
1006 mlir::LLVM::ZeroAttr::get(constRecord.getContext()));
1011 if (
auto structTy = mlir::dyn_cast<mlir::LLVM::LLVMStructType>(adjustedTy))
1012 while (loweredMembers.size() < structTy.getBody().size())
1013 loweredMembers.push_back(
1014 mlir::LLVM::ZeroAttr::get(constRecord.getContext()));
1016 return mlir::ArrayAttr::get(constRecord.getContext(), loweredMembers);
1020 mlir::Type typ,
const llvm::APInt &val) {
1021 return mlir::LLVM::ConstantOp::create(bld, loc, typ, val);
1024mlir::Value
getConst(mlir::OpBuilder &bld, mlir::Location loc, mlir::Type typ,
1026 return mlir::LLVM::ConstantOp::create(bld, loc, typ, val);
1029mlir::Value
createShL(mlir::OpBuilder &bld, mlir::Value lhs,
unsigned rhs) {
1032 mlir::Value rhsVal =
getConst(bld, lhs.getLoc(), lhs.getType(), rhs);
1033 return mlir::LLVM::ShlOp::create(bld, lhs.getLoc(), lhs, rhsVal);
1036mlir::Value
createAShR(mlir::OpBuilder &bld, mlir::Value lhs,
unsigned rhs) {
1039 mlir::Value rhsVal =
getConst(bld, lhs.getLoc(), lhs.getType(), rhs);
1040 return mlir::LLVM::AShrOp::create(bld, lhs.getLoc(), lhs, rhsVal);
1044 const llvm::APInt &rhs) {
1045 mlir::Value rhsVal =
getConstAPInt(bld, lhs.getLoc(), lhs.getType(), rhs);
1046 return mlir::LLVM::AndOp::create(bld, lhs.getLoc(), lhs, rhsVal);
1049mlir::Value
createLShR(mlir::OpBuilder &bld, mlir::Value lhs,
unsigned rhs) {
1052 mlir::Value rhsVal =
getConst(bld, lhs.getLoc(), lhs.getType(), rhs);
1053 return mlir::LLVM::LShrOp::create(bld, lhs.getLoc(), lhs, rhsVal);
static StringRef bytes(const std::vector< T, Allocator > &v)
static std::optional< mlir::Attribute > lowerPointerElementAttr(mlir::Attribute elt, mlir::MLIRContext *ctx, mlir::ModuleOp moduleOp, mlir::SymbolTableCollection &symbolTables, const mlir::TypeConverter *converter, const CIRPreConversionTypeMap &preConversionTypes)
Lower a single pointer-element of a cir.const_array to an LLVM-dialect constant leaf suitable for a b...
mlir::Attribute spellOutSplitTrailingZeros(mlir::Attribute attr)
Return attr with the trailing zeros of any array that adjustGlobalTypeForInit would split off written...
mlir::DenseElementsAttr convertStringAttrToDenseElementsAttr(cir::ConstArrayAttr attr, mlir::Type type)
static bool globalViewMatchesPointerLeaf(cir::GlobalViewAttr gv, mlir::ModuleOp moduleOp, mlir::SymbolTableCollection &symbolTables, const mlir::TypeConverter *converter, const CIRPreConversionTypeMap &preConversionTypes)
Return true when gv can be lowered to a FlatSymbolRefAttr leaf without addrspacecast or bitcast (mirr...
std::optional< mlir::DenseElementsAttr > convertToDenseElementsAttr(cir::ConstArrayAttr attr, const llvm::SmallVectorImpl< int64_t > &dims, mlir::Type convertedElementType)
mlir::Value createLShR(mlir::OpBuilder &bld, mlir::Value lhs, unsigned rhs)
static std::optional< mlir::Attribute > lowerConstRecordMemberAttr(mlir::Attribute attr, mlir::SymbolTableCollection &symbolTables, const mlir::TypeConverter *converter, const CIRPreConversionTypeMap &preConversionTypes, mlir::ModuleOp moduleOp)
Lower a constant attribute that initializes a single member of a record (or a leaf of a nested aggreg...
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)
static std::optional< mlir::Attribute > lowerConstArrayAttrByElement(cir::ConstArrayAttr constArr, mlir::SymbolTableCollection &symbolTables, const mlir::TypeConverter *converter, const CIRPreConversionTypeMap &preConversionTypes, mlir::ModuleOp moduleOp)
Lower constArr, which is or contains an array split by adjustGlobalArrayTypeForInit,...
mlir::Type convertTypeForMemory(const mlir::TypeConverter &converter, mlir::DataLayout const &dataLayout, mlir::Type type, bool isForHLSL)
static bool containsPoison(mlir::Attribute attr)
mlir::Value createAShR(mlir::OpBuilder &bld, mlir::Value lhs, unsigned rhs)
static mlir::Type adjustGlobalArrayTypeForInit(mlir::LLVM::LLVMArrayType arrayTy, cir::ConstArrayAttr arrayInit, const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout, mlir::Type *failedType)
mlir::Value createAnd(mlir::OpBuilder &bld, mlir::Value lhs, const llvm::APInt &rhs)
static mlir::Type adjustGlobalUnionTypeForInit(mlir::LLVM::LLVMStructType structTy, cir::ConstRecordAttr constRecord, const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout, mlir::Type *failedType)
static bool containsSplitTrailingZeros(mlir::Attribute attr)
mlir::Type convertTypeForLoadStore(const mlir::TypeConverter &converter, mlir::DataLayout const &dataLayout, mlir::Type type)
The type of a load/store's value, as opposed to convertTypeForMemory's type of the memory it lives in...
static bool shouldPackFAMStruct(const mlir::DataLayout &dataLayout, llvm::ArrayRef< mlir::Type > members)
static bool shouldSplitTrailingZeros(cir::ConstArrayAttr attr)
static bool containsBlockAddress(mlir::Attribute attr)
Block-address attributes (address-of-label and label differences) are lowered to relocation expressio...
std::optional< mlir::Attribute > lowerConstArrayAttr(cir::ConstArrayAttr constArr, mlir::SymbolTableCollection &symbolTables, const mlir::TypeConverter *converter, const CIRPreConversionTypeMap &preConversionTypes, mlir::ModuleOp moduleOp)
std::optional< mlir::Attribute > lowerConstRecordAttr(cir::ConstRecordAttr constRecord, mlir::SymbolTableCollection &symbolTables, const mlir::TypeConverter *converter, const CIRPreConversionTypeMap &preConversionTypes, mlir::ModuleOp moduleOp)
static constexpr int minSplitTrailingZeros
The fewest trailing zeros that adjustGlobalArrayTypeForInit splits off an array constant into a zero-...
static mlir::Type adjustGlobalStructTypeForInit(mlir::LLVM::LLVMStructType structTy, cir::ConstRecordAttr constRecord, const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout, llvm::SmallVectorImpl< unsigned > &paddingAddedIndexes, mlir::Type *failedType)
mlir::Type adjustGlobalTypeForInit(mlir::Type llvmType, mlir::Attribute init, const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout, llvm::SmallVectorImpl< unsigned > &paddingAddedIndexes, mlir::Type *failedType)
mlir::Attribute getBitIntStorageAttr(mlir::ConversionPatternRewriter &rewriter, cir::IntAttr attr, const mlir::DataLayout &dataLayout)
mlir::APInt getZeroInitFromType(mlir::Type ty)
bool convertToDenseElementsAttrImpl(cir::ConstArrayAttr attr, llvm::SmallVectorImpl< StorageTy > &values, const llvm::SmallVectorImpl< int64_t > ¤tDims, int64_t dimIndex, int64_t currentIndex, mlir::Type convertedElementType)
static unsigned getIntOrBoolBitWidth(mlir::Type ty)
mlir::Attribute spellOutSplitTrailingZeros(mlir::Attribute attr)
Return attr with the trailing zeros of any array that adjustGlobalTypeForInit would split off written...
StorageTy getZeroInitFromType(mlir::Type ty)
Return a zero with the lowered element type's width or float semantics.
llvm::StringMap< mlir::Type > CIRPreConversionTypeMap
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t int64_t
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t