MLIR 22.0.0git
Unit.cpp
Go to the documentation of this file.
1//===- Unit.cpp - Support for manipulating IR Unit ------------------------===//
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/IR/Unit.h"
10#include "mlir/IR/Operation.h"
12#include "mlir/IR/Region.h"
13#include "llvm/Support/raw_ostream.h"
14#include <iterator>
15
16using namespace mlir;
17
18static void printOp(llvm::raw_ostream &os, Operation *op,
19 OpPrintingFlags &flags) {
20 if (!op) {
21 os << "<Operation:nullptr>";
22 return;
23 }
24 op->print(os, flags);
25}
26
27static void printRegion(llvm::raw_ostream &os, Region *region,
28 OpPrintingFlags &flags) {
29 if (!region) {
30 os << "<Region:nullptr>";
31 return;
32 }
33 os << "Region #" << region->getRegionNumber() << " for op ";
34 printOp(os, region->getParentOp(), flags);
35}
36
37static void printBlock(llvm::raw_ostream &os, Block *block,
38 OpPrintingFlags &flags) {
39 Region *region = block->getParent();
40 Block *entry = &region->front();
41 int blockId = std::distance(entry->getIterator(), block->getIterator());
42 os << "Block #" << blockId << " for ";
43 bool shouldSkipRegions = flags.shouldSkipRegions();
44 printRegion(os, region, flags.skipRegions());
45 if (!shouldSkipRegions)
46 block->print(os);
47}
48
49void mlir::IRUnit::print(llvm::raw_ostream &os, OpPrintingFlags flags) const {
50 if (auto *op = llvm::dyn_cast_if_present<Operation *>(*this))
51 return printOp(os, op, flags);
52 if (auto *region = llvm::dyn_cast_if_present<Region *>(*this))
53 return printRegion(os, region, flags);
54 if (auto *block = llvm::dyn_cast_if_present<Block *>(*this))
55 return printBlock(os, block, flags);
56 llvm_unreachable("unknown IRUnit");
57}
58
59llvm::raw_ostream &mlir::operator<<(llvm::raw_ostream &os, const IRUnit &unit) {
60 unit.print(os);
61 return os;
62}
static void printOp(llvm::raw_ostream &os, Operation *op, OpPrintingFlags &flags)
Definition Unit.cpp:18
static void printRegion(llvm::raw_ostream &os, Region *region, OpPrintingFlags &flags)
Definition Unit.cpp:27
static void printBlock(llvm::raw_ostream &os, Block *block, OpPrintingFlags &flags)
Definition Unit.cpp:37
Block represents an ordered list of Operations.
Definition Block.h:33
Region * getParent() const
Provide a 'getParent' method for ilist_node_with_parent methods.
Definition Block.cpp:27
void print(raw_ostream &os)
IRUnit is a union of the different types of IR objects that constitute the IR structure (other than T...
Definition Unit.h:28
void print(raw_ostream &os, OpPrintingFlags flags=OpPrintingFlags().skipRegions().useLocalScope()) const
Print the IRUnit to the given stream.
Definition Unit.cpp:49
Set of flags used to control the behavior of the various IR print methods (e.g.
bool shouldSkipRegions() const
Return if regions should be skipped.
OpPrintingFlags & skipRegions(bool skip=true)
Skip printing regions.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
void print(raw_ostream &os, const OpPrintingFlags &flags={})
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
Block & front()
Definition Region.h:65
unsigned getRegionNumber()
Return the number of this region in the parent operation.
Definition Region.cpp:62
Operation * getParentOp()
Return the parent operation this region is attached to.
Definition Region.h:200
Include the generated interface declarations.
raw_ostream & operator<<(raw_ostream &os, const AliasResult &result)