CIRCT  19.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 
19 namespace circt {
20 
21 /// The configuration of a FIR memory.
22 struct FirMemConfig {
23  size_t numReadPorts = 0;
24  size_t numWritePorts = 0;
25  size_t numReadWritePorts = 0;
26  size_t dataWidth = 0;
27  size_t depth = 0;
28  size_t readLatency = 0;
29  size_t writeLatency = 0;
30  size_t maskBits = 0;
31  seq::RUW readUnderWrite = seq::RUW::Undefined;
32  seq::WUW writeUnderWrite = seq::WUW::Undefined;
33  SmallVector<int32_t, 1> writeClockIDs;
34  StringRef initFilename;
35  bool initIsBinary = false;
36  bool initIsInline = false;
37  Attribute outputFile;
38  StringRef prefix;
39 
40  llvm::hash_code hashValue() const {
41  return llvm::hash_combine(numReadPorts, numWritePorts, numReadWritePorts,
45  outputFile, prefix) ^
46  llvm::hash_combine_range(writeClockIDs.begin(), writeClockIDs.end());
47  }
48 
49  auto getTuple() const {
50  return std::make_tuple(numReadPorts, numWritePorts, numReadWritePorts,
55  }
56 
57  bool operator==(const FirMemConfig &other) const {
58  return getTuple() == other.getTuple();
59  }
60 };
61 
62 /**
63  * FIR memory lowering helper.
64  */
66 public:
67  /// A vector of unique `FirMemConfig`s and all the `FirMemOp`s that use it.
68  using UniqueConfigs =
69  llvm::MapVector<FirMemConfig, SmallVector<seq::FirMemOp, 1>>;
70 
71  /// Information required to lower a single memory in a module.
72  using MemoryConfig =
73  std::tuple<FirMemConfig *, hw::HWModuleGeneratedOp, seq::FirMemOp>;
74 
75  FirMemLowering(ModuleOp circuit);
76 
77  /**
78  * Groups memories by their kind from the whole design.
79  */
80  UniqueConfigs collectMemories(ArrayRef<hw::HWModuleOp> modules);
81 
82  /**
83  * Lowers a group of memories from the same module.
84  */
86  ArrayRef<MemoryConfig> mems);
87 
88  /**
89  * Creates the generated module for a given configuration.
90  */
91  hw::HWModuleGeneratedOp createMemoryModule(FirMemConfig &mem,
92  ArrayRef<seq::FirMemOp> memOps);
93 
94 private:
95  FirMemConfig collectMemory(seq::FirMemOp op);
96 
97  /**
98  * Find the schema or create it if it does not exist.
99  */
100  FlatSymbolRefAttr getOrCreateSchema();
101 
102 private:
103  MLIRContext *context;
104  ModuleOp circuit;
105 
108 
109  DenseMap<hw::HWModuleOp, size_t> moduleIndex;
110 
111  hw::HWGeneratorSchemaOp schemaOp;
112 };
113 
114 } // namespace circt
115 
116 namespace llvm {
117 template <>
118 struct DenseMapInfo<circt::FirMemConfig> {
121  cfg.depth = DenseMapInfo<size_t>::getEmptyKey();
122  return cfg;
123  }
126  cfg.depth = DenseMapInfo<size_t>::getTombstoneKey();
127  return cfg;
128  }
129  static unsigned getHashValue(const circt::FirMemConfig &cfg) {
130  return cfg.hashValue();
131  }
132  static bool isEqual(const circt::FirMemConfig &lhs,
133  const circt::FirMemConfig &rhs) {
134  return lhs == rhs;
135  }
136 };
137 } // namespace llvm
138 
139 #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:29
Default symbol cache implementation; stores associations between names (StringAttr's) to mlir::Operat...
Definition: SymCache.h:85
This file defines an intermediate representation for circuits acting as an abstraction for constraint...
Definition: DebugAnalysis.h:21
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)