xref: /llvm-project/mlir/test/lib/Conversion/FuncToLLVM/TestConvertCallOp.cpp (revision e95e94adc6bb748de015ac3053e7f0786b65f351)
1 //===- TestConvertCallOp.cpp - Test LLVM Conversion of Func 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 "TestOps.h"
11 #include "TestTypes.h"
12 #include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h"
13 #include "mlir/Conversion/LLVMCommon/Pattern.h"
14 #include "mlir/Dialect/Func/IR/FuncOps.h"
15 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
16 #include "mlir/Pass/Pass.h"
17 
18 using namespace mlir;
19 
20 namespace {
21 
22 class TestTypeProducerOpConverter
23     : public ConvertOpToLLVMPattern<test::TestTypeProducerOp> {
24 public:
25   using ConvertOpToLLVMPattern<
26       test::TestTypeProducerOp>::ConvertOpToLLVMPattern;
27 
28   LogicalResult
matchAndRewrite(test::TestTypeProducerOp op,OpAdaptor adaptor,ConversionPatternRewriter & rewriter) const29   matchAndRewrite(test::TestTypeProducerOp op, OpAdaptor adaptor,
30                   ConversionPatternRewriter &rewriter) const override {
31     rewriter.replaceOpWithNewOp<LLVM::ZeroOp>(op, getVoidPtrType());
32     return success();
33   }
34 };
35 
36 struct TestConvertCallOp
37     : public PassWrapper<TestConvertCallOp, OperationPass<ModuleOp>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID__anoncc54da550111::TestConvertCallOp38   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestConvertCallOp)
39 
40   void getDependentDialects(DialectRegistry &registry) const final {
41     registry.insert<LLVM::LLVMDialect>();
42   }
getArgument__anoncc54da550111::TestConvertCallOp43   StringRef getArgument() const final { return "test-convert-call-op"; }
getDescription__anoncc54da550111::TestConvertCallOp44   StringRef getDescription() const final {
45     return "Tests conversion of `func.call` to `llvm.call` in "
46            "presence of custom types";
47   }
48 
runOnOperation__anoncc54da550111::TestConvertCallOp49   void runOnOperation() override {
50     ModuleOp m = getOperation();
51 
52     LowerToLLVMOptions options(m.getContext());
53 
54     // Populate type conversions.
55     LLVMTypeConverter typeConverter(m.getContext(), options);
56     typeConverter.addConversion([&](test::TestType type) {
57       return LLVM::LLVMPointerType::get(m.getContext());
58     });
59     typeConverter.addConversion([&](test::SimpleAType type) {
60       return IntegerType::get(type.getContext(), 42);
61     });
62 
63     // Populate patterns.
64     RewritePatternSet patterns(m.getContext());
65     populateFuncToLLVMConversionPatterns(typeConverter, patterns);
66     patterns.add<TestTypeProducerOpConverter>(typeConverter);
67 
68     // Set target.
69     ConversionTarget target(getContext());
70     target.addLegalDialect<LLVM::LLVMDialect>();
71     target.addIllegalDialect<test::TestDialect>();
72     target.addIllegalDialect<func::FuncDialect>();
73 
74     if (failed(applyPartialConversion(m, target, std::move(patterns))))
75       signalPassFailure();
76   }
77 };
78 
79 } // namespace
80 
81 namespace mlir {
82 namespace test {
registerConvertCallOpPass()83 void registerConvertCallOpPass() { PassRegistration<TestConvertCallOp>(); }
84 } // namespace test
85 } // namespace mlir
86