[Mlir-commits] [mlir] 647e9a5 - [MLIR][SPIRVToLLVM] Conversion patterns for GLSL ops

George Mitenkov llvmlistbot at llvm.org
Thu Jul 30 00:20:45 PDT 2020


Author: George Mitenkov
Date: 2020-07-30T10:20:11+03:00
New Revision: 647e9a54c758a6fdd85a569f019f00a653b2bc40

URL: https://github.com/llvm/llvm-project/commit/647e9a54c758a6fdd85a569f019f00a653b2bc40
DIFF: https://github.com/llvm/llvm-project/commit/647e9a54c758a6fdd85a569f019f00a653b2bc40.diff

LOG: [MLIR][SPIRVToLLVM] Conversion patterns for GLSL ops

This is the first patch that adds support for GLSL extended
instruction set ops. These are direct conversions, apart from `spv.Tan`
that is lowered to `sin() / cos()`.

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D84627

Added: 
    mlir/test/Conversion/SPIRVToLLVM/glsl-ops-to-llvm.mlir

Modified: 
    mlir/lib/Conversion/SPIRVToLLVM/ConvertSPIRVToLLVM.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Conversion/SPIRVToLLVM/ConvertSPIRVToLLVM.cpp b/mlir/lib/Conversion/SPIRVToLLVM/ConvertSPIRVToLLVM.cpp
index fd0e96b79d2b..803b05a032da 100644
--- a/mlir/lib/Conversion/SPIRVToLLVM/ConvertSPIRVToLLVM.cpp
+++ b/mlir/lib/Conversion/SPIRVToLLVM/ConvertSPIRVToLLVM.cpp
@@ -802,6 +802,25 @@ class ShiftPattern : public SPIRVToLLVMConversion<SPIRVOp> {
   }
 };
 
+class TanPattern : public SPIRVToLLVMConversion<spirv::GLSLTanOp> {
+public:
+  using SPIRVToLLVMConversion<spirv::GLSLTanOp>::SPIRVToLLVMConversion;
+
+  LogicalResult
+  matchAndRewrite(spirv::GLSLTanOp tanOp, ArrayRef<Value> operands,
+                  ConversionPatternRewriter &rewriter) const override {
+    auto dstType = typeConverter.convertType(tanOp.getType());
+    if (!dstType)
+      return failure();
+
+    Location loc = tanOp.getLoc();
+    Value sin = rewriter.create<LLVM::SinOp>(loc, dstType, tanOp.operand());
+    Value cos = rewriter.create<LLVM::CosOp>(loc, dstType, tanOp.operand());
+    rewriter.replaceOpWithNewOp<LLVM::FDivOp>(tanOp, dstType, sin, cos);
+    return success();
+  }
+};
+
 class VariablePattern : public SPIRVToLLVMConversion<spirv::VariableOp> {
 public:
   using SPIRVToLLVMConversion<spirv::VariableOp>::SPIRVToLLVMConversion;
@@ -1026,6 +1045,15 @@ void mlir::populateSPIRVToLLVMConversionPatterns(
       // Function Call op
       FunctionCallPattern,
 
+      // GLSL extended instruction set ops
+      DirectConversionPattern<spirv::GLSLCeilOp, LLVM::FCeilOp>,
+      DirectConversionPattern<spirv::GLSLCosOp, LLVM::CosOp>,
+      DirectConversionPattern<spirv::GLSLExpOp, LLVM::ExpOp>,
+      DirectConversionPattern<spirv::GLSLFAbsOp, LLVM::FAbsOp>,
+      DirectConversionPattern<spirv::GLSLLogOp, LLVM::LogOp>,
+      DirectConversionPattern<spirv::GLSLSinOp, LLVM::SinOp>,
+      DirectConversionPattern<spirv::GLSLSqrtOp, LLVM::SqrtOp>, TanPattern,
+
       // Logical ops
       DirectConversionPattern<spirv::LogicalAndOp, LLVM::AndOp>,
       DirectConversionPattern<spirv::LogicalOrOp, LLVM::OrOp>,

diff  --git a/mlir/test/Conversion/SPIRVToLLVM/glsl-ops-to-llvm.mlir b/mlir/test/Conversion/SPIRVToLLVM/glsl-ops-to-llvm.mlir
new file mode 100644
index 000000000000..1907619445c9
--- /dev/null
+++ b/mlir/test/Conversion/SPIRVToLLVM/glsl-ops-to-llvm.mlir
@@ -0,0 +1,105 @@
+// RUN: mlir-opt -convert-spirv-to-llvm %s | FileCheck %s
+
+//===----------------------------------------------------------------------===//
+// spv.GLSL.Ceil
+//===----------------------------------------------------------------------===//
+
+// CHECK-LABEL: @ceil
+func @ceil(%arg0: f32, %arg1: vector<3xf16>) {
+	// CHECK: "llvm.intr.ceil"(%{{.*}}) : (!llvm.float) -> !llvm.float
+	%0 = spv.GLSL.Ceil %arg0 : f32
+	// CHECK: "llvm.intr.ceil"(%{{.*}}) : (!llvm<"<3 x half>">) -> !llvm<"<3 x half>">
+	%1 = spv.GLSL.Ceil %arg1 : vector<3xf16>
+	return
+}
+
+//===----------------------------------------------------------------------===//
+// spv.GLSL.Cos
+//===----------------------------------------------------------------------===//
+
+// CHECK-LABEL: @cos
+func @cos(%arg0: f32, %arg1: vector<3xf16>) {
+	// CHECK: "llvm.intr.cos"(%{{.*}}) : (!llvm.float) -> !llvm.float
+	%0 = spv.GLSL.Cos %arg0 : f32
+	// CHECK: "llvm.intr.cos"(%{{.*}}) : (!llvm<"<3 x half>">) -> !llvm<"<3 x half>">
+	%1 = spv.GLSL.Cos %arg1 : vector<3xf16>
+	return
+}
+
+//===----------------------------------------------------------------------===//
+// spv.GLSL.Exp
+//===----------------------------------------------------------------------===//
+
+// CHECK-LABEL: @exp
+func @exp(%arg0: f32, %arg1: vector<3xf16>) {
+	// CHECK: "llvm.intr.exp"(%{{.*}}) : (!llvm.float) -> !llvm.float
+	%0 = spv.GLSL.Exp %arg0 : f32
+	// CHECK: "llvm.intr.exp"(%{{.*}}) : (!llvm<"<3 x half>">) -> !llvm<"<3 x half>">
+	%1 = spv.GLSL.Exp %arg1 : vector<3xf16>
+	return
+}
+
+//===----------------------------------------------------------------------===//
+// spv.GLSL.FAbs
+//===----------------------------------------------------------------------===//
+
+// CHECK-LABEL: @fabs
+func @fabs(%arg0: f32, %arg1: vector<3xf16>) {
+	// CHECK: "llvm.intr.fabs"(%{{.*}}) : (!llvm.float) -> !llvm.float
+	%0 = spv.GLSL.FAbs %arg0 : f32
+	// CHECK: "llvm.intr.fabs"(%{{.*}}) : (!llvm<"<3 x half>">) -> !llvm<"<3 x half>">
+	%1 = spv.GLSL.FAbs %arg1 : vector<3xf16>
+	return
+}
+
+//===----------------------------------------------------------------------===//
+// spv.GLSL.Log
+//===----------------------------------------------------------------------===//
+
+// CHECK-LABEL: @log
+func @log(%arg0: f32, %arg1: vector<3xf16>) {
+	// CHECK: "llvm.intr.log"(%{{.*}}) : (!llvm.float) -> !llvm.float
+	%0 = spv.GLSL.Log %arg0 : f32
+	// CHECK: "llvm.intr.log"(%{{.*}}) : (!llvm<"<3 x half>">) -> !llvm<"<3 x half>">
+	%1 = spv.GLSL.Log %arg1 : vector<3xf16>
+	return
+}
+
+//===----------------------------------------------------------------------===//
+// spv.GLSL.Sin
+//===----------------------------------------------------------------------===//
+
+// CHECK-LABEL: @sin
+func @sin(%arg0: f32, %arg1: vector<3xf16>) {
+	// CHECK: "llvm.intr.sin"(%{{.*}}) : (!llvm.float) -> !llvm.float
+	%0 = spv.GLSL.Sin %arg0 : f32
+	// CHECK: "llvm.intr.sin"(%{{.*}}) : (!llvm<"<3 x half>">) -> !llvm<"<3 x half>">
+	%1 = spv.GLSL.Sin %arg1 : vector<3xf16>
+	return
+}
+
+//===----------------------------------------------------------------------===//
+// spv.GLSL.Sqrt
+//===----------------------------------------------------------------------===//
+
+// CHECK-LABEL: @sqrt
+func @sqrt(%arg0: f32, %arg1: vector<3xf16>) {
+	// CHECK: "llvm.intr.sqrt"(%{{.*}}) : (!llvm.float) -> !llvm.float
+	%0 = spv.GLSL.Sqrt %arg0 : f32
+	// CHECK: "llvm.intr.sqrt"(%{{.*}}) : (!llvm<"<3 x half>">) -> !llvm<"<3 x half>">
+	%1 = spv.GLSL.Sqrt %arg1 : vector<3xf16>
+	return
+}
+
+//===----------------------------------------------------------------------===//
+// spv.GLSL.Tan
+//===----------------------------------------------------------------------===//
+
+// CHECK-LABEL: @tan
+func @tan(%arg0: f32) {
+	// CHECK: %[[SIN:.*]] = "llvm.intr.sin"(%{{.*}}) : (!llvm.float) -> !llvm.float
+  // CHECK: %[[COS:.*]] = "llvm.intr.cos"(%{{.*}}) : (!llvm.float) -> !llvm.float
+	// CHECK: llvm.fdiv %[[SIN]], %[[COS]] : !llvm.float
+	%0 = spv.GLSL.Tan %arg0 : f32
+	return
+}


        


More information about the Mlir-commits mailing list