[Mlir-commits] [mlir] 8119a37 - [MLIR][SPIRVToLLVM] SPIR-V function call conversion pattern
Lei Zhang
llvmlistbot at llvm.org
Thu Jul 2 09:41:41 PDT 2020
Author: George Mitenkov
Date: 2020-07-02T12:38:27-04:00
New Revision: 8119a374bc3aa7ee7f135038a7c772762711d135
URL: https://github.com/llvm/llvm-project/commit/8119a374bc3aa7ee7f135038a7c772762711d135
DIFF: https://github.com/llvm/llvm-project/commit/8119a374bc3aa7ee7f135038a7c772762711d135.diff
LOG: [MLIR][SPIRVToLLVM] SPIR-V function call conversion pattern
Added conversion pattern for SPIR-V `FunctionCallOp`. Based on
specification, it returns no results or a single result, so
can be mapped directly to LLVM dialect's `llvm.call`.
Reviewed By: antiagainst, ftynse
Differential Revision: https://reviews.llvm.org/D83030
Added:
Modified:
mlir/lib/Conversion/SPIRVToLLVM/ConvertSPIRVToLLVM.cpp
mlir/test/Conversion/SPIRVToLLVM/func-to-llvm.mlir
Removed:
################################################################################
diff --git a/mlir/lib/Conversion/SPIRVToLLVM/ConvertSPIRVToLLVM.cpp b/mlir/lib/Conversion/SPIRVToLLVM/ConvertSPIRVToLLVM.cpp
index 1681fed23487..3cb8342ca287 100644
--- a/mlir/lib/Conversion/SPIRVToLLVM/ConvertSPIRVToLLVM.cpp
+++ b/mlir/lib/Conversion/SPIRVToLLVM/ConvertSPIRVToLLVM.cpp
@@ -248,6 +248,28 @@ class IndirectCastPattern : public SPIRVToLLVMConversion<SPIRVOp> {
}
};
+class FunctionCallPattern
+ : public SPIRVToLLVMConversion<spirv::FunctionCallOp> {
+public:
+ using SPIRVToLLVMConversion<spirv::FunctionCallOp>::SPIRVToLLVMConversion;
+
+ LogicalResult
+ matchAndRewrite(spirv::FunctionCallOp callOp, ArrayRef<Value> operands,
+ ConversionPatternRewriter &rewriter) const override {
+ if (callOp.getNumResults() == 0) {
+ rewriter.replaceOpWithNewOp<LLVM::CallOp>(callOp, llvm::None, operands,
+ callOp.getAttrs());
+ return success();
+ }
+
+ // Function returns a single result.
+ auto dstType = this->typeConverter.convertType(callOp.getType(0));
+ rewriter.replaceOpWithNewOp<LLVM::CallOp>(callOp, dstType, operands,
+ callOp.getAttrs());
+ return success();
+ }
+};
+
/// Converts SPIR-V floating-point comparisons to llvm.fcmp "predicate"
template <typename SPIRVOp, LLVM::FCmpPredicate predicate>
class FComparePattern : public SPIRVToLLVMConversion<SPIRVOp> {
@@ -551,6 +573,9 @@ void mlir::populateSPIRVToLLVMConversionPatterns(
IComparePattern<spirv::ULessThanEqualOp, LLVM::ICmpPredicate::ule>,
IComparePattern<spirv::ULessThanOp, LLVM::ICmpPredicate::ult>,
+ // Function Call op
+ FunctionCallPattern,
+
// Logical ops
DirectConversionPattern<spirv::LogicalAndOp, LLVM::AndOp>,
DirectConversionPattern<spirv::LogicalOrOp, LLVM::OrOp>,
diff --git a/mlir/test/Conversion/SPIRVToLLVM/func-to-llvm.mlir b/mlir/test/Conversion/SPIRVToLLVM/func-to-llvm.mlir
index 5d2c491e785e..23174f34fcec 100644
--- a/mlir/test/Conversion/SPIRVToLLVM/func-to-llvm.mlir
+++ b/mlir/test/Conversion/SPIRVToLLVM/func-to-llvm.mlir
@@ -58,5 +58,36 @@ spv.func @vector_types(%arg0: vector<2xi64>, %arg1: vector<2xi64>) -> vector<2xi
spv.ReturnValue %0 : vector<2xi64>
}
+//===----------------------------------------------------------------------===//
+// spv.FunctionCall
+//===----------------------------------------------------------------------===//
+
+// CHECK-LABEL: llvm.func @function_calls
+// CHECK-SAME: %[[ARG0:.*]]: !llvm.i32, %[[ARG1:.*]]: !llvm.i1, %[[ARG2:.*]]: !llvm.double, %[[ARG3:.*]]: !llvm<"<2 x i64>">, %[[ARG4:.*]]: !llvm<"<2 x float>">
+spv.func @function_calls(%arg0: i32, %arg1: i1, %arg2: f64, %arg3: vector<2xi64>, %arg4: vector<2xf32>) -> () "None" {
+ // CHECK: llvm.call @void_1() : () -> ()
+ spv.FunctionCall @void_1() : () -> ()
+ // CHECK: llvm.call @void_2(%[[ARG3]]) : (!llvm<"<2 x i64>">) -> ()
+ spv.FunctionCall @void_2(%arg3) : (vector<2xi64>) -> ()
+ // CHECK: %{{.*}} = llvm.call @value_scalar(%[[ARG0]], %[[ARG1]], %[[ARG2]]) : (!llvm.i32, !llvm.i1, !llvm.double) -> !llvm.i32
+ %0 = spv.FunctionCall @value_scalar(%arg0, %arg1, %arg2) : (i32, i1, f64) -> i32
+ // CHECK: %{{.*}} = llvm.call @value_vector(%[[ARG3]], %[[ARG4]]) : (!llvm<"<2 x i64>">, !llvm<"<2 x float>">) -> !llvm<"<2 x float>">
+ %1 = spv.FunctionCall @value_vector(%arg3, %arg4) : (vector<2xi64>, vector<2xf32>) -> vector<2xf32>
+ spv.Return
+}
+spv.func @void_1() -> () "None" {
+ spv.Return
+}
+spv.func @void_2(%arg0: vector<2xi64>) -> () "None" {
+ spv.Return
+}
+
+spv.func @value_scalar(%arg0: i32, %arg1: i1, %arg2: f64) -> i32 "None" {
+ spv.ReturnValue %arg0: i32
+}
+
+spv.func @value_vector(%arg0: vector<2xi64>, %arg1: vector<2xf32>) -> vector<2xf32> "None" {
+ spv.ReturnValue %arg1: vector<2xf32>
+}
More information about the Mlir-commits
mailing list