Can't find class declared in a generated header file

I'm not sure if TableGen is to blame here, but I'm trying to access the C++ type HWDialect from MLIR and I cannot (the Swift compiler says no such type), whereas a simpler type declared directly in the headers works. HWDialect is created by TableGen and lives in a header file (HWDialect.h.inc) in the build products. I can't seem to access this type from Swift, and I'm wondering if it is because the module map isn't picking up the generated header. Is this a red herring or is there something I can do here?

Yes it's likely caused by the generated header not being referenced directly from the modulemap. Can you try to add an absolute path to the generated header to the modulemap to as a workaround?

I think you should also be able to #include the generated header from any of the headers referenced in the modulemap. That way, if you pass the path to the generated header as -Xcc -I, Swift will be able to find the symbols declared in the header.

Hmm, I tried including the header directly and that didn't help the issue. Actually, I no longer think the issue is that it is a generated header. I think this type also has custom copy behavior and hence can't be represented in Swift. If I mark HWDialect as import_as_ref, I get a compiler crash:

root@461c5fdbbaee:/build-products/circt-workspace# /usr/bin/swiftc -output-file-map tools/circt/test/Bindings/Swift/CMakeFiles/circt-swift-bindings-test.dir/Debug/output-file-map.json -incremental -j 8 -emit-executable -o bin/circt-swift-bindings-test -emit-dependencies -D_DEBUG -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -g -emit-module -enable-experimental-cxx-interop -Xfrontend -validate-tbd-against-ir=none -Xcc -std=c++17 -g -I /build-products/circt-workspace/tools/circt/test/Bindings/Swift -I /workspaces/circt-workspace/circt/test/Bindings/Swift -I /build-products/circt-workspace/include -I /workspaces/circt-workspace/circt/llvm/llvm/include -I /workspaces/circt-workspace/circt/include -I /build-products/circt-workspace/tools/circt/include -I /build-products/circt-workspace/swift -I /workspaces/circt-workspace/circt/llvm/llvm/../mlir/include -I /build-products/circt-workspace/tools/mlir/include /workspaces/circt-workspace/circt/test/Bindings/Swift/BindingsTests.swift  -L /build-products/circt-workspace/lib  -L /build-products/circt-workspace/lib  -L /build-products/circt-workspace/lib  -L /build-products/circt-workspace/lib  -L /build-products/circt-workspace/lib  -L /build-products/circt-workspace/lib  -L /build-products/circt-workspace/lib   -L /usr/lib/gcc/x86_64-linux-gnu/11 -Xlinker -rpath -Xlinker "\$ORIGIN/../lib"  lib/libCIRCTSwift.a  lib/libCIRCTBindingsSwiftBridge.a  lib/libCIRCTHW.a  lib/libMLIRIR.a  lib/libMLIRSupport.a  lib/libLLVMSupport.a  -lrt  -ldl  -lm  /usr/lib/x86_64-linux-gnu/libtinfo.so  lib/libLLVMDemangle.a  -lgcc  -lgcc_s  -lc  -lgcc  -lgcc_s  -lstdc++  -lm  -lgcc_s  -lgcc  -lc  -lgcc_s  -lgcc
error: compile command failed due to signal 6 (use -v to see invocation)
<unknown>:0: warning: template instantiation for 'std::conditional' not imported: too many instantiations
/workspaces/circt-workspace/circt/include/circt/Support/SymCache.h:57:18: warning: cycle detected while resolving 'CacheIteratorImpl' in swift_name attribute for 'operator=='
    virtual bool operator==(CacheIteratorImpl *other) = 0;
                 ^
/workspaces/circt-workspace/circt/llvm/mlir/include/mlir/IR/SymbolTable.h:72:14: note: while resolving 'SymbolTable' in swift_name attribute for 'Visibility'
  enum class Visibility {
             ^
/workspaces/circt-workspace/circt/include/circt/Support/SymCache.h:53:10: note: while resolving 'SymbolCacheBase' in swift_name attribute for 'CacheIteratorImpl'
  struct CacheIteratorImpl {
         ^
/workspaces/circt-workspace/circt/include/circt/Support/SymCache.h:57:18: note: please report this issue to the owners of 'CIRCT_HW.HWSymCache'
    virtual bool operator==(CacheIteratorImpl *other) = 0;
                 ^
checked decl cannot have error type
(typealias implicit "Element" interface type='llvm.__CxxTemplateInstN4llvm11SmallVectorISt10unique_ptrIN4mlir6RegionESt14default_deleteIS3_EELj1EEE.Element.Type' access=public type='Optional<Wrapped>.Pointee')
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.      Program arguments: /usr/bin/swift-frontend -frontend -c -primary-file /workspaces/circt-workspace/circt/test/Bindings/Swift/BindingsTests.swift -emit-module-path tools/circt/test/Bindings/Swift/CMakeFiles/circt-swift-bindings-test.dir/BindingsTests.swift.swiftmodule -emit-module-doc-path tools/circt/test/Bindings/Swift/CMakeFiles/circt-swift-bindings-test.dir/BindingsTests.swift.swiftdoc -emit-module-source-info-path tools/circt/test/Bindings/Swift/CMakeFiles/circt-swift-bindings-test.dir/BindingsTests.swift.swiftsourceinfo -emit-dependencies-path tools/circt/test/Bindings/Swift/CMakeFiles/circt-swift-bindings-test.dir/BindingsTests.swift.o.d -emit-reference-dependencies-path tools/circt/test/Bindings/Swift/CMakeFiles/circt-swift-bindings-test.dir/BindingsTests.swift.o.swiftdeps -target x86_64-unknown-linux-gnu -disable-objc-interop -enable-experimental-cxx-interop -I /build-products/circt-workspace/tools/circt/test/Bindings/Swift -I /workspaces/circt-workspace/circt/test/Bindings/Swift -I /build-products/circt-workspace/include -I /workspaces/circt-workspace/circt/llvm/llvm/include -I /workspaces/circt-workspace/circt/include -I /build-products/circt-workspace/tools/circt/include -I /build-products/circt-workspace/swift -I /workspaces/circt-workspace/circt/llvm/mlir/include -I /build-products/circt-workspace/tools/mlir/include -color-diagnostics -g -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -new-driver-path /usr/bin/swift-driver -validate-tbd-against-ir=none -empty-abi-descriptor -resource-dir /usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -std=c++17 -module-name main -o tools/circt/test/Bindings/Swift/CMakeFiles/circt-swift-bindings-test.dir/BindingsTests.swift.o
1.      Swift version 5.8-dev (LLVM c3aebe50177cd03, Swift 3f020375e568042)
2.      Compiling with the current language version
3.      While walking into '__CxxTemplateInstN4llvm11SmallVectorISt10unique_ptrIN4mlir6RegionESt14default_deleteIS3_EELj1EEE' (in module '__ObjC')
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
/usr/bin/swift-frontend(+0x6c25a23)[0x55fde81eca23]
/usr/bin/swift-frontend(+0x6c237fe)[0x55fde81ea7fe]
/usr/bin/swift-frontend(+0x6c25daa)[0x55fde81ecdaa]
/lib/x86_64-linux-gnu/libc.so.6(+0x42520)[0x7f72ff583520]
/lib/x86_64-linux-gnu/libc.so.6(pthread_kill+0x12c)[0x7f72ff5d7a7c]
/lib/x86_64-linux-gnu/libc.so.6(raise+0x16)[0x7f72ff583476]
/lib/x86_64-linux-gnu/libc.so.6(abort+0xd3)[0x7f72ff5697f3]
/usr/bin/swift-frontend(+0x221967a)[0x55fde37e067a]
/usr/bin/swift-frontend(+0x2208142)[0x55fde37cf142]
/usr/bin/swift-frontend(+0x221bb5d)[0x55fde37e2b5d]
/usr/bin/swift-frontend(+0x222050d)[0x55fde37e750d]
/usr/bin/swift-frontend(+0x221f5e5)[0x55fde37e65e5]
/usr/bin/swift-frontend(+0x221bb4d)[0x55fde37e2b4d]
/usr/bin/swift-frontend(+0x221ba83)[0x55fde37e2a83]
/usr/bin/swift-frontend(+0x2204d74)[0x55fde37cbd74]
/usr/bin/swift-frontend(+0x2085bb9)[0x55fde364cbb9]
/usr/bin/swift-frontend(+0x216166a)[0x55fde372866a]
/usr/bin/swift-frontend(+0xcfb607)[0x55fde22c2607]
/usr/bin/swift-frontend(+0xcfab75)[0x55fde22c1b75]
/usr/bin/swift-frontend(+0xcf698c)[0x55fde22bd98c]
/usr/bin/swift-frontend(+0xcf5c7b)[0x55fde22bcc7b]
/usr/bin/swift-frontend(+0xd07634)[0x55fde22ce634]
/usr/bin/swift-frontend(+0xcf86c5)[0x55fde22bf6c5]
/usr/bin/swift-frontend(+0xcf7581)[0x55fde22be581]
/usr/bin/swift-frontend(+0xb359a6)[0x55fde20fc9a6]
/lib/x86_64-linux-gnu/libc.so.6(+0x29d90)[0x7f72ff56ad90]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80)[0x7f72ff56ae40]
/usr/bin/swift-frontend(+0xb35105)[0x55fde20fc105]
root@461c5fdbbaee:/build-products/circt-workspace# 

Thanks, I just submitted a patch to fix the crash: [cxx-interop] Correctly check iterator conformances by egorzhdan · Pull Request #61540 · apple/swift · GitHub

1 Like

Thanks for the quick turnaround, I'll take a look once it lands in a nightly!