[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
This commit is contained in:
Alexander Belyaev 2020-08-13 18:53:07 +02:00
parent 2632c625ed
commit fed9ff5117
4 changed files with 89 additions and 0 deletions

View File

@ -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

View File

@ -5,6 +5,7 @@ add_mlir_library(MLIRTestTransforms
TestExpandTanh.cpp
TestCallGraph.cpp
TestConstantFold.cpp
TestConvertCallOp.cpp
TestConvertGPUKernelToCubin.cpp
TestConvertGPUKernelToHsaco.cpp
TestDominance.cpp

View File

@ -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

View File

@ -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();