Skip to content

Commit 6d9f736

Browse files
authored
Make math.fpowi rewrite more systematic and test it. (iree-org#20293)
This is a follow-up to iree-org#20281 which lacked a test, and had left the c++ logic in a state that was prone to unintentional regression. Also, this ensures that the `math.fpowi` rewrite happens even with `-iree-codegen-gpu-native-math-precision`, which is intentional as `math.fpowi` is not really a math function, like the comment explains. Signed-off-by: Benoit Jacob <[email protected]>
1 parent a5e2d27 commit 6d9f736

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

compiler/src/iree/compiler/Codegen/Common/MathTransformPass.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ static void populateMathFunctionsRewritePatterns(
6363

6464
static bool predicateRewrite(StringRef name,
6565
IREE::HAL::ExecutableTargetAttr target) {
66+
if (name == math::FPowIOp::getOperationName()) {
67+
// math.fpowi is a special op: it isn't really a "math function", rather
68+
// it is generally used with a constant exponent that is a small integer,
69+
// and it is then a shorthand for a few multiplications. That rewrite needs
70+
// to happen to prevent falling back on a more expensive, more general
71+
// implementation like math.powf.
72+
return true;
73+
}
6674
if (clNativeMathPrecision) { // Legacy.
6775
if (name == math::Exp2Op::getOperationName() ||
6876
name == math::RoundEvenOp::getOperationName()) {
@@ -72,9 +80,7 @@ static bool predicateRewrite(StringRef name,
7280
if (isROCMBackend(target)) {
7381
// On ROCm, we do not need most rewrites as we can generally bottom out on
7482
// either device library functions, or handling of intrinsics in AMDGPU.
75-
// An important exception is math.fpowi, which needs to get rewritten to
76-
// multiplications.
77-
return llvm::is_contained({math::FPowIOp::getOperationName()}, name);
83+
return false;
7884
}
7985
// Currently enable all non-approximative rewrites.
8086
return true;

compiler/src/iree/compiler/Codegen/Common/test/math_transform.mlir

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,32 @@ func.func @no_approx_on_rocm(%arg0: f16) -> f16 attributes {
9191
%11 = math.erf %10 : f16
9292
return %11 : f16
9393
}
94+
95+
// -----
96+
97+
// CHECK-LABEL: @rewrite_fpowi_const_exponent_on_llvmcpu
98+
func.func @rewrite_fpowi_const_exponent_on_llvmcpu(%arg0: f32) -> f32 attributes {
99+
hal.executable.target = #hal.executable.target<"llvm-cpu", "xyz", {target_triple = "x86_64-xyz-xyz"}>
100+
} {
101+
// math.fpowi with constant exponent should always be rewritten to muls.
102+
// CHECK-NOT: math.fpowi
103+
// CHECK: arith.mulf {{.*}} : f32
104+
%c2 = arith.constant 2 : i32
105+
%0 = math.fpowi %arg0, %c2 : f32, i32
106+
return %0 : f32
107+
}
108+
109+
// -----
110+
111+
// CHECK-LABEL: @rewrite_fpowi_const_exponent_on_rocm
112+
func.func @rewrite_fpowi_const_exponent_on_rocm(%arg0: f32) -> f32 attributes {
113+
hal.executable.target = #hal.executable.target<"rocm", "rocm-hsaco-fb", {}>
114+
} {
115+
// math.fpowi with constant exponent should always be rewritten to muls.
116+
// CHECK-NOT: math.fpowi
117+
// CHECK: arith.mulf {{.*}} : f32
118+
// CHECK: arith.mulf {{.*}} : f32
119+
%c2 = arith.constant 3 : i32
120+
%0 = math.fpowi %arg0, %c2 : f32, i32
121+
return %0 : f32
122+
}

0 commit comments

Comments
 (0)