112 lines · cpp
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-exception6//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/LLVMDialect.h"15#include "mlir/Dialect/LLVMIR/LLVMTypes.h"16#include "mlir/Pass/Pass.h"17#include "mlir/Transforms/DialectConversion.h"18 19using namespace mlir;20 21namespace {22 23/// Replace this op (which is expected to have 1 result) with the operands.24struct TestDirectReplacementOp : public ConversionPattern {25 TestDirectReplacementOp(MLIRContext *ctx, const TypeConverter &converter)26 : ConversionPattern(converter, "test.direct_replacement", 1, ctx) {}27 LogicalResult28 matchAndRewrite(Operation *op, ArrayRef<Value> operands,29 ConversionPatternRewriter &rewriter) const final {30 if (op->getNumResults() != 1)31 return failure();32 rewriter.replaceOpWithMultiple(op, {operands});33 return success();34 }35};36 37struct TestLLVMLegalizePatternsPass38 : 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 [&](IntegerType type, SmallVectorImpl<Type> &result) {61 if (type.isInteger(17)) {62 // Convert i17 -> (i18, i18).63 result.append(2, Builder(ctx).getIntegerType(18));64 return success();65 }66 67 result.push_back(type);68 return success();69 });70 71 // Populate patterns.72 mlir::RewritePatternSet patterns(ctx);73 patterns.add<TestDirectReplacementOp>(ctx, 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(OperationName("test.legal_op", ctx));81 target.addLegalDialect<LLVM::LLVMDialect>();82 target.addDynamicallyLegalOp<func::FuncOp>(83 [&](func::FuncOp funcOp) { return funcOp->hasAttr("is_legal"); });84 85 // Handle a partial conversion.86 DenseSet<Operation *> unlegalizedOps;87 ConversionConfig config;88 config.unlegalizedOps = &unlegalizedOps;89 config.allowPatternRollback = allowPatternRollback;90 if (failed(applyPartialConversion(getOperation(), target,91 std::move(patterns), config)))92 getOperation()->emitError() << "applyPartialConversion failed";93 }94 95 Option<bool> allowPatternRollback{*this, "allow-pattern-rollback",96 llvm::cl::desc("Allow pattern rollback"),97 llvm::cl::init(true)};98};99} // namespace100 101//===----------------------------------------------------------------------===//102// PassRegistration103//===----------------------------------------------------------------------===//104 105namespace mlir {106namespace test {107void registerTestLLVMLegalizePatternsPass() {108 PassRegistration<TestLLVMLegalizePatternsPass>();109}110} // namespace test111} // namespace mlir112