|
| 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 |
0 commit comments