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
22using 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`.
26static LogicalResult createBackwardSliceFunction(Operation *op,
27 StringRef suffix,
28 bool omitBlockArguments) {
29 func::FuncOp parentFuncOp = op->getParentOfType<func::FuncOp>();
30 OpBuilder builder(parentFuncOp);
31 Location loc = op->getLoc();
32 std::string clonedFuncOpName = parentFuncOp.getName().str() + suffix.str();
33 func::FuncOp clonedFuncOp = builder.create<func::FuncOp>(
34 loc, clonedFuncOpName, parentFuncOp.getFunctionType());
35 IRMapping mapper;
36 builder.setInsertionPointToEnd(clonedFuncOp.addEntryBlock());
37 for (const auto &arg : enumerate(parentFuncOp.getArguments()))
38 mapper.map(arg.value(), clonedFuncOp.getArgument(arg.index()));
39 SetVector<Operation *> slice;
40 BackwardSliceOptions options;
41 options.omitBlockArguments = omitBlockArguments;
42 getBackwardSlice(op, backwardSlice: &slice, options);
43 for (Operation *slicedOp : slice)
44 builder.clone(op&: *slicedOp, mapper);
45 builder.create<func::ReturnOp>(loc);
46 return success();
47}
48
49namespace {
50/// Pass to test slice generated from slice analysis.
51struct SliceAnalysisTestPass
52 : public PassWrapper<SliceAnalysisTestPass, OperationPass<ModuleOp>> {
53 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(SliceAnalysisTestPass)
54
55 StringRef getArgument() const final { return "slice-analysis-test"; }
56 StringRef getDescription() const final {
57 return "Test Slice analysis functionality.";
58 }
59
60 Option<bool> omitBlockArguments{
61 *this, "omit-block-arguments",
62 llvm::cl::desc("Test Slice analysis with multiple blocks but slice "
63 "omiting block arguments"),
64 llvm::cl::init(Val: true)};
65
66 void runOnOperation() override;
67 SliceAnalysisTestPass() = default;
68 SliceAnalysisTestPass(const SliceAnalysisTestPass &) {}
69};
70} // namespace
71
72void SliceAnalysisTestPass::runOnOperation() {
73 ModuleOp module = getOperation();
74 auto funcOps = module.getOps<func::FuncOp>();
75 unsigned opNum = 0;
76 for (auto funcOp : funcOps) {
77 // TODO: For now this is just looking for Linalg ops. It can be generalized
78 // to look for other ops using flags.
79 funcOp.walk([&](Operation *op) {
80 if (!isa<linalg::LinalgOp>(op))
81 return WalkResult::advance();
82 std::string append =
83 std::string("__backward_slice__") + std::to_string(opNum);
84 (void)createBackwardSliceFunction(op, append, omitBlockArguments);
85 opNum++;
86 return WalkResult::advance();
87 });
88 }
89}
90
91namespace mlir {
92void registerSliceAnalysisTestPass() {
93 PassRegistration<SliceAnalysisTestPass>();
94}
95} // namespace mlir
96

source code of mlir/test/lib/IR/TestSlicing.cpp