Skip to content

Commit cd73f27

Browse files
committed
add spirv.isfinite and lower some fpclassification ops from math to spirv.
1 parent 3ede2de commit cd73f27

File tree

6 files changed

+79
-4
lines changed

6 files changed

+79
-4
lines changed

mlir/include/mlir/Dialect/SPIRV/IR/SPIRVBase.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4448,6 +4448,7 @@ def SPIRV_OC_OpUMulExtended : I32EnumAttrCase<"OpUMulExtended"
44484448
def SPIRV_OC_OpSMulExtended : I32EnumAttrCase<"OpSMulExtended", 152>;
44494449
def SPIRV_OC_OpIsNan : I32EnumAttrCase<"OpIsNan", 156>;
44504450
def SPIRV_OC_OpIsInf : I32EnumAttrCase<"OpIsInf", 157>;
4451+
def SPIRV_OC_OpIsFinite : I32EnumAttrCase<"OpIsFinite", 158>;
44514452
def SPIRV_OC_OpOrdered : I32EnumAttrCase<"OpOrdered", 162>;
44524453
def SPIRV_OC_OpUnordered : I32EnumAttrCase<"OpUnordered", 163>;
44534454
def SPIRV_OC_OpLogicalEqual : I32EnumAttrCase<"OpLogicalEqual", 164>;
@@ -4630,7 +4631,8 @@ def SPIRV_OpcodeAttr :
46304631
SPIRV_OC_OpVectorTimesMatrix, SPIRV_OC_OpMatrixTimesVector,
46314632
SPIRV_OC_OpMatrixTimesMatrix, SPIRV_OC_OpDot, SPIRV_OC_OpIAddCarry,
46324633
SPIRV_OC_OpISubBorrow, SPIRV_OC_OpUMulExtended, SPIRV_OC_OpSMulExtended,
4633-
SPIRV_OC_OpIsNan, SPIRV_OC_OpIsInf, SPIRV_OC_OpOrdered, SPIRV_OC_OpUnordered,
4634+
SPIRV_OC_OpIsNan, SPIRV_OC_OpIsInf, SPIRV_OC_OpIsFinite,
4635+
SPIRV_OC_OpOrdered, SPIRV_OC_OpUnordered,
46344636
SPIRV_OC_OpLogicalEqual, SPIRV_OC_OpLogicalNotEqual, SPIRV_OC_OpLogicalOr,
46354637
SPIRV_OC_OpLogicalAnd, SPIRV_OC_OpLogicalNot, SPIRV_OC_OpSelect,
46364638
SPIRV_OC_OpIEqual, SPIRV_OC_OpINotEqual, SPIRV_OC_OpUGreaterThan,

mlir/include/mlir/Dialect/SPIRV/IR/SPIRVLogicalOps.td

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,28 @@ def SPIRV_INotEqualOp : SPIRV_LogicalBinaryOp<"INotEqual",
403403

404404
// -----
405405

406+
def SPIRV_IsFiniteOp : SPIRV_LogicalUnaryOp<"IsFinite", SPIRV_Float, []> {
407+
let summary = "Result is true if x is an IEEE Finite, otherwise result is false";
408+
409+
let description = [{
410+
Result Type must be a scalar or vector of Boolean type.
411+
412+
x must be a scalar or vector of floating-point type. It must have the
413+
same number of components as Result Type.
414+
415+
Results are computed per component.
416+
417+
#### Example:
418+
419+
```mlir
420+
%2 = spirv.IsFinite %0: f32
421+
%3 = spirv.IsFinite %1: vector<4xf32>
422+
```
423+
}];
424+
}
425+
426+
// -----
427+
406428
def SPIRV_IsInfOp : SPIRV_LogicalUnaryOp<"IsInf", SPIRV_Float, []> {
407429
let summary = "Result is true if x is an IEEE Inf, otherwise result is false";
408430

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

419441
```mlir
420442
%2 = spirv.IsInf %0: f32
421-
%3 = spirv.IsInf %1: vector<4xi32>
443+
%3 = spirv.IsInf %1: vector<4xf32>
422444
```
423445
}];
424446
}
@@ -442,7 +464,7 @@ def SPIRV_IsNanOp : SPIRV_LogicalUnaryOp<"IsNan", SPIRV_Float, []> {
442464

443465
```mlir
444466
%2 = spirv.IsNan %0: f32
445-
%3 = spirv.IsNan %1: vector<4xi32>
467+
%3 = spirv.IsNan %1: vector<4xf32>
446468
```
447469
}];
448470
}

mlir/lib/Conversion/MathToSPIRV/MathToSPIRV.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,11 @@ namespace mlir {
488488
void populateMathToSPIRVPatterns(const SPIRVTypeConverter &typeConverter,
489489
RewritePatternSet &patterns) {
490490
// Core patterns
491-
patterns.add<CopySignPattern>(typeConverter, patterns.getContext());
491+
patterns.add<CopySignPattern,
492+
CheckedElementwiseOpPattern<math::IsInfOp, spirv::IsInfOp>,
493+
CheckedElementwiseOpPattern<math::IsNaNOp, spirv::IsNanOp>,
494+
CheckedElementwiseOpPattern<math::IsFiniteOp, spirv::IsFiniteOp>>(
495+
typeConverter, patterns.getContext());
492496

493497
// GLSL patterns
494498
patterns
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: mlir-opt -split-input-file -convert-math-to-spirv -verify-diagnostics %s -o - | FileCheck %s
2+
3+
module attributes {
4+
spirv.target_env = #spirv.target_env<#spirv.vce<v1.0, [Shader], []>, #spirv.resource_limits<>>
5+
} {
6+
7+
// CHECK-LABEL: @fpclassify
8+
func.func @fpclassify(%x: f32, %v: vector<4xf32>) {
9+
// CHECK: spirv.IsFinite %{{.*}} : f32
10+
%0 = math.isfinite %x : f32
11+
// CHECK: spirv.IsFinite %{{.*}} : vector<4xf32>
12+
%1 = math.isfinite %v : vector<4xf32>
13+
14+
// CHECK: spirv.IsNan %{{.*}} : f32
15+
%2 = math.isnan %x : f32
16+
// CHECK: spirv.IsNan %{{.*}} : vector<4xf32>
17+
%3 = math.isnan %v : vector<4xf32>
18+
19+
// CHECK: spirv.IsInf %{{.*}} : f32
20+
%4 = math.isinf %x : f32
21+
// CHECK: spirv.IsInf %{{.*}} : vector<4xf32>
22+
%5 = math.isinf %v : vector<4xf32>
23+
24+
return
25+
}
26+
27+
}

mlir/test/Dialect/SPIRV/IR/logical-ops.mlir

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ func.func @inotequal_vector(%arg0: vector<4xi32>, %arg1: vector<4xi32>) -> vecto
3232

3333
// -----
3434

35+
//===----------------------------------------------------------------------===//
36+
// spirv.IsFinite
37+
//===----------------------------------------------------------------------===//
38+
39+
func.func @isfinite_scalar(%arg0: f32) -> i1 {
40+
// CHECK: spirv.IsFinite {{.*}} : f32
41+
%0 = spirv.IsFinite %arg0 : f32
42+
return %0 : i1
43+
}
44+
45+
func.func @isfinite_vector(%arg0: vector<2xf32>) -> vector<2xi1> {
46+
// CHECK: spirv.IsFinite {{.*}} : vector<2xf32>
47+
%0 = spirv.IsFinite %arg0 : vector<2xf32>
48+
return %0 : vector<2xi1>
49+
}
50+
51+
// -----
52+
3553
//===----------------------------------------------------------------------===//
3654
// spirv.IsInf
3755
//===----------------------------------------------------------------------===//

mlir/test/Target/SPIRV/logical-ops.mlir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader], []> {
8484
%15 = spirv.IsNan %arg0 : f32
8585
// CHECK: spirv.IsInf
8686
%16 = spirv.IsInf %arg1 : f32
87+
// CHECK: spirv.IsFinite
88+
%17 = spirv.IsFinite %arg0 : f32
8789
spirv.Return
8890
}
8991
}

0 commit comments

Comments
 (0)