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

Inline asm docs incorrectly state that k0 (x86 AVX-512) is hard-wired to zero #94977

Closed
pcordes opened this issue Mar 15, 2022 · 1 comment · Fixed by #95740
Closed

Inline asm docs incorrectly state that k0 (x86 AVX-512) is hard-wired to zero #94977

pcordes opened this issue Mar 15, 2022 · 1 comment · Fixed by #95740
Labels
A-inline-assembly Area: Inline assembly (`asm!(…)`) C-bug Category: This is a bug.

Comments

@pcordes
Copy link

pcordes commented Mar 15, 2022

https://doc.rust-lang.org/nightly/reference/inline-assembly.html#register-names
says about k0 "This is a constant zero register which can't be modified."
That's the same phrasing as AArch64 xzr, so I assume the author thought it was architecturally hard-wired, rather than a software choice to keep it zeroed (like often done with AVR r0). (Which I don't think would make much sense; most instructions with an all-zero mask are no-ops so you don't normally want one.)

k0 is special in that it can't be used as a mask, because that register number means no-masking if used in an instruction like vmovaps (%rdi), %xmm0{%k1}{z}. But it can be the destination of instructions like compare-into-mask or vptestmd %ymm0, %ymm0, %k0, and then stored to memory or copied to a GP-integer reg with kmovd %k0, %eax, tested with kortest, combine with other masks with kunpck or kadd or whatever.

Leaving it unused prevents false dependencies when materializing an all-ones mask with kxnord %k0, %k0, %k1 (e.g. before a gather), but otherwise there's no reason to avoid it if your uses for that mask don't include applying masking to other instructions.

You do definitely want there to be a constraint that can only pick k1..7, not k0, like GNU C inline asm's Yk (https://stackoverflow.com/questions/55946103/gnu-c-inline-asm-input-constraint-for-avx512-mask-registers-k1-k7), but there's no reason to disallow k0 as an explicit choice. If people don't want to know about details like k0 being special, they can use intrinsics.

Probably not much reason to invent a constraint like Yk but which can pick k0..7, though, but it's possible it could help register allocation of mask regs with surrounding code. (But would be more documentation to wade through, and could end up wasting some people's time if they find the constraint which can pick k0 when they need one which can't.)

@pcordes pcordes added the C-bug Category: This is a bug. label Mar 15, 2022
@pcordes pcordes changed the title Inline asm docs incorrectly state the k0 (x86 AVX-512) is hard-wired to zero Inline asm docs incorrectly state that k0 (x86 AVX-512) is hard-wired to zero Mar 15, 2022
@ehuss
Copy link
Contributor

ehuss commented Mar 16, 2022

cc @Amanieu

@ehuss ehuss added the A-inline-assembly Area: Inline assembly (`asm!(…)`) label Mar 16, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Apr 19, 2022
asm: Add a kreg0 register class on x86 which includes k0

Previously we only exposed a kreg register class which excludes the k0
register since it can't be used in many instructions. However k0 is a
valid register and we need to have a way of marking it as clobbered for
clobber_abi.

Fixes rust-lang#94977
@bors bors closed this as completed in b2bc469 Apr 20, 2022
antoyo pushed a commit to antoyo/rust that referenced this issue Jun 7, 2022
Previously we only exposed a kreg register class which excludes the k0
register since it can't be used in many instructions. However k0 is a
valid register and we need to have a way of marking it as clobbered for
clobber_abi.

Fixes rust-lang#94977
antoyo pushed a commit to antoyo/rust that referenced this issue Jun 7, 2022
asm: Add a kreg0 register class on x86 which includes k0

Previously we only exposed a kreg register class which excludes the k0
register since it can't be used in many instructions. However k0 is a
valid register and we need to have a way of marking it as clobbered for
clobber_abi.

Fixes rust-lang#94977
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inline-assembly Area: Inline assembly (`asm!(…)`) C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants