|
46 | 46 | #include "mlir/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.h"
|
47 | 47 | #include "mlir/Target/LLVMIR/Export.h"
|
48 | 48 | #include "mlir/Transforms/DialectConversion.h"
|
| 49 | +#include "clang/CIR/LoweringHelpers.h" |
49 | 50 | #include "clang/CIR/Dialect/IR/CIRAttrs.h"
|
50 | 51 | #include "clang/CIR/Dialect/IR/CIRDialect.h"
|
51 | 52 | #include "clang/CIR/Dialect/IR/CIROpsEnums.h"
|
@@ -962,134 +963,6 @@ class CIRStoreLowering : public mlir::OpConversionPattern<mlir::cir::StoreOp> {
|
962 | 963 | }
|
963 | 964 | };
|
964 | 965 |
|
965 |
| -mlir::DenseElementsAttr |
966 |
| -convertStringAttrToDenseElementsAttr(mlir::cir::ConstArrayAttr attr, |
967 |
| - mlir::Type type) { |
968 |
| - auto values = llvm::SmallVector<mlir::APInt, 8>{}; |
969 |
| - auto stringAttr = mlir::dyn_cast<mlir::StringAttr>(attr.getElts()); |
970 |
| - assert(stringAttr && "expected string attribute here"); |
971 |
| - for (auto element : stringAttr) |
972 |
| - values.push_back({8, (uint64_t)element}); |
973 |
| - return mlir::DenseElementsAttr::get( |
974 |
| - mlir::RankedTensorType::get({(int64_t)values.size()}, type), |
975 |
| - llvm::ArrayRef(values)); |
976 |
| -} |
977 |
| - |
978 |
| -template <typename StorageTy> StorageTy getZeroInitFromType(mlir::Type Ty); |
979 |
| - |
980 |
| -template <> mlir::APInt getZeroInitFromType(mlir::Type Ty) { |
981 |
| - assert(mlir::isa<mlir::cir::IntType>(Ty) && "expected int type"); |
982 |
| - auto IntTy = mlir::cast<mlir::cir::IntType>(Ty); |
983 |
| - return mlir::APInt::getZero(IntTy.getWidth()); |
984 |
| -} |
985 |
| - |
986 |
| -template <> mlir::APFloat getZeroInitFromType(mlir::Type Ty) { |
987 |
| - assert((mlir::isa<mlir::cir::SingleType, mlir::cir::DoubleType>(Ty)) && |
988 |
| - "only float and double supported"); |
989 |
| - if (Ty.isF32() || mlir::isa<mlir::cir::SingleType>(Ty)) |
990 |
| - return mlir::APFloat(0.f); |
991 |
| - if (Ty.isF64() || mlir::isa<mlir::cir::DoubleType>(Ty)) |
992 |
| - return mlir::APFloat(0.0); |
993 |
| - llvm_unreachable("NYI"); |
994 |
| -} |
995 |
| - |
996 |
| -// return the nested type and quantity of elements for cir.array type. |
997 |
| -// e.g: for !cir.array<!cir.array<!s32i x 3> x 1> |
998 |
| -// it returns !s32i as return value and stores 3 to elemQuantity. |
999 |
| -mlir::Type getNestedTypeAndElemQuantity(mlir::Type Ty, unsigned &elemQuantity) { |
1000 |
| - assert(mlir::isa<mlir::cir::ArrayType>(Ty) && "expected ArrayType"); |
1001 |
| - |
1002 |
| - elemQuantity = 1; |
1003 |
| - mlir::Type nestTy = Ty; |
1004 |
| - while (auto ArrTy = mlir::dyn_cast<mlir::cir::ArrayType>(nestTy)) { |
1005 |
| - nestTy = ArrTy.getEltType(); |
1006 |
| - elemQuantity *= ArrTy.getSize(); |
1007 |
| - } |
1008 |
| - |
1009 |
| - return nestTy; |
1010 |
| -} |
1011 |
| - |
1012 |
| -template <typename AttrTy, typename StorageTy> |
1013 |
| -void convertToDenseElementsAttrImpl(mlir::cir::ConstArrayAttr attr, |
1014 |
| - llvm::SmallVectorImpl<StorageTy> &values) { |
1015 |
| - auto arrayAttr = mlir::cast<mlir::ArrayAttr>(attr.getElts()); |
1016 |
| - for (auto eltAttr : arrayAttr) { |
1017 |
| - if (auto valueAttr = mlir::dyn_cast<AttrTy>(eltAttr)) { |
1018 |
| - values.push_back(valueAttr.getValue()); |
1019 |
| - } else if (auto subArrayAttr = |
1020 |
| - mlir::dyn_cast<mlir::cir::ConstArrayAttr>(eltAttr)) { |
1021 |
| - convertToDenseElementsAttrImpl<AttrTy>(subArrayAttr, values); |
1022 |
| - } else if (auto zeroAttr = mlir::dyn_cast<mlir::cir::ZeroAttr>(eltAttr)) { |
1023 |
| - unsigned numStoredZeros = 0; |
1024 |
| - auto nestTy = |
1025 |
| - getNestedTypeAndElemQuantity(zeroAttr.getType(), numStoredZeros); |
1026 |
| - values.insert(values.end(), numStoredZeros, |
1027 |
| - getZeroInitFromType<StorageTy>(nestTy)); |
1028 |
| - } else { |
1029 |
| - llvm_unreachable("unknown element in ConstArrayAttr"); |
1030 |
| - } |
1031 |
| - } |
1032 |
| - |
1033 |
| - // Only fill in trailing zeros at the local cir.array level where the element |
1034 |
| - // type isn't another array (for the mult-dim case). |
1035 |
| - auto numTrailingZeros = attr.getTrailingZerosNum(); |
1036 |
| - if (numTrailingZeros) { |
1037 |
| - auto localArrayTy = mlir::dyn_cast<mlir::cir::ArrayType>(attr.getType()); |
1038 |
| - assert(localArrayTy && "expected !cir.array"); |
1039 |
| - |
1040 |
| - auto nestTy = localArrayTy.getEltType(); |
1041 |
| - if (!mlir::isa<mlir::cir::ArrayType>(nestTy)) |
1042 |
| - values.insert(values.end(), localArrayTy.getSize() - numTrailingZeros, |
1043 |
| - getZeroInitFromType<StorageTy>(nestTy)); |
1044 |
| - } |
1045 |
| -} |
1046 |
| - |
1047 |
| -template <typename AttrTy, typename StorageTy> |
1048 |
| -mlir::DenseElementsAttr |
1049 |
| -convertToDenseElementsAttr(mlir::cir::ConstArrayAttr attr, |
1050 |
| - const llvm::SmallVectorImpl<int64_t> &dims, |
1051 |
| - mlir::Type type) { |
1052 |
| - auto values = llvm::SmallVector<StorageTy, 8>{}; |
1053 |
| - convertToDenseElementsAttrImpl<AttrTy>(attr, values); |
1054 |
| - return mlir::DenseElementsAttr::get(mlir::RankedTensorType::get(dims, type), |
1055 |
| - llvm::ArrayRef(values)); |
1056 |
| -} |
1057 |
| - |
1058 |
| -std::optional<mlir::Attribute> |
1059 |
| -lowerConstArrayAttr(mlir::cir::ConstArrayAttr constArr, |
1060 |
| - const mlir::TypeConverter *converter) { |
1061 |
| - |
1062 |
| - // Ensure ConstArrayAttr has a type. |
1063 |
| - auto typedConstArr = mlir::dyn_cast<mlir::TypedAttr>(constArr); |
1064 |
| - assert(typedConstArr && "cir::ConstArrayAttr is not a mlir::TypedAttr"); |
1065 |
| - |
1066 |
| - // Ensure ConstArrayAttr type is a ArrayType. |
1067 |
| - auto cirArrayType = |
1068 |
| - mlir::dyn_cast<mlir::cir::ArrayType>(typedConstArr.getType()); |
1069 |
| - assert(cirArrayType && "cir::ConstArrayAttr is not a cir::ArrayType"); |
1070 |
| - |
1071 |
| - // Is a ConstArrayAttr with an cir::ArrayType: fetch element type. |
1072 |
| - mlir::Type type = cirArrayType; |
1073 |
| - auto dims = llvm::SmallVector<int64_t, 2>{}; |
1074 |
| - while (auto arrayType = mlir::dyn_cast<mlir::cir::ArrayType>(type)) { |
1075 |
| - dims.push_back(arrayType.getSize()); |
1076 |
| - type = arrayType.getEltType(); |
1077 |
| - } |
1078 |
| - |
1079 |
| - // Convert array attr to LLVM compatible dense elements attr. |
1080 |
| - if (mlir::isa<mlir::StringAttr>(constArr.getElts())) |
1081 |
| - return convertStringAttrToDenseElementsAttr(constArr, |
1082 |
| - converter->convertType(type)); |
1083 |
| - if (mlir::isa<mlir::cir::IntType>(type)) |
1084 |
| - return convertToDenseElementsAttr<mlir::cir::IntAttr, mlir::APInt>( |
1085 |
| - constArr, dims, converter->convertType(type)); |
1086 |
| - if (mlir::isa<mlir::cir::CIRFPTypeInterface>(type)) |
1087 |
| - return convertToDenseElementsAttr<mlir::cir::FPAttr, mlir::APFloat>( |
1088 |
| - constArr, dims, converter->convertType(type)); |
1089 |
| - |
1090 |
| - return std::nullopt; |
1091 |
| -} |
1092 |
| - |
1093 | 966 | bool hasTrailingZeros(mlir::cir::ConstArrayAttr attr) {
|
1094 | 967 | auto array = mlir::dyn_cast<mlir::ArrayAttr>(attr.getElts());
|
1095 | 968 | return attr.hasTrailingZeros() ||
|
|
0 commit comments