55 assert(type.getRank() > 0 &&
"Cannot handle rank-0 vectors");
56 auto vShape = type.getShape();
57 assert(llvm::all_of(vShape, [](int64_t dim) {
return dim > 0; }) &&
58 "Vector dimensions cannot be dynamic");
59 return std::accumulate(vShape.begin(), vShape.end(), 1,
60 std::multiplies<int64_t>());
80 mlir::Type stype = type.getElementType();
82 if (
auto itype = llvm::dyn_cast<mlir::IntegerType>(stype)) {
84 assert(itype.getWidth() <= 64);
87 width = itype.getWidth() <= 16 ? 32 : 64;
89 width = itype.getWidth() <= 16 ? 48 : 80;
91 mlir::Type ctype = mlir::IntegerType::get(itype.getContext(), width);
92 return mlir::VectorType::get(type.getShape(), ctype);
95 if (
auto ftype = llvm::dyn_cast<mlir::FloatType>(stype)) {
96 if (
AIE2 && ftype.getWidth() == 16)
97 return mlir::VectorType::get(type.getShape(),
98 mlir::Float32Type::get(ftype.getContext()));
105 llvm::report_fatal_error(
"Unsupported destination type");
111 llvm::ArrayRef<mlir::AffineExpr> exprs,
112 mlir::MLIRContext *context) {
114 if (sizes.empty() || exprs.empty())
117 if (is_contained(sizes, 0))
118 return getAffineConstantExpr(0, context);
120 auto maps = mlir::AffineMap::inferFromExprList(exprs, context);
124 unsigned nSymbols = maps[0].getNumSymbols();
126 mlir::AffineExpr expr;
127 bool dynamicPoisonBit =
false;
128 int64_t runningSize = 1;
129 for (
auto en : zip(reverse(exprs), reverse(sizes))) {
130 int64_t size = std::get<1>(en);
134 mlir::AffineExpr dimExpr = std::get<0>(en);
135 mlir::AffineExpr stride = dynamicPoisonBit
136 ? getAffineSymbolExpr(nSymbols++, context)
137 : getAffineConstantExpr(runningSize, context);
138 expr = expr ? expr + dimExpr * stride : dimExpr * stride;
141 if (runningSize <= 0)
144 dynamicPoisonBit =
true;
151 auto memRefType = llvm::cast<mlir::MemRefType>(updOp.getSource().getType());
152 mlir::MLIRContext *context = memRefType.getContext();
154 llvm::SmallVector<mlir::AffineExpr, 8> exprVec;
155 llvm::SmallDenseMap<mlir::Value, mlir::AffineExpr, 8> indexToExprDimMap;
156 for (
auto idxAndValue : llvm::enumerate(updOp.getIndices())) {
157 auto value = idxAndValue.value();
158 if (
auto apOf = value.getDefiningOp<mlir::affine::AffineApplyOp>()) {
159 mlir::AffineMap map = apOf.getAffineMap();
161 if (map.getNumResults() != 1)
164 llvm::SmallVector<mlir::AffineExpr, 4> indexExprs;
166 for (
auto index : apOf.getMapOperands())
167 if (
auto cIdx = index.getDefiningOp<mlir::arith::ConstantOp>()) {
169 llvm::cast<mlir::IntegerAttr>(cIdx.getValue()).getValue();
170 unsigned idx = idxVal.getSExtValue();
171 indexExprs.push_back(getAffineConstantExpr(idx, context));
173 if (!indexToExprDimMap.count(index))
174 indexToExprDimMap[index] =
175 getAffineDimExpr(indexToExprDimMap.size(), context);
176 indexExprs.push_back(indexToExprDimMap[index]);
179 exprVec.push_back(map.getResult(0).replaceDims(indexExprs));
180 }
else if (
auto cOp = value.getDefiningOp<mlir::arith::ConstantOp>()) {
181 auto idxVal = llvm::cast<mlir::IntegerAttr>(cOp.getValue()).getValue();
182 unsigned idx = idxVal.getSExtValue();
183 exprVec.push_back(getAffineConstantExpr(idx, context));
185 if (!indexToExprDimMap.count(value))
186 indexToExprDimMap[value] =
187 getAffineDimExpr(indexToExprDimMap.size(), context);
188 exprVec.push_back(indexToExprDimMap[value]);
196 memRefType.getContext());
206 mlir::AffineExpr base = expr;
209 if (
auto constExpr = llvm::dyn_cast<mlir::AffineConstantExpr>(expr)) {
211 offset += constExpr.getValue();
212 }
else if (
auto binopExpr = llvm::dyn_cast<mlir::AffineBinaryOpExpr>(expr)) {
213 if (binopExpr.getKind() == mlir::AffineExprKind::Add) {
214 mlir::AffineExpr lhs = binopExpr.getLHS(), rhs = binopExpr.getRHS();
215 if (
auto constExpr = llvm::dyn_cast<mlir::AffineConstantExpr>(lhs)) {
217 offset += constExpr.getValue();
219 if (
auto constExpr = llvm::dyn_cast<mlir::AffineConstantExpr>(rhs)) {
220 base = base == rhs ? nullptr : lhs;
221 offset += constExpr.getValue();
225 return std::make_pair(base, offset);