[Codegen] Register the Vector dialect in math transforms (#25212) While testing vector `math.cbrt` with `iree-codegen-math-transform`, `iree-opt` crashes because the approximation creates `vector.broadcast` operations without loading the Vector dialect. This registers the dialect and adds a CPU regression test. The input uses vector types without any Vector operations, so parsing doesn't load the dialect for us. Signed-off-by: anutosh491 <andersonbhat491@gmail.com>
diff --git a/compiler/src/iree/compiler/Codegen/Common/MathTransform.cpp b/compiler/src/iree/compiler/Codegen/Common/MathTransform.cpp index f4552cd..b67da56 100644 --- a/compiler/src/iree/compiler/Codegen/Common/MathTransform.cpp +++ b/compiler/src/iree/compiler/Codegen/Common/MathTransform.cpp
@@ -11,6 +11,7 @@ #include "mlir/Dialect/Math/Transforms/Approximation.h" #include "mlir/Dialect/Math/Transforms/Passes.h" #include "mlir/Dialect/SCF/IR/SCF.h" +#include "mlir/Dialect/Vector/IR/VectorOps.h" #include "mlir/IR/PatternMatch.h" #include "mlir/Pass/Pass.h" #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
diff --git a/compiler/src/iree/compiler/Codegen/Common/Passes.td b/compiler/src/iree/compiler/Codegen/Common/Passes.td index d599c56..4c26a1c 100644 --- a/compiler/src/iree/compiler/Codegen/Common/Passes.td +++ b/compiler/src/iree/compiler/Codegen/Common/Passes.td
@@ -1039,7 +1039,8 @@ let summary = "Apply math ops transformations: approximations, rewrites to other math ops, operand casts."; let dependentDialects = [ "mlir::scf::SCFDialect", - "mlir::arith::ArithDialect" + "mlir::arith::ArithDialect", + "mlir::vector::VectorDialect" ]; }
diff --git a/compiler/src/iree/compiler/Codegen/Common/test/math_transform.mlir b/compiler/src/iree/compiler/Codegen/Common/test/math_transform.mlir index ddcdd93..1e0c42b 100644 --- a/compiler/src/iree/compiler/Codegen/Common/test/math_transform.mlir +++ b/compiler/src/iree/compiler/Codegen/Common/test/math_transform.mlir
@@ -1,5 +1,22 @@ // RUN: iree-opt --pass-pipeline='builtin.module(func.func(iree-codegen-math-transform))' --split-input-file %s | FileCheck %s +// CHECK-LABEL: @cbrt_cpu_vector +func.func @cbrt_cpu_vector(%arg0: vector<4xf32>) -> vector<4xf32> attributes { + hal.executable.target = #hal.executable.target<"llvm-cpu", "xyz", {target_triple = "x86_64-xyz-xyz"}> +} { + // Vector types alone do not load the Vector dialect. The pass must load it + // before the approximation creates vector.broadcast operations. + // CHECK: math.absf + // CHECK: arith.bitcast + // CHECK: math.copysign + // CHECK-NOT: math.cbrt + // CHECK: return + %0 = math.cbrt %arg0 : vector<4xf32> + return %0 : vector<4xf32> +} + +// ----- + // CHECK-LABEL: @rewrite_tan func.func @rewrite_tan(%arg0: f16) -> f16 attributes { hal.executable.target = #hal.executable.target<"llvm-cpu", "xyz", {target_triple = "x86_64-xyz-xyz"}>