diff --git a/lib/Dialect/HW/InstanceImplementation.cpp b/lib/Dialect/HW/InstanceImplementation.cpp index 3927d44058c2..02eecbf2c8b4 100644 --- a/lib/Dialect/HW/InstanceImplementation.cpp +++ b/lib/Dialect/HW/InstanceImplementation.cpp @@ -21,8 +21,7 @@ instance_like_impl::getReferencedModule(const HWSymbolCache *cache, if (auto *result = cache->getDefinition(moduleName)) return result; - auto topLevelModuleOp = instanceOp->getParentOfType(); - return topLevelModuleOp.lookupSymbol(moduleName.getValue()); + return SymbolTable::lookupNearestSymbolFrom(instanceOp, moduleName); } LogicalResult instance_like_impl::verifyReferencedModule( diff --git a/test/Dialect/HW/errors.mlir b/test/Dialect/HW/errors.mlir index 598cea6f8732..66414fced552 100644 --- a/test/Dialect/HW/errors.mlir +++ b/test/Dialect/HW/errors.mlir @@ -393,3 +393,17 @@ hw.module @struct(in %a: !hw.struct // expected-error @+2 {{duplicate field name 'foo' in hw.union type}} // expected-error @+1 {{duplicate field name 'bar' in hw.union type}} hw.module @union(in %a: !hw.union) {} + +// ----- + +// Test that nested symbol tables fail in the correct way when trying to use an +// instance to escape its containing table. + +hw.module @Foo () {} + +builtin.module @Nested { + hw.module @Bar () { + // expected-error @+1 {{Cannot find module definition 'Foo'}} + hw.instance "inst" @Foo () -> () + } +}