Skip to content

Conversation

@TDecking
Copy link
Contributor

@TDecking TDecking commented May 8, 2025

The current minimum LLVM version seems to be 18, way larger than required for resolving this FIXME.

core::arch::asm!(
"repe movsb (%rsi), (%rdi)",
asm!(
"repe movsb [rdi], rsi",
Copy link
Contributor

Choose a reason for hiding this comment

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

In the original asm both rsi and rdi are dereferenced, here it’s only rdi. Is this still equivalent for some reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think it is. I fixed this.

core::arch::asm!(
"repe movsb (%rsi), (%rdi)",
asm!(
"repe movsb [rdi], [rsi]",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"repe movsb [rdi], [rsi]",
"rep movsb [rdi], [rsi]",

The repe mnemonic is for "repeat while equal" and only makes sense for the string comparison operations
https://www.felixcloutier.com/x86/rep:repe:repz:repne:repnz
(but the encoding is the same so I guess assemblers don't care)

core::arch::asm!(
"repe stosb %al, (%rdi)",
asm!(
"repe stosb [rdi], al",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"repe stosb [rdi], al",
"rep stosb [rdi], al",

inout("rsi") src.add(count - 1) => _,
// We modify flags, but we restore it afterwards
options(att_syntax, nostack, preserves_flags)
options(nostack, preserves_flags)
Copy link
Contributor

Choose a reason for hiding this comment

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

This definitely shouldn't have preserves_flags. Flags are modified by each of add,sub,test. The comment refers to how std/cld are used to set/clear the direction flag, but that's separate since it must be cleared before exiting the inline assembly.

Actually, what is the point of the test instruction here? It only affects flags, and none of the subsequent instructions depend on any flags.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it can be removed: ae557bd had an early exit but ef37a23 removed it.

Copy link
Contributor

Choose a reason for hiding this comment

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

This diff gave me a similar question about

"2:",
"sub $0x1000,%rcx",
"test %rcx,(%rcx)",
"sub $0x1000,%rax",
"cmp $0x1000,%rax",
"ja 2b",
"1:",
. Aren't flags set by test overwritten by the sub then cmp before ja checks them?

Copy link
Contributor

@quaternic quaternic Dec 5, 2025

Choose a reason for hiding this comment

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

Yeah, that looked weird at first, but I think the test there just used as a way to load from memory without modifying any registers (except flags). So the loop is just reading from memory at 4kB intervals for the side-effects only, which is exactly what a chkstk should be doing.

Comment on lines 43 to 47
"test %ecx,(%ecx)",
"lea 4(%esp),%eax", // load pointer to the return address into eax
"mov %ecx,%esp", // install the new top of stack pointer into esp
"mov -4(%eax),%ecx", // restore ecx
"push (%eax)", // push return address onto the stack
"sub %esp,%eax", // restore the original value in eax
"3:",
"sub ecx, eax",
"test [ecx], ecx",
"lea eax, [esp + 4]", // load pointer to the return address into eax
"mov esp, ecx", // install the new top of stack pointer into esp
"mov ecx, [eax - 4]", // restore ecx
"push [eax]", // push return address onto the stack
"sub eax, esp", // restore the original value in eax
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: comments used to be aligned

@tgross35
Copy link
Contributor

tgross35 commented Dec 5, 2025

@quaternic the issues you mentioned seem to be preexisting. Would you mind putting up a PR to fix them? We can do that independent of the syntax swap.

Then @TDecking you won't have to make any changes aside from resolving conflicts. Would you be able to rebase?

@quaternic
Copy link
Contributor

@tgross35 I can, but I'd much prefer doing that in intel syntax, so merging this with those preexisting issues would be fine.

@rustbot
Copy link
Collaborator

rustbot commented Dec 5, 2025

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@TDecking
Copy link
Contributor Author

TDecking commented Dec 5, 2025

I've rebased and removed the final instances of att_syntax in probestack.rs.

@TDecking TDecking requested a review from tgross35 December 5, 2025 16:31
Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

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

LGTM as a 1:1 change, thanks for the updates!

@tgross35 tgross35 merged commit f867373 into rust-lang:main Dec 5, 2025
38 checks passed
@TDecking TDecking deleted the att-syntax branch December 5, 2025 19:25
tgross35 pushed a commit that referenced this pull request Dec 6, 2025
* `repe` is "repeat while equal", which only makes sense for string
  comparisons. Change it to `rep`. (The encoding is the same so there is
  no performance change.)
* Remove an unneeded `test`. This was added in ae557bd ("Skip rep
  movsb in copy_backward if possible"). The `jz` was removed in
  ef37a23 ("Remove branches around rep movsb/stosb") but the `test`
  was missed.
* Remove an incorrect `preserves_flags`; `add` and `sub` affect flags.

Discussion: #911
Fixes: ef37a23 ("Remove branches around rep movsb/stosb")
Fixes: c30322a ("Align destination in mem* instructions.")

[ Added details to the commit message - Trevor ]
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