[Mlir-commits] [mlir] fed9ff5 - [mlir] Test CallOp STD->LLVM conversion.

Alexander Belyaev llvmlistbot at llvm.org
Thu Aug 13 10:14:29 PDT 2020


Author: Alexander Belyaev
Date: 2020-08-13T19:10:21+02:00
New Revision: fed9ff511711762ac8cccbb9954eb4c0554fe622

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

LOG: [mlir] Test CallOp STD->LLVM conversion.

This exercises the corner case that was fixed in
https://reviews.llvm.org/rG8979a9cdf226066196f1710903d13492e6929563.

The bug can be reproduced when there is a @callee with a custom type argument and @caller has a producer of this argument passed to the @callee.

Example:
func @callee(!test.test_type) -> i32
func @caller() -> i32 {
  %arg = "test.type_producer"() : () -> !test.test_type
  %out = call @callee(%arg) : (!test.test_type) -> i32
  return %out : i32
}

Even though there is a type conversion for !test.test_type, the output IR (before the fix) contained a DialectCastOp:

module {
  llvm.func @callee(!llvm.ptr<i8>) -> !llvm.i32
  llvm.func @caller() -> !llvm.i32 {
    %0 = llvm.mlir.null : !llvm.ptr<i8>
    %1 = llvm.mlir.cast %0 : !llvm.ptr<i8> to !test.test_type
    %2 = llvm.call @callee(%1) : (!test.test_type) -> !llvm.i32
    llvm.return %2 : !llvm.i32
  }
}

instead of

module {
  llvm.func @callee(!llvm.ptr<i8>) -> !llvm.i32
  llvm.func @caller() -> !llvm.i32 {
    %0 = llvm.mlir.null : !llvm.ptr<i8>
    %1 = llvm.call @callee(%0) : (!llvm.ptr<i8>) -> !llvm.i32
    llvm.return %1 : !llvm.i32
  }
}

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

Added: 
    mlir/test/Transforms/test-convert-call-op.mlir
    mlir/test/lib/Transforms/TestConvertCallOp.cpp

Modified: 
    mlir/test/lib/Transforms/CMakeLists.txt
    mlir/tools/mlir-opt/mlir-opt.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/test/Transforms/test-convert-call-op.mlir b/mlir/test/Transforms/test-convert-call-op.mlir
new file mode 100644
index 000000000000..d1a1d5c35812
--- /dev/null
+++ b/mlir/test/Transforms/test-convert-call-op.mlir
@@ -0,0 +1,14 @@
+// RUN: mlir-opt %s -test-convert-call-op | FileCheck %s
+
+// CHECK-LABEL: llvm.func @callee(!llvm.ptr<i8>) -> !llvm.i32
+func @callee(!test.test_type) -> i32
+
+// CHECK-NEXT: llvm.func @caller() -> !llvm.i32
+func @caller() -> i32 {
+  %arg = "test.type_producer"() : () -> !test.test_type
+  %out = call @callee(%arg) : (!test.test_type) -> i32
+  return %out : i32
+}
+// CHECK-NEXT: [[ARG:%.*]] = llvm.mlir.null : !llvm.ptr<i8>
+// CHECK-NEXT: [[OUT:%.*]] = llvm.call @callee([[ARG]])
+// CHECK-SAME:     : (!llvm.ptr<i8>) -> !llvm.i32

diff  --git a/mlir/test/lib/Transforms/CMakeLists.txt b/mlir/test/lib/Transforms/CMakeLists.txt
index c3318316c508..de894467d63d 100644
--- a/mlir/test/lib/Transforms/CMakeLists.txt
+++ b/mlir/test/lib/Transforms/CMakeLists.txt
@@ -5,6 +5,7 @@ add_mlir_library(MLIRTestTransforms
   TestExpandTanh.cpp
   TestCallGraph.cpp
   TestConstantFold.cpp
+  TestConvertCallOp.cpp
   TestConvertGPUKernelToCubin.cpp
   TestConvertGPUKernelToHsaco.cpp
   TestDominance.cpp

diff  --git a/mlir/test/lib/Transforms/TestConvertCallOp.cpp b/mlir/test/lib/Transforms/TestConvertCallOp.cpp
new file mode 100644
index 000000000000..6cb596bfc71a
--- /dev/null
+++ b/mlir/test/lib/Transforms/TestConvertCallOp.cpp
@@ -0,0 +1,72 @@
+//===- TestConvertCallOp.cpp - Test LLVM Convesion of Standard CallOp -----===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "TestDialect.h"
+#include "TestTypes.h"
+#include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h"
+#include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVMPass.h"
+#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
+#include "mlir/Dialect/StandardOps/IR/Ops.h"
+#include "mlir/Pass/Pass.h"
+
+using namespace mlir;
+
+namespace {
+
+class TestTypeProducerOpConverter
+    : public ConvertOpToLLVMPattern<TestTypeProducerOp> {
+public:
+  using ConvertOpToLLVMPattern<TestTypeProducerOp>::ConvertOpToLLVMPattern;
+
+  LogicalResult
+  matchAndRewrite(Operation *op, ArrayRef<Value> operands,
+                  ConversionPatternRewriter &rewriter) const override {
+    rewriter.replaceOpWithNewOp<LLVM::NullOp>(op, getVoidPtrType());
+    return success();
+  }
+};
+
+class TestConvertCallOp
+    : public PassWrapper<TestConvertCallOp, OperationPass<ModuleOp>> {
+public:
+  void runOnOperation() override {
+    ModuleOp m = getOperation();
+
+    // Populate type conversions.
+    LLVMTypeConverter type_converter(m.getContext());
+    type_converter.addConversion([&](TestType type) {
+      return LLVM::LLVMType::getInt8PtrTy(m.getContext());
+    });
+
+    // Populate patterns.
+    OwningRewritePatternList patterns;
+    populateStdToLLVMConversionPatterns(type_converter, patterns);
+    patterns.insert<TestTypeProducerOpConverter>(type_converter);
+
+    // Set target.
+    ConversionTarget target(getContext());
+    target.addLegalDialect<LLVM::LLVMDialect>();
+    target.addIllegalDialect<TestDialect>();
+    target.addIllegalDialect<StandardOpsDialect>();
+
+    if (failed(applyPartialConversion(m, target, patterns))) {
+      signalPassFailure();
+    }
+  }
+};
+
+} // namespace
+
+namespace mlir {
+void registerConvertCallOpPass() {
+  PassRegistration<TestConvertCallOp>(
+      "test-convert-call-op",
+      "Tests conversion of `std.call` to `llvm.call` in "
+      "presence of custom types");
+}
+} // namespace mlir

diff  --git a/mlir/tools/mlir-opt/mlir-opt.cpp b/mlir/tools/mlir-opt/mlir-opt.cpp
index 3be470d4e3de..efcb32856607 100644
--- a/mlir/tools/mlir-opt/mlir-opt.cpp
+++ b/mlir/tools/mlir-opt/mlir-opt.cpp
@@ -29,6 +29,7 @@ using namespace mlir;
 
 namespace mlir {
 // Defined in the test directory, no public header.
+void registerConvertCallOpPass();
 void registerConvertToTargetEnvPass();
 void registerInliner();
 void registerMemRefBoundCheck();
@@ -102,6 +103,7 @@ static cl::opt<bool> allowUnregisteredDialects(
 
 #ifdef MLIR_INCLUDE_TESTS
 void registerTestPasses() {
+  registerConvertCallOpPass();
   registerConvertToTargetEnvPass();
   registerInliner();
   registerMemRefBoundCheck();


        


More information about the Mlir-commits mailing list