[Mlir-commits] [mlir] cfa9ae9 - [mlir][SPIRV] Add lowering for math.log1p operation to SPIR-V dialect.

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Jun 3 16:27:45 PDT 2021


Author: MaheshRavishankar
Date: 2021-06-03T16:27:19-07:00
New Revision: cfa9ae99408fceb063473d9339b7006075c686a9

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

LOG: [mlir][SPIRV] Add lowering for math.log1p operation to SPIR-V dialect.

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

Added: 
    

Modified: 
    mlir/lib/Conversion/StandardToSPIRV/StandardToSPIRV.cpp
    mlir/test/Conversion/StandardToSPIRV/std-ops-to-spirv.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Conversion/StandardToSPIRV/StandardToSPIRV.cpp b/mlir/lib/Conversion/StandardToSPIRV/StandardToSPIRV.cpp
index 3851bacc1f84..9a43214e1f2f 100644
--- a/mlir/lib/Conversion/StandardToSPIRV/StandardToSPIRV.cpp
+++ b/mlir/lib/Conversion/StandardToSPIRV/StandardToSPIRV.cpp
@@ -317,6 +317,28 @@ class UnaryAndBinaryOpPattern final : public OpConversionPattern<StdOp> {
   }
 };
 
+/// Converts math.log1p to SPIR-V ops.
+///
+/// SPIR-V does not have a direct operations for log(1+x). Explicitly lower to
+/// these operations.
+class Log1pOpPattern final : public OpConversionPattern<math::Log1pOp> {
+public:
+  using OpConversionPattern<math::Log1pOp>::OpConversionPattern;
+
+  LogicalResult
+  matchAndRewrite(math::Log1pOp operation, ArrayRef<Value> operands,
+                  ConversionPatternRewriter &rewriter) const override {
+    assert(operands.size() == 1);
+    Location loc = operation.getLoc();
+    auto type =
+        this->getTypeConverter()->convertType(operation.operand().getType());
+    auto one = spirv::ConstantOp::getOne(type, operation.getLoc(), rewriter);
+    auto onePlus = rewriter.create<spirv::FAddOp>(loc, one, operands[0]);
+    rewriter.replaceOpWithNewOp<spirv::GLSLLogOp>(operation, type, onePlus);
+    return success();
+  }
+};
+
 /// Converts std.remi_signed to SPIR-V ops.
 ///
 /// This cannot be merged into the template unary/binary pattern due to
@@ -1347,7 +1369,7 @@ void populateStandardToSPIRVPatterns(SPIRVTypeConverter &typeConverter,
       UnaryAndBinaryOpPattern<UnsignedDivIOp, spirv::UDivOp>,
       UnaryAndBinaryOpPattern<UnsignedRemIOp, spirv::UModOp>,
       UnaryAndBinaryOpPattern<UnsignedShiftRightOp, spirv::ShiftRightLogicalOp>,
-      SignedRemIOpPattern, XOrOpPattern, BoolXOrOpPattern,
+      Log1pOpPattern, SignedRemIOpPattern, XOrOpPattern, BoolXOrOpPattern,
 
       // Comparison patterns
       BoolCmpIOpPattern, CmpFOpPattern, CmpFOpNanNonePattern, CmpIOpPattern,

diff  --git a/mlir/test/Conversion/StandardToSPIRV/std-ops-to-spirv.mlir b/mlir/test/Conversion/StandardToSPIRV/std-ops-to-spirv.mlir
index c9ce74b92c0b..e20062d00557 100644
--- a/mlir/test/Conversion/StandardToSPIRV/std-ops-to-spirv.mlir
+++ b/mlir/test/Conversion/StandardToSPIRV/std-ops-to-spirv.mlir
@@ -53,6 +53,10 @@ func @float32_unary_scalar(%arg0: f32) {
   %3 = math.exp %arg0 : f32
   // CHECK: spv.GLSL.Log %{{.*}}: f32
   %4 = math.log %arg0 : f32
+  // CHECK: %[[ONE:.+]] = spv.Constant 1.000000e+00 : f32
+  // CHECK: %[[ADDONE:.+]] = spv.FAdd %[[ONE]], %{{.+}}
+  // CHECK: spv.GLSL.Log %[[ADDONE]]
+  %40 = math.log1p %arg0 : f32
   // CHECK: spv.FNegate %{{.*}}: f32
   %5 = negf %arg0 : f32
   // CHECK: spv.GLSL.InverseSqrt %{{.*}}: f32


        


More information about the Mlir-commits mailing list