Skip to content

Fix and speedup builds with TSan#13201

Merged
gasche merged 4 commits intoocaml:trunkfrom
shindere:fix-speedup-tsan-builds
Jun 4, 2024
Merged

Fix and speedup builds with TSan#13201
gasche merged 4 commits intoocaml:trunkfrom
shindere:fix-speedup-tsan-builds

Conversation

@shindere
Copy link
Copy Markdown
Contributor

This PR fixes issue #13042 and also addresses the problem PR #12781 attempted to resolve.

Both of these are instances of artefacts for bytecode programs or libraries being instrumented whereas they should not be.

The present PR makes sure only the artefacts related to native programs and libraries get instrumented. As a side effect, the build time for the compiler when configured with --endable-tsan is considerably reduced, as can be seen from these results obtained with make -j:

Trunk:

real    6m27,551s
user    41m52,165s
sys     7m14,887s

PR branch:

real    3m18,914s
user    18m52,346s
sys     1m57,559s

This PR consists of the four following commits:

  1. Variable renamings local to the configure script.
  2. Make sure runtime/tsan.c is compiled and linked only for native programs and libraries.
  3. Restoring the possibility to use different C and C preprocessor flags when compiling C programs in bytecode and native contexts.
  4. Taking advantage of the previous commit to make sure only the bytecode-related artefacts get instrumented.

Of these four commits, the third one can certainly be seen as the core of this PR. It basically reverts 2b4fe09, which had been submitted as part of PR #8631. However, the present PR proposes new names for the different types of flags, which are based on the backend for which we compile rather than on the name of the associated compiler, since the backend names seem more stable than the compiler names. So we use bytecode_cflags rather than ocamlc_cflags, etc., for the day when ocamlc and ocamlopt will have a unified compiler driver. ;)

This work may be of interest to @fabbing and @OlivierNicole, among others.

Copy link
Copy Markdown

@ghost ghost left a comment

Choose a reason for hiding this comment

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

I was worried about the removal of -fsanitize=thread from cclibs in configure.ac (last chunk), but it turns out to be indeed unnecessary as long as *cflags are correct.

@gasche
Copy link
Copy Markdown
Member

gasche commented May 30, 2024

There are weird mscv64 failure logs in the CI, and I don't know if they are a fluke (a bug in the mscv64 CI in trunk at the time, that was fixed since) or related to the PR. @shindere, could you rebase the PR on top of the current trunk, so that the CI reruns in a known-good configuration?

@shindere
Copy link
Copy Markdown
Contributor Author

shindere commented May 30, 2024 via email

@ghost
Copy link
Copy Markdown

ghost commented May 30, 2024

I will rebase yes, and intend to go through the PR again because I think there is still room for improvemnet. In particular, I think it's important to pass the TSan flags at link time because, as I understood it, some platforms do require that.

All platforms do - TSan-enabled binaries need to be linked against the TSan runtime library.

@gasche gasche added the run-thread-sanitizer Makes the CI run the testsuite with TSAN enabled label May 30, 2024
@shindere shindere force-pushed the fix-speedup-tsan-builds branch 4 times, most recently from 1e11bed to 76852d3 Compare May 30, 2024 15:09
@shindere
Copy link
Copy Markdown
Contributor Author

I just pushed an updated version of the PR that passes CI.

(I know it does because it did and I just squashed a tentative fix in the
commit it was actually fixing.)

Basically, the problem was that there were some flags missing in those
ocamlopt must pass to the C files it compiles to be linked with native code.

This is, in turn, the consequence of two things. On the one hand, it is
currently true that the C flags used for native code are a superset of those
used for bytecode. On the other hand, this assumption, which is true
currently but may not always be true, is expressed at several places in the
build system, namely everytime we say that the native flags are the bytecode
flags plus something else.

I think this should be fixed and the build system shoudl not compute the
native code flags in terms of the bytecode ones. It should rather use the
native code flags, whatever they are, and those should be computed
once and for all during the configure stage, where we can compute
them in connection with the bytecode flags as appropriate.
But that's quite a bit of work and quite orthogonal to the current
PR so I would like to leave it for a dedicated PR which I intend to
submit soon.

@shindere shindere force-pushed the fix-speedup-tsan-builds branch from 76852d3 to c0f8478 Compare May 31, 2024 13:21
shindere added 3 commits June 3, 2024 11:36
This commit removes the oc_ prefix from the tsan_cflags and tsan_cppflags
configure variables since the OC_ prefix is used only for build variables
which have a non OC_ prefixed counterpart that can be defined by
users to provide additional flags.
To make it possible to instrument only native code, we need to have
two sets of flags to be passed to the C compiler, one when called
by ocamlc and one when called by ocamlopt.

This commit re-introduces this distinction by reverting
commit 2b4fe09 (PR ocaml#8631).
@shindere shindere force-pushed the fix-speedup-tsan-builds branch from c0f8478 to 29e854b Compare June 3, 2024 09:38
configure.ac Outdated
[PTHREAD_LIBS=''],
[AX_PTHREAD(
[common_cflags="$common_cflags $PTHREAD_CFLAGS"
AC_MSG_NOTICE([SEB: common_cflags="$common_cflags"])
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You forgot that leftover debug message.

Suggested change
AC_MSG_NOTICE([SEB: common_cflags="$common_cflags"])

@shindere shindere force-pushed the fix-speedup-tsan-builds branch from 29e854b to 3dcf8ec Compare June 3, 2024 10:12
@shindere
Copy link
Copy Markdown
Contributor Author

shindere commented Jun 3, 2024 via email

This commit does two things:

1. Add TSAN CPP flags to native CPP flags rather than to common CPP flags

2. Remove -fsanitize=thread from CCLIBS
@shindere shindere force-pushed the fix-speedup-tsan-builds branch from 3dcf8ec to 3d3f099 Compare June 4, 2024 09:40
Copy link
Copy Markdown
Contributor

@OlivierNicole OlivierNicole left a comment

Choose a reason for hiding this comment

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

This looks good to me too (after some discussion with the author to learn more about the workings of autoconf and some naming conventions).

I’m also very happy to speed up TSan and above all get rid of those CI false positives.

Comment on lines +179 to +192
# The following variable defines flags to be passed to the C preprocessor
# when compiling C files to be linked with native code. This includes
# the native runtime itself and can also include the stub code around
# C libraries when it needs to be different from the one used to
# link with bytecode.

# These flags should be passed *in addition* to those in OC_CPPFLAGS, they
# should not replace them.

OC_NATIVE_CPPFLAGS=@native_cppflags@

# Same as above, for CFLAGS
OC_NATIVE_CFLAGS=@native_cflags@

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As I understand it from private discussion, these two sets of flags cannot currently be complemented by the user, but may be in the future (which is fine).

@shindere
Copy link
Copy Markdown
Contributor Author

shindere commented Jun 4, 2024 via email

@gasche gasche merged commit 2ed20b9 into ocaml:trunk Jun 4, 2024
@shindere
Copy link
Copy Markdown
Contributor Author

shindere commented Jun 4, 2024 via email

@anmonteiro
Copy link
Copy Markdown
Contributor

So we use bytecode_cflags rather than ocamlc_cflags, etc., for the day when ocamlc and ocamlopt will have a unified compiler driver. ;)

Could someone provide a stronger argument for breaking every single user of ocamlc -config starting in 5.3 other than the hand-wavy "look, guys, it'll be better when the compiler driver is unified"?

@gasche
Copy link
Copy Markdown
Member

gasche commented Jun 15, 2024

I think that you are reporting an unintended regression, but there is too much sarcasm and not enough information to know what the regression is and how to fix it. Could you try again with a gentler explanation of the issue?

@anmonteiro
Copy link
Copy Markdown
Contributor

No sarcasm intended, this is a pretty serious regression. On OCaml 5.2:

$ ocamlc.opt -config
...
ocamlc_cflags:  -O2 -fno-strict-aliasing -fwrapv -pthread   -pthread
ocamlc_cppflags:  -D_FILE_OFFSET_BITS=64
ocamlopt_cflags:  -O2 -fno-strict-aliasing -fwrapv -pthread   -pthread
ocamlopt_cppflags:  -D_FILE_OFFSET_BITS=64
...

On OCaml trunk (what will be 5.3):

$ ocamlc.opt -config
...
bytecode_cflags:  -O2 -fno-strict-aliasing -fwrapv -pthread   -pthread
bytecode_cppflags:  -D_FILE_OFFSET_BITS=64
native_cflags:  -O2 -fno-strict-aliasing -fwrapv -pthread   -pthread
native_cppflags:  -D_FILE_OFFSET_BITS=64  -DNATIVE_CODE -DTARGET_arm64 -DMODEL_default -DSYS_macosx
...

Tooling such as dune relies on those variables being present.

@gasche
Copy link
Copy Markdown
Member

gasche commented Jun 15, 2024

Thanks! This sounds easy to fix by undoing the renaming from ocamlc, ocamlopt to bytecode, native, which is pleasant but not worth any userland breakage.

@gasche
Copy link
Copy Markdown
Member

gasche commented Jun 15, 2024

(I'm not planning to work on this myself right away as I suppose that @shindere or @OlivierNicole will probably do it, but of course anyone else is welcome to propose a PR.)

@anmonteiro
Copy link
Copy Markdown
Contributor

If there's a strong desire to rename theses variables, you can still introduce the new variables alongside the current ones and establish some kind of deprecation strategy.

That's what puzzled me so much, given the OCaml compiler's track record of not introducing breaking changes like these.

@shindere
Copy link
Copy Markdown
Contributor Author

shindere commented Jun 17, 2024 via email

@gasche
Copy link
Copy Markdown
Member

gasche commented Jun 17, 2024

I think that we should just go back to the old names. ocamlc and ocamlopt are clear enough and the new names are marginally nicer, but not worth the work of deprecating and communicating about it and asking our users to change their practices and and... This is a standard "perfect is the enemy of good" situation.

unless people feel there is a need for a dedicated PR

The renaming would be a very small change that can easily be reviewed and merged, and un-break trunk for our downstream users. I would be in favor of doing this now instead of tying it to something bigger.

@shindere
Copy link
Copy Markdown
Contributor Author

shindere commented Jun 17, 2024 via email

@gasche
Copy link
Copy Markdown
Member

gasche commented Jun 17, 2024

In my opinion the priority is to un-break the various CI systems that are following trunk to make the ecosystem better ahead of time, with the minimal change that achieves it. I can either (1) just let you do whatever you want, even if I don't like the result, because other approaches lead to potential conflicts, or (2) do the minimal thing myself. I guess that a last option is to (3) revert the present PR from trunk for the time being. What do you think is best?

@shindere
Copy link
Copy Markdown
Contributor Author

shindere commented Jun 17, 2024 via email

shindere added a commit to shindere/ocaml that referenced this pull request Jun 18, 2024
Thes e variables have been renamed by ocaml#13201 but should actually be
preserved so that the use of `ocamlc -config` remains backward-compatible.
shindere added a commit to shindere/ocaml that referenced this pull request Jun 18, 2024
These variables have been renamed by ocaml#13201 but should actually have been
preserved so that the use of `ocamlc -config` remains backward-compatible.
@shindere
Copy link
Copy Markdown
Contributor Author

Just opened #13244 to fix ocamlc -config

shindere added a commit to shindere/ocaml that referenced this pull request Jun 18, 2024
These variables have been renamed by ocaml#13201 but should actually have been
preserved so that the use of `ocamlc -config` remains backward-compatible.
punchagan added a commit to punchagan/sandmark that referenced this pull request Jun 21, 2024
This reverts commits 19c98b1 and
7e5deb0. Upstream restored the
variables that were removed in ocaml/ocaml#13201 in ocaml/ocaml#13244
for backward-compatibility.
punchagan added a commit to ocaml-bench/sandmark that referenced this pull request Jun 21, 2024
This reverts commits 19c98b1 and
7e5deb0. Upstream restored the
variables that were removed in ocaml/ocaml#13201 in ocaml/ocaml#13244
for backward-compatibility.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-thread-sanitizer Makes the CI run the testsuite with TSAN enabled

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants