Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CMake] Add compile-time check that .so files have no undefined symbols #8178

Merged

Conversation

Lunderberg
Copy link
Contributor

Adds -Wl,-z,defs flag for linking libtvm.so and libtvm_runtime.so. While undefined symbols in libtvm.so would be caught by the CI as tests run, undefined symbols in libtvm_runtime.so (e.g. dependency on Target introduced in #8127, removed in #8171), would otherwise pass the CI.

@Lunderberg
Copy link
Contributor Author

Potential reviewers: @areusch as this impacts the CI.

Copy link
Contributor

@areusch areusch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for adding this, @Lunderberg . i'm supportive of this, would be great to get opinions from those who work more with external libraries cc @tqchen @junrushao1994 @d-smirnov @masahi

@junrushao
Copy link
Member

I am supportive too, but we probably need to make it work (or have it disabled) on windows & macOS

@Lunderberg Lunderberg force-pushed the tvm_runtime_so_forbid_undefined_symbols branch from 7fba3cb to 599cc0e Compare June 2, 2021 18:49
@Lunderberg
Copy link
Contributor Author

Sounds good, and that is my goal. The MSVC/Windows side should work automatically with no extra flag, since its default is to give an error on undefined symbols. For MacOS, I've updated the check to pass -undefined,error on OSX platforms, and will keep an eye on the CI to make sure that it works.

@Lunderberg Lunderberg force-pushed the tvm_runtime_so_forbid_undefined_symbols branch 4 times, most recently from c55aec6 to b2b7ce8 Compare June 3, 2021 19:28
…d symbols.

If libtvm_runtime.so erroneously requires definitions that are only
present in libtvm.so, the -Wl,--no-undefined flag forces them to be
compile-time errors rather than runtime, and would be caught by the
CI.
@Lunderberg Lunderberg force-pushed the tvm_runtime_so_forbid_undefined_symbols branch from b2b7ce8 to 0415733 Compare June 4, 2021 00:16
@Lunderberg
Copy link
Contributor Author

The check now works on all systems. For MSVC, undefined symbols already result in an error, no extra flag is needed. For MacOS, the equivalent -Wl,-undefined,error flag is used. I also switched to the equivalent but more readable -Wl,--no-undefined flag on linux.

The check is applied only on libtvm.so and libtvm_runtime.so, and not to the more general CMAKE_SHARED_LINKER_FLAGS, because some of the other libraries have undefined symbols that are resolved when linking against the tvm libraries.

Copy link
Member

@junrushao junrushao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Lunderberg!

else()
set(TVM_NO_UNDEFINED_SYMBOLS "-Wl,--no-undefined")
endif()
message(STATUS "Forbidding undefined symbols in shared library, using ${TVM_NO_UNDEFINED_SYMBOLS} on platform ${CMAKE_SYSTEM_NAME}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this could be confusing to a user if BUILD_STATIC_RUNTIME is set

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, good point. The flag is applied to both libtvm.so and (if it is being built), libtvm_runtime.so, so I wouldn't want to drop the message entirely for a static runtime build. What are your thoughts on the following phrasing?

"Forbidding undefined symbols in shared libraries libtvm and (if applicable) libtvm_runtime, using ${TVM_NO_UNDEFINED_SYMBOLS} on platform ${CMAKE_SYSTEM_NAME}"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh oops, i meant to resolve this first but got trigger happy. i don't mind that phrasing, but since it applies in all cases, i think it's probably fine as-is too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, and I'll keep it as is for now. Added an item to my todo list to update it later, but low priority since the as-is phrasing also works.

@areusch
Copy link
Contributor

areusch commented Jun 4, 2021

@Lunderberg great! I think we can merge this, I have one nit that I'm not sure if you prefer to address. if you don't have cycles for that right now, i think we should just merge--the message does say "forbidding undefined symbols in shared library"

@junrushao
Copy link
Member

@Lunderberg BTW, maybe out of the scope of this PR, just curious which libraries will have undefined symbols (other than libtvm and libtvm_runtime)?

Copy link
Contributor

@d-smirnov d-smirnov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks legitimate to me. Thanks @Lunderberg

@Lunderberg
Copy link
Contributor Author

@junrushao1994 I think it was the vta-hw build, though I'd have to tinker with it a bit to reproduce the failure.

@areusch areusch merged commit 3e34e11 into apache:main Jun 4, 2021
@Lunderberg Lunderberg deleted the tvm_runtime_so_forbid_undefined_symbols branch June 7, 2021 17:29
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Jun 17, 2021
…d symbols. (apache#8178)

If libtvm_runtime.so erroneously requires definitions that are only
present in libtvm.so, the -Wl,--no-undefined flag forces them to be
compile-time errors rather than runtime, and would be caught by the
CI.

Co-authored-by: Eric Lunderberg <[email protected]>
trevor-m pushed a commit to neo-ai/tvm that referenced this pull request Jun 17, 2021
…d symbols. (apache#8178)

If libtvm_runtime.so erroneously requires definitions that are only
present in libtvm.so, the -Wl,--no-undefined flag forces them to be
compile-time errors rather than runtime, and would be caught by the
CI.

Co-authored-by: Eric Lunderberg <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants