CIRCT  20.0.0git
FirMemLowering.h
Go to the documentation of this file.
1 //===- FirMemLowering.h - FirMem lowering utilities ===========--*- 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 // NOLINTNEXTLINE(llvm-header-guard)
10 #ifndef CONVERSION_SEQTOSV_FIRMEMLOWERING_H
11 #define CONVERSION_SEQTOSV_FIRMEMLOWERING_H
12 
13 #include "circt/Dialect/HW/HWOps.h"
15 #include "circt/Support/LLVM.h"
17 #include "circt/Support/SymCache.h"
18 #include "llvm/ADT/MapVector.h"
19 
20 namespace circt {
21 
22 /// The configuration of a FIR memory.
23 struct FirMemConfig {
24  size_t numReadPorts = 0;
25  size_t numWritePorts = 0;
26  size_t numReadWritePorts = 0;
27  size_t dataWidth = 0;
28  size_t depth = 0;
29  size_t readLatency = 0;
30  size_t writeLatency = 0;
31  size_t maskBits = 0;
32  seq::RUW readUnderWrite = seq::RUW::Undefined;
33  seq::WUW writeUnderWrite = seq::WUW::Undefined;
34  SmallVector<int32_t, 1> writeClockIDs;
35  StringRef initFilename;
36  bool initIsBinary = false;
37  bool initIsInline = false;
38  Attribute outputFile;
39  StringRef prefix;
40 
41  llvm::hash_code hashValue() const {
46  outputFile, prefix) ^
47  llvm::hash_combine_range(writeClockIDs.begin(), writeClockIDs.end());
48  }
49 
50  auto getTuple() const {
51  return std::make_tuple(numReadPorts, numWritePorts, numReadWritePorts,
56  }
57 
58  bool operator==(const FirMemConfig &other) const {
59  return getTuple() == other.getTuple();
60  }
61 };
62 
63 /**
64  * FIR memory lowering helper.
65  */
67 public:
68  /// A vector of unique `FirMemConfig`s and all the `FirMemOp`s that use it.
69  using UniqueConfigs =
70  llvm::MapVector<FirMemConfig, SmallVector<seq::FirMemOp, 1>>;
71 
72  /// Information required to lower a single memory in a module.
73  using MemoryConfig =
74  std::tuple<FirMemConfig *, hw::HWModuleGeneratedOp, seq::FirMemOp>;
75 
76  FirMemLowering(ModuleOp circuit);
77 
78  /**
79  * Groups memories by their kind from the whole design.
80  */
81  UniqueConfigs collectMemories(ArrayRef<hw::HWModuleOp> modules);
82 
83  /**
84  * Lowers a group of memories from the same module.
85  */
87  ArrayRef<MemoryConfig> mems);
88 
89  /**
90  * Creates the generated module for a given configuration.
91  */
92  hw::HWModuleGeneratedOp createMemoryModule(FirMemConfig &mem,
93  ArrayRef<seq::FirMemOp> memOps);
94 
95 private:
96  FirMemConfig collectMemory(seq::FirMemOp op);
97 
98  /**
99  * Find the schema or create it if it does not exist.
100  */
101  FlatSymbolRefAttr getOrCreateSchema();
102 
103 private:
104  MLIRContext *context;
105  ModuleOp circuit;
106 
109 
110  DenseMap<hw::HWModuleOp, size_t> moduleIndex;
111 
112  hw::HWGeneratorSchemaOp schemaOp;
113 };
114 
115 } // namespace circt
116 
117 namespace llvm {
118 template <>
119 struct DenseMapInfo<circt::FirMemConfig> {
123  return cfg;
124  }
128  return cfg;
129  }
130  static unsigned getHashValue(const circt::FirMemConfig &cfg) {
131  return cfg.hashValue();
132  }
133  static bool isEqual(const circt::FirMemConfig &lhs,
134  const circt::FirMemConfig &rhs) {
135  return lhs == rhs;
136  }
137 };
138 } // namespace llvm
139 
140 #endif // CONVERSION_SEQTOSV_FIRMEMLOWERING_H
FIR memory lowering helper.
UniqueConfigs collectMemories(ArrayRef< hw::HWModuleOp > modules)
Groups memories by their kind from the whole design.
void lowerMemoriesInModule(hw::HWModuleOp module, ArrayRef< MemoryConfig > mems)
Lowers a group of memories from the same module.
hw::HWGeneratorSchemaOp schemaOp
FirMemLowering(ModuleOp circuit)
hw::HWModuleGeneratedOp createMemoryModule(FirMemConfig &mem, ArrayRef< seq::FirMemOp > memOps)
Creates the generated module for a given configuration.
FlatSymbolRefAttr getOrCreateSchema()
Find the schema or create it if it does not exist.
DenseMap< hw::HWModuleOp, size_t > moduleIndex
std::tuple< FirMemConfig *, hw::HWModuleGeneratedOp, seq::FirMemOp > MemoryConfig
Information required to lower a single memory in a module.
llvm::MapVector< FirMemConfig, SmallVector< seq::FirMemOp, 1 > > UniqueConfigs
A vector of unique FirMemConfigs and all the FirMemOps that use it.
FirMemConfig collectMemory(seq::FirMemOp op)
Determine the exact parametrization of the memory that should be generated for a given FirMemOp.
A namespace that is used to store existing names and generate new names in some scope within the IR.
Definition: Namespace.h:30
Default symbol cache implementation; stores associations between names (StringAttr's) to mlir::Operat...
Definition: SymCache.h:85
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
size_t hash_combine(size_t h1, size_t h2)
C++'s stdlib doesn't have a hash_combine function. This is a simple one.
Definition: Utils.h:32
The configuration of a FIR memory.
llvm::hash_code hashValue() const
bool operator==(const FirMemConfig &other) const
SmallVector< int32_t, 1 > writeClockIDs
auto getTuple() const
static bool isEqual(const circt::FirMemConfig &lhs, const circt::FirMemConfig &rhs)
static circt::FirMemConfig getTombstoneKey()
static circt::FirMemConfig getEmptyKey()
static unsigned getHashValue(const circt::FirMemConfig &cfg)