Repository navigation
Build model methods with -fno-gnu-unique on Linux with GCC, so that they can be loaded together with rstan - #1327
Merged
Merged
Conversation
…methods # Conflicts: # R/expose.R
Member
|
Thanks! I'll test this now. |
R sets CXX_VISIBILITY for each compiler, while -fno-gnu-unique needed a GCC check because clang rejects it.
Member
|
A few changes I pushed to test them on CI:
|
Member
Author
|
|
jgabry
approved these changes
Oct 14, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## v1.0 #1327 +/- ##
=======================================
Coverage ? 93.95%
=======================================
Files ? 19
Lines ? 9158
Branches ? 0
=======================================
Hits ? 8604
Misses ? 554
Partials ? 0 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1177.
Fixes the "mixed mode" crash in #1177: on Linux with GCC, R segfaults when the model methods and rstan are loaded in the same R process, in either order.
This PR was assisted by Claude
Summary
Cause. GCC emits the static data of templates such as
stan::math::AutodiffStackSingleton<vari_base, chainable_alloc>asSTB_GNU_UNIQUEsymbols. The dynamic loader binds a unique symbol once for the whole process, also for libraries loaded withRTLD_LOCAL. The model methods library andrstan.soboth defineAutodiffStackSingleton<...>::instance_andinit()::is_initialized. So the library that loads second uses the autodiff stack of the first one, which comes from another Stan Math build, and it segfaults in its static initialization. The TBB is not the cause: the crash also happens when only one TBB is in the process (RcppParallel 6.2.1 withrcppparallel_tbb()active), and it does not happen with-fno-gnu-uniquewhile two TBB builds are in the process (RcppParallel 5.1.11.2 and TBB 2020.3).Change.
rcpp_source_stan()adds-fno-gnu-uniquetoPKG_CXXFLAGSon Linux when R's C++ compiler is GCC. The symbols then become ordinary weak symbols, and each library keeps its own autodiff stack. This covers$init_model_methods()and$expose_functions().r_cxx_is_gcc(): runsR CMD config CXXand<CXX> --versionand looks for "Free Software Foundation", which GCC prints and clang does not. A leadingccacheorsccacheis skipped. The result is cached in.cmdstanr. clang does not emitSTB_GNU_UNIQUE, so it does not need the flag.test-model-methods.R:$init_model_methods()andloadNamespace("rstan")in a fresh R process, in both orders, with the cmdstanr under test. It runs only on Linux with rstan installed.Results.
test-model-methods.Rtest-model-expose-functions.RTested on Ubuntu 22.04 (R 4.6.1, g++ 11.4.0, CmdStan 2.40.0) and on a RHEL 9 cluster (R 4.5.3, GCC 13.3.0, CmdStan 2.40.0-rc0), with rstan 2.36.0.9000, StanHeaders 2.36.0.9000 and RcppParallel 5.1.11.2. The two orders also work with RcppParallel 6.2.1, StanHeaders 2.39.1 and rstan 2.39.0.9000, where
rcppparallel_tbb()is active.Not tested: clang on Linux. The PR does not pass the flag to clang.
rstan has the same unique symbols, but the change on the cmdstanr side is enough: when one of the two libraries does not take part in the unique binding, each keeps its own autodiff stack.
Copyright and Licensing
Please list the copyright holder for the work you are submitting
(this will be you or your assignee, such as a university or company):
Aalto University
By submitting this pull request, the copyright holder is agreeing to
license the submitted work under the following licenses: