CIRCT 23.0.0git
Loading...
Searching...
No Matches
LowerXMR.cpp
Go to the documentation of this file.
1//===- LowerXMR.cpp - FIRRTL Lower to XMR -----------------------*- 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// This file implements FIRRTL XMR Lowering.
10//
11//===----------------------------------------------------------------------===//
12
22#include "mlir/IR/ImplicitLocOpBuilder.h"
23#include "mlir/Pass/Pass.h"
24#include "llvm/ADT/BitVector.h"
25#include "llvm/ADT/DenseMap.h"
26#include "llvm/ADT/EquivalenceClasses.h"
27#include "llvm/ADT/PostOrderIterator.h"
28#include "llvm/Support/Debug.h"
29
30#define DEBUG_TYPE "firrtl-lower-xmr"
31
32namespace circt {
33namespace firrtl {
34#define GEN_PASS_DEF_LOWERXMR
35#include "circt/Dialect/FIRRTL/Passes.h.inc"
36} // namespace firrtl
37} // namespace circt
38
39using namespace circt;
40using namespace firrtl;
41using hw::InnerRefAttr;
42
43/// The LowerXMRPass will replace every RefResolveOp with an XMR encoded within
44/// a verbatim expr op. This also removes every RefType port from the modules
45/// and corresponding instances. This is a dataflow analysis over a very
46/// constrained RefType. Domain of the dataflow analysis is the set of all
47/// RefSendOps. It computes an interprocedural reaching definitions (of
48/// RefSendOp) analysis. Essentially every RefType value must be mapped to one
49/// and only one RefSendOp. The analysis propagates the dataflow from every
50/// RefSendOp to every value of RefType across modules. The RefResolveOp is the
51/// final leaf into which the dataflow must reach.
52///
53/// Since there can be multiple readers, multiple RefResolveOps can be reachable
54/// from a single RefSendOp. To support multiply instantiated modules and
55/// multiple readers, it is essential to track the path to the RefSendOp, other
56/// than just the RefSendOp. For example, if there exists a wire `xmr_wire` in
57/// module `Foo`, the algorithm needs to support generating Top.Bar.Foo.xmr_wire
58/// and Top.Foo.xmr_wire and Top.Zoo.Foo.xmr_wire for different instance paths
59/// that exist in the circuit.
60
61namespace {
62struct XMRNode {
63 using NextNodeOnPath = std::optional<size_t>;
64 using SymOrIndexOp = PointerUnion<Attribute, Operation *>;
65 SymOrIndexOp info;
66 NextNodeOnPath next;
67};
68[[maybe_unused]] llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
69 const XMRNode &node) {
70 os << "node(";
71 if (auto attr = dyn_cast<Attribute>(node.info))
72 os << "path=" << attr;
73 else {
74 auto subOp = cast<RefSubOp>(cast<Operation *>(node.info));
75 os << "index=" << subOp.getIndex() << " (-> " << subOp.getType() << ")";
76 }
77 os << ", next=" << node.next << ")";
78 return os;
79}
80
81/// Track information about operations being created in a module. This is used
82/// to generate more compact code and reuse operations where possible.
83class ModuleState {
84
85public:
86 ModuleState(FModuleOp &moduleOp) : body(moduleOp.getBodyBlock()) {}
87
88 /// Return the existing XMRRefOp for this type, symbol, and suffix for this
89 /// module. Otherwise, create a new one. The first XMRRefOp will be created
90 /// at the beginning of the module. Subsequent XMRRefOps will be created
91 /// immediately following the first one.
92 Value getOrCreateXMRRefOp(Type type, FlatSymbolRefAttr symbol,
93 StringAttr suffix, ImplicitLocOpBuilder &builder) {
94 // Return the saved XMRRefOp.
95 auto it = xmrRefCache.find({type, symbol, suffix});
96 if (it != xmrRefCache.end())
97 return it->getSecond();
98
99 // Create a new XMRRefOp.
100 OpBuilder::InsertionGuard guard(builder);
101 if (xmrRefPoint.isSet())
102 builder.restoreInsertionPoint(xmrRefPoint);
103 else
104 builder.setInsertionPointToStart(body);
105
106 Value xmr = XMRRefOp::create(builder, type, symbol, suffix);
107 xmrRefCache.insert({{type, symbol, suffix}, xmr});
108
109 xmrRefPoint = builder.saveInsertionPoint();
110 return xmr;
111 };
112
113private:
114 /// The module's body. This is used to set the insertion point for the first
115 /// created operation.
116 Block *body;
117
118 /// Map used to know if we created this XMRRefOp before.
119 DenseMap<std::tuple<Type, SymbolRefAttr, StringAttr>, Value> xmrRefCache;
120
121 /// The saved insertion point for XMRRefOps.
122 OpBuilder::InsertPoint xmrRefPoint;
123};
124} // end anonymous namespace
125
127
128 void runOnOperation() override {
129 // Populate a CircuitNamespace that can be used to generate unique
130 // circuit-level symbols.
131 CircuitNamespace ns(getOperation());
132 circuitNamespace = &ns;
133
135 &ns, OpBuilder::InsertPoint(getOperation().getBodyBlock(),
136 getOperation().getBodyBlock()->begin()));
137 hierPathCache = &pc;
138
139 llvm::EquivalenceClasses<Value> eq;
140 dataFlowClasses = &eq;
141
142 InstanceGraph &instanceGraph = getAnalysis<InstanceGraph>();
143 SmallVector<RefResolveOp> resolveOps;
144 SmallVector<RefSubOp> indexingOps;
145 SmallVector<Operation *> forceAndReleaseOps;
146 // The dataflow function, that propagates the reachable RefSendOp across
147 // RefType Ops.
148 auto transferFunc = [&](Operation *op) -> LogicalResult {
149 return TypeSwitch<Operation *, LogicalResult>(op)
150 .Case<RefSendOp>([&](RefSendOp send) {
151 // Get a reference to the actual signal to which the XMR will be
152 // generated.
153 Value xmrDef = send.getBase();
154 if (isZeroWidth(send.getType().getType())) {
155 markForRemoval(send);
156 return success();
157 }
158
159 if (auto verbExpr = xmrDef.getDefiningOp<VerbatimExprOp>())
160 if (verbExpr.getSymbolsAttr().empty() && verbExpr->hasOneUse()) {
161 // This represents the internal path into a module. For
162 // generating the correct XMR, no node can be created in this
163 // module. Create a null InnerRef and ensure the hierarchical
164 // path ends at the parent that instantiates this module.
165 auto inRef = InnerRefAttr();
166 auto ind = addReachingSendsEntry(send.getResult(), inRef);
167 xmrPathSuffix[ind] = verbExpr.getText();
168 markForRemoval(verbExpr);
169 markForRemoval(send);
170 return success();
171 }
172
173 // If the value is a block argument (port), add an inner symbol
174 // directly to the port instead of creating a node.
175 if (isa<BlockArgument>(xmrDef)) {
176 addReachingSendsEntry(send.getResult(), getInnerRefTo(xmrDef));
177 markForRemoval(send);
178 return success();
179 }
180
181 // Get an InnerRefAttr to the value being sent.
182 auto *xmrDefOp = xmrDef.getDefiningOp();
183
184 // Add the symbol directly if the operation targets a specific
185 // result. This ensures that operations like InstanceOp and MemOp,
186 // which have inner symbols that target the operation itself (not a
187 // specific result), still get nodes created to distinguish which
188 // result is being referenced.
189 if (auto innerSymOp =
190 dyn_cast_or_null<hw::InnerSymbolOpInterface>(xmrDefOp))
191 if (innerSymOp.getTargetResultIndex()) {
192 addReachingSendsEntry(send.getResult(), getInnerRefTo(xmrDef));
193 markForRemoval(send);
194 return success();
195 }
196
197 // The operation cannot support an inner symbol, or it has
198 // multiple results and doesn't target a specific result, so
199 // create a node and replace all uses of the original value with
200 // the node (except the node itself).
201 ImplicitLocOpBuilder b(xmrDef.getLoc(), &getContext());
202 b.setInsertionPointAfterValue(xmrDef);
203 SmallString<32> opName;
204 auto nameKind = NameKindEnum::DroppableName;
205
206 if (auto [name, rootKnown] = getFieldName(
207 getFieldRefFromValue(xmrDef, /*lookThroughCasts=*/true),
208 /*nameSafe=*/true);
209 rootKnown) {
210 opName = name + "_probe";
211 nameKind = NameKindEnum::InterestingName;
212 } else if (xmrDefOp) {
213 // Inspect "name" directly for ops that aren't named by above.
214 // (e.g., firrtl.constant)
215 if (auto name = xmrDefOp->getAttrOfType<StringAttr>("name")) {
216 (Twine(name.strref()) + "_probe").toVector(opName);
217 nameKind = NameKindEnum::InterestingName;
218 }
219 }
220 auto node = NodeOp::create(b, xmrDef, opName, nameKind);
221 auto newValue = node.getResult();
222 // Replace all uses except the node itself and except when the value
223 // is the destination of a connect (operand 0). We need to preserve
224 // connect destinations to maintain proper flow semantics.
225 xmrDef.replaceUsesWithIf(newValue, [&](OpOperand &operand) {
226 if (operand.getOwner() == node.getOperation())
227 return false;
228 if (isa<FConnectLike>(operand.getOwner()) &&
229 operand.getOperandNumber() == 0)
230 return false;
231 return true;
232 });
233 xmrDef = newValue;
234
235 // Create a new entry for this RefSendOp. The path is currently
236 // local.
237 addReachingSendsEntry(send.getResult(), getInnerRefTo(xmrDef));
238 markForRemoval(send);
239 return success();
240 })
241 .Case<RWProbeOp>([&](RWProbeOp rwprobe) {
242 if (!isZeroWidth(rwprobe.getType().getType()))
243 addReachingSendsEntry(rwprobe.getResult(), rwprobe.getTarget());
244 markForRemoval(rwprobe);
245 return success();
246 })
247 .Case<MemOp>([&](MemOp mem) {
248 // MemOp can produce debug ports of RefType. Each debug port
249 // represents the RefType for the corresponding register of the
250 // memory. Since the memory is not yet generated the register name
251 // is assumed to be "Memory". Note that MemOp creates RefType
252 // without a RefSend.
253 for (const auto &res : llvm::enumerate(mem.getResults()))
254 if (isa<RefType>(mem.getResult(res.index()).getType())) {
255 auto inRef = getInnerRefTo(mem);
256 auto ind = addReachingSendsEntry(res.value(), inRef);
257 xmrPathSuffix[ind] = "Memory";
258 // Just node that all the debug ports of memory must be removed.
259 // So this does not record the port index.
260 refPortsToRemoveMap[mem].resize(1);
261 }
262 return success();
263 })
264 .Case<InstanceOp>(
265 [&](auto inst) { return handleInstanceOp(inst, instanceGraph); })
266 .Case<FConnectLike>([&](FConnectLike connect) {
267 // Ignore BaseType.
268 if (!isa<RefType>(connect.getSrc().getType()))
269 return success();
270 markForRemoval(connect);
271 if (isZeroWidth(
272 type_cast<RefType>(connect.getSrc().getType()).getType()))
273 return success();
274 // Merge the dataflow classes of destination into the source of the
275 // Connect. This handles two cases:
276 // 1. If the dataflow at the source is known, then the
277 // destination is also inferred. By merging the dataflow class of
278 // destination with source, every value reachable from the
279 // destination automatically infers a reaching RefSend.
280 // 2. If dataflow at source is unkown, then just record that both
281 // source and destination will have the same dataflow information.
282 // Later in the pass when the reaching RefSend is inferred at the
283 // leader of the dataflowClass, then we automatically infer the
284 // dataflow at this connect and every value reachable from the
285 // destination.
286 dataFlowClasses->unionSets(connect.getSrc(), connect.getDest());
287 return success();
288 })
289 .Case<RefSubOp>([&](RefSubOp op) -> LogicalResult {
290 markForRemoval(op);
291 if (isZeroWidth(op.getType().getType()))
292 return success();
293
294 // Enqueue for processing after visiting other operations.
295 indexingOps.push_back(op);
296 return success();
297 })
298 .Case<RefResolveOp>([&](RefResolveOp resolve) {
299 // Merge dataflow, under the same conditions as above for Connect.
300 // 1. If dataflow at the resolve.getRef is known, propagate that to
301 // the result. This is true for downward scoped XMRs, that is,
302 // RefSendOp must be visited before the corresponding RefResolveOp
303 // is visited.
304 // 2. Else, just record that both result and ref should have the
305 // same reaching RefSend. This condition is true for upward scoped
306 // XMRs. That is, RefResolveOp can be visited before the
307 // corresponding RefSendOp is recorded.
308
310 if (!isZeroWidth(resolve.getType()))
311 dataFlowClasses->unionSets(resolve.getRef(), resolve.getResult());
312 resolveOps.push_back(resolve);
313 return success();
314 })
315 .Case<RefCastOp>([&](RefCastOp op) {
316 markForRemoval(op);
317 if (!isZeroWidth(op.getType().getType()))
318 dataFlowClasses->unionSets(op.getInput(), op.getResult());
319 return success();
320 })
321 .Case<Forceable>([&](Forceable op) {
322 // Handle declarations containing refs as "data".
323 if (type_isa<RefType>(op.getDataRaw().getType())) {
324 markForRemoval(op);
325 return success();
326 }
327
328 // Otherwise, if forceable track the rwprobe result.
329 if (!op.isForceable() || op.getDataRef().use_empty() ||
330 isZeroWidth(op.getDataType()))
331 return success();
332
333 addReachingSendsEntry(op.getDataRef(), getInnerRefTo(op));
334 return success();
335 })
336 .Case<RefForceOp, RefForceInitialOp, RefReleaseOp,
337 RefReleaseInitialOp>([&](auto op) {
338 forceAndReleaseOps.push_back(op);
339 return success();
340 })
341 .Default([&](auto) { return success(); });
342 };
343
344 SmallVector<FModuleOp> publicModules;
345
346 // Traverse the modules in post order.
347 auto result = instanceGraph.walkPostOrder([&](auto &node) -> LogicalResult {
348 auto module = dyn_cast<FModuleOp>(*node.getModule());
349 if (!module)
350 return success();
351 LLVM_DEBUG(llvm::dbgs()
352 << "Traversing module:" << module.getModuleNameAttr() << "\n");
353
354 moduleStates.insert({module, ModuleState(module)});
355
356 if (module.isPublic())
357 publicModules.push_back(module);
358
359 auto result = module.walk([&](Operation *op) {
360 if (transferFunc(op).failed())
361 return WalkResult::interrupt();
362 return WalkResult::advance();
363 });
364
365 if (result.wasInterrupted())
366 return failure();
367
368 // Since we walk operations pre-order and not along dataflow edges,
369 // ref.sub may not be resolvable when we encounter them (they're not
370 // just unification). This can happen when refs go through an output
371 // port or input instance result and back into the design. Handle these
372 // by walking them, resolving what we can, until all are handled or
373 // nothing can be resolved.
374 while (!indexingOps.empty()) {
375 // Grab the set of unresolved ref.sub's.
376 decltype(indexingOps) worklist;
377 worklist.swap(indexingOps);
378
379 for (auto op : worklist) {
380 auto inputEntry =
381 getRemoteRefSend(op.getInput(), /*errorIfNotFound=*/false);
382 // If we can't resolve, add back and move on.
383 if (!inputEntry)
384 indexingOps.push_back(op);
385 else
386 addReachingSendsEntry(op.getResult(), op.getOperation(),
387 inputEntry);
388 }
389 // If nothing was resolved, give up.
390 if (worklist.size() == indexingOps.size()) {
391 auto op = worklist.front();
392 getRemoteRefSend(op.getInput());
393 op.emitError(
394 "indexing through probe of unknown origin (input probe?)")
395 .attachNote(op.getInput().getLoc())
396 .append("indexing through this reference");
397 return failure();
398 }
399 }
400
401 // Record all the RefType ports to be removed later.
402 size_t numPorts = module.getNumPorts();
403 for (size_t portNum = 0; portNum < numPorts; ++portNum)
404 if (isa<RefType>(module.getPortType(portNum)))
405 setPortToRemove(module, portNum, numPorts);
406
407 return success();
408 });
409 if (failed(result))
410 return signalPassFailure();
411
412 LLVM_DEBUG({
413 for (const auto &I :
414 *dataFlowClasses) { // Iterate over all of the equivalence sets.
415 if (!I->isLeader())
416 continue; // Ignore non-leader sets.
417 // Print members in this set.
418 llvm::interleave(dataFlowClasses->members(*I), llvm::dbgs(), "\n");
419 llvm::dbgs() << "\n dataflow at leader::" << I->getData() << "\n =>";
420 auto iter = dataflowAt.find(I->getData());
421 if (iter != dataflowAt.end()) {
422 for (auto init = refSendPathList[iter->getSecond()]; init.next;
423 init = refSendPathList[*init.next])
424 llvm::dbgs() << "\n " << init;
425 }
426 llvm::dbgs() << "\n Done\n"; // Finish set.
427 }
428 });
429 for (auto refResolve : resolveOps)
430 if (handleRefResolve(refResolve).failed())
431 return signalPassFailure();
432 for (auto *op : forceAndReleaseOps)
433 if (failed(handleForceReleaseOp(op)))
434 return signalPassFailure();
435 for (auto module : publicModules) {
436 if (failed(handlePublicModuleRefPorts(module)))
437 return signalPassFailure();
438 }
440
441 // Clean up
442 moduleNamespaces.clear();
443 visitedModules.clear();
444 dataflowAt.clear();
445 refSendPathList.clear();
446 dataFlowClasses = nullptr;
447 refPortsToRemoveMap.clear();
448 opsToRemove.clear();
449 xmrPathSuffix.clear();
450 circuitNamespace = nullptr;
451 hierPathCache = nullptr;
452 }
453
454 /// Generate the ABI ref_<module> prefix string into `prefix`.
455 void getRefABIPrefix(FModuleLike mod, SmallVectorImpl<char> &prefix) {
456 auto modName = mod.getModuleName();
457 if (auto ext = dyn_cast<FExtModuleOp>(*mod))
458 modName = ext.getExtModuleName();
459 (Twine("ref_") + modName).toVector(prefix);
460 }
461
462 /// Get full macro name as StringAttr for the specified ref port.
463 /// Uses existing 'prefix', optionally preprends the backtick character.
464 StringAttr getRefABIMacroForPort(FModuleLike mod, size_t portIndex,
465 const Twine &prefix, bool backTick = false) {
466 return StringAttr::get(&getContext(), Twine(backTick ? "`" : "") + prefix +
467 "_" + mod.getPortName(portIndex));
468 }
469
470 LogicalResult resolveReferencePath(mlir::TypedValue<RefType> refVal,
471 ImplicitLocOpBuilder builder,
472 mlir::FlatSymbolRefAttr &ref,
473 SmallString<128> &stringLeaf) {
474 assert(stringLeaf.empty());
475
476 auto remoteOpPath = getRemoteRefSend(refVal);
477 if (!remoteOpPath)
478 return failure();
479 SmallVector<Attribute> refSendPath;
480 SmallVector<RefSubOp> indexing;
481 size_t lastIndex;
482 while (remoteOpPath) {
483 lastIndex = *remoteOpPath;
484 auto entr = refSendPathList[*remoteOpPath];
485 if (entr.info)
486 TypeSwitch<XMRNode::SymOrIndexOp>(entr.info)
487 .Case<Attribute>([&](auto attr) {
488 // If the path is a singular verbatim expression, the attribute of
489 // the send path list entry will be null.
490 if (attr)
491 refSendPath.push_back(attr);
492 })
493 .Case<Operation *>(
494 [&](auto *op) { indexing.push_back(cast<RefSubOp>(op)); });
495 remoteOpPath = entr.next;
496 }
497 auto iter = xmrPathSuffix.find(lastIndex);
498
499 // If this xmr has a suffix string (internal path into a module, that is not
500 // yet generated).
501 if (iter != xmrPathSuffix.end()) {
502 if (!refSendPath.empty())
503 stringLeaf.append(".");
504 stringLeaf.append(iter->getSecond());
505 }
506
507 assert(!(refSendPath.empty() && stringLeaf.empty()) &&
508 "nothing to index through");
509
510 // All indexing done as the ref is plumbed around indexes through
511 // the target/referent, not the current point of the path which
512 // describes how to access the referent we're indexing through.
513 // Above we gathered all indexing operations, so now append them
514 // to the path (after any relevant `xmrPathSuffix`) to reach
515 // the target element.
516 // Generating these strings here (especially if ref is sent
517 // out from a different design) is fragile but should get this
518 // working well enough while sorting out how to do this better.
519 // Some discussion of this can be found here:
520 // https://github.com/llvm/circt/pull/5551#discussion_r1258908834
521 for (auto subOp : llvm::reverse(indexing)) {
522 TypeSwitch<FIRRTLBaseType>(subOp.getInput().getType().getType())
523 .Case<FVectorType, OpenVectorType>([&](auto vecType) {
524 (Twine("[") + Twine(subOp.getIndex()) + "]").toVector(stringLeaf);
525 })
526 .Case<BundleType, OpenBundleType>([&](auto bundleType) {
527 auto fieldName = bundleType.getElementName(subOp.getIndex());
528 stringLeaf.append({".", fieldName});
529 });
530 }
531
532 if (!refSendPath.empty())
533 // Compute the HierPathOp that stores the path.
534 ref = FlatSymbolRefAttr::get(
535 hierPathCache
536 ->getOrCreatePath(builder.getArrayAttr(refSendPath),
537 builder.getLoc())
538 .getSymNameAttr());
539
540 return success();
541 }
542
543 LogicalResult resolveReference(mlir::TypedValue<RefType> refVal,
544 ImplicitLocOpBuilder &builder,
545 FlatSymbolRefAttr &ref, StringAttr &xmrAttr) {
546 auto remoteOpPath = getRemoteRefSend(refVal);
547 if (!remoteOpPath)
548 return failure();
549
550 SmallString<128> xmrString;
551 if (failed(resolveReferencePath(refVal, builder, ref, xmrString)))
552 return failure();
553 xmrAttr =
554 xmrString.empty() ? StringAttr{} : builder.getStringAttr(xmrString);
555
556 return success();
557 }
558
559 // Replace the Force/Release's ref argument with a resolved XMRRef.
560 LogicalResult handleForceReleaseOp(Operation *op) {
561 return TypeSwitch<Operation *, LogicalResult>(op)
562 .Case<RefForceOp, RefForceInitialOp, RefReleaseOp, RefReleaseInitialOp>(
563 [&](auto op) {
564 // Drop if zero-width target.
565 auto destType = op.getDest().getType();
566 if (isZeroWidth(destType.getType())) {
567 op.erase();
568 return success();
569 }
570
571 ImplicitLocOpBuilder builder(op.getLoc(), op);
572 FlatSymbolRefAttr ref;
573 StringAttr str;
574 if (failed(resolveReference(op.getDest(), builder, ref, str)))
575 return failure();
576
577 Value xmr =
578 moduleStates.find(op->template getParentOfType<FModuleOp>())
579 ->getSecond()
580 .getOrCreateXMRRefOp(destType, ref, str, builder);
581 op.getDestMutable().assign(xmr);
582 return success();
583 })
584 .Default([](auto *op) {
585 return op->emitError("unexpected operation kind");
586 });
587 }
588
589 // Replace the RefResolveOp with verbatim op representing the XMR.
590 LogicalResult handleRefResolve(RefResolveOp resolve) {
591 auto resWidth = getBitWidth(resolve.getType());
592 if (resWidth.has_value() && *resWidth == 0) {
593 // Donot emit 0 width XMRs, replace it with constant 0.
594 ImplicitLocOpBuilder builder(resolve.getLoc(), resolve);
595 auto zeroUintType = UIntType::get(builder.getContext(), 0);
596 auto zeroC = builder.createOrFold<BitCastOp>(
597 resolve.getType(), ConstantOp::create(builder, zeroUintType,
598 getIntZerosAttr(zeroUintType)));
599 resolve.getResult().replaceAllUsesWith(zeroC);
600 return success();
601 }
602
603 FlatSymbolRefAttr ref;
604 StringAttr str;
605 ImplicitLocOpBuilder builder(resolve.getLoc(), resolve);
606 if (failed(resolveReference(resolve.getRef(), builder, ref, str)))
607 return failure();
608
609 Value result = XMRDerefOp::create(builder, resolve.getType(), ref, str);
610 resolve.getResult().replaceAllUsesWith(result);
611 return success();
612 }
613
614 void setPortToRemove(Operation *op, size_t index, size_t numPorts) {
615 if (refPortsToRemoveMap[op].size() < numPorts)
616 refPortsToRemoveMap[op].resize(numPorts);
617 refPortsToRemoveMap[op].set(index);
618 }
619
620 // Propagate the reachable RefSendOp across modules.
621 LogicalResult handleInstanceOp(InstanceOp inst,
622 InstanceGraph &instanceGraph) {
623 Operation *mod = inst.getReferencedModule(instanceGraph);
624 if (auto extRefMod = dyn_cast<FExtModuleOp>(mod)) {
625 auto numPorts = inst.getNumResults();
626 SmallString<128> circuitRefPrefix;
627
628 /// Get the resolution string for this ref-type port.
629 auto getPath = [&](size_t portNo) {
630 // Otherwise, we're using the ref ABI. Generate the prefix string
631 // and return the macro for the specified port.
632 if (circuitRefPrefix.empty())
633 getRefABIPrefix(extRefMod, circuitRefPrefix);
634
635 return getRefABIMacroForPort(extRefMod, portNo, circuitRefPrefix, true);
636 };
637
638 for (const auto &res : llvm::enumerate(inst.getResults())) {
639 if (!isa<RefType>(inst.getResult(res.index()).getType()))
640 continue;
641
642 auto inRef = getInnerRefTo(inst);
643 auto ind = addReachingSendsEntry(res.value(), inRef);
644
645 xmrPathSuffix[ind] = getPath(res.index());
646 // The instance result and module port must be marked for removal.
647 setPortToRemove(inst, res.index(), numPorts);
648 setPortToRemove(extRefMod, res.index(), numPorts);
649 }
650 return success();
651 }
652 auto refMod = dyn_cast<FModuleOp>(mod);
653 bool multiplyInstantiated = !visitedModules.insert(refMod).second;
654 for (size_t portNum = 0, numPorts = inst.getNumResults();
655 portNum < numPorts; ++portNum) {
656 auto instanceResult = inst.getResult(portNum);
657 if (!isa<RefType>(instanceResult.getType()))
658 continue;
659 if (!refMod)
660 return inst.emitOpError("cannot lower ext modules with RefType ports");
661 // Reference ports must be removed.
662 setPortToRemove(inst, portNum, numPorts);
663 // Drop the dead-instance-ports.
664 if (instanceResult.use_empty() ||
665 isZeroWidth(type_cast<RefType>(instanceResult.getType()).getType()))
666 continue;
667 auto refModuleArg = refMod.getArgument(portNum);
668 if (inst.getPortDirection(portNum) == Direction::Out) {
669 // For output instance ports, the dataflow is into this module.
670 // Get the remote RefSendOp, that flows through the module ports.
671 // If dataflow at remote module argument does not exist, error out.
672 auto remoteOpPath = getRemoteRefSend(refModuleArg);
673 if (!remoteOpPath)
674 return failure();
675 // Get the path to reaching refSend at the referenced module argument.
676 // Now append this instance to the path to the reaching refSend.
677 addReachingSendsEntry(instanceResult, getInnerRefTo(inst),
678 remoteOpPath);
679 } else {
680 // For input instance ports, the dataflow is into the referenced module.
681 // Input RefType port implies, generating an upward scoped XMR.
682 // No need to add the instance context, since downward reference must be
683 // through single instantiated modules.
684 if (multiplyInstantiated)
685 return refMod.emitOpError(
686 "multiply instantiated module with input RefType port '")
687 << refMod.getPortName(portNum) << "'";
688 dataFlowClasses->unionSets(
689 dataFlowClasses->getOrInsertLeaderValue(refModuleArg),
690 dataFlowClasses->getOrInsertLeaderValue(instanceResult));
691 }
692 }
693 return success();
694 }
695
696 LogicalResult handlePublicModuleRefPorts(FModuleOp module) {
697 auto *body = getOperation().getBodyBlock();
698
699 // Find all the output reference ports.
700 SmallString<128> circuitRefPrefix;
701 SmallVector<std::tuple<StringAttr, StringAttr, ArrayAttr>> ports;
702 auto declBuilder =
703 ImplicitLocOpBuilder::atBlockBegin(module.getLoc(), body);
704 for (size_t portIndex = 0, numPorts = module.getNumPorts();
705 portIndex != numPorts; ++portIndex) {
706 auto refType = type_dyn_cast<RefType>(module.getPortType(portIndex));
707 if (!refType || isZeroWidth(refType.getType()) ||
708 module.getPortDirection(portIndex) != Direction::Out)
709 continue;
710 auto portValue =
711 cast<mlir::TypedValue<RefType>>(module.getArgument(portIndex));
712 mlir::FlatSymbolRefAttr ref;
713 SmallString<128> stringLeaf;
714 if (failed(resolveReferencePath(portValue, declBuilder, ref, stringLeaf)))
715 return failure();
716
717 SmallString<128> formatString;
718 if (ref)
719 formatString += "{{0}}";
720 formatString += stringLeaf;
721
722 // Insert a macro with the format:
723 // ref_<module-name>_<ref-name> <path>
724 if (circuitRefPrefix.empty())
725 getRefABIPrefix(module, circuitRefPrefix);
726 auto macroName =
727 getRefABIMacroForPort(module, portIndex, circuitRefPrefix);
728 sv::MacroDeclOp::create(declBuilder, macroName, ArrayAttr(),
729 StringAttr());
730 ports.emplace_back(macroName, declBuilder.getStringAttr(formatString),
731 ref ? declBuilder.getArrayAttr({ref}) : ArrayAttr{});
732 }
733
734 // Create a file only if the module has at least one ref port.
735 if (ports.empty())
736 return success();
737
738 // The macros will be exported to a `ref_<module-name>.sv` file.
739 // In the IR, the file is inserted before the module.
740 auto fileBuilder = ImplicitLocOpBuilder(module.getLoc(), module);
741 emit::FileOp::create(fileBuilder, circuitRefPrefix + ".sv", [&] {
742 for (auto [macroName, formatString, symbols] : ports) {
743 sv::MacroDefOp::create(fileBuilder, FlatSymbolRefAttr::get(macroName),
744 formatString, symbols);
745 }
746 });
747
748 return success();
749 }
750
751 /// Get the cached namespace for a module.
753 return moduleNamespaces.try_emplace(module, module).first->second;
754 }
755
756 InnerRefAttr getInnerRefTo(Value val) {
757 if (auto arg = dyn_cast<BlockArgument>(val))
758 return ::getInnerRefTo(
759 cast<FModuleLike>(arg.getParentBlock()->getParentOp()),
760 arg.getArgNumber(),
761 [&](FModuleLike mod) -> hw::InnerSymbolNamespace & {
762 return getModuleNamespace(mod);
763 });
764 return getInnerRefTo(val.getDefiningOp());
765 }
766
767 InnerRefAttr getInnerRefTo(Operation *op) {
768 return ::getInnerRefTo(op,
769 [&](FModuleLike mod) -> hw::InnerSymbolNamespace & {
770 return getModuleNamespace(mod);
771 });
772 }
773
774 void markForRemoval(Operation *op) { opsToRemove.push_back(op); }
775
776 std::optional<size_t> getRemoteRefSend(Value val,
777 bool errorIfNotFound = true) {
778 auto iter = dataflowAt.find(dataFlowClasses->getOrInsertLeaderValue(val));
779 if (iter != dataflowAt.end())
780 return iter->getSecond();
781 if (!errorIfNotFound)
782 return std::nullopt;
783 // The referenced module must have already been analyzed, error out if the
784 // dataflow at the child module is not resolved.
785 if (BlockArgument arg = dyn_cast<BlockArgument>(val))
786 arg.getOwner()->getParentOp()->emitError(
787 "reference dataflow cannot be traced back to the remote read op "
788 "for module port '")
789 << dyn_cast<FModuleOp>(arg.getOwner()->getParentOp())
790 .getPortName(arg.getArgNumber())
791 << "'";
792 else
793 val.getDefiningOp()->emitOpError(
794 "reference dataflow cannot be traced back to the remote read op");
795 signalPassFailure();
796 return std::nullopt;
797 }
798
799 size_t
800 addReachingSendsEntry(Value atRefVal, XMRNode::SymOrIndexOp info,
801 std::optional<size_t> continueFrom = std::nullopt) {
802 auto leader = dataFlowClasses->getOrInsertLeaderValue(atRefVal);
803 auto indx = refSendPathList.size();
804 dataflowAt[leader] = indx;
805 refSendPathList.push_back({info, continueFrom});
806 return indx;
807 }
808
810 // Now erase all the Ops and ports of RefType.
811 // This needs to be done as the last step to ensure uses are erased before
812 // the def is erased.
813 for (Operation *op : llvm::reverse(opsToRemove))
814 op->erase();
815 for (auto iter : refPortsToRemoveMap)
816 if (auto mod = dyn_cast<FModuleOp>(iter.getFirst()))
817 mod.erasePorts(iter.getSecond());
818 else if (auto mod = dyn_cast<FExtModuleOp>(iter.getFirst()))
819 mod.erasePorts(iter.getSecond());
820 else if (auto inst = dyn_cast<InstanceOp>(iter.getFirst())) {
821 inst.cloneWithErasedPortsAndReplaceUses(iter.getSecond());
822 inst.erase();
823 } else if (auto mem = dyn_cast<MemOp>(iter.getFirst())) {
824 // Remove all debug ports of the memory.
825 ImplicitLocOpBuilder builder(mem.getLoc(), mem);
826 SmallVector<Attribute, 4> resultNames;
827 SmallVector<Type, 4> resultTypes;
828 SmallVector<Attribute, 4> portAnnotations;
829 SmallVector<Value, 4> oldResults;
830 for (const auto &res : llvm::enumerate(mem.getResults())) {
831 if (isa<RefType>(mem.getResult(res.index()).getType()))
832 continue;
833 resultNames.push_back(mem.getPortNameAttr(res.index()));
834 resultTypes.push_back(res.value().getType());
835 portAnnotations.push_back(mem.getPortAnnotation(res.index()));
836 oldResults.push_back(res.value());
837 }
838 auto newMem = MemOp::create(
839 builder, resultTypes, mem.getReadLatency(), mem.getWriteLatency(),
840 mem.getDepth(), RUWBehavior::Undefined,
841 builder.getArrayAttr(resultNames), mem.getNameAttr(),
842 mem.getNameKind(), mem.getAnnotations(),
843 builder.getArrayAttr(portAnnotations), mem.getInnerSymAttr(),
844 mem.getInitAttr(), mem.getPrefixAttr());
845 for (const auto &res : llvm::enumerate(oldResults))
846 res.value().replaceAllUsesWith(newMem.getResult(res.index()));
847 mem.erase();
848 }
849 opsToRemove.clear();
850 refPortsToRemoveMap.clear();
851 dataflowAt.clear();
852 refSendPathList.clear();
853 moduleStates.clear();
854 }
855
857
858private:
859 /// Cached module namespaces.
860 DenseMap<Operation *, hw::InnerSymbolNamespace> moduleNamespaces;
861
862 DenseSet<Operation *> visitedModules;
863 /// Map of a reference value to an entry into refSendPathList. Each entry in
864 /// refSendPathList represents the path to RefSend.
865 /// The path is required since there can be multiple paths to the RefSend and
866 /// we need to identify a unique path.
867 DenseMap<Value, size_t> dataflowAt;
868
869 /// refSendPathList is used to construct a path to the RefSendOp. Each entry
870 /// is an XMRNode, with an InnerRefAttr or indexing op, and a pointer to the
871 /// next node in the path. The InnerRefAttr can be to an InstanceOp or to the
872 /// XMR defining op, the index op records narrowing along path. All the nodes
873 /// representing an InstanceOp or indexing operation must have a valid
874 /// NextNodeOnPath. Only the node representing the final XMR defining op has
875 /// no NextNodeOnPath, which denotes a leaf node on the path.
876 SmallVector<XMRNode> refSendPathList;
877
878 llvm::EquivalenceClasses<Value> *dataFlowClasses;
879 // Instance and module ref ports that needs to be removed.
880 DenseMap<Operation *, llvm::BitVector> refPortsToRemoveMap;
881
882 /// RefResolve, RefSend, and Connects involving them that will be removed.
883 SmallVector<Operation *> opsToRemove;
884
885 /// Record the internal path to an external module or a memory.
886 DenseMap<size_t, SmallString<128>> xmrPathSuffix;
887
889
890 /// Utility to create HerPathOps at a predefined location in the circuit.
891 /// This handles caching and keeps the order consistent.
893
894 /// Per-module helpers for creating operations within modules.
895 DenseMap<FModuleOp, ModuleState> moduleStates;
896};
assert(baseType &&"element must be base type")
static mlir::Operation * resolve(Context &context, mlir::SymbolRefAttr sym)
static std::vector< mlir::Value > toVector(mlir::ValueRange range)
static Block * getBodyBlock(FModuleLike mod)
LogicalResult resolveReference(mlir::TypedValue< RefType > refVal, ImplicitLocOpBuilder &builder, FlatSymbolRefAttr &ref, StringAttr &xmrAttr)
Definition LowerXMR.cpp:543
DenseMap< Operation *, hw::InnerSymbolNamespace > moduleNamespaces
Cached module namespaces.
Definition LowerXMR.cpp:860
llvm::EquivalenceClasses< Value > * dataFlowClasses
Definition LowerXMR.cpp:878
DenseMap< size_t, SmallString< 128 > > xmrPathSuffix
Record the internal path to an external module or a memory.
Definition LowerXMR.cpp:886
InnerRefAttr getInnerRefTo(Value val)
Definition LowerXMR.cpp:756
size_t addReachingSendsEntry(Value atRefVal, XMRNode::SymOrIndexOp info, std::optional< size_t > continueFrom=std::nullopt)
Definition LowerXMR.cpp:800
DenseMap< FModuleOp, ModuleState > moduleStates
Per-module helpers for creating operations within modules.
Definition LowerXMR.cpp:895
LogicalResult resolveReferencePath(mlir::TypedValue< RefType > refVal, ImplicitLocOpBuilder builder, mlir::FlatSymbolRefAttr &ref, SmallString< 128 > &stringLeaf)
Definition LowerXMR.cpp:470
DenseMap< Value, size_t > dataflowAt
Map of a reference value to an entry into refSendPathList.
Definition LowerXMR.cpp:867
void setPortToRemove(Operation *op, size_t index, size_t numPorts)
Definition LowerXMR.cpp:614
hw::InnerSymbolNamespace & getModuleNamespace(FModuleLike module)
Get the cached namespace for a module.
Definition LowerXMR.cpp:752
void markForRemoval(Operation *op)
Definition LowerXMR.cpp:774
hw::HierPathCache * hierPathCache
Utility to create HerPathOps at a predefined location in the circuit.
Definition LowerXMR.cpp:892
void garbageCollect()
Definition LowerXMR.cpp:809
LogicalResult handlePublicModuleRefPorts(FModuleOp module)
Definition LowerXMR.cpp:696
void getRefABIPrefix(FModuleLike mod, SmallVectorImpl< char > &prefix)
Generate the ABI ref_<module> prefix string into prefix.
Definition LowerXMR.cpp:455
void runOnOperation() override
Definition LowerXMR.cpp:128
LogicalResult handleRefResolve(RefResolveOp resolve)
Definition LowerXMR.cpp:590
DenseMap< Operation *, llvm::BitVector > refPortsToRemoveMap
Definition LowerXMR.cpp:880
SmallVector< XMRNode > refSendPathList
refSendPathList is used to construct a path to the RefSendOp.
Definition LowerXMR.cpp:876
LogicalResult handleInstanceOp(InstanceOp inst, InstanceGraph &instanceGraph)
Definition LowerXMR.cpp:621
LogicalResult handleForceReleaseOp(Operation *op)
Definition LowerXMR.cpp:560
std::optional< size_t > getRemoteRefSend(Value val, bool errorIfNotFound=true)
Definition LowerXMR.cpp:776
DenseSet< Operation * > visitedModules
Definition LowerXMR.cpp:862
InnerRefAttr getInnerRefTo(Operation *op)
Definition LowerXMR.cpp:767
StringAttr getRefABIMacroForPort(FModuleLike mod, size_t portIndex, const Twine &prefix, bool backTick=false)
Get full macro name as StringAttr for the specified ref port.
Definition LowerXMR.cpp:464
CircuitNamespace * circuitNamespace
Definition LowerXMR.cpp:888
bool isZeroWidth(FIRRTLBaseType t)
Definition LowerXMR.cpp:856
SmallVector< Operation * > opsToRemove
RefResolve, RefSend, and Connects involving them that will be removed.
Definition LowerXMR.cpp:883
int32_t getBitWidthOrSentinel()
If this is an IntType, AnalogType, or sugar type for a single bit (Clock, Reset, etc) then return the...
This graph tracks modules and where they are instantiated.
decltype(auto) walkPostOrder(Fn &&fn)
Perform a post-order walk across the modules.
FieldRef getFieldRefFromValue(Value value, bool lookThroughCasts=false)
Get the FieldRef from a value.
hw::InnerRefAttr getInnerRefTo(const hw::InnerSymTarget &target, GetNamespaceCallback getNamespace)
Obtain an inner reference to the target (operation or port), adding an inner symbol as necessary.
llvm::raw_ostream & operator<<(llvm::raw_ostream &os, const InstanceInfo::LatticeValue &value)
std::pair< std::string, bool > getFieldName(const FieldRef &fieldRef, bool nameSafe=false)
Get a string identifier representing the FieldRef.
std::optional< int64_t > getBitWidth(FIRRTLBaseType type, bool ignoreFlip=false)
IntegerAttr getIntZerosAttr(Type type)
Utility for generating a constant zero attribute.
void info(Twine message)
Definition LSPUtils.cpp:20
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
The namespace of a CircuitOp, generally inhabited by modules.
Definition Namespace.h:24