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

Compile Apple tools as fat binaries if possible #13452

Conversation

keith
Copy link
Member

@keith keith commented May 9, 2021

The Apple toolchain has 2 native binaries that are inputs to every
single action. Because of this if you want to share caches between Apple
Silicon machines and Intel machines, you either need to force them to be
x86_64 binaries and suffer the performance loss on Apple Silicon
machiens, or use fat binaries so the sha's match on both architectures,
which is what this change does. These binaries are so small that the
size impact of this doesn't matter. Since Apple Silicon support requires
Xcode 12 this falls back to compiling the single architecture binary if
it fails, under the assumption that means you're on Xcode 11 or lower.
We don't have a better indication at this point of what Xcode version
you're using, so this seems like a fine workaround until Xcode 12 is the
minimum supported version.

@google-cla google-cla bot added the cla: yes label May 9, 2021
@keith
Copy link
Member Author

keith commented May 9, 2021

Note this doesn't touch xcode-locator because that's not an input to other actions (or the embedded version is used instead)

@keith keith force-pushed the ks/compile-apple-tools-as-fat-binaries-if-possible branch from 519c82f to 53100e4 Compare May 10, 2021 15:17
Comment on lines +101 to +103
"-arch",
"arm64",
"-arch",
"x86_64",
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the only difference from the function above, but since you can't recursively call functions in starlark I had to duplicate this

@keith
Copy link
Member Author

keith commented May 10, 2021

I tested this with Xcode 11.7 and it works as expected by falling back to the single arch binary

@keith keith force-pushed the ks/compile-apple-tools-as-fat-binaries-if-possible branch from 53100e4 to 81c98ca Compare May 27, 2021 19:54
@brentleyjones
Copy link
Contributor

@dmaclach @allevato Any thoughts on this?

@brentleyjones
Copy link
Contributor

AFAIK, this is needed in order to kick off macOS remote execution from an Apple Silicon machine where the executors are Intel.

@brentleyjones
Copy link
Contributor

@kaylathar Per this #13873 (comment), could you please take a look at this? 🙏

@thii
Copy link
Member

thii commented Oct 1, 2021

@keith Are you using this internally? We noticed significant cache misses after using this patch.

@keith
Copy link
Member Author

keith commented Oct 1, 2021

We're not yet, we're only testing M1s atm. Did you debug the misses back to this?

@thii
Copy link
Member

thii commented Oct 1, 2021

I did a quick debug and it turned out that the fat wrapped_clang is not deterministic. Might be a bug in clang.

@thii
Copy link
Member

thii commented Oct 1, 2021

I can confirm that our cache hit rate went back to normal after reverting this change.

The root cause seems because clang adhoc signs binaries for arm64, and since the signatures are different each time, the result fat binaries would be different between builds. This results in a lot of cache misses because wrapped_clang is an input of most actions when building for Apple platforms.

Update: Building for each arch and manually lipo'ing yields a consistent binary.

@keith keith force-pushed the ks/compile-apple-tools-as-fat-binaries-if-possible branch from 81c98ca to 29eabbe Compare October 6, 2021 00:01
The Apple toolchain has 2 native binaries that are inputs to every
single action. Because of this if you want to share caches between Apple
Silicon machines and Intel machines, you either need to force them to be
x86_64 binaries and suffer the performance loss on Apple Silicon
machiens, or use fat binaries so the sha's match on both architectures,
which is what this change does. These binaries are so small that the
size impact of this doesn't matter. Since Apple Silicon support requires
Xcode 12 this falls back to compiling the single architecture binary if
it fails, under the assumption that means you're on Xcode 11 or lower.
We don't have a better indication at this point of what Xcode version
you're using, so this seems like a fine workaround until Xcode 12 is the
minimum supported version.
@keith keith force-pushed the ks/compile-apple-tools-as-fat-binaries-if-possible branch from 29eabbe to b075144 Compare October 6, 2021 00:02
@brentleyjones
Copy link
Contributor

@kaylathar this would be great to get in before the 5.0 cut 🙏

@brentleyjones brentleyjones mentioned this pull request Oct 19, 2021
9 tasks
@keith
Copy link
Member Author

keith commented Oct 20, 2021

@oquenchil can you import?

@kaylathar
Copy link
Member

kaylathar commented Oct 20, 2021

LGTM for merge

@brentleyjones
Copy link
Contributor

@comius Can you import this? 🙏

@comius
Copy link
Contributor

comius commented Oct 25, 2021

Imported.

@keith
Copy link
Member Author

keith commented Oct 25, 2021

@comius thanks, I don't see it on master yet, is something stuck?

@bazel-io bazel-io closed this in 80c56ff Oct 27, 2021
@keith keith deleted the ks/compile-apple-tools-as-fat-binaries-if-possible branch October 27, 2021 22:12
keith added a commit to keith/bazel that referenced this pull request Oct 27, 2021
The Apple toolchain has 2 native binaries that are inputs to every
single action. Because of this if you want to share caches between Apple
Silicon machines and Intel machines, you either need to force them to be
x86_64 binaries and suffer the performance loss on Apple Silicon
machiens, or use fat binaries so the sha's match on both architectures,
which is what this change does. These binaries are so small that the
size impact of this doesn't matter. Since Apple Silicon support requires
Xcode 12 this falls back to compiling the single architecture binary if
it fails, under the assumption that means you're on Xcode 11 or lower.
We don't have a better indication at this point of what Xcode version
you're using, so this seems like a fine workaround until Xcode 12 is the
minimum supported version.

Closes bazelbuild#13452.

PiperOrigin-RevId: 405842940
(cherry picked from commit 80c56ff)
Wyverald pushed a commit that referenced this pull request Oct 28, 2021
The Apple toolchain has 2 native binaries that are inputs to every
single action. Because of this if you want to share caches between Apple
Silicon machines and Intel machines, you either need to force them to be
x86_64 binaries and suffer the performance loss on Apple Silicon
machiens, or use fat binaries so the sha's match on both architectures,
which is what this change does. These binaries are so small that the
size impact of this doesn't matter. Since Apple Silicon support requires
Xcode 12 this falls back to compiling the single architecture binary if
it fails, under the assumption that means you're on Xcode 11 or lower.
We don't have a better indication at this point of what Xcode version
you're using, so this seems like a fine workaround until Xcode 12 is the
minimum supported version.

Closes #13452.

PiperOrigin-RevId: 405842940
(cherry picked from commit 80c56ff)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants