Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
41 changes: 41 additions & 0 deletions mlir/include/mlir/Dialect/SPIRV/IR/SPIRVGLOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,47 @@ def SPIRV_GLFMixOp :

// -----

def SPIRV_GLLengthOp : SPIRV_GLOp<"Length", 66, [
Pure,
TypesMatchWith<"result type must match operand element type",
"operand", "result",
"::mlir::getElementTypeOrSelf($_self)">
]> {
let summary = "Return the length of a vector x";

let description = [{
Result is the length of vector x, i.e., sqrt(x[0]**2 + x[1]**2 + ...).

The operand x must be a scalar or vector whose component type is floating-point.

Result Type must be a scalar of the same type as the component type of x.

#### Example:

```mlir
%2 = spirv.GL.Length %0 : vector<3xf32> -> f32
%3 = spirv.GL.Length %1 : f32 -> f32
```
}];

let arguments = (ins
SPIRV_ScalarOrVectorOf<SPIRV_Float>:$operand
);

let results = (outs
SPIRV_Float:$result
);

let assemblyFormat = [{
$operand attr-dict `:` type($operand) `->` type($result)
}];

let hasVerifier = 0;
let hasCanonicalizer = 1;
}

// -----

def SPIRV_GLDistanceOp : SPIRV_GLOp<"Distance", 67, [
Pure,
AllTypesMatch<["p0", "p1"]>,
Expand Down
9 changes: 9 additions & 0 deletions mlir/lib/Dialect/SPIRV/IR/SPIRVCanonicalization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1403,3 +1403,12 @@ void spirv::SelectionOp::getCanonicalizationPatterns(RewritePatternSet &results,
MLIRContext *context) {
results.add<ConvertSelectionOpToSelect>(context);
}

//===----------------------------------------------------------------------===//
// spirv.GL.Length
//===----------------------------------------------------------------------===//

void spirv::GLLengthOp::getCanonicalizationPatterns(RewritePatternSet &results,
MLIRContext *context) {
results.add<ConvertGLLengthToGLFAbs>(context);
}
8 changes: 8 additions & 0 deletions mlir/lib/Dialect/SPIRV/IR/SPIRVCanonicalization.td
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,11 @@ def ConvertComparisonIntoClamp2_#CmpClampPair[0] : Pat<
)),
(CmpClampPair[1] $input, $min, $max)>;
}

//===----------------------------------------------------------------------===//
// spirv.GL.Length -> spirv.GL.FAbs
//===----------------------------------------------------------------------===//

def ConvertGLLengthToGLFAbs : Pat<
(SPIRV_GLLengthOp SPIRV_Float:$operand),
(SPIRV_GLFAbsOp $operand)>;
66 changes: 66 additions & 0 deletions mlir/test/Dialect/SPIRV/IR/gl-ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -983,3 +983,69 @@ func.func @unpack_half_2x16_scalar_out(%arg0 : i32) -> () {
%0 = spirv.GL.UnpackHalf2x16 %arg0 : i32 -> f32
return
}

// -----

//===----------------------------------------------------------------------===//
// spirv.GL.Length
//===----------------------------------------------------------------------===//

func.func @length(%arg0 : f32) -> () {
// CHECK: spirv.GL.Length {{%.*}} : f32 -> f32
%0 = spirv.GL.Length %arg0 : f32 -> f32
return
}

func.func @lengthvec(%arg0 : vector<3xf32>) -> () {
// CHECK: spirv.GL.Length {{%.*}} : vector<3xf32> -> f32
%0 = spirv.GL.Length %arg0 : vector<3xf32> -> f32
return
}

// -----

func.func @length_i32_in(%arg0 : i32) -> () {
// expected-error @+1 {{op operand #0 must be 16/32/64-bit float or vector of 16/32/64-bit float values of length 2/3/4/8/16, but got 'i32'}}
%0 = spirv.GL.Length %arg0 : i32 -> f32
return
}

// -----

func.func @length_f16_in(%arg0 : f16) -> () {
// expected-error @+1 {{op failed to verify that result type must match operand element type}}
%0 = spirv.GL.Length %arg0 : f16 -> f32
return
}

// -----

func.func @length_i32vec_in(%arg0 : vector<3xi32>) -> () {
// expected-error @+1 {{op operand #0 must be 16/32/64-bit float or vector of 16/32/64-bit float values of length 2/3/4/8/16, but got 'vector<3xi32>'}}
%0 = spirv.GL.Length %arg0 : vector<3xi32> -> f32
return
}

// -----

func.func @length_f16vec_in(%arg0 : vector<3xf16>) -> () {
// expected-error @+1 {{op failed to verify that result type must match operand element type}}
%0 = spirv.GL.Length %arg0 : vector<3xf16> -> f32
return
}

// -----

func.func @length_i32_out(%arg0 : vector<3xf32>) -> () {
// expected-error @+1 {{op result #0 must be 16/32/64-bit float, but got 'i32'}}
%0 = spirv.GL.Length %arg0 : vector<3xf32> -> i32
return
}

// -----

func.func @length_vec_out(%arg0 : vector<3xf32>) -> () {
// expected-error @+1 {{op result #0 must be 16/32/64-bit float, but got 'vector<3xf32>'}}
%0 = spirv.GL.Length %arg0 : vector<3xf32> -> vector<3xf32>
return
}
21 changes: 21 additions & 0 deletions mlir/test/Dialect/SPIRV/Transforms/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -2490,3 +2490,24 @@ func.func @cannot_canonicalize_selection_op_4(%cond: i1) -> () {
}
spirv.Return
}

// -----

//===----------------------------------------------------------------------===//
// spirv.GL.Length
//===----------------------------------------------------------------------===//

// CHECK-LABEL: @convert_length_into_fabs_scalar
func.func @convert_length_in_fabs_scalar(%arg0 : f32) -> f32 {

%0 = spirv.GL.Length %arg0 : f32 -> f32
//%11 = spirv.GL.Length %arg1 : vector<3xf32> -> f32
spirv.ReturnValue %0 : f32
}

// CHECK-LABEL: @dont_convert_length_into_fabs_vec
func.func @dont_convert_length_in_fabs_vec(%arg0 : vector<3xf32>) -> f32 {

%0 = spirv.GL.Length %arg0 : vector<3xf32> -> f32
spirv.ReturnValue %0 : f32
}
4 changes: 4 additions & 0 deletions mlir/test/Target/SPIRV/gl-ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader], []> {
%8 = spirv.GL.FindSMsb %arg3 : vector<3xi32>
// CHECK: {{%.*}} = spirv.GL.FindUMsb {{%.*}} : vector<3xi32>
%9 = spirv.GL.FindUMsb %arg3 : vector<3xi32>
// CHECK: {{%.*}} = spirv.GL.Length {{%.*}} : f32 -> f32
%10 = spirv.GL.Length %arg0 : f32 -> f32
// CHECK: {{%.*}} = spirv.GL.Length {{%.*}} : vector<3xf32> -> f32
%11 = spirv.GL.Length %arg1 : vector<3xf32> -> f32
spirv.Return
}

Expand Down
Loading