17 #include "mlir/Pass/Pass.h"
21 #define GEN_PASS_DEF_RESOLVEPATHS
22 #include "circt/Dialect/FIRRTL/Passes.h.inc"
26 using namespace circt;
27 using namespace firrtl;
32 : circuit(circuit), symbolTable(circuit), instanceGraph(instanceGraph),
33 instancePathCache(instanceGraph), hierPathCache(circuit, symbolTable),
34 builder(OpBuilder::atBlockBegin(circuit->getBlock())) {}
39 LogicalResult resolveHierPath(Location loc, FModuleOp owningModule,
41 FlatSymbolRefAttr &result) {
52 module = target.
instances.front()->getParentOfType<FModuleLike>();
53 auto *node = instanceGraph[module];
56 if (node->getModule() == owningModule)
62 <<
"unable to resolve path relative to owning module "
63 << owningModule.getModuleNameAttr();
66 if (!node->hasOneUse()) {
67 auto diag = emitError(loc) <<
"unable to uniquely resolve target due "
68 "to multiple instantiation";
69 for (
auto *use : node->uses())
70 diag.attachNote(use->getInstance().getLoc()) <<
"instance here";
73 node = (*node->usesBegin())->getParent();
77 SmallVector<Attribute> insts;
80 std::back_inserter(insts), [&](InstanceOp instance) {
81 return OpAnnoTarget(instance).getNLAReference(
82 namespaces[instance->getParentOfType<FModuleLike>()]);
92 result = hierPathCache.getRefFor(instAttr);
98 auto loc = unresolved.getLoc();
99 ImplicitLocOpBuilder b(loc, unresolved);
100 auto *context = b.getContext();
104 auto target = unresolved.getTarget();
109 if (target.consume_front(
"OMDeleted:")) {
111 return emitError(loc,
"OMDeleted references can not have targets");
117 auto resolved = b.create<PathOp>(targetKind, id);
118 unresolved->replaceAllUsesWith(resolved);
124 TargetKind targetKind;
125 if (target.consume_front(
"OMDontTouchedReferenceTarget")) {
126 targetKind = TargetKind::DontTouch;
127 }
else if (target.consume_front(
"OMInstanceTarget")) {
128 targetKind = TargetKind::Instance;
129 }
else if (target.consume_front(
"OMMemberInstanceTarget")) {
130 targetKind = TargetKind::MemberInstance;
131 }
else if (target.consume_front(
"OMMemberReferenceTarget")) {
132 targetKind = TargetKind::MemberReference;
133 }
else if (target.consume_front(
"OMReferenceTarget")) {
134 targetKind = TargetKind::Reference;
136 return emitError(loc)
137 <<
"unknown or missing OM reference type in target string: \""
143 if (!target.consume_front(
":"))
144 return emitError(loc,
"expected ':' in target string");
148 return emitError(loc)
149 <<
"cannot tokenize annotation path \"" << target <<
"\"";
152 auto path =
resolveEntities(*token, circuit, symbolTable, targetCache);
159 if (Type targetType = path->ref.getType()) {
160 auto fieldId = path->fieldIdx;
161 auto baseType = type_dyn_cast<FIRRTLBaseType>(targetType);
163 return emitError(loc,
"unable to target non-hardware type ")
166 if (type_isa<BundleType, FVectorType>(targetType))
167 return emitError(loc,
"unable to target aggregate type ") << targetType;
170 auto owningModule = cache.
lookup(unresolved);
171 StringRef moduleName =
"nullptr";
173 moduleName = owningModule.getModuleName();
175 return unresolved->emitError(
"path does not have a single owning module");
178 FlatSymbolRefAttr hierPathName;
179 if (failed(resolveHierPath(loc, owningModule, *path, hierPathName)))
182 auto createAnnotation = [&](FlatSymbolRefAttr hierPathName) {
187 NamedAttrList fields;
188 fields.append(
"id",
id);
191 fields.append(
"circt.nonlocal", hierPathName);
192 if (path->fieldIdx != 0)
193 fields.append(
"circt.fieldID", b.getI64IntegerAttr(path->fieldIdx));
199 Attribute annotation = createAnnotation(hierPathName);
202 auto annoTarget = path->ref;
203 auto targetAnnotations = annoTarget.getAnnotations();
204 targetAnnotations.addAnnotations({annotation});
205 if (targetKindAttr.getValue() == TargetKind::DontTouch)
206 targetAnnotations.addDontTouch();
207 annoTarget.setAnnotations(targetAnnotations);
210 auto dictAttr = cast<DictionaryAttr>(annotation);
211 auto id = cast<DistinctAttr>(dictAttr.get(
"id"));
212 auto resolved = b.create<PathOp>(targetKindAttr, id);
215 unresolved->replaceAllUsesWith(resolved);
222 SymbolTable symbolTable;
226 hw::InnerSymbolNamespaceCollection namespaces;
237 struct ResolvePathsPass
238 :
public circt::firrtl::impl::ResolvePathsBase<ResolvePathsPass> {
239 void runOnOperation()
override;
243 void ResolvePathsPass::runOnOperation() {
244 auto circuit = getOperation();
245 auto &instanceGraph = getAnalysis<InstanceGraph>();
246 PathResolver resolver(circuit, instanceGraph);
248 auto result = circuit.walk([&](UnresolvedPathOp unresolved) {
249 if (failed(resolver.resolve(cache, unresolved))) {
251 return WalkResult::interrupt();
253 return WalkResult::advance();
255 if (result.wasInterrupted())
257 markAnalysesPreserved<InstanceGraph>();
261 return std::make_unique<ResolvePathsPass>();
This graph tracks modules and where they are instantiated.
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
std::unique_ptr< mlir::Pass > createResolvePathsPass()
std::optional< AnnoPathValue > resolveEntities(TokenAnnoTarget path, CircuitOp circuit, SymbolTable &symTbl, CircuitTargetCache &cache)
Convert a parsed target string to a resolved target structure.
std::optional< TokenAnnoTarget > tokenizePath(StringRef origTarget)
Parse a FIRRTL annotation path into its constituent parts.
::mlir::Type getFinalTypeByFieldID(Type type, uint64_t fieldID)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
SmallVector< InstanceOp > instances
FModuleLike getModule() const
Get the parent module of the target.
Cache AnnoTargets for a circuit's modules, walked as needed.
A cache of existing HierPathOps, mostly used to facilitate HierPathOp reuse.
This implements an analysis to determine which module owns a given path operation.
FModuleOp lookup(ClassOp classOp)
Return this operation's owning module.
A data structure that caches and provides absolute paths to module instances in the IR.