Skip to content

Commit cc99086

Browse files
[mlir][IR] Add Builder::getArrayAttr overload for concrete attributes
1 parent 1df7b51 commit cc99086

File tree

6 files changed

+36
-35
lines changed

6 files changed

+36
-35
lines changed

mlir/include/mlir/IR/Builders.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ class Builder {
112112
StringAttr getStringAttr(const Twine &bytes);
113113
ArrayAttr getArrayAttr(ArrayRef<Attribute> value);
114114

115+
// Convenience method for containers of specific attribute types. E.g., this
116+
// overload will match SmallVector<IntegerAttr>.
117+
template <typename ContainerTy>
118+
ArrayAttr getArrayAttr(const ContainerTy &value) {
119+
auto ref = ArrayRef(value);
120+
return getArrayAttr(ArrayRef<Attribute>(
121+
static_cast<const Attribute *>(ref.data()), ref.size()));
122+
}
123+
115124
// Returns a 0-valued attribute of the given `type`. This function only
116125
// supports boolean, integer, and 16-/32-/64-bit float types, and vector or
117126
// ranked tensor of them. Returns null attribute otherwise.

mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,10 @@ static void buildMatmulOp(OpBuilder &b, OperationState &state,
193193
RegionBuilderFn regionBuilder,
194194
ArrayRef<AffineMap> indexingMaps) {
195195
// Initialize indexingMaps attribute, for MatmulOp.
196-
SmallVector<Attribute, 3> indexingMapsAttrVal;
197-
indexingMapsAttrVal =
198-
llvm::map_to_vector(indexingMaps, [](AffineMap map) -> Attribute {
199-
return AffineMapAttr::get(map);
200-
});
201-
state.addAttribute("indexing_maps", b.getArrayAttr(indexingMapsAttrVal));
196+
state.addAttribute("indexing_maps", b.getArrayAttr(llvm::map_to_vector(
197+
indexingMaps, [](AffineMap map) {
198+
return AffineMapAttr::get(map);
199+
})));
202200
return buildStructuredOp(b, state, resultTensorTypes, inputs, outputs,
203201
attributes, regionBuilder);
204202
}
@@ -210,12 +208,10 @@ static void buildBatchMatmulOp(OpBuilder &b, OperationState &state,
210208
RegionBuilderFn regionBuilder,
211209
ArrayRef<AffineMap> indexingMaps) {
212210
// Initialize indexingMaps attribute, for BatchMatmulOp.
213-
SmallVector<Attribute, 4> indexingMapsAttrVal;
214-
indexingMapsAttrVal =
215-
llvm::map_to_vector(indexingMaps, [](AffineMap map) -> Attribute {
216-
return AffineMapAttr::get(map);
217-
});
218-
state.addAttribute("indexing_maps", b.getArrayAttr(indexingMapsAttrVal));
211+
state.addAttribute("indexing_maps", b.getArrayAttr(llvm::map_to_vector(
212+
indexingMaps, [](AffineMap map) {
213+
return AffineMapAttr::get(map);
214+
})));
219215
return buildStructuredOp(b, state, resultTensorTypes, inputs, outputs,
220216
attributes, regionBuilder);
221217
}
@@ -227,12 +223,10 @@ static void buildBatchReduceMatmulOp(OpBuilder &b, OperationState &state,
227223
RegionBuilderFn regionBuilder,
228224
ArrayRef<AffineMap> indexingMaps) {
229225
// Initialize indexingMaps attribute, for BatchReduceMatmulOp.
230-
SmallVector<Attribute, 4> indexingMapsAttrVal;
231-
indexingMapsAttrVal =
232-
llvm::map_to_vector(indexingMaps, [](AffineMap map) -> Attribute {
233-
return AffineMapAttr::get(map);
234-
});
235-
state.addAttribute("indexing_maps", b.getArrayAttr(indexingMapsAttrVal));
226+
state.addAttribute("indexing_maps", b.getArrayAttr(llvm::map_to_vector(
227+
indexingMaps, [](AffineMap map) {
228+
return AffineMapAttr::get(map);
229+
})));
236230
return buildStructuredOp(b, state, resultTensorTypes, inputs, outputs,
237231
attributes, regionBuilder);
238232
}
@@ -1121,7 +1115,7 @@ void GenericOp::build(
11211115
builder.getAffineMapArrayAttr(indexingMaps),
11221116
builder.getArrayAttr(llvm::to_vector(llvm::map_range(
11231117
iteratorTypes,
1124-
[&](utils::IteratorType iter) -> mlir::Attribute {
1118+
[&](utils::IteratorType iter) {
11251119
return IteratorTypeAttr::get(builder.getContext(), iter);
11261120
}))),
11271121
doc.empty() ? StringAttr() : builder.getStringAttr(doc),
@@ -3914,7 +3908,7 @@ ParseResult MatmulOp::parse(OpAsmParser &parser, OperationState &result) {
39143908
if (*indexingMapsAttr == nullptr) {
39153909
auto indexingMapAttrs = llvm::map_to_vector(
39163910
MatmulOp::getDefaultIndexingMaps(parser.getContext()),
3917-
[](AffineMap map) -> Attribute { return AffineMapAttr::get(map); });
3911+
[](AffineMap map) { return AffineMapAttr::get(map); });
39183912
indexingMapsAttr = parser.getBuilder().getArrayAttr(indexingMapAttrs);
39193913
}
39203914

mlir/lib/Dialect/SparseTensor/Transforms/SparseReinterpretMap.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,9 @@ translateMap(linalg::GenericOp op, PatternRewriter &rewriter) {
325325
}
326326
};
327327

328-
SmallVector<Attribute> iterAttr =
329-
llvm::map_to_vector(itTps, [ctx](auto itTp) -> Attribute {
330-
return linalg::IteratorTypeAttr::get(ctx, itTp);
331-
});
328+
auto iterAttr = llvm::map_to_vector(itTps, [ctx](auto itTp) {
329+
return linalg::IteratorTypeAttr::get(ctx, itTp);
330+
});
332331

333332
return std::make_pair(rewriter.getAffineMapArrayAttr(idxMapArray),
334333
rewriter.getArrayAttr(iterAttr));

mlir/lib/Dialect/Utils/ReshapeOpsUtils.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,10 @@ static unsigned getMaxPosOfType(ArrayRef<ReassociationExprs> exprArrays) {
422422

423423
ArrayAttr mlir::getReassociationIndicesAttribute(
424424
Builder &b, ArrayRef<ReassociationIndices> reassociation) {
425-
SmallVector<Attribute, 4> reassociationAttr =
426-
llvm::to_vector<4>(llvm::map_range(
427-
reassociation, [&](const ReassociationIndices &indices) -> Attribute {
428-
return cast<Attribute>(b.getI64ArrayAttr(indices));
429-
}));
425+
auto reassociationAttr = llvm::map_to_vector(
426+
reassociation, [&](const ReassociationIndices &indices) {
427+
return b.getI64ArrayAttr(indices);
428+
});
430429
return b.getArrayAttr(reassociationAttr);
431430
}
432431

mlir/lib/Dialect/Vector/IR/VectorOps.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -798,10 +798,10 @@ void vector::ContractionOp::build(OpBuilder &builder, OperationState &result,
798798
AffineMap::inferFromExprList(indexingExprs, builder.getContext())));
799799
result.addAttribute(
800800
getIteratorTypesAttrName(result.name),
801-
builder.getArrayAttr(llvm::to_vector(llvm::map_range(
802-
iteratorTypes, [&](IteratorType t) -> mlir::Attribute {
801+
builder.getArrayAttr(
802+
llvm::map_to_vector(iteratorTypes, [&](IteratorType t) {
803803
return IteratorTypeAttr::get(builder.getContext(), t);
804-
}))));
804+
})));
805805
}
806806

807807
void vector::ContractionOp::build(OpBuilder &builder, OperationState &result,

mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ struct MultiReduceToContract
105105
rewriter.replaceOpWithNewOp<mlir::vector::ContractionOp>(
106106
reduceOp, mulOp->getOperand(0), mulOp->getOperand(1), reduceOp.getAcc(),
107107
rewriter.getAffineMapArrayAttr({srcMap, srcMap, dstMap}),
108-
rewriter.getArrayAttr(llvm::to_vector(llvm::map_range(
109-
iteratorTypes, [&](IteratorType t) -> mlir::Attribute {
108+
rewriter.getArrayAttr(
109+
llvm::map_to_vector(iteratorTypes, [&](IteratorType t) {
110110
return IteratorTypeAttr::get(rewriter.getContext(), t);
111-
}))));
111+
})));
112112
return success();
113113
}
114114
};

0 commit comments

Comments
 (0)