Skip to content

Commit fed9ff5

Browse files
committed
[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
1 parent 2632c62 commit fed9ff5

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: mlir-opt %s -test-convert-call-op | FileCheck %s
2+
3+
// CHECK-LABEL: llvm.func @callee(!llvm.ptr<i8>) -> !llvm.i32
4+
func @callee(!test.test_type) -> i32
5+
6+
// CHECK-NEXT: llvm.func @caller() -> !llvm.i32
7+
func @caller() -> i32 {
8+
%arg = "test.type_producer"() : () -> !test.test_type
9+
%out = call @callee(%arg) : (!test.test_type) -> i32
10+
return %out : i32
11+
}
12+
// CHECK-NEXT: [[ARG:%.*]] = llvm.mlir.null : !llvm.ptr<i8>
13+
// CHECK-NEXT: [[OUT:%.*]] = llvm.call @callee([[ARG]])
14+
// CHECK-SAME: : (!llvm.ptr<i8>) -> !llvm.i32

mlir/test/lib/Transforms/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_mlir_library(MLIRTestTransforms
55
TestExpandTanh.cpp
66
TestCallGraph.cpp
77
TestConstantFold.cpp
8+
TestConvertCallOp.cpp
89
TestConvertGPUKernelToCubin.cpp
910
TestConvertGPUKernelToHsaco.cpp
1011
TestDominance.cpp
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===- TestConvertCallOp.cpp - Test LLVM Convesion of Standard CallOp -----===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "TestDialect.h"
10+
#include "TestTypes.h"
11+
#include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h"
12+
#include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVMPass.h"
13+
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
14+
#include "mlir/Dialect/StandardOps/IR/Ops.h"
15+
#include "mlir/Pass/Pass.h"
16+
17+
using namespace mlir;
18+
19+
namespace {
20+
21+
class TestTypeProducerOpConverter
22+
: public ConvertOpToLLVMPattern<TestTypeProducerOp> {
23+
public:
24+
using ConvertOpToLLVMPattern<TestTypeProducerOp>::ConvertOpToLLVMPattern;
25+
26+
LogicalResult
27+
matchAndRewrite(Operation *op, ArrayRef<Value> operands,
28+
ConversionPatternRewriter &rewriter) const override {
29+
rewriter.replaceOpWithNewOp<LLVM::NullOp>(op, getVoidPtrType());
30+
return success();
31+
}
32+
};
33+
34+
class TestConvertCallOp
35+
: public PassWrapper<TestConvertCallOp, OperationPass<ModuleOp>> {
36+
public:
37+
void runOnOperation() override {
38+
ModuleOp m = getOperation();
39+
40+
// Populate type conversions.
41+
LLVMTypeConverter type_converter(m.getContext());
42+
type_converter.addConversion([&](TestType type) {
43+
return LLVM::LLVMType::getInt8PtrTy(m.getContext());
44+
});
45+
46+
// Populate patterns.
47+
OwningRewritePatternList patterns;
48+
populateStdToLLVMConversionPatterns(type_converter, patterns);
49+
patterns.insert<TestTypeProducerOpConverter>(type_converter);
50+
51+
// Set target.
52+
ConversionTarget target(getContext());
53+
target.addLegalDialect<LLVM::LLVMDialect>();
54+
target.addIllegalDialect<TestDialect>();
55+
target.addIllegalDialect<StandardOpsDialect>();
56+
57+
if (failed(applyPartialConversion(m, target, patterns))) {
58+
signalPassFailure();
59+
}
60+
}
61+
};
62+
63+
} // namespace
64+
65+
namespace mlir {
66+
void registerConvertCallOpPass() {
67+
PassRegistration<TestConvertCallOp>(
68+
"test-convert-call-op",
69+
"Tests conversion of `std.call` to `llvm.call` in "
70+
"presence of custom types");
71+
}
72+
} // namespace mlir

mlir/tools/mlir-opt/mlir-opt.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ using namespace mlir;
2929

3030
namespace mlir {
3131
// Defined in the test directory, no public header.
32+
void registerConvertCallOpPass();
3233
void registerConvertToTargetEnvPass();
3334
void registerInliner();
3435
void registerMemRefBoundCheck();
@@ -102,6 +103,7 @@ static cl::opt<bool> allowUnregisteredDialects(
102103

103104
#ifdef MLIR_INCLUDE_TESTS
104105
void registerTestPasses() {
106+
registerConvertCallOpPass();
105107
registerConvertToTargetEnvPass();
106108
registerInliner();
107109
registerMemRefBoundCheck();

0 commit comments

Comments
 (0)