| 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 |
| 29 | 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>> { |
| 38 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestConvertCallOp) |
| 39 | |
| 40 | void getDependentDialects(DialectRegistry ®istry) const final { |
| 41 | registry.insert<LLVM::LLVMDialect>(); |
| 42 | } |
| 43 | StringRef getArgument() const final { return "test-convert-call-op" ; } |
| 44 | StringRef getDescription() const final { |
| 45 | return "Tests conversion of `func.call` to `llvm.call` in " |
| 46 | "presence of custom types" ; |
| 47 | } |
| 48 | |
| 49 | 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(callback: [&](test::TestType type) { |
| 57 | return LLVM::LLVMPointerType::get(m.getContext()); |
| 58 | }); |
| 59 | typeConverter.addConversion(callback: [&](test::SimpleAType type) { |
| 60 | return IntegerType::get(type.getContext(), 42); |
| 61 | }); |
| 62 | |
| 63 | // Populate patterns. |
| 64 | RewritePatternSet patterns(m.getContext()); |
| 65 | populateFuncToLLVMConversionPatterns(converter: typeConverter, patterns); |
| 66 | patterns.add<TestTypeProducerOpConverter>(arg&: 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 { |
| 83 | void registerConvertCallOpPass() { PassRegistration<TestConvertCallOp>(); } |
| 84 | } // namespace test |
| 85 | } // namespace mlir |
| 86 | |