[Mlir-commits] [mlir] [mlir][arith] `arith-to-apfloat`: Add vector support (PR #171024)

Matthias Springer llvmlistbot at llvm.org
Sun Dec 7 10:42:33 PST 2025


================
@@ -90,6 +91,75 @@ static Value getSemanticsValue(OpBuilder &b, Location loc, FloatType floatTy) {
                                    b.getIntegerAttr(b.getI32Type(), sem));
 }
 
+/// Given two operands of vector type and vector result type (with the same
+/// shape), call the given function for each pair of scalar operands and
+/// package the result into a vector. If the given operands and result type are
+/// not vectors, call the function directly. The second operand is optional.
+template <typename Fn, typename... Values>
+static Value forEachScalarValue(RewriterBase &rewriter, Location loc,
+                                Value operand1, Value operand2, Type resultType,
+                                Fn fn) {
+  auto vecTy1 = dyn_cast<VectorType>(operand1.getType());
+  if (operand2) {
+    // Sanity check: Operand types must match.
+    assert(vecTy1 == dyn_cast<VectorType>(operand2.getType()) &&
+           "expected same vector types");
+  }
+  if (!vecTy1) {
+    // Not a vector. Call the function directly.
+    return fn(operand1, operand2, resultType);
+  }
+
+  // Prepare scalar operands.
+  auto sclars1 = vector::ToElementsOp::create(rewriter, loc, operand1);
+  SmallVector<Value> scalars2;
+  if (!operand2) {
+    // No second operand. Create a vector of empty values.
+    scalars2.assign(vecTy1.getNumElements(), Value());
+  } else {
+    llvm::append_range(
+        scalars2,
+        vector::ToElementsOp::create(rewriter, loc, operand2)->getResults());
+  }
+
+  // Call the function for each pair of scalar operands.
+  auto resultVecType = cast<VectorType>(resultType);
+  SmallVector<Value> results;
+  for (auto [scalar1, scalar2] : llvm::zip(sclars1->getResults(), scalars2)) {
----------------
matthias-springer wrote:

The above `assert` covers it, but I added `zipped_equal` just in case.

https://github.com/llvm/llvm-project/pull/171024


More information about the Mlir-commits mailing list