1//===- TestRegions.cpp - Pass to test Region's methods --------------------===//
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/Dialect/Func/IR/FuncOps.h"
10#include "mlir/IR/BuiltinOps.h"
11#include "mlir/Pass/Pass.h"
12
13using namespace mlir;
14
15namespace {
16/// This is a test pass that tests Region's takeBody method by making the first
17/// function take the body of the second.
18struct TakeBodyPass
19 : public PassWrapper<TakeBodyPass, OperationPass<ModuleOp>> {
20 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TakeBodyPass)
21
22 StringRef getArgument() const final { return "test-take-body"; }
23 StringRef getDescription() const final { return "Test Region's takeBody"; }
24
25 void runOnOperation() override {
26 auto module = getOperation();
27
28 SmallVector<func::FuncOp> functions =
29 llvm::to_vector(Range: module.getOps<func::FuncOp>());
30 if (functions.size() != 2) {
31 module.emitError(message: "Expected only two functions in test");
32 signalPassFailure();
33 return;
34 }
35
36 functions[0].getBody().takeBody(other&: functions[1].getBody());
37 }
38};
39
40} // namespace
41
42namespace mlir {
43void registerRegionTestPasses() { PassRegistration<TakeBodyPass>(); }
44} // namespace mlir
45