Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 40 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,46 @@ 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;
}

// -----

def SPIRV_GLDistanceOp : SPIRV_GLOp<"Distance", 67, [
Pure,
AllTypesMatch<["p0", "p1"]>,
Expand Down
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
}
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