CIRCT 23.0.0git
Loading...
Searching...
No Matches
HWSymCache.h
Go to the documentation of this file.
1//===- HWSymCache.h - Declare Symbol Cache ---------------------*- 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// This file declares a Symbol Cache specialized for HW instances.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef CIRCT_DIALECT_HW_SYMCACHE_H
14#define CIRCT_DIALECT_HW_SYMCACHE_H
15
18#include <limits>
19
20namespace circt {
21namespace hw {
22
23/// This stores lookup tables to make manipulating and working with the IR more
24/// efficient. There are two phases to this object: the "building" phase in
25/// which it is "write only" and then the "using" phase which is read-only (and
26/// thus can be used by multiple threads). The "freeze" method transitions
27/// between the two states.
29public:
30 class Item {
31 public:
32 Item(mlir::Operation *op) : op(op), port(invalidPort) {}
33 Item(mlir::Operation *op, size_t port) : op(op), port(port) {}
34 bool hasPort() const { return port != invalidPort; }
35 size_t getPort() const { return port; }
36 mlir::Operation *getOp() const { return op; }
37
38 private:
39 mlir::Operation *op;
40 size_t port;
41 };
42
43 // Add inner names, which might be ports
44 void addDefinition(mlir::StringAttr modSymbol, mlir::StringAttr name,
45 mlir::Operation *op, size_t port = invalidPort) {
46 auto key = InnerRefAttr::get(modSymbol, name);
47 symbolCache.try_emplace(key, op, port);
48 }
49
50 void addDefinition(mlir::Attribute key, mlir::Operation *op) override {
51 assert(!isFrozen && "cannot mutate a frozen cache");
52 symbolCache.try_emplace(key, op);
53 }
54
55 // Pull in getDefinition(mlir::FlatSymbolRefAttr symbol)
57 mlir::Operation *getDefinition(mlir::Attribute attr) const override {
58 assert(isFrozen && "cannot read from this cache until it is frozen");
59 auto it = symbolCache.find(attr);
60 if (it == symbolCache.end())
61 return nullptr;
62 assert(!it->second.hasPort() && "Module names should never be ports");
63 return it->second.getOp();
64 }
65
66 HWSymbolCache::Item getInnerDefinition(mlir::StringAttr modSymbol,
67 mlir::StringAttr name) const {
68 return lookupInner(InnerRefAttr::get(modSymbol, name));
69 }
70
71 HWSymbolCache::Item getInnerDefinition(InnerRefAttr inner) const {
72 return lookupInner(inner);
73 }
74
75 /// Mark the cache as frozen, which allows it to be shared across threads.
76 void freeze() { isFrozen = true; }
77
78private:
79 static constexpr size_t invalidPort = std::numeric_limits<size_t>::max();
80 Item lookupInner(InnerRefAttr attr) const {
81 assert(isFrozen && "cannot read from this cache until it is frozen");
82 auto it = symbolCache.find(attr);
83 return it == symbolCache.end() ? Item{nullptr, invalidPort} : it->second;
84 }
85
86 bool isFrozen = false;
87
88 /// This stores a lookup table from symbol attribute to the item
89 /// that defines it.
90 llvm::DenseMap<mlir::Attribute, Item> symbolCache;
91
92private:
93 // Iterator support. Map from Item's to their inner operations.
94 using Iterator = decltype(symbolCache)::iterator;
97 CacheItem operator*() override {
98 return {it->getFirst(), it->getSecond().getOp()};
99 }
100 void operator++() override { it++; }
101 bool operator==(CacheIteratorImpl *other) override {
102 return it == static_cast<HwSymbolCacheIteratorImpl *>(other)->it;
103 }
105 };
106
107public:
110 std::make_unique<HwSymbolCacheIteratorImpl>(symbolCache.begin()));
111 }
114 std::make_unique<HwSymbolCacheIteratorImpl>(symbolCache.end()));
115 }
116};
117
118} // namespace hw
119} // namespace circt
120
121#endif // CIRCT_DIALECT_HW_SYMCACHE_H
assert(baseType &&"element must be base type")
Base symbol cache class to allow for cache lookup through a pointer to some abstract cache.
Definition SymCache.h:25
virtual mlir::Operation * getDefinition(mlir::Attribute symbol) const =0
Lookup a definition for 'symbol' in the cache.
std::pair< mlir::Attribute, mlir::Operation * > CacheItem
Iterator support through a pointer to some abstract cache.
Definition SymCache.h:52
Item(mlir::Operation *op)
Definition HWSymCache.h:32
mlir::Operation * getOp() const
Definition HWSymCache.h:36
Item(mlir::Operation *op, size_t port)
Definition HWSymCache.h:33
This stores lookup tables to make manipulating and working with the IR more efficient.
Definition HWSymCache.h:28
decltype(symbolCache)::iterator Iterator
Definition HWSymCache.h:94
HWSymbolCache::Item getInnerDefinition(InnerRefAttr inner) const
Definition HWSymCache.h:71
void freeze()
Mark the cache as frozen, which allows it to be shared across threads.
Definition HWSymCache.h:76
void addDefinition(mlir::Attribute key, mlir::Operation *op) override
Defines 'op' as associated with the 'symbol' in the cache.
Definition HWSymCache.h:50
HWSymbolCache::Item getInnerDefinition(mlir::StringAttr modSymbol, mlir::StringAttr name) const
Definition HWSymCache.h:66
SymbolCacheBase::Iterator end() override
Definition HWSymCache.h:112
static constexpr size_t invalidPort
Definition HWSymCache.h:79
llvm::DenseMap< mlir::Attribute, Item > symbolCache
This stores a lookup table from symbol attribute to the item that defines it.
Definition HWSymCache.h:90
SymbolCacheBase::Iterator begin() override
Definition HWSymCache.h:108
mlir::Operation * getDefinition(mlir::Attribute attr) const override
Lookup a definition for 'symbol' in the cache.
Definition HWSymCache.h:57
Item lookupInner(InnerRefAttr attr) const
Definition HWSymCache.h:80
void addDefinition(mlir::StringAttr modSymbol, mlir::StringAttr name, mlir::Operation *op, size_t port=invalidPort)
Definition HWSymCache.h:44
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition hw.py:1
bool operator==(CacheIteratorImpl *other) override
Definition HWSymCache.h:101