1//===- TestConvertFuncOp.cpp - Test LLVM Conversion of Func FuncOp --------===//
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
11#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h"
12#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
13#include "mlir/Conversion/LLVMCommon/Pattern.h"
14#include "mlir/Dialect/Func/IR/FuncOps.h"
15#include "mlir/Dialect/LLVMIR/LLVMModuleOps.h"
16#include "mlir/Dialect/LLVMIR/LLVMTerminatorOps.h"
17#include "mlir/IR/PatternMatch.h"
18#include "mlir/Pass/Pass.h"
19
20using namespace mlir;
21
22namespace {
23
24/// Test helper Conversion Pattern to directly call `convertFuncOpToLLVMFuncOp`
25/// to verify this utility function includes all functionalities of conversion
26struct FuncOpConversion : public ConvertOpToLLVMPattern<func::FuncOp> {
27 FuncOpConversion(const LLVMTypeConverter &converter)
28 : ConvertOpToLLVMPattern(converter) {}
29
30 LogicalResult
31 matchAndRewrite(func::FuncOp funcOp, OpAdaptor adaptor,
32 ConversionPatternRewriter &rewriter) const override {
33 FailureOr<LLVM::LLVMFuncOp> newFuncOp = mlir::convertFuncOpToLLVMFuncOp(
34 funcOp: cast<FunctionOpInterface>(Val: funcOp.getOperation()), rewriter,
35 converter: *getTypeConverter());
36 if (failed(Result: newFuncOp))
37 return rewriter.notifyMatchFailure(arg&: funcOp, msg: "Could not convert funcop");
38
39 rewriter.eraseOp(op: funcOp);
40 return success();
41 }
42};
43
44struct ReturnOpConversion : public ConvertOpToLLVMPattern<func::ReturnOp> {
45 ReturnOpConversion(const LLVMTypeConverter &converter)
46 : ConvertOpToLLVMPattern(converter) {}
47
48 LogicalResult
49 matchAndRewrite(func::ReturnOp returnOp, OpAdaptor adaptor,
50 ConversionPatternRewriter &rewriter) const override {
51 SmallVector<Type> resTys;
52 if (failed(Result: typeConverter->convertTypes(types: returnOp->getResultTypes(), results&: resTys)))
53 return failure();
54
55 rewriter.replaceOpWithNewOp<LLVM::ReturnOp>(op: returnOp, args&: resTys,
56 args: adaptor.getOperands());
57 return success();
58 }
59};
60
61static std::optional<Type>
62convertSimpleATypeToStruct(test::SimpleAType simpleTy) {
63 MLIRContext *ctx = simpleTy.getContext();
64 SmallVector<Type> memberTys(2, IntegerType::get(context: ctx, /*width=*/8));
65 return LLVM::LLVMStructType::getLiteral(context: ctx, types: memberTys);
66}
67
68struct TestConvertFuncOp
69 : public PassWrapper<TestConvertFuncOp, OperationPass<ModuleOp>> {
70 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestConvertFuncOp)
71
72 void getDependentDialects(DialectRegistry &registry) const final {
73 registry.insert<LLVM::LLVMDialect>();
74 }
75
76 StringRef getArgument() const final { return "test-convert-func-op"; }
77
78 StringRef getDescription() const final {
79 return "Tests conversion of `func.func` to `llvm.func` for different "
80 "attributes";
81 }
82
83 void runOnOperation() override {
84 MLIRContext *ctx = &getContext();
85
86 LowerToLLVMOptions options(ctx);
87 // Populate type conversions.
88 LLVMTypeConverter typeConverter(ctx, options);
89 typeConverter.addConversion(convertSimpleATypeToStruct);
90
91 RewritePatternSet patterns(ctx);
92 patterns.add<FuncOpConversion>(arg&: typeConverter);
93 patterns.add<ReturnOpConversion>(arg&: typeConverter);
94
95 LLVMConversionTarget target(getContext());
96 if (failed(Result: applyPartialConversion(op: getOperation(), target,
97 patterns: std::move(patterns))))
98 signalPassFailure();
99 }
100};
101
102} // namespace
103
104namespace mlir::test {
105void registerConvertFuncOpPass() { PassRegistration<TestConvertFuncOp>(); }
106} // namespace mlir::test
107