[Mlir-commits] [mlir] [TOSA] Convert tosa.elementwise to linalg.elementwise (PR #224344)
Chuanqi Xu
llvmlistbot at llvm.org
Mon Sep 21 20:51:31 PDT 2026
================
@@ -18,15 +18,161 @@
#include "mlir/Dialect/Tosa/IR/TosaOps.h"
#include "mlir/Dialect/Tosa/Utils/ConversionUtils.h"
#include "mlir/Dialect/Utils/ReshapeOpsUtils.h"
+#include "mlir/IR/Matchers.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Transforms/DialectConversion.h"
#include "llvm/ADT/SmallVectorExtras.h"
+#include "llvm/ADT/TypeSwitch.h"
+#include <optional>
#include <type_traits>
using namespace mlir;
using namespace mlir::tosa;
+// Number of leading data operands the op forwards to linalg.elementwise,
+// matching its unary/binary/ternary arity group. Trailing metadata operands
+// (e.g. MulOp's shift, NegateOp's zero points) are excluded.
+static unsigned getArity(Operation *op) {
+ return llvm::TypeSwitch<Operation *, unsigned>(op)
+ .Case<tosa::AbsOp, tosa::CeilOp, tosa::FloorOp, tosa::ExpOp, tosa::LogOp,
+ tosa::RsqrtOp, tosa::SinOp, tosa::CosOp, tosa::TanhOp, tosa::ErfOp,
+ tosa::ReciprocalOp, tosa::NegateOp>([](Operation *) { return 1u; })
+ .Case<tosa::AddOp, tosa::SubOp, tosa::IntDivOp, tosa::MulOp, tosa::PowOp,
+ tosa::MaximumOp, tosa::MinimumOp>([](Operation *) { return 2u; })
+ .Case<tosa::SelectOp>([](Operation *) { return 3u; })
+ .DefaultUnreachable("Invalid elementwise operation");
+}
+
+// Return the linalg.elementwise kind that is semantically equivalent to the
+// given TOSA elementwise op for the provided element type, or std::nullopt if
+// the op has no direct linalg.elementwise counterpart. Only ops whose TOSA
+// lowering maps to a single linalg.elementwise function are handled; the more
+// involved lowerings (e.g. quantized negate, mul with shift, casts) are left to
+// the generic TosaToLinalg conversion.
+static std::optional<linalg::ElementwiseKind>
+getElementwiseKind(Operation *op, Type elementTy) {
+ using linalg::ElementwiseKind;
+ const bool isFloat = isa<FloatType>(elementTy);
+ const bool isSignlessInt =
+ elementTy.isSignlessInteger() && !elementTy.isInteger(1);
----------------
ChuanqiXu9 wrote:
Why `!elementTy.isInteger(1)`? This reads unnatural.
https://github.com/llvm/llvm-project/pull/224344
More information about the Mlir-commits
mailing list