[Mlir-commits] [mlir] [mlir][SPIR-V] Convert math.clampf to spirv.GLFClamp and math.ctpop to spirv.BitCount (PR #200454)
Arseniy Obolenskiy
llvmlistbot at llvm.org
Fri May 29 09:46:42 PDT 2026
https://github.com/aobolensk created https://github.com/llvm/llvm-project/pull/200454
None
>From 1fdccc8939b872580f0cb34b37909b4b317803a1 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Fri, 29 May 2026 18:27:33 +0200
Subject: [PATCH] [mlir][SPIR-V] Convert math.clampf to spirv.GLFClamp and
math.ctpop to spirv.BitCount
---
mlir/lib/Conversion/MathToSPIRV/MathToSPIRV.cpp | 2 ++
.../MathToSPIRV/math-to-fpclassify-spirv.mlir | 9 +++++++++
.../Conversion/MathToSPIRV/math-to-gl-spirv.mlir | 15 +++++++++++++++
3 files changed, 26 insertions(+)
diff --git a/mlir/lib/Conversion/MathToSPIRV/MathToSPIRV.cpp b/mlir/lib/Conversion/MathToSPIRV/MathToSPIRV.cpp
index cf5ea9716b9f7..674fb1e586701 100644
--- a/mlir/lib/Conversion/MathToSPIRV/MathToSPIRV.cpp
+++ b/mlir/lib/Conversion/MathToSPIRV/MathToSPIRV.cpp
@@ -522,6 +522,7 @@ void populateMathToSPIRVPatterns(const SPIRVTypeConverter &typeConverter,
// Core patterns
patterns
.add<CopySignPattern,
+ CheckedElementwiseOpPattern<math::CtPopOp, spirv::BitCountOp>,
CheckedElementwiseOpPattern<math::IsInfOp, spirv::IsInfOp>,
CheckedElementwiseOpPattern<math::IsNaNOp, spirv::IsNanOp>,
CheckedElementwiseOpPattern<math::IsFiniteOp, spirv::IsFiniteOp>,
@@ -536,6 +537,7 @@ void populateMathToSPIRVPatterns(const SPIRVTypeConverter &typeConverter,
CheckedElementwiseOpPattern<math::AbsIOp, spirv::GLSAbsOp>,
CheckedElementwiseOpPattern<math::AtanOp, spirv::GLAtanOp>,
CheckedElementwiseOpPattern<math::CeilOp, spirv::GLCeilOp>,
+ CheckedElementwiseOpPattern<math::ClampFOp, spirv::GLFClampOp>,
CheckedElementwiseOpPattern<math::CosOp, spirv::GLCosOp>,
CheckedElementwiseOpPattern<math::ExpOp, spirv::GLExpOp>,
CheckedElementwiseOpPattern<math::Exp2Op, spirv::GLExp2Op>,
diff --git a/mlir/test/Conversion/MathToSPIRV/math-to-fpclassify-spirv.mlir b/mlir/test/Conversion/MathToSPIRV/math-to-fpclassify-spirv.mlir
index 157a485826a33..5b875c97c17c9 100644
--- a/mlir/test/Conversion/MathToSPIRV/math-to-fpclassify-spirv.mlir
+++ b/mlir/test/Conversion/MathToSPIRV/math-to-fpclassify-spirv.mlir
@@ -29,4 +29,13 @@ module attributes {
return
}
+ // CHECK-LABEL: @ctpop
+ func.func @ctpop(%i: i32, %iv: vector<4xi32>) {
+ // CHECK: spirv.BitCount %{{.*}} : i32
+ %0 = math.ctpop %i : i32
+ // CHECK: spirv.BitCount %{{.*}} : vector<4xi32>
+ %1 = math.ctpop %iv : vector<4xi32>
+ return
+ }
+
}
diff --git a/mlir/test/Conversion/MathToSPIRV/math-to-gl-spirv.mlir b/mlir/test/Conversion/MathToSPIRV/math-to-gl-spirv.mlir
index 08d7822d04cc1..9dee79914b0dc 100644
--- a/mlir/test/Conversion/MathToSPIRV/math-to-gl-spirv.mlir
+++ b/mlir/test/Conversion/MathToSPIRV/math-to-gl-spirv.mlir
@@ -135,6 +135,21 @@ func.func @float32_ternary_vector(%a: vector<4xf32>, %b: vector<4xf32>,
return
}
+// CHECK-LABEL: @float32_clamp_scalar
+func.func @float32_clamp_scalar(%value: f32, %min: f32, %max: f32) {
+ // CHECK: spirv.GL.FClamp %{{.*}}, %{{.*}}, %{{.*}} : f32
+ %0 = math.clampf %value to [%min, %max] : f32
+ return
+}
+
+// CHECK-LABEL: @float32_clamp_vector
+func.func @float32_clamp_vector(%value: vector<4xf32>, %min: vector<4xf32>,
+ %max: vector<4xf32>) {
+ // CHECK: spirv.GL.FClamp %{{.*}}, %{{.*}}, %{{.*}} : vector<4xf32>
+ %0 = math.clampf %value to [%min, %max] : vector<4xf32>
+ return
+}
+
// CHECK-LABEL: @int_unary
func.func @int_unary(%arg0: i32) {
// CHECK: spirv.GL.SAbs %{{.*}}
More information about the Mlir-commits
mailing list