xref: /llvm-project/mlir/test/lib/Analysis/TestCFGLoopInfo.cpp (revision 34a35a8b244243f5a4ad5d531007bccfeaa0b02e)
11ef51e04SChristian Ulmann //===- TestCFGLoopInfo.cpp - Test CFG loop info analysis ------------------===//
21ef51e04SChristian Ulmann //
31ef51e04SChristian Ulmann // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
41ef51e04SChristian Ulmann // See https://llvm.org/LICENSE.txt for license information.
51ef51e04SChristian Ulmann // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61ef51e04SChristian Ulmann //
71ef51e04SChristian Ulmann //===----------------------------------------------------------------------===//
81ef51e04SChristian Ulmann //
91ef51e04SChristian Ulmann // This file implements logic for testing the CFGLoopInfo analysis.
101ef51e04SChristian Ulmann //
111ef51e04SChristian Ulmann //===----------------------------------------------------------------------===//
121ef51e04SChristian Ulmann 
131ef51e04SChristian Ulmann #include "mlir/Analysis/CFGLoopInfo.h"
14*34a35a8bSMartin Erhart #include "mlir/Interfaces/FunctionInterfaces.h"
151ef51e04SChristian Ulmann #include "mlir/Pass/Pass.h"
161ef51e04SChristian Ulmann 
171ef51e04SChristian Ulmann using namespace mlir;
181ef51e04SChristian Ulmann 
191ef51e04SChristian Ulmann namespace {
201ef51e04SChristian Ulmann /// A testing pass that applies the CFGLoopInfo analysis on a region and prints
211ef51e04SChristian Ulmann /// the information it collected to llvm::errs().
221ef51e04SChristian Ulmann struct TestCFGLoopInfo
231ef51e04SChristian Ulmann     : public PassWrapper<TestCFGLoopInfo, InterfacePass<FunctionOpInterface>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID__anone491a2740111::TestCFGLoopInfo241ef51e04SChristian Ulmann   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestCFGLoopInfo)
251ef51e04SChristian Ulmann 
261ef51e04SChristian Ulmann   StringRef getArgument() const final { return "test-cfg-loop-info"; }
getDescription__anone491a2740111::TestCFGLoopInfo271ef51e04SChristian Ulmann   StringRef getDescription() const final {
281ef51e04SChristian Ulmann     return "Test the loop info analysis.";
291ef51e04SChristian Ulmann   }
301ef51e04SChristian Ulmann 
311ef51e04SChristian Ulmann   void runOnOperation() override;
321ef51e04SChristian Ulmann };
331ef51e04SChristian Ulmann } // namespace
341ef51e04SChristian Ulmann 
runOnOperation()351ef51e04SChristian Ulmann void TestCFGLoopInfo::runOnOperation() {
361ef51e04SChristian Ulmann   auto func = getOperation();
371ef51e04SChristian Ulmann   DominanceInfo &domInfo = getAnalysis<DominanceInfo>();
381ef51e04SChristian Ulmann   Region &region = func.getFunctionBody();
391ef51e04SChristian Ulmann 
401ef51e04SChristian Ulmann   // Prints the label of the test.
411ef51e04SChristian Ulmann   llvm::errs() << "Testing : " << func.getNameAttr() << "\n";
421ef51e04SChristian Ulmann   if (region.empty()) {
431ef51e04SChristian Ulmann     llvm::errs() << "empty region\n";
441ef51e04SChristian Ulmann     return;
451ef51e04SChristian Ulmann   }
461ef51e04SChristian Ulmann 
471ef51e04SChristian Ulmann   // Print all the block identifiers first such that the tests can match them.
481ef51e04SChristian Ulmann   llvm::errs() << "Blocks : ";
491ef51e04SChristian Ulmann   region.front().printAsOperand(llvm::errs());
501ef51e04SChristian Ulmann   for (auto &block : region.getBlocks()) {
511ef51e04SChristian Ulmann     llvm::errs() << ", ";
521ef51e04SChristian Ulmann     block.printAsOperand(llvm::errs());
531ef51e04SChristian Ulmann   }
541ef51e04SChristian Ulmann   llvm::errs() << "\n";
551ef51e04SChristian Ulmann 
561ef51e04SChristian Ulmann   if (region.getBlocks().size() == 1) {
571ef51e04SChristian Ulmann     llvm::errs() << "no loops\n";
581ef51e04SChristian Ulmann     return;
591ef51e04SChristian Ulmann   }
601ef51e04SChristian Ulmann 
611ef51e04SChristian Ulmann   llvm::DominatorTreeBase<mlir::Block, false> &domTree =
621ef51e04SChristian Ulmann       domInfo.getDomTree(&region);
631ef51e04SChristian Ulmann   mlir::CFGLoopInfo loopInfo(domTree);
641ef51e04SChristian Ulmann 
651ef51e04SChristian Ulmann   if (loopInfo.getTopLevelLoops().empty())
661ef51e04SChristian Ulmann     llvm::errs() << "no loops\n";
671ef51e04SChristian Ulmann   else
681ef51e04SChristian Ulmann     loopInfo.print(llvm::errs());
691ef51e04SChristian Ulmann }
701ef51e04SChristian Ulmann 
711ef51e04SChristian Ulmann namespace mlir {
721ef51e04SChristian Ulmann namespace test {
registerTestCFGLoopInfoPass()731ef51e04SChristian Ulmann void registerTestCFGLoopInfoPass() { PassRegistration<TestCFGLoopInfo>(); }
741ef51e04SChristian Ulmann } // namespace test
751ef51e04SChristian Ulmann } // namespace mlir
76