LLVM 20.0.0git
Region.h
Go to the documentation of this file.
1//===- Region.h -------------------------------------------------*- C++ -*-===//
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#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_REGION_H
10#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_REGION_H
11
12#include <memory>
13
14#include "llvm/ADT/SetVector.h"
18
19namespace llvm::sandboxir {
20
21/// The main job of the Region is to point to new instructions generated by
22/// vectorization passes. It is the unit that RegionPasses operate on with their
23/// runOnRegion() function.
24///
25/// The region allows us to stack transformations horizontally, meaning that
26/// each transformation operates on a single region and the resulting region is
27/// the input to the next transformation, as opposed to vertically, which is the
28/// common way of applying a transformation across the whole function. This
29/// enables us to check for profitability and decide whether we accept or
30/// rollback at a region granularity, which is much better than doing this at
31/// the function level.
32///
33// Traditional approach: transformations applied vertically for the whole
34// function
35// F
36// +----+
37// | |
38// | |
39// | | -> Transform1 -> ... -> TransformN -> Check Cost
40// | |
41// | |
42// +----+
43//
44// Region-based approach: transformations applied horizontally, for each Region
45// F
46// +----+
47// |Rgn1| -> Transform1 -> ... -> TransformN -> Check Cost
48// | |
49// |Rgn2| -> Transform1 -> ... -> TransformN -> Check Cost
50// | |
51// |Rgn3| -> Transform1 -> ... -> TransformN -> Check Cost
52// +----+
53
54class Region {
55 /// All the instructions in the Region. Only new instructions generated during
56 /// vectorization are part of the Region.
58
59 /// MDNode that we'll use to mark instructions as being part of the region.
60 MDNode *RegionMDN;
61 static constexpr const char *MDKind = "sandboxvec";
62 static constexpr const char *RegionStr = "sandboxregion";
63
64 Context &Ctx;
65
66 /// ID (for later deregistration) of the "create instruction" callback.
67 Context::CallbackID CreateInstCB;
68 /// ID (for later deregistration) of the "erase instruction" callback.
69 Context::CallbackID EraseInstCB;
70
71 // TODO: Add cost modeling.
72 // TODO: Add a way to encode/decode region info to/from metadata.
73
74public:
75 Region(Context &Ctx);
76 ~Region();
77
78 Context &getContext() const { return Ctx; }
79
80 /// Adds I to the set.
81 void add(Instruction *I);
82 /// Removes I from the set.
83 void remove(Instruction *I);
84 /// Returns true if I is in the Region.
85 bool contains(Instruction *I) const { return Insts.contains(I); }
86 /// Returns true if the Region has no instructions.
87 bool empty() const { return Insts.empty(); }
88
89 using iterator = decltype(Insts.begin());
90 iterator begin() { return Insts.begin(); }
91 iterator end() { return Insts.end(); }
93
95
96#ifndef NDEBUG
97 /// This is an expensive check, meant for testing.
98 bool operator==(const Region &Other) const;
99 bool operator!=(const Region &other) const { return !(*this == other); }
100
101 void dump(raw_ostream &OS) const;
102 void dump() const;
104 Rgn.dump(OS);
105 return OS;
106 }
107#endif
108};
109
110} // namespace llvm::sandboxir
111
112#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_REGION_H
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
raw_pwrite_stream & OS
This file implements a set that has insertion order iteration characteristics.
Metadata node.
Definition: Metadata.h:1069
A vector that has set insertion semantics.
Definition: SetVector.h:57
iterator end()
Get an iterator to the end of the SetVector.
Definition: SetVector.h:113
bool empty() const
Determine if the SetVector is empty or not.
Definition: SetVector.h:93
iterator begin()
Get an iterator to the beginning of the SetVector.
Definition: SetVector.h:103
bool contains(const key_type &key) const
Check if the SetVector contains the given key.
Definition: SetVector.h:254
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition: Instruction.h:42
The main job of the Region is to point to new instructions generated by vectorization passes.
Definition: Region.h:54
void dump() const
Definition: Region.cpp:55
Context & getContext() const
Definition: Region.h:78
bool contains(Instruction *I) const
Returns true if I is in the Region.
Definition: Region.h:85
iterator_range< iterator > insts()
Definition: Region.h:92
bool operator!=(const Region &other) const
Definition: Region.h:99
void add(Instruction *I)
Adds I to the set.
Definition: Region.cpp:30
void remove(Instruction *I)
Removes I from the set.
Definition: Region.cpp:36
static SmallVector< std::unique_ptr< Region > > createRegionsFromMD(Function &F)
Definition: Region.cpp:61
friend raw_ostream & operator<<(raw_ostream &OS, const Region &Rgn)
Definition: Region.h:103
iterator begin()
Definition: Region.h:90
iterator end()
Definition: Region.h:91
decltype(Insts.begin()) iterator
Definition: Region.h:89
void dump(raw_ostream &OS) const
Definition: Region.cpp:50
bool empty() const
Returns true if the Region has no instructions.
Definition: Region.h:87
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
@ Other
Any other memory.