CIRCT  19.0.0git
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 
17 #include "circt/Support/SymCache.h"
18 
19 namespace circt {
20 namespace hw {
21 
22 /// This stores lookup tables to make manipulating and working with the IR more
23 /// efficient. There are two phases to this object: the "building" phase in
24 /// which it is "write only" and then the "using" phase which is read-only (and
25 /// thus can be used by multiple threads). The "freeze" method transitions
26 /// between the two states.
28 public:
29  class Item {
30  public:
31  Item(mlir::Operation *op) : op(op), port(~0ULL) {}
32  Item(mlir::Operation *op, size_t port) : op(op), port(port) {}
33  bool hasPort() const { return port != ~0ULL; }
34  size_t getPort() const { return port; }
35  mlir::Operation *getOp() const { return op; }
36 
37  private:
38  mlir::Operation *op;
39  size_t port;
40  };
41 
42  // Add inner names, which might be ports
43  void addDefinition(mlir::StringAttr modSymbol, mlir::StringAttr name,
44  mlir::Operation *op, size_t port = ~0ULL) {
45  auto key = InnerRefAttr::get(modSymbol, name);
46  symbolCache.try_emplace(key, op, port);
47  }
48 
49  void addDefinition(mlir::Attribute key, mlir::Operation *op) override {
50  assert(!isFrozen && "cannot mutate a frozen cache");
51  symbolCache.try_emplace(key, op);
52  }
53 
54  // Pull in getDefinition(mlir::FlatSymbolRefAttr symbol)
56  mlir::Operation *getDefinition(mlir::Attribute attr) const override {
57  assert(isFrozen && "cannot read from this cache until it is frozen");
58  auto it = symbolCache.find(attr);
59  if (it == symbolCache.end())
60  return nullptr;
61  assert(!it->second.hasPort() && "Module names should never be ports");
62  return it->second.getOp();
63  }
64 
65  HWSymbolCache::Item getInnerDefinition(mlir::StringAttr modSymbol,
66  mlir::StringAttr name) const {
67  return lookupInner(InnerRefAttr::get(modSymbol, name));
68  }
69 
70  HWSymbolCache::Item getInnerDefinition(InnerRefAttr inner) const {
71  return lookupInner(inner);
72  }
73 
74  /// Mark the cache as frozen, which allows it to be shared across threads.
75  void freeze() { isFrozen = true; }
76 
77 private:
78  Item lookupInner(InnerRefAttr attr) const {
79  assert(isFrozen && "cannot read from this cache until it is frozen");
80  auto it = symbolCache.find(attr);
81  return it == symbolCache.end() ? Item{nullptr, ~0ULL} : it->second;
82  }
83 
84  bool isFrozen = false;
85 
86  /// This stores a lookup table from symbol attribute to the item
87  /// that defines it.
88  llvm::DenseMap<mlir::Attribute, Item> symbolCache;
89 
90 private:
91  // Iterator support. Map from Item's to their inner operations.
92  using Iterator = decltype(symbolCache)::iterator;
95  CacheItem operator*() override {
96  return {it->getFirst(), it->getSecond().getOp()};
97  }
98  void operator++() override { it++; }
99  bool operator==(CacheIteratorImpl *other) override {
100  return it == static_cast<HwSymbolCacheIteratorImpl *>(other)->it;
101  }
103  };
104 
105 public:
108  std::make_unique<HwSymbolCacheIteratorImpl>(symbolCache.begin()));
109  }
112  std::make_unique<HwSymbolCacheIteratorImpl>(symbolCache.end()));
113  }
114 };
115 
116 } // namespace hw
117 } // namespace circt
118 
119 #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
mlir::Operation * getOp() const
Definition: HWSymCache.h:35
Item(mlir::Operation *op)
Definition: HWSymCache.h:31
Item(mlir::Operation *op, size_t port)
Definition: HWSymCache.h:32
This stores lookup tables to make manipulating and working with the IR more efficient.
Definition: HWSymCache.h:27
mlir::Operation * getDefinition(mlir::Attribute attr) const override
Lookup a definition for 'symbol' in the cache.
Definition: HWSymCache.h:56
decltype(symbolCache)::iterator Iterator
Definition: HWSymCache.h:92
HWSymbolCache::Item getInnerDefinition(InnerRefAttr inner) const
Definition: HWSymCache.h:70
void freeze()
Mark the cache as frozen, which allows it to be shared across threads.
Definition: HWSymCache.h:75
void addDefinition(mlir::Attribute key, mlir::Operation *op) override
Defines 'op' as associated with the 'symbol' in the cache.
Definition: HWSymCache.h:49
HWSymbolCache::Item getInnerDefinition(mlir::StringAttr modSymbol, mlir::StringAttr name) const
Definition: HWSymCache.h:65
SymbolCacheBase::Iterator end() override
Definition: HWSymCache.h:110
llvm::DenseMap< mlir::Attribute, Item > symbolCache
This stores a lookup table from symbol attribute to the item that defines it.
Definition: HWSymCache.h:88
void addDefinition(mlir::StringAttr modSymbol, mlir::StringAttr name, mlir::Operation *op, size_t port=~0ULL)
Definition: HWSymCache.h:43
SymbolCacheBase::Iterator begin() override
Definition: HWSymCache.h:106
Item lookupInner(InnerRefAttr attr) const
Definition: HWSymCache.h:78
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition: CalyxOps.cpp:54
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
Definition: hw.py:1
bool operator==(CacheIteratorImpl *other) override
Definition: HWSymCache.h:99