Skip to content

[MLIR][SPIRV] Add spirv.IsFinite and lower math.{isfinite,isinf,isnan} to spirv. #151552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mlir/include/mlir/Dialect/SPIRV/IR/SPIRVBase.td
Original file line number Diff line number Diff line change
Expand Up @@ -4448,6 +4448,7 @@ def SPIRV_OC_OpUMulExtended : I32EnumAttrCase<"OpUMulExtended"
def SPIRV_OC_OpSMulExtended : I32EnumAttrCase<"OpSMulExtended", 152>;
def SPIRV_OC_OpIsNan : I32EnumAttrCase<"OpIsNan", 156>;
def SPIRV_OC_OpIsInf : I32EnumAttrCase<"OpIsInf", 157>;
def SPIRV_OC_OpIsFinite : I32EnumAttrCase<"OpIsFinite", 158>;
def SPIRV_OC_OpOrdered : I32EnumAttrCase<"OpOrdered", 162>;
def SPIRV_OC_OpUnordered : I32EnumAttrCase<"OpUnordered", 163>;
def SPIRV_OC_OpLogicalEqual : I32EnumAttrCase<"OpLogicalEqual", 164>;
Expand Down Expand Up @@ -4630,7 +4631,8 @@ def SPIRV_OpcodeAttr :
SPIRV_OC_OpVectorTimesMatrix, SPIRV_OC_OpMatrixTimesVector,
SPIRV_OC_OpMatrixTimesMatrix, SPIRV_OC_OpDot, SPIRV_OC_OpIAddCarry,
SPIRV_OC_OpISubBorrow, SPIRV_OC_OpUMulExtended, SPIRV_OC_OpSMulExtended,
SPIRV_OC_OpIsNan, SPIRV_OC_OpIsInf, SPIRV_OC_OpOrdered, SPIRV_OC_OpUnordered,
SPIRV_OC_OpIsNan, SPIRV_OC_OpIsInf, SPIRV_OC_OpIsFinite,
SPIRV_OC_OpOrdered, SPIRV_OC_OpUnordered,
SPIRV_OC_OpLogicalEqual, SPIRV_OC_OpLogicalNotEqual, SPIRV_OC_OpLogicalOr,
SPIRV_OC_OpLogicalAnd, SPIRV_OC_OpLogicalNot, SPIRV_OC_OpSelect,
SPIRV_OC_OpIEqual, SPIRV_OC_OpINotEqual, SPIRV_OC_OpUGreaterThan,
Expand Down
26 changes: 24 additions & 2 deletions mlir/include/mlir/Dialect/SPIRV/IR/SPIRVLogicalOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,28 @@ def SPIRV_INotEqualOp : SPIRV_LogicalBinaryOp<"INotEqual",

// -----

def SPIRV_IsFiniteOp : SPIRV_LogicalUnaryOp<"IsFinite", SPIRV_Float, []> {
let summary = "Result is true if x is an IEEE Finite, otherwise result is false";

let description = [{
Result Type must be a scalar or vector of Boolean type.

x must be a scalar or vector of floating-point type. It must have the
same number of components as Result Type.

Results are computed per component.

#### Example:

```mlir
%2 = spirv.IsFinite %0: f32
%3 = spirv.IsFinite %1: vector<4xf32>
```
}];
}

// -----

def SPIRV_IsInfOp : SPIRV_LogicalUnaryOp<"IsInf", SPIRV_Float, []> {
let summary = "Result is true if x is an IEEE Inf, otherwise result is false";

Expand All @@ -418,7 +440,7 @@ def SPIRV_IsInfOp : SPIRV_LogicalUnaryOp<"IsInf", SPIRV_Float, []> {

```mlir
%2 = spirv.IsInf %0: f32
%3 = spirv.IsInf %1: vector<4xi32>
%3 = spirv.IsInf %1: vector<4xf32>
```
}];
}
Expand All @@ -442,7 +464,7 @@ def SPIRV_IsNanOp : SPIRV_LogicalUnaryOp<"IsNan", SPIRV_Float, []> {

```mlir
%2 = spirv.IsNan %0: f32
%3 = spirv.IsNan %1: vector<4xi32>
%3 = spirv.IsNan %1: vector<4xf32>
```
}];
}
Expand Down
7 changes: 6 additions & 1 deletion mlir/lib/Conversion/MathToSPIRV/MathToSPIRV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,12 @@ namespace mlir {
void populateMathToSPIRVPatterns(const SPIRVTypeConverter &typeConverter,
RewritePatternSet &patterns) {
// Core patterns
patterns.add<CopySignPattern>(typeConverter, patterns.getContext());
patterns
.add<CopySignPattern,
CheckedElementwiseOpPattern<math::IsInfOp, spirv::IsInfOp>,
CheckedElementwiseOpPattern<math::IsNaNOp, spirv::IsNanOp>,
CheckedElementwiseOpPattern<math::IsFiniteOp, spirv::IsFiniteOp>>(
typeConverter, patterns.getContext());

// GLSL patterns
patterns
Expand Down
27 changes: 27 additions & 0 deletions mlir/test/Conversion/MathToSPIRV/math-to-fpclassify-spirv.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: mlir-opt --convert-math-to-spirv %s | FileCheck %s

module attributes {
spirv.target_env = #spirv.target_env<#spirv.vce<v1.0, [Shader], []>, #spirv.resource_limits<>>
} {

// CHECK-LABEL: @fpclassify
func.func @fpclassify(%x: f32, %v: vector<4xf32>) {
// CHECK: spirv.IsFinite %{{.*}} : f32
%0 = math.isfinite %x : f32
// CHECK: spirv.IsFinite %{{.*}} : vector<4xf32>
%1 = math.isfinite %v : vector<4xf32>

// CHECK: spirv.IsNan %{{.*}} : f32
%2 = math.isnan %x : f32
// CHECK: spirv.IsNan %{{.*}} : vector<4xf32>
%3 = math.isnan %v : vector<4xf32>

// CHECK: spirv.IsInf %{{.*}} : f32
%4 = math.isinf %x : f32
// CHECK: spirv.IsInf %{{.*}} : vector<4xf32>
%5 = math.isinf %v : vector<4xf32>

return
}

}
18 changes: 18 additions & 0 deletions mlir/test/Dialect/SPIRV/IR/logical-ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ func.func @inotequal_vector(%arg0: vector<4xi32>, %arg1: vector<4xi32>) -> vecto

// -----

//===----------------------------------------------------------------------===//
// spirv.IsFinite
//===----------------------------------------------------------------------===//

func.func @isfinite_scalar(%arg0: f32) -> i1 {
// CHECK: spirv.IsFinite {{.*}} : f32
%0 = spirv.IsFinite %arg0 : f32
return %0 : i1
}

func.func @isfinite_vector(%arg0: vector<2xf32>) -> vector<2xi1> {
// CHECK: spirv.IsFinite {{.*}} : vector<2xf32>
%0 = spirv.IsFinite %arg0 : vector<2xf32>
return %0 : vector<2xi1>
}

// -----

//===----------------------------------------------------------------------===//
// spirv.IsInf
//===----------------------------------------------------------------------===//
Expand Down
2 changes: 2 additions & 0 deletions mlir/test/Target/SPIRV/logical-ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader], []> {
%15 = spirv.IsNan %arg0 : f32
// CHECK: spirv.IsInf
%16 = spirv.IsInf %arg1 : f32
// CHECK: spirv.IsFinite
%17 = spirv.IsFinite %arg0 : f32
spirv.Return
}
}
Expand Down