Files
clang-p2996/mlir/test/lib/Interfaces/LoopLikeInterface/TestBlockInLoop.cpp
Tom Eccles 81a79ee446 [mlir] Add function for checking if a block is inside a loop
This function returns whether a block is nested inside of a loop. There
can be three kinds of loop:
  1) The block is nested inside of a LoopLikeOpInterface
  2) The block is nested inside another block which is in a loop
  3) There is a cycle in the control flow graph

This will be useful for Flang's stack arrays pass, which moves array
allocations from the heap to the stack. Special handling is needed when
allocations occur inside of loops to ensure additional stack space is
not allocated on each loop iteration.

Differential Revision: https://reviews.llvm.org/D141401
2023-02-10 16:14:17 +00:00

48 lines
1.5 KiB
C++

//===- TestBlockInLoop.cpp - Pass to test mlir::blockIsInLoop -------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/Interfaces/LoopLikeInterface.h"
#include "mlir/Pass/Pass.h"
#include "llvm/Support/raw_ostream.h"
using namespace mlir;
namespace {
/// This is a test pass that tests Blocks's isInLoop method by checking if each
/// block in a function is in a loop and outputing if it is
struct IsInLoopPass
: public PassWrapper<IsInLoopPass, OperationPass<func::FuncOp>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(IsInLoopPass)
StringRef getArgument() const final { return "test-block-is-in-loop"; }
StringRef getDescription() const final {
return "Test mlir::blockIsInLoop()";
}
void runOnOperation() override {
mlir::func::FuncOp func = getOperation();
func.walk([](mlir::Block *block) {
llvm::outs() << "Block is ";
if (LoopLikeOpInterface::blockIsInLoop(block))
llvm::outs() << "in a loop\n";
else
llvm::outs() << "not in a loop\n";
block->print(llvm::outs());
llvm::outs() << "\n";
});
}
};
} // namespace
namespace mlir {
void registerLoopLikeInterfaceTestPasses() { PassRegistration<IsInLoopPass>(); }
} // namespace mlir