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

x86_cpuid_halide must preserve all 64 bits of rbx/rsi #6409

Merged
merged 1 commit into from
Nov 12, 2021

Conversation

steven-johnson
Copy link
Contributor

The existing code attempts to preserve ebx (since the cpuid instruction can trash it), but it only preserves the lower 32 bits; on 64-bit systems, this (amazingly) usually works OK unless you are compiling in (e.g.) ASAN mode, which can subtly change codegen such that the full 32 bits of rbx must be preserved.

I'm genuinely astonished this hasn't bitten us before now!

The existing code attempts to preserve ebx (since the cpuid instruction can trash it), but it only preserves the lower 32 bits; on 64-bit systems, this (amazingly) usually works OK unless you are compiling in (e.g.) ASAN mode, which can subtly change codegen such that the full 32 bits of rbx must be preserved.

I'm genuinely astonished this hasn't bitten us before now!
@abadams
Copy link
Member

abadams commented Nov 12, 2021

I think it hasn't bitten us because the inline assembly marks ebx as clobbered, so there's no actual need to restore it. For the purposes of register allocation, that causes llvm to push/pop all of rbx in the containing function (because it's callee-saved), and then not otherwise use rbx. So the effect of marking ebx as clobbered is, most of the time, to get llvm to not use rbx in that function, and to save and restore it at the containing function entry/exit. Asan mode must be doing something different that makes the compiler actually care about the difference between clobbering ebx and clobbering rbx.

@abadams
Copy link
Member

abadams commented Nov 12, 2021

So +1 to the change, but I'm confused about why we explicitly save and restore e/rbx in the inline assembly as well as marking it as a clobber.

@steven-johnson steven-johnson merged commit 02a394d into master Nov 12, 2021
@steven-johnson steven-johnson deleted the srj/broken-cpuid branch November 12, 2021 03:25
@abadams abadams added the backport me This change should be backported to release versions label Nov 12, 2021
derek-gerstmann pushed a commit to derek-gerstmann/Halide that referenced this pull request Nov 12, 2021
The existing code attempts to preserve ebx (since the cpuid instruction can trash it), but it only preserves the lower 32 bits; on 64-bit systems, this (amazingly) usually works OK unless you are compiling in (e.g.) ASAN mode, which can subtly change codegen such that the full 32 bits of rbx must be preserved.

I'm genuinely astonished this hasn't bitten us before now!

(cherry picked from commit 02a394d)
derek-gerstmann pushed a commit that referenced this pull request Nov 15, 2021
The existing code attempts to preserve ebx (since the cpuid instruction can trash it), but it only preserves the lower 32 bits; on 64-bit systems, this (amazingly) usually works OK unless you are compiling in (e.g.) ASAN mode, which can subtly change codegen such that the full 32 bits of rbx must be preserved.

I'm genuinely astonished this hasn't bitten us before now!

(cherry picked from commit 02a394d)
@alexreinking alexreinking added bug and removed backport me This change should be backported to release versions labels Jan 4, 2022
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