-
Notifications
You must be signed in to change notification settings - Fork 183
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
bazel compiles everything twice: as -fpic and without ... could this be tweaked ? #1936
Comments
Should we set this to |
Might be interesting to see the impact, though ideally that would be a global setting not per test (as it might actually also have the impact of slowing down things as now each test binary is very large and needs to be linked). I am more wondering the other way: typically an object file compiled with Ther might be a reason why bazel is not doing that, but I wonder why and if that reason applies ? |
The third compilation of This of course only would make sense if there was a reason to compile tools differently than regular binaries, e.g. if So if we count the number of times the same file is compiled multiple times, then aggregate these counts, we see that we actually have 4687 files that are comiled 3 times due to that (and some files that are only compiled once: these are all the *_test.cc and *_main.cc files) grep "cc_wrapper.*std=" /tmp/allcompile-commands.txt | sed 's|.*-c \([^ ]*\).*|\1|p;d' | grep -v xls/common/build_embed.cc | sort | uniq -c | awk '{print $1}' | sort | uniq -c
3188 1
1778 2
4687 3
17 5 cross-check and compare with the numbers from #1936 (comment) $ echo "3188 + 1778 + 4687 + 17" | bc # number of unique files compiled
9670
$ echo "3188 + 1778 * 2 + 4687 * 3 + 17 * 5" | bc # number of compilations seen
20890 |
and
You can try to build with, e.g. A note about [tool] vs. not: The Exec configuration used to build tools typically sanitizes the build configuration so that you get a consistent and cacheable (and optimized) binary for use in your build pipeline. It's not always about architecture. If you turn on debug mode or assertions, you typically don't want your tools to be subject to those options (though sometimes you might). If you are concerned about tool dependencies, prebuilding is an option. Are subsequent incremental rebuilds slow as well? (i.e. is the concern primarily about cold builds?) |
Will try dymaic_mode=off, but wouldn't the other suggestion work as well ? Always build I can understand if I build a debug build and the tool should be compiled in opt mode. However in this particular case I did Since we're building our own tools as part of the build that then are used, they are affected similar to the rest of the code. (Things not changing might be tools built as part of the environment, Incremental builds will also have to compile the same file 3 times, so yes it affects them as well of course. |
Compiling with
It still runs a compile with and with pic
|
What toolchain are you using? There are unfortunately a number of knobs that could do what you want. First, maybe try |
We use the external llvm toolchain: https://github.com/google/xls/blob/main/MODULE.bazel#L7-L16 |
OK. that toolchain seems to rely on the legacy features. |
Nice, that did the trick. That now essentially reduces triple compilation to dual compilation:
Now we don't need a bit more than double but only +50%-ish of compliations:
So Is there a way to further reduce the now double compilation to single compilation for the tools ? We typically compile with |
here the example of the singled out file: we see that it now only is compiled as PIC, yay. ... and it also shows that the tool building is still there, but it actually does not adhere to the
|
Bazel compiles each *.cc with and without position independent code to link dynamically against tests and statically for binaries. However, we can use the PIC-compiled object files as well to link static binaries. This removes one compiler invocation per *.cc file. We still have an additional compilation for tool compilations (as we use dslx in our own *.bzl toolchains). So we only went from 3x overhead to 2x overhead, so more possibly to be done. Issues: #1936 PiperOrigin-RevId: 728824079
This is now enabled in the To be tested if this works on macOS; if not, we can make it a platform-specific |
I can confirm that on three machines I tested at home, |
[this is probably more a meta question about how bazel deals with compilations, but it is interesting in this context as XLS is a particularly large project for playing along at home]
Compiling XLS on machines even with many cores is still taking some time; my laptop with 20 cores takes about 2.5 hours.
Let's compile, extract the compile commands, and remember these:
Now, let's fish out all the compiler invocations, extract the source filename after
-c
, and see how many compiler invocations there are.There is one file
xls/common/build_embed.cc
that is compiled particularly often, but that seems to just be due to thelinkstamp
setting in//xls/common:init_xls
, so let's ignore that for now; still over 20k compilations:Looking at the output, it appears that the same file is mentioned more than once... let's get the data and see how many times the same file is mentioned uniquely:
That is ... less than half!
Let's just look at just one file, and also capture the rest of the command line to see the
-o
output, as these are probably different:yes indeed: apparently every file is compiled 'regular' and as
-fPIC
position independent code. And this one apparently into yet another location ?All the binaries are linked statically, so they don't need
-fPIC
object files to create shared libraries. But looks like all the unit tests are linked dynamically:This is probably done to avoid linking large binaries for all the tests, however this means every source file is compiled twice!
I wonder if the
-fPIC
object files could also be used for the statically linked version (just linking them in a static context) to avoid the multi-compilation, and if there are options that I can give to bazel to achieve that.The text was updated successfully, but these errors were encountered: