19#include "mlir/Pass/Pass.h"
23#define GEN_PASS_DEF_FREEZEPATHS
24#include "circt/Dialect/OM/OMPasses.h.inc"
33 PathVisitor(hw::InstanceGraph &instanceGraph, hw::InnerRefNamespace &irn,
34 std::function<StringAttr(Operation *)> &getOpName)
35 : instanceGraph(instanceGraph), irn(irn), getOpNameFallback(getOpName) {}
38 LogicalResult processPath(Location loc, hw::HierPathOp hierPathOp,
39 PathAttr &targetPath, StringAttr &bottomModule,
40 StringAttr &component, StringAttr &field);
41 LogicalResult process(BasePathCreateOp pathOp);
42 LogicalResult process(PathCreateOp pathOp);
43 LogicalResult process(EmptyPathOp pathOp);
44 LogicalResult
run(ModuleOp module);
45 hw::InstanceGraph &instanceGraph;
46 hw::InnerRefNamespace &irn;
47 std::function<StringAttr(Operation *)> getOpNameFallback;
51static LogicalResult
getAccessPath(Location loc, Type type,
size_t fieldId,
53 SmallString<64> field;
55 if (
auto aliasType = dyn_cast<hw::TypeAliasType>(type))
56 type = aliasType.getCanonicalType();
57 if (
auto structType = dyn_cast<hw::StructType>(type)) {
58 auto index = structType.getIndexForFieldID(fieldId);
59 auto &element = structType.getElements()[index];
61 llvm::append_range(field, element.name.getValue());
63 fieldId -= structType.getFieldID(index);
64 }
else if (
auto arrayType = dyn_cast<hw::ArrayType>(type)) {
65 auto index = arrayType.getIndexForFieldID(fieldId);
67 Twine(index).toVector(field);
69 type = arrayType.getElementType();
70 fieldId -= arrayType.getFieldID(index);
72 return emitError(loc) <<
"can't create access path with fieldID "
73 << fieldId <<
" in type " << type;
76 result = StringAttr::get(loc->getContext(), field);
82 bool isPathType =
false;
83 type.walk([&](Type innerType) {
84 if (isa<BasePathType, PathType>(innerType))
92 mlir::AttrTypeReplacer replacer;
93 replacer.addReplacement([](BasePathType innerType) {
94 return FrozenBasePathType::get(innerType.getContext());
96 replacer.addReplacement([](PathType innerType) {
97 return FrozenPathType::get(innerType.getContext());
105 return replacer.replace(type);
108LogicalResult PathVisitor::processPath(Location loc, hw::HierPathOp hierPathOp,
109 PathAttr &targetPath,
110 StringAttr &bottomModule,
111 StringAttr &component,
113 auto *
context = hierPathOp->getContext();
115 auto namepath = hierPathOp.getNamepathAttr().getValue();
116 SmallVector<PathElement> modules;
119 for (
auto attr : namepath.drop_back()) {
120 auto innerRef = cast<hw::InnerRefAttr>(attr);
121 auto target = irn.lookup(innerRef);
122 assert(target.isOpOnly() &&
"can't target a port the middle of a namepath");
123 auto *op = target.getOp();
125 auto verilogName = op->getAttrOfType<StringAttr>(
"hw.verilogName");
126 if (!verilogName && getOpNameFallback)
127 verilogName = getOpNameFallback(op);
129 auto diag = emitError(loc,
"component does not have verilog name");
130 diag.attachNote(op->getLoc()) <<
"component here";
133 modules.emplace_back(innerRef.getModule(), verilogName);
137 auto &
end = namepath.back();
138 if (
auto innerRef = dyn_cast<hw::InnerRefAttr>(end)) {
139 auto target = irn.lookup(innerRef);
140 if (target.isPort()) {
142 auto module = cast<hw::HWModuleLike>(target.getOp());
143 auto index = target.getPort();
144 bottomModule =
module.getModuleNameAttr();
145 component = StringAttr::get(
context, module.getPortName(index));
146 auto loc =
module.getPortLoc(index);
147 auto type =
module.getPortTypes()[index];
148 if (failed(
getAccessPath(loc, type, target.getField(), field)))
151 auto *op = target.getOp();
152 assert(op &&
"innerRef should be targeting something");
154 auto currentModule = innerRef.getModule();
156 auto verilogName = op->getAttrOfType<StringAttr>(
"hw.verilogName");
157 if (!verilogName && getOpNameFallback)
158 verilogName = getOpNameFallback(op);
160 auto diag = emitError(loc,
"component does not have verilog name");
161 diag.attachNote(op->getLoc()) <<
"component here";
167 if (
auto inst = dyn_cast<hw::HWInstanceLike>(op)) {
169 auto mods = inst.getReferencedModuleNamesAttr();
171 return op->emitError(
"unsupported instance operation");
173 modules.emplace_back(currentModule, verilogName);
174 bottomModule = cast<StringAttr>(mods[0]);
175 component = StringAttr::get(
context,
"");
176 field = StringAttr::get(
context,
"");
179 bottomModule = currentModule;
180 component = verilogName;
181 auto innerSym = cast<hw::InnerSymbolOpInterface>(op);
182 auto value = innerSym.getTargetResult();
184 target.getField(), field)))
190 auto symbolRef = cast<FlatSymbolRefAttr>(end);
191 bottomModule = symbolRef.getAttr();
192 component = StringAttr::get(
context,
"");
193 field = StringAttr::get(
context,
"");
197 targetPath = PathAttr::get(
context, modules);
201LogicalResult PathVisitor::process(PathCreateOp path) {
203 irn.symTable.lookup<hw::HierPathOp>(path.getTargetAttr().getAttr());
205 StringAttr bottomModule;
208 if (failed(processPath(path.getLoc(), hierPathOp, targetPath, bottomModule,
213 OpBuilder builder(path);
214 auto frozenPath = FrozenPathCreateOp::create(
215 builder, path.getLoc(), path.getTargetKindAttr(), path->getOperand(0),
216 targetPath, bottomModule, ref, field);
217 path.replaceAllUsesWith(frozenPath.getResult());
223LogicalResult PathVisitor::process(BasePathCreateOp path) {
225 irn.symTable.lookup<hw::HierPathOp>(path.getTargetAttr().getAttr());
227 StringAttr bottomModule;
230 if (failed(processPath(path.getLoc(), hierPathOp, targetPath, bottomModule,
235 return path->emitError(
"basepath must target an instance");
237 return path->emitError(
"basepath must not target a field");
240 OpBuilder builder(path);
241 auto frozenPath = FrozenBasePathCreateOp::create(
242 builder, path.getLoc(), path->getOperand(0), targetPath);
243 path.replaceAllUsesWith(frozenPath.getResult());
249LogicalResult PathVisitor::process(EmptyPathOp path) {
250 OpBuilder builder(path);
251 auto frozenPath = FrozenEmptyPathOp::create(builder, path.getLoc());
252 path.replaceAllUsesWith(frozenPath.getResult());
257LogicalResult PathVisitor::run(ModuleOp module) {
258 auto updatePathType = [&](Value value) {
263 for (
auto classLike : module.getOps<ClassLike>()) {
265 for (
auto arg : classLike.getBodyBlock()->getArguments())
267 auto result = classLike->walk([&](Operation *op) -> WalkResult {
268 if (
auto basePath = dyn_cast<BasePathCreateOp>(op)) {
269 if (failed(process(basePath)))
270 return WalkResult::interrupt();
271 }
else if (
auto path = dyn_cast<PathCreateOp>(op)) {
272 if (failed(process(path)))
273 return WalkResult::interrupt();
274 }
else if (
auto path = dyn_cast<EmptyPathOp>(op)) {
275 if (failed(process(path)))
276 return WalkResult::interrupt();
277 }
else if (isa<ListCreateOp, ListConcatOp, RegistryCreateOp,
278 ObjectFieldOp, UnknownValueOp>(op)) {
280 for (
auto result : op->getResults())
281 updatePathType(result);
282 }
else if (isa<ConstantOp>(op)) {
283 replacer.replaceElementsIn(op,
true,
287 return WalkResult::advance();
289 if (result.wasInterrupted())
293 classLike.replaceFieldTypes(replacer);
299struct FreezePathsPass
300 :
public circt::om::impl::FreezePathsBase<FreezePathsPass> {
301 FreezePathsPass(std::function<StringAttr(Operation *)> getOpName)
302 : getOpName(std::move(getOpName)) {}
303 void runOnOperation()
override;
305 std::function<StringAttr(Operation *)> getOpName;
309void FreezePathsPass::runOnOperation() {
310 auto module = getOperation();
311 auto &instanceGraph = getAnalysis<hw::InstanceGraph>();
312 mlir::SymbolTableCollection symbolTableCollection;
313 auto &symbolTable = getAnalysis<SymbolTable>();
314 hw::InnerSymbolTableCollection collection(module);
315 hw::InnerRefNamespace irn{symbolTable, collection};
316 if (failed(PathVisitor(instanceGraph, irn, getOpName).
run(module)))
321 std::function<StringAttr(Operation *)> getOpName) {
322 return std::make_unique<FreezePathsPass>(getOpName);
assert(baseType &&"element must be base type")
static std::unique_ptr< Context > context
static bool hasPathType(Type type)
static LogicalResult getAccessPath(Location loc, Type type, size_t fieldId, StringAttr &result)
mlir::AttrTypeReplacer makeReplacer()
static Type processType(Type type)
false return failure()
Parse an optional keyword or string and set instance into 'result'.
std::unique_ptr< mlir::Pass > createFreezePathsPass(std::function< StringAttr(Operation *)> getOpNameFallback={})
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
int run(Type[Generator] generator=CppGenerator, List[str] cmdline_args=sys.argv)