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"
14
15namespace circt {
16// Forward declarations.
17struct Reduction;
18
19namespace reduce {
20
21/// Starting from an initial worklist of operations, traverse through it and its
22/// operands and erase operations that have no more uses. This is useful when
23/// there are repeated calls to `pruneUnusedOp` that want to delete the same
24/// operations.
25void pruneUnusedOps(SmallVectorImpl<Operation *> &worklist,
26 Reduction &reduction);
27
28/// Starting at the given `op`, traverse through it and its operands and erase
29/// operations that have no more uses.
30void pruneUnusedOps(Operation *initialOp, Reduction &reduction);
31
32/// A utility class that generates metasyntactic variable names for use in
33/// reductions. This provides a consistent naming scheme across different
34/// reduction patterns.
36public:
38
39 /// Get the next metasyntactic name in the sequence.
40 const char *getNextName();
41
42 /// Reset the generator to start from the beginning of the sequence.
43 void reset() { index = 0; }
44
45private:
46 size_t index = 0;
47 constexpr static const char *names[48] = {
48 "Foo", "Bar", "Baz", "Qux", "Quux", "Quuux", "Quuuux",
49 "Quz", "Corge", "Grault", "Bazola", "Ztesch", "Thud", "Grunt",
50 "Bletch", "Fum", "Fred", "Jim", "Sheila", "Barney", "Flarp",
51 "Zxc", "Spqr", "Wombat", "Shme", "Bongo", "Spam", "Eggs",
52 "Snork", "Zot", "Blarg", "Wibble", "Toto", "Titi", "Tata",
53 "Tutu", "Pippo", "Pluto", "Paperino", "Aap", "Noot", "Mies",
54 "Oogle", "Foogle", "Boogle", "Zork", "Gork", "Bork"};
55};
56
57/// A helper struct that scans a root operation and all its nested operations
58/// for `InnerRefAttr`s.
60 InnerSymbolUses(Operation *root);
61
62 InnerSymbolUses() = default;
63 InnerSymbolUses(const InnerSymbolUses &) = default;
65
68
69 /// Check whether an op is targeted by an inner ref. Considers both the
70 /// `sym_name` and the `inner_sym` attributes on the given op.
71 bool hasInnerRef(Operation *op) const;
72 /// Check if the given inner ref is used.
73 bool hasInnerRef(hw::InnerRefAttr innerRef) const;
74 /// Check if the given symbol name is targeted by an inner ref.
75 bool hasInnerRef(StringAttr symbol) const;
76 /// Check if the given symbol and inner symbol name pair is targeted by an
77 /// inner ref.
78 bool hasInnerRef(StringAttr symbol, StringAttr innerSym) const;
79
80 /// Check whether the given symbol is targeted by a symbol ref.
81 bool hasSymbolRef(Operation *op) const;
82 /// Check whether the given symbol name is targeted by a symbol ref.
83 bool hasSymbolRef(StringAttr symbol) const;
84
85 /// Check whether the given symbol is targeted by a symbol ref or inner ref.
86 bool hasRef(Operation *op) const;
87 /// Check whether the given symbol name is targeted by a symbol ref or inner
88 /// ref.
89 bool hasRef(StringAttr symbol) const;
90
91private:
92 /// Symbol and inner symbol name pairs used in inner refs.
93 DenseSet<std::pair<StringAttr, StringAttr>> innerRefs;
94 /// Symbol names used in inner refs.
95 DenseSet<StringAttr> innerRefModules;
96 /// Symbol names used in symbol or inner refs.
97 DenseSet<StringAttr> symbolRefs;
98};
99
100} // namespace reduce
101} // namespace circt
102
103#endif // CIRCT_REDUCE_REDUCTIONUTILS_H
A utility class that generates metasyntactic variable names for use in reductions.
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.