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) {
48 module = target.
instances.front()->getParentOfType<FModuleLike>();
49 auto *node = instanceGraph[module];
52 if (node->getModule() == owningModule)
58 <<
"unable to resolve path relative to owning module "
59 << owningModule.getModuleNameAttr();
62 if (!node->hasOneUse()) {
63 auto diag = emitError(loc) <<
"unable to uniquely resolve target due "
64 "to multiple instantiation";
65 for (
auto *use : node->uses())
66 diag.attachNote(use->getInstance().getLoc()) <<
"instance here";
69 node = (*node->usesBegin())->getParent();
79 SmallVector<Attribute> insts;
82 std::back_inserter(insts), [&](InstanceOp instance) {
83 return OpAnnoTarget(instance).getNLAReference(
84 namespaces[instance->getParentOfType<FModuleLike>()]);
94 result = hierPathCache.getRefFor(instAttr);
100 auto loc = unresolved.getLoc();
101 ImplicitLocOpBuilder b(loc, unresolved);
102 auto *context = b.getContext();
106 auto target = unresolved.getTarget();
111 if (target.consume_front(
"OMDeleted:")) {
113 return emitError(loc,
"OMDeleted references can not have targets");
119 auto resolved = b.create<PathOp>(targetKind, id);
120 unresolved->replaceAllUsesWith(resolved);
126 TargetKind targetKind;
127 if (target.consume_front(
"OMDontTouchedReferenceTarget")) {
128 targetKind = TargetKind::DontTouch;
129 }
else if (target.consume_front(
"OMInstanceTarget")) {
130 targetKind = TargetKind::Instance;
131 }
else if (target.consume_front(
"OMMemberInstanceTarget")) {
132 targetKind = TargetKind::MemberInstance;
133 }
else if (target.consume_front(
"OMMemberReferenceTarget")) {
134 targetKind = TargetKind::MemberReference;
135 }
else if (target.consume_front(
"OMReferenceTarget")) {
136 targetKind = TargetKind::Reference;
138 return emitError(loc)
139 <<
"unknown or missing OM reference type in target string: \""
145 if (!target.consume_front(
":"))
146 return emitError(loc,
"expected ':' in target string");
150 return emitError(loc)
151 <<
"cannot tokenize annotation path \"" << target <<
"\"";
154 auto path =
resolveEntities(*token, circuit, symbolTable, targetCache);
161 if (Type targetType = path->ref.getType()) {
162 auto fieldId = path->fieldIdx;
163 auto baseType = type_dyn_cast<FIRRTLBaseType>(targetType);
165 return emitError(loc,
"unable to target non-hardware type ")
168 if (type_isa<BundleType, FVectorType>(targetType))
169 return emitError(loc,
"unable to target aggregate type ") << targetType;
172 auto owningModule = cache.
lookup(unresolved);
173 StringRef moduleName =
"nullptr";
175 moduleName = owningModule.getModuleName();
177 return unresolved->emitError(
"path does not have a single owning module");
180 FlatSymbolRefAttr hierPathName;
181 if (failed(resolveHierPath(loc, owningModule, *path, hierPathName)))
184 auto createAnnotation = [&](FlatSymbolRefAttr hierPathName) {
189 NamedAttrList fields;
190 fields.append(
"id",
id);
193 fields.append(
"circt.nonlocal", hierPathName);
194 if (path->fieldIdx != 0)
195 fields.append(
"circt.fieldID", b.getI64IntegerAttr(path->fieldIdx));
201 Attribute annotation = createAnnotation(hierPathName);
204 auto annoTarget = path->ref;
205 auto targetAnnotations = annoTarget.getAnnotations();
206 targetAnnotations.addAnnotations({annotation});
207 if (targetKindAttr.getValue() == TargetKind::DontTouch)
208 targetAnnotations.addDontTouch();
209 annoTarget.setAnnotations(targetAnnotations);
212 auto dictAttr = cast<DictionaryAttr>(annotation);
213 auto id = cast<DistinctAttr>(dictAttr.get(
"id"));
214 auto resolved = b.create<PathOp>(targetKindAttr, id);
217 unresolved->replaceAllUsesWith(resolved);
224 SymbolTable symbolTable;
228 hw::InnerSymbolNamespaceCollection namespaces;
239 struct ResolvePathsPass
240 :
public circt::firrtl::impl::ResolvePathsBase<ResolvePathsPass> {
241 void runOnOperation()
override;
245 void ResolvePathsPass::runOnOperation() {
246 auto circuit = getOperation();
247 auto &instanceGraph = getAnalysis<InstanceGraph>();
248 PathResolver resolver(circuit, instanceGraph);
250 auto result = circuit.walk([&](UnresolvedPathOp unresolved) {
251 if (failed(resolver.resolve(cache, unresolved))) {
253 return WalkResult::interrupt();
255 return WalkResult::advance();
257 if (result.wasInterrupted())
259 markAnalysesPreserved<InstanceGraph>();
263 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.