CIRCT 23.0.0git
Loading...
Searching...
No Matches
ReductionUtils.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 CIRCT_REDUCE_REDUCTIONUTILS_H
10#define CIRCT_REDUCE_REDUCTIONUTILS_H
11
13#include "circt/Support/LLVM.h"
15
16namespace circt {
17// Forward declarations.
18struct Reduction;
19
20namespace reduce {
21
22/// Starting from an initial worklist of operations, traverse through it and its
23/// operands and erase operations that have no more uses. This is useful when
24/// there are repeated calls to `pruneUnusedOp` that want to delete the same
25/// operations.
26void pruneUnusedOps(SmallVectorImpl<Operation *> &worklist,
27 Reduction &reduction);
28
29/// Starting at the given `op`, traverse through it and its operands and erase
30/// operations that have no more uses.
31void pruneUnusedOps(Operation *initialOp, Reduction &reduction);
32
33/// A utility class that generates metasyntactic variable names for use in
34/// reductions. This provides a consistent naming scheme across different
35/// reduction patterns.
37public:
39
40 /// Get the next metasyntactic name in the sequence.
41 const char *getNextName();
42
43 /// Get the next metasyntactic name that is not already reserved in \p ns,
44 /// and reserve it. If the candidate name is already taken, \p ns appends a
45 /// numeric suffix (e.g. "Foo_0") to make it unique.
46 StringRef getNextName(Namespace &ns);
47
48 /// Return true if \p name already has a metasyntactic prefix, i.e. the
49 /// substring before the first '_' (or the whole string if there is no '_')
50 /// is one of the names in the generator sequence. Examples:
51 /// isMetasyntacticName("Foo") → true
52 /// isMetasyntacticName("Foo_0") → true
53 /// isMetasyntacticName("Foo_0_0_0") → true
54 /// isMetasyntacticName("FooBar") → false
55 /// isMetasyntacticName("MyModule") → false
56 static bool isMetasyntacticName(StringRef name);
57
58 /// Reset the generator to start from the beginning of the sequence.
59 void reset() { index = 0; }
60
61private:
62 size_t index = 0;
63 constexpr static const char *names[48] = {
64 "Foo", "Bar", "Baz", "Qux", "Quux", "Quuux", "Quuuux",
65 "Quz", "Corge", "Grault", "Bazola", "Ztesch", "Thud", "Grunt",
66 "Bletch", "Fum", "Fred", "Jim", "Sheila", "Barney", "Flarp",
67 "Zxc", "Spqr", "Wombat", "Shme", "Bongo", "Spam", "Eggs",
68 "Snork", "Zot", "Blarg", "Wibble", "Toto", "Titi", "Tata",
69 "Tutu", "Pippo", "Pluto", "Paperino", "Aap", "Noot", "Mies",
70 "Oogle", "Foogle", "Boogle", "Zork", "Gork", "Bork"};
71};
72
73/// A helper struct that scans a root operation and all its nested operations
74/// for `InnerRefAttr`s.
76 InnerSymbolUses(Operation *root);
77
78 InnerSymbolUses() = default;
79 InnerSymbolUses(const InnerSymbolUses &) = default;
81
84
85 /// Check whether an op is targeted by an inner ref. Considers both the
86 /// `sym_name` and the `inner_sym` attributes on the given op.
87 bool hasInnerRef(Operation *op) const;
88 /// Check if the given inner ref is used.
89 bool hasInnerRef(hw::InnerRefAttr innerRef) const;
90 /// Check if the given symbol name is targeted by an inner ref.
91 bool hasInnerRef(StringAttr symbol) const;
92 /// Check if the given symbol and inner symbol name pair is targeted by an
93 /// inner ref.
94 bool hasInnerRef(StringAttr symbol, StringAttr innerSym) const;
95
96 /// Check whether the given symbol is targeted by a symbol ref.
97 bool hasSymbolRef(Operation *op) const;
98 /// Check whether the given symbol name is targeted by a symbol ref.
99 bool hasSymbolRef(StringAttr symbol) const;
100
101 /// Check whether the given symbol is targeted by a symbol ref or inner ref.
102 bool hasRef(Operation *op) const;
103 /// Check whether the given symbol name is targeted by a symbol ref or inner
104 /// ref.
105 bool hasRef(StringAttr symbol) const;
106
107private:
108 /// Symbol and inner symbol name pairs used in inner refs.
109 DenseSet<std::pair<StringAttr, StringAttr>> innerRefs;
110 /// Symbol names used in inner refs.
111 DenseSet<StringAttr> innerRefModules;
112 /// Symbol names used in symbol or inner refs.
113 DenseSet<StringAttr> symbolRefs;
114};
115
116} // namespace reduce
117} // namespace circt
118
119#endif // CIRCT_REDUCE_REDUCTIONUTILS_H
A namespace that is used to store existing names and generate new names in some scope within the IR.
Definition Namespace.h:30
A utility class that generates metasyntactic variable names for use in reductions.
static bool isMetasyntacticName(StringRef name)
Return true if name already has a metasyntactic prefix, i.e.
void reset()
Reset the generator to start from the beginning of the sequence.
const char * getNextName()
Get the next metasyntactic name in the sequence.
static constexpr const char * names[48]
void pruneUnusedOps(SmallVectorImpl< Operation * > &worklist, Reduction &reduction)
Starting from an initial worklist of operations, traverse through it and its operands and erase opera...
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
An abstract reduction pattern.
Definition Reduction.h:24
A helper struct that scans a root operation and all its nested operations for InnerRefAttrs.
DenseSet< StringAttr > innerRefModules
Symbol names used in inner refs.
bool hasSymbolRef(Operation *op) const
Check whether the given symbol is targeted by a symbol ref.
bool hasInnerRef(Operation *op) const
Check whether an op is targeted by an inner ref.
DenseSet< StringAttr > symbolRefs
Symbol names used in symbol or inner refs.
InnerSymbolUses & operator=(InnerSymbolUses &&)=default
bool hasRef(Operation *op) const
Check whether the given symbol is targeted by a symbol ref or inner ref.
InnerSymbolUses & operator=(const InnerSymbolUses &)=default
InnerSymbolUses(const InnerSymbolUses &)=default
InnerSymbolUses(InnerSymbolUses &&)=default
DenseSet< std::pair< StringAttr, StringAttr > > innerRefs
Symbol and inner symbol name pairs used in inner refs.