| 1 | //===- TestPatterns.cpp - LLVM dialect test patterns ----------------------===// |
| 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 "mlir/Conversion/ArithToLLVM/ArithToLLVM.h" |
| 10 | #include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h" |
| 11 | #include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h" |
| 12 | #include "mlir/Conversion/LLVMCommon/TypeConverter.h" |
| 13 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
| 14 | #include "mlir/Dialect/LLVMIR/LLVMDialectDecl.h" |
| 15 | #include "mlir/Dialect/LLVMIR/LLVMTypes.h" |
| 16 | #include "mlir/Pass/Pass.h" |
| 17 | #include "mlir/Transforms/DialectConversion.h" |
| 18 | |
| 19 | using namespace mlir; |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | /// Replace this op (which is expected to have 1 result) with the operands. |
| 24 | struct TestDirectReplacementOp : public ConversionPattern { |
| 25 | TestDirectReplacementOp(MLIRContext *ctx, const TypeConverter &converter) |
| 26 | : ConversionPattern(converter, "test.direct_replacement" , 1, ctx) {} |
| 27 | LogicalResult |
| 28 | matchAndRewrite(Operation *op, ArrayRef<Value> operands, |
| 29 | ConversionPatternRewriter &rewriter) const final { |
| 30 | if (op->getNumResults() != 1) |
| 31 | return failure(); |
| 32 | rewriter.replaceOpWithMultiple(op, newValues: {operands}); |
| 33 | return success(); |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | struct TestLLVMLegalizePatternsPass |
| 38 | : public PassWrapper<TestLLVMLegalizePatternsPass, OperationPass<>> { |
| 39 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestLLVMLegalizePatternsPass) |
| 40 | |
| 41 | TestLLVMLegalizePatternsPass() = default; |
| 42 | TestLLVMLegalizePatternsPass(const TestLLVMLegalizePatternsPass &other) |
| 43 | : PassWrapper(other) {} |
| 44 | |
| 45 | StringRef getArgument() const final { return "test-llvm-legalize-patterns" ; } |
| 46 | StringRef getDescription() const final { |
| 47 | return "Run LLVM dialect legalization patterns" ; |
| 48 | } |
| 49 | |
| 50 | void getDependentDialects(DialectRegistry ®istry) const override { |
| 51 | registry.insert<LLVM::LLVMDialect>(); |
| 52 | } |
| 53 | |
| 54 | void runOnOperation() override { |
| 55 | MLIRContext *ctx = &getContext(); |
| 56 | |
| 57 | // Set up type converter. |
| 58 | LLVMTypeConverter converter(ctx); |
| 59 | converter.addConversion( |
| 60 | callback: [&](IntegerType type, SmallVectorImpl<Type> &result) { |
| 61 | if (type.isInteger(width: 17)) { |
| 62 | // Convert i17 -> (i18, i18). |
| 63 | result.append(NumInputs: 2, Elt: Builder(ctx).getIntegerType(width: 18)); |
| 64 | return success(); |
| 65 | } |
| 66 | |
| 67 | result.push_back(Elt: type); |
| 68 | return success(); |
| 69 | }); |
| 70 | |
| 71 | // Populate patterns. |
| 72 | mlir::RewritePatternSet patterns(ctx); |
| 73 | patterns.add<TestDirectReplacementOp>(arg&: ctx, args&: converter); |
| 74 | arith::populateArithToLLVMConversionPatterns(converter, patterns); |
| 75 | populateFuncToLLVMConversionPatterns(converter, patterns); |
| 76 | cf::populateControlFlowToLLVMConversionPatterns(converter, patterns); |
| 77 | |
| 78 | // Define the conversion target used for the test. |
| 79 | ConversionTarget target(*ctx); |
| 80 | target.addLegalOp(op: OperationName("test.legal_op" , ctx)); |
| 81 | target.addLegalDialect<LLVM::LLVMDialect>(); |
| 82 | target.addDynamicallyLegalOp<func::FuncOp>(callback: [&](func::FuncOp funcOp) { |
| 83 | return funcOp->hasDiscardableAttr(name: "is_legal" ); |
| 84 | }); |
| 85 | |
| 86 | // Handle a partial conversion. |
| 87 | DenseSet<Operation *> unlegalizedOps; |
| 88 | ConversionConfig config; |
| 89 | config.unlegalizedOps = &unlegalizedOps; |
| 90 | config.allowPatternRollback = allowPatternRollback; |
| 91 | if (failed(Result: applyPartialConversion(op: getOperation(), target, |
| 92 | patterns: std::move(patterns), config))) |
| 93 | getOperation()->emitError() << "applyPartialConversion failed" ; |
| 94 | } |
| 95 | |
| 96 | Option<bool> allowPatternRollback{*this, "allow-pattern-rollback" , |
| 97 | llvm::cl::desc("Allow pattern rollback" ), |
| 98 | llvm::cl::init(Val: true)}; |
| 99 | }; |
| 100 | } // namespace |
| 101 | |
| 102 | //===----------------------------------------------------------------------===// |
| 103 | // PassRegistration |
| 104 | //===----------------------------------------------------------------------===// |
| 105 | |
| 106 | namespace mlir { |
| 107 | namespace test { |
| 108 | void registerTestLLVMLegalizePatternsPass() { |
| 109 | PassRegistration<TestLLVMLegalizePatternsPass>(); |
| 110 | } |
| 111 | } // namespace test |
| 112 | } // namespace mlir |
| 113 | |