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

Avoid using ebx as an asm! operand #185

Merged
merged 3 commits into from
Oct 5, 2021
Merged

Conversation

andjo403
Copy link
Contributor

@andjo403 andjo403 commented Oct 3, 2021

when I tried to rebase rust-lang/rust#78781 I noticed that due to rust-lang/rust#84658 this code did not compile any longer.

also due to rust-lang/rust#83387 is merged we can use the intel asm and remove the duplication of all asm instructions

rust LLVM minimum version is now 10

This reverts commit 349183e.
failes compilation as it is sometimes reserved by LLVM.
makes it more clear what clobbers registers
also results in one redundant move is removed for rdpmc_pair
@michaelwoerister
Copy link
Member

r? @eddyb

Copy link
Member

@eddyb eddyb left a comment

Choose a reason for hiding this comment

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

Had to review one commit at a time, but this makes sense. Sorry I haven't gone back to this yet. I ran into the ebx thing while trying to get the standalone "SpecLockMap checker" compiling on recent nightlies, for someone, and this looks like the correct fix.

There's only one concern I have (about the cpuid(0) code size with the ebx workaround), for which I left comments.

Comment on lines 546 to 550
// LLVM sometimes reserves `ebx` for its internal use, we so we need to use
// a scratch register for it instead.
"mov {tmp_rbx:r}, rbx",
"cpuid",
"mov rbx, {tmp_rbx:r}",
Copy link
Member

Choose a reason for hiding this comment

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

I've noticed this. This is frustrating, as it makes cpuid undesirably troublesome (I wish Intel and AMD would agree on e.g. mfence/lfence being serializing). I suspect that using a LLVM intrinsic for cpuid would result in shorter codegen, can you try this? Either way, can you PM me on Zulip?

I've let that rustc PR sit, sadly (long story short, other factors forced us to abandon the grant it was part of), and it would be good to take stock of what's useful and what's just hacks we only needed to get the results we wanted.

Comment on lines +586 to +591
asm!(
"rdpmc",
in("ecx") reg_idx,
lateout("eax") lo,
lateout("edx") hi,
options(nostack)
Copy link
Member

Choose a reason for hiding this comment

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

I suppose this is good because it gives LLVM just one instruction and all of the register-wiring insight it needs.

Comment on lines +556 to +558
/// Dummy `cpuid(0)` to serialize instruction execution.
#[inline(always)]
fn serialize_instruction_execution() {
Copy link
Member

Choose a reason for hiding this comment

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

Should be able to take this fn in isolation and try it on godbolt, for the comparison with std::arch::x86_64::__cpuid(0);.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

minor difference with using arch version one extra xor and xchg instead of the mov that I used.

https://godbolt.org/z/8jv41bYsq

@eddyb eddyb merged commit b4da534 into rust-lang:master Oct 5, 2021
@andjo403 andjo403 deleted the ebx_asm branch October 5, 2021 20:28
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.

3 participants