Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mechanism to specify extended instruction sets in SPIR-V.
Add support for specifying extended instructions sets. The operations in SPIR-V dialect are named as 'spv.<extension-name>.<op-name>'. Use this mechanism to define a 'Exp' operation from GLSL(450) instructions. Later CLs will add support for (de)serialization of these operations, and update the dialect generation scripts to auto-generate the specification using the spec directly. Additional changes: Add a Type Constraint to OpBase.td to check for vector of specified lengths. This is used to check that the vector type used in SPIR-V dialect are of lengths 2, 3 or 4. Update SPIRVBase.td to use this Type constraints for vectors. PiperOrigin-RevId: 269234377
- Loading branch information
1 parent
faaa1ce
commit 9814b3f
Showing
10 changed files
with
302 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//===- SPIRVGLSLOps.h - MLIR SPIR-V extended ops for GLSL --------*- C++-*-===// | ||
// | ||
// Copyright 2019 The MLIR Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ============================================================================= | ||
// | ||
// This file declares the extended operations for GLSL in the SPIR-V dialect. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef MLIR_DIALECT_SPIRV_SPIRVGLSLOPS_H_ | ||
#define MLIR_DIALECT_SPIRV_SPIRVGLSLOPS_H_ | ||
|
||
#include "mlir/Dialect/SPIRV/SPIRVTypes.h" | ||
#include "mlir/IR/OpDefinition.h" | ||
|
||
namespace mlir { | ||
namespace spirv { | ||
|
||
#define GET_OP_CLASSES | ||
#include "mlir/Dialect/SPIRV/SPIRVGLSLOps.h.inc" | ||
|
||
} // namespace spirv | ||
} // namespace mlir | ||
|
||
#endif // MLIR_DIALECT_SPIRV_SPIRVGLSLOPS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
//===- SPIRVGLSLOps.td - GLSL extended insts spec file -----*- tablegen -*-===// | ||
// | ||
// Copyright 2019 The MLIR Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ============================================================================= | ||
// | ||
// This is the op definition spec of GLSL extension ops. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifdef SPIRV_GLSL_OPS | ||
#else | ||
#define SPIRV_GLSL_OPS | ||
|
||
#ifdef SPIRV_BASE | ||
#else | ||
include "mlir/Dialect/SPIRV/SPIRVBase.td" | ||
#endif // SPIRV_BASE | ||
|
||
//===----------------------------------------------------------------------===// | ||
// SPIR-V GLSL 4.50 opcode specification. | ||
//===----------------------------------------------------------------------===// | ||
|
||
// Base class for all GLSL ops. | ||
class SPV_GLSLOp<string mnemonic, int opcode, list<OpTrait> traits = []> : | ||
SPV_Op<"glsl." # mnemonic, traits> { | ||
|
||
// Do not use the default auto-generation serializer/deserializer. | ||
let hasOpcode = 0; | ||
|
||
// Opcode within the extended instruction set. | ||
int glslOpcode = opcode; | ||
|
||
// Name used to refer to the extended instruction set. | ||
string extensionSetName = "GLSL.std.450"; | ||
} | ||
|
||
// Base class for GLSL unary ops. | ||
class SPV_GLSLUnaryOp<string mnemonic, Type resultType, Type operandType, | ||
int opcode, list<OpTrait> traits = []> : | ||
SPV_GLSLOp<mnemonic, opcode, traits> { | ||
|
||
let arguments = (ins | ||
SPV_ScalarOrVectorOf<operandType>:$operand | ||
); | ||
|
||
let results = (outs | ||
SPV_ScalarOrVectorOf<resultType>:$result | ||
); | ||
|
||
let parser = [{ return parseGLSLUnaryOp(parser, result); }]; | ||
|
||
let printer = [{ return printGLSLUnaryOp(getOperation(), p); }]; | ||
|
||
let verifier = [{ return success(); }]; | ||
} | ||
|
||
// Base class for GLSL Unary arithmatic ops where return type matches | ||
// the operand type. | ||
class SPV_GLSLUnaryArithmaticOp<string mnemonic, int opcode, Type type, | ||
list<OpTrait> traits = []> : | ||
SPV_GLSLUnaryOp<mnemonic, type, type, opcode, traits>; | ||
|
||
// ----- | ||
|
||
def SPV_GLSLExpOp : SPV_GLSLUnaryArithmaticOp<"Exp", 27, FloatOfWidths<[16, 32]>> { | ||
let summary = "Exponentiation of Operand 1"; | ||
|
||
let description = [{ | ||
Result is the natural exponentiation of x; e^x. | ||
|
||
The operand x must be a scalar or vector whose component type is | ||
16-bit or 32-bit floating-point. | ||
|
||
Result Type and the type of x must be the same type. Results are | ||
computed per component."; | ||
|
||
### Custom assembly format | ||
``` {.ebnf} | ||
restricted-float-scalar-type ::= `f16` | `f32` | ||
restricted-float-scalar-vector-type ::= | ||
restricted-float-scalar-type | | ||
`vector<` integer-literal `x` restricted-float-scalar-type `>` | ||
exp-op ::= ssa-id `=` `spv.glsl.Exp` ssa-use `:` | ||
restricted-float-scalar-vector-type | ||
``` | ||
For example: | ||
|
||
``` | ||
%2 = spv.glsl.Exp %0 : f32 | ||
%3 = spv.glsl.Exp %1 : vector<3xf16> | ||
``` | ||
}]; | ||
} | ||
|
||
#endif // SPIRV_GLSL_OPS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//===- SPIRVGLSLOps.cpp - MLIR SPIR-V GLSL extended operations ------------===// | ||
// | ||
// Copyright 2019 The MLIR Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ============================================================================= | ||
// | ||
// This file defines the operations in the SPIR-V extended instructions set for | ||
// GLSL | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Dialect/SPIRV/SPIRVGLSLOps.h" | ||
#include "mlir/Dialect/SPIRV/SPIRVDialect.h" | ||
#include "mlir/Dialect/SPIRV/SPIRVTypes.h" | ||
#include "mlir/IR/OpImplementation.h" | ||
|
||
using namespace mlir; | ||
|
||
//===----------------------------------------------------------------------===// | ||
// spv.glsl.UnaryOp | ||
//===----------------------------------------------------------------------===// | ||
|
||
static ParseResult parseGLSLUnaryOp(OpAsmParser *parser, | ||
OperationState *state) { | ||
OpAsmParser::OperandType operandInfo; | ||
Type type; | ||
if (parser->parseOperand(operandInfo) || parser->parseColonType(type) || | ||
parser->resolveOperands(operandInfo, type, state->operands)) { | ||
return failure(); | ||
} | ||
state->addTypes(type); | ||
return success(); | ||
} | ||
|
||
static void printGLSLUnaryOp(Operation *unaryOp, OpAsmPrinter *printer) { | ||
*printer << unaryOp->getName() << ' ' << *unaryOp->getOperand(0) << " : " | ||
<< unaryOp->getOperand(0)->getType(); | ||
} | ||
|
||
namespace mlir { | ||
namespace spirv { | ||
|
||
#define GET_OP_CLASSES | ||
#include "mlir/Dialect/SPIRV/SPIRVGLSLOps.cpp.inc" | ||
|
||
} // namespace spirv | ||
} // namespace mlir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// RUN: mlir-opt -split-input-file -verify-diagnostics %s | FileCheck %s | ||
|
||
//===----------------------------------------------------------------------===// | ||
// spv.glsl.Exp | ||
//===----------------------------------------------------------------------===// | ||
|
||
func @exp(%arg0 : f32) -> () { | ||
// CHECK: spv.glsl.Exp {{%.*}} : f32 | ||
%2 = spv.glsl.Exp %arg0 : f32 | ||
return | ||
} | ||
|
||
func @expvec(%arg0 : vector<3xf16>) -> () { | ||
// CHECK: spv.glsl.Exp {{%.*}} : vector<3xf16> | ||
%2 = spv.glsl.Exp %arg0 : vector<3xf16> | ||
return | ||
} | ||
|
||
// ----- | ||
|
||
func @exp(%arg0 : i32) -> () { | ||
// expected-error @+1 {{op operand #0 must be 16/32-bit float or vector of 16/32-bit float values}} | ||
%2 = spv.glsl.Exp %arg0 : i32 | ||
return | ||
} | ||
|
||
// ----- | ||
|
||
func @exp(%arg0 : vector<5xf32>) -> () { | ||
// expected-error @+1 {{op operand #0 must be 16/32-bit float or vector of 16/32-bit float values of length 2/3/4}} | ||
%2 = spv.glsl.Exp %arg0 : vector<5xf32> | ||
return | ||
} | ||
|
||
// ----- | ||
|
||
func @exp(%arg0 : f32, %arg1 : f32) -> () { | ||
// expected-error @+1 {{expected ':'}} | ||
%2 = spv.glsl.Exp %arg0, %arg1 : i32 | ||
return | ||
} | ||
|
||
// ----- | ||
|
||
func @exp(%arg0 : i32) -> () { | ||
// expected-error @+2 {{expected non-function type}} | ||
%2 = spv.glsl.Exp %arg0 : | ||
return | ||
} |