1//===- TestBlockInLoop.cpp - Pass to test mlir::blockIsInLoop -------------===//
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/Interfaces/LoopLikeInterface.h"
12#include "mlir/Pass/Pass.h"
13#include "llvm/Support/raw_ostream.h"
14
15using namespace mlir;
16
17namespace {
18/// This is a test pass that tests Blocks's isInLoop method by checking if each
19/// block in a function is in a loop and outputing if it is
20struct IsInLoopPass
21 : public PassWrapper<IsInLoopPass, OperationPass<ModuleOp>> {
22 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(IsInLoopPass)
23
24 StringRef getArgument() const final { return "test-block-is-in-loop"; }
25 StringRef getDescription() const final {
26 return "Test mlir::blockIsInLoop()";
27 }
28
29 void runOnOperation() override {
30 ModuleOp module = getOperation();
31 for (auto func : module.getOps<func::FuncOp>()) {
32 func.walk(callback: [](mlir::Block *block) {
33 llvm::outs() << "Block is ";
34 if (LoopLikeOpInterface::blockIsInLoop(block))
35 llvm::outs() << "in a loop\n";
36 else
37 llvm::outs() << "not in a loop\n";
38 block->print(os&: llvm::outs());
39 llvm::outs() << "\n";
40 });
41 }
42 }
43};
44
45} // namespace
46
47namespace mlir {
48void registerLoopLikeInterfaceTestPasses() { PassRegistration<IsInLoopPass>(); }
49} // namespace mlir
50