Skip to content

Commit

Permalink
[NFC][InstanceGraph] Remove the slow target getter helper
Browse files Browse the repository at this point in the history
  • Loading branch information
nandor committed Nov 21, 2023
1 parent 1aa8ff5 commit 2711dd5
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 35 deletions.
3 changes: 1 addition & 2 deletions include/circt/Dialect/FIRRTL/FIRRTLDeclarations.td
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ def InstanceOp : HardwareDeclOp<"instance", [
DeclareOpInterfaceMethods<FInstanceLike>,
DeclareOpInterfaceMethods<SymbolUserOpInterface>,
DeclareOpInterfaceMethods<InstanceGraphInstanceOpInterface, [
"getModuleName",
"getReferencedModuleSlow"
"getModuleName"
]>,
]> {
let summary = "Instantiate an instance of a module";
Expand Down
11 changes: 0 additions & 11 deletions include/circt/Support/InstanceGraphInterface.td
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,6 @@ def InstanceGraphInstanceOpInterface : OpInterface<"InstanceOpInterface"> {
"::mlir::StringAttr", "getReferencedModuleNameAttr", (ins),
/*methodBody=*/[{}],
/*defaultImplementation=*/[{ return $_op.getModuleNameAttr().getAttr(); }]>,

InterfaceMethod<[{
Get the referenced module (slow, unsafe). This function directly accesses
the parent operation to lookup a symbol, which is unsafe in many contexts.
}],
"::mlir::Operation *", "getReferencedModuleSlow", (ins),
/*methodBody=*/[{}],
/*defaultImplementation=*/[{
return SymbolTable::lookupNearestSymbolFrom(
$_op, getReferencedModuleNameAttr());
}]>
];
}

Expand Down
4 changes: 3 additions & 1 deletion lib/Conversion/HandshakeToHW/HandshakeToHW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,9 @@ static LogicalResult convertExtMemoryOps(HWModuleOp mod) {
// Get the attached extmemory external module.
auto extmemInstance = cast<hw::InstanceOp>(*arg.getUsers().begin());
auto extmemMod =
cast<hw::HWModuleExternOp>(extmemInstance.getReferencedModuleSlow());
cast<hw::HWModuleExternOp>(SymbolTable::lookupNearestSymbolFrom(
extmemInstance, extmemInstance.getReferencedModuleNameAttr()));

ModulePortInfo portInfo(extmemMod.getPortList());

// The extmemory external module's interface is a direct wrapping of the
Expand Down
21 changes: 3 additions & 18 deletions lib/Dialect/FIRRTL/FIRRTLOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1913,18 +1913,11 @@ bool ExtClassOp::canDiscardOnUseEmpty() {
// Declarations
//===----------------------------------------------------------------------===//

/// Lookup the module or extmodule for the symbol. This returns null on
/// invalid IR.
Operation *InstanceOp::getReferencedModuleSlow() {
SmallVector<::circt::hw::PortInfo> InstanceOp::getPortList() {
auto circuit = (*this)->getParentOfType<CircuitOp>();
if (!circuit)
return nullptr;

return circuit.lookupSymbol<FModuleLike>(getModuleNameAttr());
}

SmallVector<::circt::hw::PortInfo> InstanceOp::getPortList() {
return cast<hw::PortList>(getReferencedModuleSlow()).getPortList();
llvm::report_fatal_error("instance op not in circuit");
return circuit.lookupSymbol<hw::PortList>(getModuleNameAttr()).getPortList();
}

void InstanceOp::build(OpBuilder &builder, OperationState &result,
Expand Down Expand Up @@ -2930,14 +2923,6 @@ Operation *ObjectOp::getReferencedModule(const SymbolTable &symtbl) {
return getReferencedClass(symtbl);
}

Operation *ObjectOp::getReferencedModuleSlow() {
auto circuit = (*this)->getParentOfType<CircuitOp>();
if (!circuit)
return nullptr;

return circuit.lookupSymbol<ClassLike>(getClassNameAttr());
}

StringRef ObjectOp::getInstanceName() { return getName(); }

StringAttr ObjectOp::getInstanceNameAttr() { return getNameAttr(); }
Expand Down
8 changes: 6 additions & 2 deletions lib/Dialect/HW/Transforms/FlattenIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ struct InstanceOpConversion : public OpConversionPattern<hw::InstanceOp> {
}

// Create the new instance...
Operation *targetModule = SymbolTable::lookupNearestSymbolFrom(
op, op.getReferencedModuleNameAttr());
auto newInstance = rewriter.create<hw::InstanceOp>(
loc, op.getReferencedModuleSlow(), op.getInstanceName(), convOperands);
loc, targetModule, op.getInstanceName(), convOperands);

// re-create any structs in the result.
llvm::SmallVector<Value> convResults;
Expand Down Expand Up @@ -381,7 +383,9 @@ static LogicalResult flattenOpsOfType(ModuleOp module, bool recursive) {

// And likewise with the converted instance ops.
for (auto instanceOp : convertedInstances) {
Operation *targetModule = instanceOp.getReferencedModuleSlow();
Operation *targetModule = SymbolTable::lookupNearestSymbolFrom(
instanceOp, instanceOp.getReferencedModuleNameAttr());

auto ioInfo = ioInfoMap[targetModule];
instanceOp.setInputNames(ArrayAttr::get(
instanceOp.getContext(),
Expand Down
3 changes: 2 additions & 1 deletion lib/Dialect/SystemC/SystemCOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,8 @@ InstanceDeclOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
}

SmallVector<hw::PortInfo> InstanceDeclOp::getPortList() {
return cast<hw::PortList>(getReferencedModuleSlow()).getPortList();
return cast<hw::PortList>(getReferencedModuleCached(/*cache=*/nullptr))
.getPortList();
}

//===----------------------------------------------------------------------===//
Expand Down

0 comments on commit 2711dd5

Please sign in to comment.