[Mlir-commits] [mlir] [TOSA] Convert tosa.elementwise to linalg.elementwise (PR #224344)
Sayan Saha
llvmlistbot at llvm.org
Sat Sep 26 10:43:20 PDT 2026
================
@@ -603,6 +728,81 @@ class DepthwiseConvConverter
}
};
+// Lower TOSA elementwise ops that have a direct linalg.elementwise named op
+// counterpart. Unlike linalg.generic, linalg.elementwise dispatches on a single
+// function kind and infers the body, so the conversion is a handful of checks
+// (kind, ranks, static shapes) followed by construction of the indexing maps
+// and the named op.
+template <typename TosaOp>
+class ElementwiseConverter : public OpConversionPattern<TosaOp> {
+public:
+ using OpConversionPattern<TosaOp>::OpConversionPattern;
+ using typename OpConversionPattern<TosaOp>::OpAdaptor;
+
+ LogicalResult
+ matchAndRewrite(TosaOp op, OpAdaptor adaptor,
+ ConversionPatternRewriter &rewriter) const final {
+ if (!isConvertibleToLinalgElementwise(op))
+ return rewriter.notifyMatchFailure(
+ op, "op has no linalg.elementwise counterpart");
+
+ Location loc = op.getLoc();
+ auto resultTy = cast<RankedTensorType>(op.getType());
+ const int64_t rank = resultTy.getRank();
+
+ std::optional<linalg::ElementwiseKind> kind =
+ getElementwiseKind(op, resultTy.getElementType());
+ const unsigned arity =
+ llvm::to_underlying(linalg::getArityGroupAndKind(*kind).arityGroup);
+
+ // Trailing metadata operands (e.g. MulOp's shift, NegateOp's zero points)
+ // are not forwarded to linalg.elementwise.
+ ValueRange inputs = adaptor.getOperands().take_front(arity);
+
+ // Build one indexing map per input, broadcasting static size-1 dimensions,
+ // followed by the identity map for the output.
+ SmallVector<AffineMap> indexingMaps;
+ for (Value operand : inputs) {
+ ArrayRef<int64_t> shape = cast<ShapedType>(operand.getType()).getShape();
+ SmallVector<AffineExpr> exprs;
+ for (const auto &dim : llvm::enumerate(shape)) {
+ bool requiresBroadcast =
----------------
sahas3 wrote:
>From the comment on L:734 about static shape I assume dynamic dims in operands are not supported but there's no guard for that -- so I think here `tosa.add` on `tensor<?xf32>` and `tensor<4xf32>` producing `tensor<4xf32>` will produce incorrect IR since `requiresBroadcast` will be `false`.
https://github.com/llvm/llvm-project/pull/224344
More information about the Mlir-commits
mailing list