17 #include "mlir/IR/ImplicitLocOpBuilder.h"
18 #include "mlir/Pass/Pass.h"
22 #define GEN_PASS_DEF_RESOLVEPATHS
23 #include "circt/Dialect/FIRRTL/Passes.h.inc"
27 using namespace circt;
28 using namespace firrtl;
33 : circuit(circuit), symbolTable(circuit), instanceGraph(instanceGraph),
34 instancePathCache(instanceGraph), hierPathCache(circuit, symbolTable),
35 builder(OpBuilder::atBlockBegin(circuit->getBlock())) {}
40 LogicalResult resolveHierPath(Location loc, FModuleOp owningModule,
42 FlatSymbolRefAttr &result) {
53 module = target.
instances.front()->getParentOfType<FModuleLike>();
54 auto *node = instanceGraph[module];
57 if (node->getModule() == owningModule)
63 <<
"unable to resolve path relative to owning module "
64 << owningModule.getModuleNameAttr();
67 if (!node->hasOneUse()) {
68 auto diag = emitError(loc) <<
"unable to uniquely resolve target due "
69 "to multiple instantiation";
70 for (
auto *use : node->uses())
71 diag.attachNote(use->getInstance().getLoc()) <<
"instance here";
74 node = (*node->usesBegin())->getParent();
78 SmallVector<Attribute> insts;
81 std::back_inserter(insts), [&](InstanceOp instance) {
82 return OpAnnoTarget(instance).getNLAReference(
83 namespaces[instance->getParentOfType<FModuleLike>()]);
93 result = hierPathCache.getRefFor(instAttr);
99 auto loc = unresolved.getLoc();
100 ImplicitLocOpBuilder b(loc, unresolved);
101 auto *context = b.getContext();
105 auto target = unresolved.getTarget();
110 if (target.consume_front(
"OMDeleted:")) {
112 return emitError(loc,
"OMDeleted references can not have targets");
118 auto resolved = b.create<PathOp>(targetKind, id);
119 unresolved->replaceAllUsesWith(resolved);
125 TargetKind targetKind;
126 if (target.consume_front(
"OMDontTouchedReferenceTarget")) {
127 targetKind = TargetKind::DontTouch;
128 }
else if (target.consume_front(
"OMInstanceTarget")) {
129 targetKind = TargetKind::Instance;
130 }
else if (target.consume_front(
"OMMemberInstanceTarget")) {
131 targetKind = TargetKind::MemberInstance;
132 }
else if (target.consume_front(
"OMMemberReferenceTarget")) {
133 targetKind = TargetKind::MemberReference;
134 }
else if (target.consume_front(
"OMReferenceTarget")) {
135 targetKind = TargetKind::Reference;
137 return emitError(loc)
138 <<
"unknown or missing OM reference type in target string: \""
144 if (!target.consume_front(
":"))
145 return emitError(loc,
"expected ':' in target string");
149 return emitError(loc)
150 <<
"cannot tokenize annotation path \"" << target <<
"\"";
153 auto path =
resolveEntities(*token, circuit, symbolTable, targetCache);
160 if (Type targetType = path->ref.getType()) {
161 auto fieldId = path->fieldIdx;
162 auto baseType = type_dyn_cast<FIRRTLBaseType>(targetType);
164 return emitError(loc,
"unable to target non-hardware type ")
167 if (type_isa<BundleType, FVectorType>(targetType))
168 return emitError(loc,
"unable to target aggregate type ") << targetType;
171 auto owningModule = cache.
lookup(unresolved);
172 StringRef moduleName =
"nullptr";
174 moduleName = owningModule.getModuleName();
176 return unresolved->emitError(
"path does not have a single owning module");
179 FlatSymbolRefAttr hierPathName;
180 if (failed(resolveHierPath(loc, owningModule, *path, hierPathName)))
183 auto createAnnotation = [&](FlatSymbolRefAttr hierPathName) {
188 NamedAttrList fields;
189 fields.append(
"id",
id);
192 fields.append(
"circt.nonlocal", hierPathName);
193 if (path->fieldIdx != 0)
194 fields.append(
"circt.fieldID", b.getI64IntegerAttr(path->fieldIdx));
200 Attribute annotation = createAnnotation(hierPathName);
203 auto annoTarget = path->ref;
204 auto targetAnnotations = annoTarget.getAnnotations();
205 targetAnnotations.addAnnotations({annotation});
206 if (targetKindAttr.getValue() == TargetKind::DontTouch)
207 targetAnnotations.addDontTouch();
208 annoTarget.setAnnotations(targetAnnotations);
211 auto dictAttr = cast<DictionaryAttr>(annotation);
212 auto id = cast<DistinctAttr>(dictAttr.get(
"id"));
213 auto resolved = b.create<PathOp>(targetKindAttr, id);
216 unresolved->replaceAllUsesWith(resolved);
223 SymbolTable symbolTable;
227 hw::InnerSymbolNamespaceCollection namespaces;
238 struct ResolvePathsPass
239 :
public circt::firrtl::impl::ResolvePathsBase<ResolvePathsPass> {
240 void runOnOperation()
override;
244 void ResolvePathsPass::runOnOperation() {
245 auto circuit = getOperation();
246 auto &instanceGraph = getAnalysis<InstanceGraph>();
247 PathResolver resolver(circuit, instanceGraph);
249 auto result = circuit.walk([&](UnresolvedPathOp unresolved) {
250 if (failed(resolver.resolve(cache, unresolved))) {
252 return WalkResult::interrupt();
254 return WalkResult::advance();
256 if (result.wasInterrupted())
258 markAnalysesPreserved<InstanceGraph>();
262 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.