[Codegen][WebGPU] Enable the existing cbrt approximation (#25213) Depends on #25212. I'll rebase this onto main once that lands. The WebGPU path leaves `math.cbrt` unlowered, which fails SPIR-V conversion because WGSL has no cube-root builtin. This enables MLIR's existing cube-root approximation for WebGPU. It uses only finite constants, so the other polynomial approximations remain disabled. I've added scalar and vector compiler tests. 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 b67da56..9b585b0 100644 --- a/compiler/src/iree/compiler/Codegen/Common/MathTransform.cpp +++ b/compiler/src/iree/compiler/Codegen/Common/MathTransform.cpp
@@ -104,7 +104,8 @@ // https://github.com/gpuweb/gpuweb/issues/5109 means we get compilation // errors whenever Inf or NaN values arise at compile-time, which is not // something that we can really prevent. Avoiding this rewrite helps a bit. - return false; + // cbrt has no WGSL builtin and its expansion uses only finite constants. + return name == math::CbrtOp::getOperationName(); } // Compute hasFastExp from target attribute.
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 1e0c42b..d726eab 100644 --- a/compiler/src/iree/compiler/Codegen/Common/test/math_transform.mlir +++ b/compiler/src/iree/compiler/Codegen/Common/test/math_transform.mlir
@@ -17,6 +17,41 @@ // ----- +// CHECK-LABEL: @cbrt_webgpu_scalar +func.func @cbrt_webgpu_scalar(%arg0: f32) -> f32 attributes { + hal.executable.target = #hal.executable.target<"webgpu-spirv", "webgpu-wgsl-fb", {}> +} { + // WebGPU has no native cbrt. Use the bit estimate and Newton refinement. + // CHECK: math.absf + // CHECK: arith.bitcast + // CHECK: arith.divf + // CHECK: arith.select + // CHECK: math.copysign + // CHECK-NOT: math.cbrt + // CHECK: return + %0 = math.cbrt %arg0 : f32 + return %0 : f32 +} + +// ----- + +// CHECK-LABEL: @cbrt_webgpu_vector +func.func @cbrt_webgpu_vector(%arg0: vector<4xf32>) -> vector<4xf32> attributes { + hal.executable.target = #hal.executable.target<"webgpu-spirv", "webgpu-wgsl-fb", {}> +} { + // CHECK: math.absf {{.*}} : vector<4xf32> + // CHECK: arith.bitcast {{.*}} : vector<4xf32> to vector<4xi32> + // CHECK: arith.divf {{.*}} : vector<4xf32> + // CHECK: arith.select + // CHECK: math.copysign {{.*}} : vector<4xf32> + // 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"}>