67#include "mlir/Pass/Pass.h"
68#include "llvm/Support/Debug.h"
70#define DEBUG_TYPE "firrtl-inject-dut-hier"
74#define GEN_PASS_DEF_INJECTDUTHIERARCHY
75#include "circt/Dialect/FIRRTL/Passes.h.inc"
80using namespace firrtl;
83struct InjectDUTHierarchy
84 :
public circt::firrtl::impl::InjectDUTHierarchyBase<InjectDUTHierarchy> {
85 void runOnOperation()
override;
107 InstanceOp wrapperInst, StringAttr oldDutNameAttr) {
109 auto namepath = path.getNamepath().getValue();
112 SmallVector<Attribute> newNamepath;
113 newNamepath.reserve(namepath.size() + 1);
114 while (path.modPart(nlaIdx) != oldDutNameAttr)
115 newNamepath.push_back(namepath[nlaIdx++]);
116 newNamepath.push_back(hw::InnerRefAttr::get(dut.getModuleNameAttr(),
120 if (
auto dutRef = dyn_cast<hw::InnerRefAttr>(namepath[nlaIdx]))
121 newNamepath.push_back(hw::InnerRefAttr::get(
122 wrapperInst.getModuleNameAttr().getAttr(), dutRef.getName()));
124 newNamepath.push_back(
125 FlatSymbolRefAttr::get(wrapperInst.getModuleNameAttr().getAttr()));
128 auto back = namepath.drop_front(nlaIdx + 1);
129 newNamepath.append(back.begin(), back.end());
130 path.setNamepathAttr(ArrayAttr::get(dut.getContext(), newNamepath));
133void InjectDUTHierarchy::runOnOperation() {
136 CircuitOp circuit = getOperation();
142 StringAttr wrapperName;
145 bool moveDut =
false;
157 auto name = anno.getMember<StringAttr>(
"name");
159 emitError(circuit->getLoc())
160 <<
"contained a malformed "
161 "'sifive.enterprise.firrtl.InjectDUTHierarchyAnnotation' "
162 "annotation that did not contain a 'name' field";
168 emitError(circuit->getLoc())
169 <<
"contained multiple "
170 "'sifive.enterprise.firrtl.InjectDUTHierarchyAnnotation' "
171 "annotations when at most one is allowed";
177 if (
auto moveDutAnnoAttr = anno.getMember<BoolAttr>(
"moveDut"))
178 moveDut = moveDutAnnoAttr.getValue();
182 return signalPassFailure();
187 return markAllAnalysesPreserved();
191 InstanceInfo &instanceInfo = getAnalysis<InstanceInfo>();
192 if (!instanceInfo.
getDut()) {
193 emitError(circuit->getLoc())
196 return signalPassFailure();
201 FModuleOp dut = cast<FModuleOp>(instanceInfo.
getDut());
210 OpBuilder b(circuit.getContext());
212 b.setInsertionPointAfter(dut);
216 auto oldDutNameAttr = dut.getNameAttr();
218 dut = b.create<FModuleOp>(dut.getLoc(), oldDutNameAttr,
219 dut.getConventionAttr(), dut.getPorts());
239 auto emptyArray = b.getArrayAttr({});
240 auto name = circuitNS.newName(wrapperName.getValue());
242 dut.setPortAnnotationsAttr(emptyArray);
243 dut.setName(b.getStringAttr(name));
246 if (circuit.getNameAttr() == wrapper.getNameAttr()) {
247 circuit.setNameAttr(dut.getNameAttr());
254 for (
auto *use : instanceGraph.lookup(wrapper.getNameAttr())->uses()) {
255 auto instanceOp = use->getInstance<InstanceOp>();
257 use->getInstance().emitOpError()
258 <<
"instantiates the design-under-test, but "
259 "is not a 'firrtl.instance'";
260 return signalPassFailure();
262 instanceOp.setModuleNameAttr(FlatSymbolRefAttr::get(dut.getNameAttr()));
265 dut.setVisibility(wrapper.getVisibility());
266 dut.setAnnotationsAttr(wrapper.getAnnotationsAttr());
267 dut.setPortAnnotationsAttr(wrapper.getPortAnnotationsAttr());
268 wrapper.setPrivate();
269 wrapper.setAnnotationsAttr(emptyArray);
270 wrapper.setPortAnnotationsAttr(emptyArray);
271 wrapper.setName(name);
275 b.setInsertionPointToStart(dut.getBodyBlock());
278 b.create<InstanceOp>(b.getUnknownLoc(), wrapper, wrapper.getModuleName(),
279 NameKindEnum::DroppableName, ArrayRef<Attribute>{},
280 ArrayRef<Attribute>{},
false,
false,
281 hw::InnerSymAttr::get(b.getStringAttr(
282 dutNS.newName(wrapper.getModuleName()))));
283 for (
const auto &pair :
llvm::enumerate(wrapperInst.getResults())) {
284 Value lhs = dut.getArgument(pair.index());
285 Value rhs = pair.value();
286 if (dut.getPortDirection(pair.index()) == Direction::In)
292 DenseSet<StringAttr> dutPaths, dutPortSyms;
294 auto sym = anno.getMember<FlatSymbolRefAttr>(
"circt.nonlocal");
296 dutPaths.insert(sym.getAttr());
298 for (
size_t i = 0, e = dut.getNumPorts(); i != e; ++i) {
299 auto portSym = dut.getPortSymbolAttr(i);
301 dutPortSyms.insert(portSym.getSymName());
303 auto sym = anno.getMember<FlatSymbolRefAttr>(
"circt.nonlocal");
305 dutPaths.insert(sym.getAttr());
310 llvm::dbgs() <<
"DUT Symbol Users:\n";
311 for (
auto path : dutPaths)
312 llvm::dbgs() <<
" - " << FlatSymbolRefAttr::
get(path) <<
"\n";
313 llvm::dbgs() <<
"Port Symbols:\n";
314 for (
auto sym : dutPortSyms)
315 llvm::dbgs() <<
" - " << FlatSymbolRefAttr::
get(sym) <<
"\n";
330 LLVM_DEBUG(llvm::dbgs() <<
"Processing hierarchical paths:\n");
331 auto &nlaTable = getAnalysis<NLATable>();
332 DenseMap<StringAttr, hw::HierPathOp> dutRenames;
333 for (
auto nla :
llvm::make_early_inc_range(nlaTable.lookup(oldDutNameAttr))) {
334 LLVM_DEBUG(llvm::dbgs() <<
" - " << nla <<
"\n");
335 auto namepath = nla.getNamepath().getValue();
345 if (nla.root() == oldDutNameAttr) {
346 assert(namepath.size() > 1 &&
"namepath size must be greater than one");
347 SmallVector<Attribute> newNamepath{hw::InnerRefAttr::get(
348 wrapper.getNameAttr(),
349 cast<hw::InnerRefAttr>(namepath.front()).getName())};
350 auto tail = namepath.drop_front();
351 newNamepath.append(tail.begin(), tail.end());
352 nla->setAttr(
"namepath", b.getArrayAttr(newNamepath));
368 if (nla.leafMod() == oldDutNameAttr) {
371 if (nla.isComponent() && dutPortSyms.count(nla.ref()))
377 if (nla.isModule() && dutPaths.contains(nla.getSymNameAttr())) {
378 OpBuilder::InsertionGuard guard(b);
379 b.setInsertionPoint(nla);
380 auto clone = cast<hw::HierPathOp>(b.clone(*nla));
381 clone.setSymNameAttr(b.getStringAttr(
382 circuitNS.newName(clone.getSymNameAttr().getValue())));
383 dutRenames.insert({nla.getSymNameAttr(), clone});
392 SmallVector<Annotation> newAnnotations;
393 auto removeAndUpdateNLAs = [&](
Annotation anno) ->
bool {
394 auto sym = anno.getMember<FlatSymbolRefAttr>(
"circt.nonlocal");
397 if (!dutRenames.count(sym.getAttr()))
401 FlatSymbolRefAttr::get(dutRenames[sym.getAttr()].getSymNameAttr()));
402 newAnnotations.push_back(anno);
408 annotations.removeAnnotations(removeAndUpdateNLAs);
409 annotations.addAnnotations(newAnnotations);
410 annotations.applyToOperation(dut);
411 for (
size_t i = 0, e = dut.getNumPorts(); i != e; ++i) {
412 newAnnotations.clear();
414 annotations.removeAnnotations(removeAndUpdateNLAs);
415 annotations.addAnnotations(newAnnotations);
416 annotations.applyToPort(dut, i);
420 wrapper.walk([&](RWProbeOp rwp) {
421 rwp.setTargetAttr(hw::InnerRefAttr::get(wrapper.getModuleNameAttr(),
422 rwp.getTarget().getName()));
assert(baseType &&"element must be base type")
static AnnotationSet forPort(Operation *op, size_t portNo)
static void addHierarchy(hw::HierPathOp path, FModuleOp dut, InstanceOp wrapperInst, StringAttr oldDutNameAttr)
Add an extra level of hierarchy to a hierarchical path that places the wrapper instance after the DUT...
This class provides a read-only projection over the MLIR attributes that represent a set of annotatio...
static AnnotationSet forPort(FModuleLike op, size_t portNo)
Get an annotation set for the specified port.
This class provides a read-only projection of an annotation.
This graph tracks modules and where they are instantiated.
igraph::ModuleOpInterface getDut()
Return the design-under-test if one is defined for the circuit, otherwise return null.
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
constexpr const char * injectDUTHierarchyAnnoClass
constexpr const char * dutAnnoClass
void emitConnect(OpBuilder &builder, Location loc, Value lhs, Value rhs)
Emit a connect between two values.
StringAttr getInnerSymName(Operation *op)
Return the StringAttr for the inner_sym name, if it exists.
void error(Twine message)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
llvm::raw_ostream & debugPassHeader(const mlir::Pass *pass, int width=80)
Write a boilerplate header for a pass to the debug stream.
The namespace of a CircuitOp, generally inhabited by modules.