xref: /llvm-project/mlir/test/lib/IR/TestSlicing.cpp (revision c114dba3f3e1ccd342bf249b08c94caceb7b776b)
1 //===- TestSlicing.cpp - Testing slice functionality ----------------------===//
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 // This file implements a simple testing pass for slicing.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Analysis/SliceAnalysis.h"
14 #include "mlir/Dialect/Func/IR/FuncOps.h"
15 #include "mlir/Dialect/Linalg/IR/Linalg.h"
16 #include "mlir/IR/BuiltinOps.h"
17 #include "mlir/IR/IRMapping.h"
18 #include "mlir/IR/PatternMatch.h"
19 #include "mlir/Pass/Pass.h"
20 #include "mlir/Support/LLVM.h"
21 
22 using namespace mlir;
23 
24 /// Create a function with the same signature as the parent function of `op`
25 /// with name being the function name and a `suffix`.
26 static LogicalResult createBackwardSliceFunction(Operation *op,
27                                                  StringRef suffix) {
28   func::FuncOp parentFuncOp = op->getParentOfType<func::FuncOp>();
29   OpBuilder builder(parentFuncOp);
30   Location loc = op->getLoc();
31   std::string clonedFuncOpName = parentFuncOp.getName().str() + suffix.str();
32   func::FuncOp clonedFuncOp = builder.create<func::FuncOp>(
33       loc, clonedFuncOpName, parentFuncOp.getFunctionType());
34   IRMapping mapper;
35   builder.setInsertionPointToEnd(clonedFuncOp.addEntryBlock());
36   for (const auto &arg : enumerate(parentFuncOp.getArguments()))
37     mapper.map(arg.value(), clonedFuncOp.getArgument(arg.index()));
38   SetVector<Operation *> slice;
39   getBackwardSlice(op, &slice);
40   for (Operation *slicedOp : slice)
41     builder.clone(*slicedOp, mapper);
42   builder.create<func::ReturnOp>(loc);
43   return success();
44 }
45 
46 namespace {
47 /// Pass to test slice generated from slice analysis.
48 struct SliceAnalysisTestPass
49     : public PassWrapper<SliceAnalysisTestPass, OperationPass<ModuleOp>> {
50   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(SliceAnalysisTestPass)
51 
52   StringRef getArgument() const final { return "slice-analysis-test"; }
53   StringRef getDescription() const final {
54     return "Test Slice analysis functionality.";
55   }
56   void runOnOperation() override;
57   SliceAnalysisTestPass() = default;
58   SliceAnalysisTestPass(const SliceAnalysisTestPass &) {}
59 };
60 } // namespace
61 
62 void SliceAnalysisTestPass::runOnOperation() {
63   ModuleOp module = getOperation();
64   auto funcOps = module.getOps<func::FuncOp>();
65   unsigned opNum = 0;
66   for (auto funcOp : funcOps) {
67     if (!llvm::hasSingleElement(funcOp.getBody())) {
68       funcOp->emitOpError("Does not support functions with multiple blocks");
69       signalPassFailure();
70       return;
71     }
72     // TODO: For now this is just looking for Linalg ops. It can be generalized
73     // to look for other ops using flags.
74     funcOp.walk([&](Operation *op) {
75       if (!isa<linalg::LinalgOp>(op))
76         return WalkResult::advance();
77       std::string append =
78           std::string("__backward_slice__") + std::to_string(opNum);
79       (void)createBackwardSliceFunction(op, append);
80       opNum++;
81       return WalkResult::advance();
82     });
83   }
84 }
85 
86 namespace mlir {
87 void registerSliceAnalysisTestPass() {
88   PassRegistration<SliceAnalysisTestPass>();
89 }
90 } // namespace mlir
91