|
| 1 | +//===- CheckInstanceChoice.cpp - Check instance choice configurations -----===// |
| 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 | +#include "circt/Dialect/FIRRTL/InstanceChoiceInfo.h" |
| 10 | +#include "circt/Dialect/FIRRTL/Passes.h" |
| 11 | +#include "circt/Support/LLVM.h" |
| 12 | +#include "mlir/Pass/Pass.h" |
| 13 | +#include "llvm/ADT/DepthFirstIterator.h" |
| 14 | +#include "llvm/ADT/PostOrderIterator.h" |
| 15 | + |
| 16 | +namespace circt { |
| 17 | +namespace firrtl { |
| 18 | +#define GEN_PASS_DEF_CHECKINSTANCECHOICE |
| 19 | +#include "circt/Dialect/FIRRTL/Passes.h.inc" |
| 20 | +} // namespace firrtl |
| 21 | +} // namespace circt |
| 22 | + |
| 23 | +using namespace circt; |
| 24 | +using namespace firrtl; |
| 25 | +using namespace mlir; |
| 26 | + |
| 27 | +LogicalResult InstanceChoiceInfo::run() { |
| 28 | + for (auto &op : *circuit.getBodyBlock()) { |
| 29 | + auto module = dyn_cast<FModuleLike>(op); |
| 30 | + if (!module || !module.isPublic()) |
| 31 | + continue; |
| 32 | + |
| 33 | + auto *rootNode = instanceGraph.lookup(module); |
| 34 | + if (!rootNode) |
| 35 | + continue; |
| 36 | + |
| 37 | + alwaysReachable[module].insert(module); |
| 38 | + computeAlwaysReachable(rootNode, module); |
| 39 | + |
| 40 | + moduleChoices[module][module] = {}; |
| 41 | + |
| 42 | + DenseSet<igraph::InstanceGraphNode *> visited; |
| 43 | + SmallVector<igraph::InstanceGraphNode *> postOrderNodes; |
| 44 | + for (auto *node : llvm::post_order_ext(rootNode, visited)) |
| 45 | + postOrderNodes.push_back(node); |
| 46 | + |
| 47 | + for (auto *node : llvm::reverse(postOrderNodes)) |
| 48 | + if (failed(processNode(node, module))) |
| 49 | + return failure(); |
| 50 | + } |
| 51 | + |
| 52 | + return success(); |
| 53 | +} |
| 54 | + |
| 55 | +void InstanceChoiceInfo::computeAlwaysReachable(igraph::InstanceGraphNode *node, |
| 56 | + FModuleLike publicModule) { |
| 57 | + SmallVector<igraph::InstanceGraphNode *> worklist; |
| 58 | + worklist.push_back(node); |
| 59 | + auto &reachableSet = alwaysReachable[publicModule]; |
| 60 | + |
| 61 | + while (!worklist.empty()) { |
| 62 | + auto *currentNode = worklist.pop_back_val(); |
| 63 | + auto module = |
| 64 | + dyn_cast<FModuleLike>(currentNode->getModule().getOperation()); |
| 65 | + if (!module) |
| 66 | + continue; |
| 67 | + |
| 68 | + for (auto *record : *currentNode) { |
| 69 | + auto *targetNode = record->getTarget(); |
| 70 | + if (!targetNode) |
| 71 | + continue; |
| 72 | + |
| 73 | + if (!isa<InstanceOp>(record->getInstance().getOperation())) |
| 74 | + continue; |
| 75 | + |
| 76 | + auto targetModule = |
| 77 | + dyn_cast<FModuleLike>(targetNode->getModule().getOperation()); |
| 78 | + if (targetModule && reachableSet.insert(targetModule).second) |
| 79 | + worklist.push_back(targetNode); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +LogicalResult InstanceChoiceInfo::processNode(igraph::InstanceGraphNode *node, |
| 85 | + FModuleLike publicModule) { |
| 86 | + auto module = dyn_cast<FModuleLike>(node->getModule().getOperation()); |
| 87 | + if (!module) |
| 88 | + return success(); |
| 89 | + |
| 90 | + if (isAlwaysReachable(publicModule, module)) { |
| 91 | + moduleChoices[publicModule][module] = {}; |
| 92 | + return success(); |
| 93 | + } |
| 94 | + |
| 95 | + assert(module != publicModule && "public module should be pre-initialized"); |
| 96 | + |
| 97 | + for (auto *use : node->uses()) { |
| 98 | + auto *instOp = use->getInstance().getOperation(); |
| 99 | + auto *parentNode = use->getParent(); |
| 100 | + if (!parentNode) |
| 101 | + continue; |
| 102 | + |
| 103 | + auto parentModule = |
| 104 | + dyn_cast<FModuleLike>(parentNode->getModule().getOperation()); |
| 105 | + if (!parentModule) |
| 106 | + continue; |
| 107 | + |
| 108 | + auto parentIt = moduleChoices[publicModule].find(parentModule); |
| 109 | + // It means the parent module is not reachable (regardless of instance or |
| 110 | + // instance choice) from the public module. |
| 111 | + if (parentIt == moduleChoices[publicModule].end()) |
| 112 | + continue; |
| 113 | + |
| 114 | + const auto &parentChoices = parentIt->second; |
| 115 | + |
| 116 | + if (auto choice = dyn_cast<InstanceChoiceOp>(instOp)) { |
| 117 | + SymbolRefAttr optionName = |
| 118 | + FlatSymbolRefAttr::get(choice.getOptionNameAttr()); |
| 119 | + |
| 120 | + for (const auto &parentChoice : parentChoices) { |
| 121 | + if (parentChoice.option != optionName) { |
| 122 | + auto diag = choice.emitOpError() |
| 123 | + << "nested instance choice with option '" |
| 124 | + << optionName.getLeafReference().getValue() |
| 125 | + << "' conflicts with option '" |
| 126 | + << parentChoice.option.getLeafReference().getValue() |
| 127 | + << "' already on the path from public module '" |
| 128 | + << publicModule.getModuleName() << "'"; |
| 129 | + diag.attachNote(publicModule.getLoc()) << "public module here"; |
| 130 | + return failure(); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + auto &choices = moduleChoices[publicModule][module]; |
| 135 | + choices.insert(parentChoices.begin(), parentChoices.end()); |
| 136 | + |
| 137 | + auto checkAndInsert = [&](SymbolRefAttr caseRef) -> LogicalResult { |
| 138 | + for (const auto &parentChoice : parentChoices) { |
| 139 | + if (parentChoice.option == optionName && |
| 140 | + parentChoice.caseAttr != caseRef) { |
| 141 | + auto diag = choice.emitOpError() |
| 142 | + << "nested instance choice with option '" |
| 143 | + << optionName.getLeafReference().getValue() << "'"; |
| 144 | + if (caseRef) |
| 145 | + diag << " and case '" << caseRef << "'"; |
| 146 | + else |
| 147 | + diag << " and default case"; |
| 148 | + diag << " conflicts with "; |
| 149 | + if (parentChoice.caseAttr) |
| 150 | + diag << "case '" << parentChoice.caseAttr << "'"; |
| 151 | + else |
| 152 | + diag << "default case"; |
| 153 | + diag << " already on the path from public module '" |
| 154 | + << publicModule.getModuleName() << "'"; |
| 155 | + diag.attachNote(publicModule.getLoc()) << "public module here"; |
| 156 | + return failure(); |
| 157 | + } |
| 158 | + } |
| 159 | + choices.insert({optionName, caseRef}); |
| 160 | + return success(); |
| 161 | + }; |
| 162 | + |
| 163 | + if (choice.getDefaultTargetAttr().getAttr() == module.getModuleNameAttr()) |
| 164 | + if (failed(checkAndInsert(SymbolRefAttr()))) |
| 165 | + return failure(); |
| 166 | + |
| 167 | + for (auto [caseRef, moduleRef] : choice.getTargetChoices()) |
| 168 | + if (moduleRef.getAttr() == module.getModuleNameAttr()) |
| 169 | + if (failed(checkAndInsert(caseRef))) |
| 170 | + return failure(); |
| 171 | + |
| 172 | + } else if (isa<InstanceOp>(instOp)) { |
| 173 | + moduleChoices[publicModule][module].insert(parentChoices.begin(), |
| 174 | + parentChoices.end()); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + return success(); |
| 179 | +} |
| 180 | + |
| 181 | +void InstanceChoiceInfo::dump(raw_ostream &os) const { |
| 182 | + for (auto [publicModule, destMap] : moduleChoices) { |
| 183 | + os << "Public module: " << publicModule.getModuleName() << "\n"; |
| 184 | + |
| 185 | + for (auto [destModule, choices] : destMap) { |
| 186 | + // Only output modules that have choices |
| 187 | + if (choices.empty()) { |
| 188 | + os << " -> " << destModule.getModuleName() << ": <always>\n"; |
| 189 | + continue; |
| 190 | + } |
| 191 | + |
| 192 | + os << " -> " << destModule.getModuleName() << ": "; |
| 193 | + llvm::interleaveComma(choices, os, [&](const ChoiceKey &key) { |
| 194 | + os << key.option.getLeafReference().getValue(); |
| 195 | + if (key.caseAttr) |
| 196 | + os << "=" << key.caseAttr.getLeafReference().getValue(); |
| 197 | + else |
| 198 | + os << "=<default>"; |
| 199 | + }); |
| 200 | + os << "\n"; |
| 201 | + } |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +namespace { |
| 206 | +class CheckInstanceChoicePass |
| 207 | + : public circt::firrtl::impl::CheckInstanceChoiceBase< |
| 208 | + CheckInstanceChoicePass> { |
| 209 | +public: |
| 210 | + using CheckInstanceChoiceBase::CheckInstanceChoiceBase; |
| 211 | + |
| 212 | + void runOnOperation() override { |
| 213 | + auto circuit = getOperation(); |
| 214 | + auto &instanceGraph = getAnalysis<InstanceGraph>(); |
| 215 | + InstanceChoiceInfo info(circuit, instanceGraph); |
| 216 | + if (failed(info.run())) |
| 217 | + return signalPassFailure(); |
| 218 | + |
| 219 | + if (dumpInfo) |
| 220 | + info.dump(llvm::errs()); |
| 221 | + |
| 222 | + markAllAnalysesPreserved(); |
| 223 | + } |
| 224 | +}; |
| 225 | +} // namespace |
0 commit comments