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());
243 dut.setPortAnnotationsAttr(emptyArray);
244 dut.setName(b.getStringAttr(name));
247 for (
auto *use : instanceGraph.lookup(wrapper.getNameAttr())->uses()) {
248 auto instanceOp = use->getInstance<InstanceOp>();
250 use->getInstance().emitOpError()
251 <<
"instantiates the design-under-test, but "
252 "is not a 'firrtl.instance'";
253 return signalPassFailure();
255 instanceOp.setModuleNameAttr(FlatSymbolRefAttr::get(dut.getNameAttr()));
258 dut.setVisibility(wrapper.getVisibility());
259 dut.setAnnotationsAttr(wrapper.getAnnotationsAttr());
260 dut.setPortAnnotationsAttr(wrapper.getPortAnnotationsAttr());
261 wrapper.setPrivate();
262 wrapper.setAnnotationsAttr(emptyArray);
263 wrapper.setPortAnnotationsAttr(emptyArray);
264 wrapper.setName(name);
268 b.setInsertionPointToStart(dut.getBodyBlock());
271 b.create<InstanceOp>(b.getUnknownLoc(), wrapper, wrapper.getModuleName(),
272 NameKindEnum::DroppableName, ArrayRef<Attribute>{},
273 ArrayRef<Attribute>{},
false,
false,
274 hw::InnerSymAttr::get(b.getStringAttr(
275 dutNS.newName(wrapper.getModuleName()))));
276 for (
const auto &pair :
llvm::enumerate(wrapperInst.getResults())) {
277 Value lhs = dut.getArgument(pair.index());
278 Value rhs = pair.value();
279 if (dut.getPortDirection(pair.index()) == Direction::In)
285 DenseSet<StringAttr> dutPaths, dutPortSyms;
287 auto sym = anno.getMember<FlatSymbolRefAttr>(
"circt.nonlocal");
289 dutPaths.insert(sym.getAttr());
291 for (
size_t i = 0, e = dut.getNumPorts(); i != e; ++i) {
292 auto portSym = dut.getPortSymbolAttr(i);
294 dutPortSyms.insert(portSym.getSymName());
296 auto sym = anno.getMember<FlatSymbolRefAttr>(
"circt.nonlocal");
298 dutPaths.insert(sym.getAttr());
303 llvm::dbgs() <<
"DUT Symbol Users:\n";
304 for (
auto path : dutPaths)
305 llvm::dbgs() <<
" - " << FlatSymbolRefAttr::
get(path) <<
"\n";
306 llvm::dbgs() <<
"Port Symbols:\n";
307 for (
auto sym : dutPortSyms)
308 llvm::dbgs() <<
" - " << FlatSymbolRefAttr::
get(sym) <<
"\n";
323 LLVM_DEBUG(llvm::dbgs() <<
"Processing hierarchical paths:\n");
324 auto &nlaTable = getAnalysis<NLATable>();
325 DenseMap<StringAttr, hw::HierPathOp> dutRenames;
326 for (
auto nla :
llvm::make_early_inc_range(nlaTable.lookup(oldDutNameAttr))) {
327 LLVM_DEBUG(llvm::dbgs() <<
" - " << nla <<
"\n");
328 auto namepath = nla.getNamepath().getValue();
338 if (nla.root() == oldDutNameAttr) {
339 assert(namepath.size() > 1 &&
"namepath size must be greater than one");
340 SmallVector<Attribute> newNamepath{hw::InnerRefAttr::get(
341 wrapper.getNameAttr(),
342 cast<hw::InnerRefAttr>(namepath.front()).getName())};
343 auto tail = namepath.drop_front();
344 newNamepath.append(tail.begin(), tail.end());
345 nla->setAttr(
"namepath", b.getArrayAttr(newNamepath));
361 if (nla.leafMod() == oldDutNameAttr) {
364 if (nla.isComponent() && dutPortSyms.count(nla.ref()))
370 if (nla.isModule() && dutPaths.contains(nla.getSymNameAttr())) {
371 OpBuilder::InsertionGuard guard(b);
372 b.setInsertionPoint(nla);
373 auto clone = cast<hw::HierPathOp>(b.clone(*nla));
374 clone.setSymNameAttr(b.getStringAttr(
375 circuitNS.newName(clone.getSymNameAttr().getValue())));
376 dutRenames.insert({nla.getSymNameAttr(), clone});
385 SmallVector<Annotation> newAnnotations;
386 auto removeAndUpdateNLAs = [&](
Annotation anno) ->
bool {
387 auto sym = anno.getMember<FlatSymbolRefAttr>(
"circt.nonlocal");
390 if (!dutRenames.count(sym.getAttr()))
394 FlatSymbolRefAttr::get(dutRenames[sym.getAttr()].getSymNameAttr()));
395 newAnnotations.push_back(anno);
401 annotations.removeAnnotations(removeAndUpdateNLAs);
402 annotations.addAnnotations(newAnnotations);
403 annotations.applyToOperation(dut);
404 for (
size_t i = 0, e = dut.getNumPorts(); i != e; ++i) {
405 newAnnotations.clear();
407 annotations.removeAnnotations(removeAndUpdateNLAs);
408 annotations.addAnnotations(newAnnotations);
409 annotations.applyToPort(dut, i);
413 wrapper.walk([&](RWProbeOp rwp) {
414 rwp.setTargetAttr(hw::InnerRefAttr::get(wrapper.getModuleNameAttr(),
415 rwp.getTarget().getName()));
424 return std::make_unique<InjectDUTHierarchy>();
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
std::unique_ptr< mlir::Pass > createInjectDUTHierarchyPass()
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.