|
| 1 | +//====- CIRLoweringHelpers.cpp - Lowering helper funcstions ---------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This file contains helper functions for lowering from CIR to LLVM or MLIR. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +#include "clang/CIR/CIRLoweringHelpers.h" |
| 13 | + |
| 14 | +mlir::DenseElementsAttr |
| 15 | +convertStringAttrToDenseElementsAttr(mlir::cir::ConstArrayAttr attr, |
| 16 | + mlir::Type type) { |
| 17 | + auto values = llvm::SmallVector<mlir::APInt, 8>{}; |
| 18 | + auto stringAttr = mlir::dyn_cast<mlir::StringAttr>(attr.getElts()); |
| 19 | + auto arrayTy = mlir::dyn_cast<mlir::cir::ArrayType>(attr.getType()); |
| 20 | + assert(arrayTy && "String attribute must have an array type"); |
| 21 | + assert(stringAttr && "expected string attribute here"); |
| 22 | + |
| 23 | + for (auto element : stringAttr) |
| 24 | + values.push_back({8, (uint64_t)element}); |
| 25 | + // Insert trailing zeros when the array size greater than the string. |
| 26 | + // E.g. char big_string[100] = "123"; |
| 27 | + if (arrayTy.getSize() > stringAttr.size()) { |
| 28 | + for (unsigned i = 0; i < arrayTy.getSize() - stringAttr.size(); ++i) |
| 29 | + values.push_back({8, (uint64_t)0}); |
| 30 | + } |
| 31 | + return mlir::DenseElementsAttr::get( |
| 32 | + mlir::RankedTensorType::get({(int64_t)values.size()}, type), |
| 33 | + llvm::ArrayRef(values)); |
| 34 | +} |
| 35 | + |
| 36 | +template <> mlir::APInt getZeroInitFromType(mlir::Type Ty) { |
| 37 | + assert(mlir::isa<mlir::cir::IntType>(Ty) && "expected int type"); |
| 38 | + auto IntTy = mlir::cast<mlir::cir::IntType>(Ty); |
| 39 | + return mlir::APInt::getZero(IntTy.getWidth()); |
| 40 | +} |
| 41 | + |
| 42 | +template <> mlir::APFloat getZeroInitFromType(mlir::Type Ty) { |
| 43 | + assert((mlir::isa<mlir::cir::SingleType, mlir::cir::DoubleType>(Ty)) && |
| 44 | + "only float and double supported"); |
| 45 | + if (Ty.isF32() || mlir::isa<mlir::cir::SingleType>(Ty)) |
| 46 | + return mlir::APFloat(0.f); |
| 47 | + if (Ty.isF64() || mlir::isa<mlir::cir::DoubleType>(Ty)) |
| 48 | + return mlir::APFloat(0.0); |
| 49 | + llvm_unreachable("NYI"); |
| 50 | +} |
| 51 | + |
| 52 | +// return the nested type and quantity of elements for cir.array type. |
| 53 | +// e.g: for !cir.array<!cir.array<!s32i x 3> x 1> |
| 54 | +// it returns !s32i as return value and stores 3 to elemQuantity. |
| 55 | +mlir::Type getNestedTypeAndElemQuantity(mlir::Type Ty, unsigned &elemQuantity) { |
| 56 | + assert(mlir::isa<mlir::cir::ArrayType>(Ty) && "expected ArrayType"); |
| 57 | + |
| 58 | + elemQuantity = 1; |
| 59 | + mlir::Type nestTy = Ty; |
| 60 | + while (auto ArrTy = mlir::dyn_cast<mlir::cir::ArrayType>(nestTy)) { |
| 61 | + nestTy = ArrTy.getEltType(); |
| 62 | + elemQuantity *= ArrTy.getSize(); |
| 63 | + } |
| 64 | + |
| 65 | + return nestTy; |
| 66 | +} |
| 67 | + |
| 68 | +template <typename AttrTy, typename StorageTy> |
| 69 | +void convertToDenseElementsAttrImpl(mlir::cir::ConstArrayAttr attr, |
| 70 | + llvm::SmallVectorImpl<StorageTy> &values) { |
| 71 | + auto arrayAttr = mlir::cast<mlir::ArrayAttr>(attr.getElts()); |
| 72 | + for (auto eltAttr : arrayAttr) { |
| 73 | + if (auto valueAttr = mlir::dyn_cast<AttrTy>(eltAttr)) { |
| 74 | + values.push_back(valueAttr.getValue()); |
| 75 | + } else if (auto subArrayAttr = |
| 76 | + mlir::dyn_cast<mlir::cir::ConstArrayAttr>(eltAttr)) { |
| 77 | + convertToDenseElementsAttrImpl<AttrTy>(subArrayAttr, values); |
| 78 | + } else if (auto zeroAttr = mlir::dyn_cast<mlir::cir::ZeroAttr>(eltAttr)) { |
| 79 | + unsigned numStoredZeros = 0; |
| 80 | + auto nestTy = |
| 81 | + getNestedTypeAndElemQuantity(zeroAttr.getType(), numStoredZeros); |
| 82 | + values.insert(values.end(), numStoredZeros, |
| 83 | + getZeroInitFromType<StorageTy>(nestTy)); |
| 84 | + } else { |
| 85 | + llvm_unreachable("unknown element in ConstArrayAttr"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Only fill in trailing zeros at the local cir.array level where the element |
| 90 | + // type isn't another array (for the mult-dim case). |
| 91 | + auto numTrailingZeros = attr.getTrailingZerosNum(); |
| 92 | + if (numTrailingZeros) { |
| 93 | + auto localArrayTy = mlir::dyn_cast<mlir::cir::ArrayType>(attr.getType()); |
| 94 | + assert(localArrayTy && "expected !cir.array"); |
| 95 | + |
| 96 | + auto nestTy = localArrayTy.getEltType(); |
| 97 | + if (!mlir::isa<mlir::cir::ArrayType>(nestTy)) |
| 98 | + values.insert(values.end(), localArrayTy.getSize() - numTrailingZeros, |
| 99 | + getZeroInitFromType<StorageTy>(nestTy)); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +template <typename AttrTy, typename StorageTy> |
| 104 | +mlir::DenseElementsAttr |
| 105 | +convertToDenseElementsAttr(mlir::cir::ConstArrayAttr attr, |
| 106 | + const llvm::SmallVectorImpl<int64_t> &dims, |
| 107 | + mlir::Type type) { |
| 108 | + auto values = llvm::SmallVector<StorageTy, 8>{}; |
| 109 | + convertToDenseElementsAttrImpl<AttrTy>(attr, values); |
| 110 | + return mlir::DenseElementsAttr::get(mlir::RankedTensorType::get(dims, type), |
| 111 | + llvm::ArrayRef(values)); |
| 112 | +} |
| 113 | + |
| 114 | +std::optional<mlir::Attribute> |
| 115 | +lowerConstArrayAttr(mlir::cir::ConstArrayAttr constArr, |
| 116 | + const mlir::TypeConverter *converter) { |
| 117 | + |
| 118 | + // Ensure ConstArrayAttr has a type. |
| 119 | + auto typedConstArr = mlir::dyn_cast<mlir::TypedAttr>(constArr); |
| 120 | + assert(typedConstArr && "cir::ConstArrayAttr is not a mlir::TypedAttr"); |
| 121 | + |
| 122 | + // Ensure ConstArrayAttr type is a ArrayType. |
| 123 | + auto cirArrayType = |
| 124 | + mlir::dyn_cast<mlir::cir::ArrayType>(typedConstArr.getType()); |
| 125 | + assert(cirArrayType && "cir::ConstArrayAttr is not a cir::ArrayType"); |
| 126 | + |
| 127 | + // Is a ConstArrayAttr with an cir::ArrayType: fetch element type. |
| 128 | + mlir::Type type = cirArrayType; |
| 129 | + auto dims = llvm::SmallVector<int64_t, 2>{}; |
| 130 | + while (auto arrayType = mlir::dyn_cast<mlir::cir::ArrayType>(type)) { |
| 131 | + dims.push_back(arrayType.getSize()); |
| 132 | + type = arrayType.getEltType(); |
| 133 | + } |
| 134 | + |
| 135 | + if (mlir::isa<mlir::StringAttr>(constArr.getElts())) |
| 136 | + return convertStringAttrToDenseElementsAttr(constArr, |
| 137 | + converter->convertType(type)); |
| 138 | + if (mlir::isa<mlir::cir::IntType>(type)) |
| 139 | + return convertToDenseElementsAttr<mlir::cir::IntAttr, mlir::APInt>( |
| 140 | + constArr, dims, converter->convertType(type)); |
| 141 | + if (mlir::isa<mlir::cir::CIRFPTypeInterface>(type)) |
| 142 | + return convertToDenseElementsAttr<mlir::cir::FPAttr, mlir::APFloat>( |
| 143 | + constArr, dims, converter->convertType(type)); |
| 144 | + |
| 145 | + return std::nullopt; |
| 146 | +} |
0 commit comments