Skip to content

Use emit_ptr_va_arg for va_arg on AVR - #165462

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
beetrees:va-arg-avr
Nov 8, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
beetrees:va-arg-avr

Conversation

@beetrees

@beetrees beetrees commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

This avoids using LLVM's va_arg instruction and also gives much better codegen for types larger than two bytes. This matches GCC and LLVM (checked by comparing the assembly).

Ping target maintainer @Patryk27

r? @folkertdev

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 4, 2026
@beetrees beetrees added the O-AVR Target: AVR processors (ATtiny, ATmega, etc.) label Nov 4, 2026
Comment on lines +32 to 33
// CHECK-NEXT: ret
ap.next_arg()

@folkertdev folkertdev Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Did you configure GCC in any particular way to make the checking easier?

https://godbolt.org/z/Ev8nMhd9P

The GCC output looks quite different, and also clang's output looks way shorter than what we asserted before.

View changes since the review

@beetrees beetrees Nov 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I mainly just compared the effects of the assembly, as GCC and Clang have differing opinions on instruction selection. Using -mmcu=atmega328p to match the test will allow GCC/Clang to use the movw, increasing the similarity. __tmp_reg__ is just r0. subi r18,-2, sbci r19,-1 is just GCC's way of adding 2 to r19:r18 (the subtraction effectively adds two by subtracting 254, and the carry will make sbci r19, -1 a no-op, unless their is no carry which means that the addition would have overflowed which means sbci needs to add 1 to r19 by subtracting 255). X+ increments X after the operation.

The LLVM va_arg instruction has a repeating pattern that it emits for every two bytes of the argument (that's why the assembly for read_i16 was the only assembly that didn't change). Trying a longer type like long long (8 bytes, instead of int's two bytes) results in Clang emitting assembly comparable to the old assembly in the test.

@rust-bors

This comment has been minimized.

@Patryk27 Patryk27 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@rustbot

rustbot commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator

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.

@folkertdev folkertdev left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@rust-bors

rust-bors Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 97854e6 has been approved by folkertdev

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 7, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 8, 2026
Rollup of 8 pull requests

Successful merges:

 - #164720 (Expand E0271 on return type)
 - #165301 (Fix struct-literal suggestion spans in macros)
 - #165573 ([tiny] Tweak incorrect `#[default]` errors)
 - #164036 ([rustdoc] Allow `.` to be used the same as `::` in the search)
 - #165457 (tests: llvm 24 can merge addrspace declarations again)
 - #165462 (Use `emit_ptr_va_arg` for `va_arg` on AVR)
 - #165609 (OIDC artifact pushes)
 - #165620 (change return type of FloatErrorKind::kind)
rust-bors Bot pushed a commit that referenced this pull request Nov 8, 2026
Rollup of 8 pull requests

Successful merges:

 - #164720 (Expand E0271 on return type)
 - #165301 (Fix struct-literal suggestion spans in macros)
 - #165573 ([tiny] Tweak incorrect `#[default]` errors)
 - #164036 ([rustdoc] Allow `.` to be used the same as `::` in the search)
 - #165457 (tests: llvm 24 can merge addrspace declarations again)
 - #165462 (Use `emit_ptr_va_arg` for `va_arg` on AVR)
 - #165609 (OIDC artifact pushes)
 - #165620 (change return type of FloatErrorKind::kind)
@rust-bors
rust-bors Bot merged commit 575f178 into rust-lang:main Nov 8, 2026
14 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 8, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 8, 2026
Rollup merge of #165462 - beetrees:va-arg-avr, r=folkertdev

Use `emit_ptr_va_arg` for `va_arg` on AVR

This avoids using LLVM's `va_arg` instruction and also gives much better codegen for types larger than two bytes. This matches GCC and LLVM (checked by comparing the assembly).

Ping target maintainer @Patryk27

r? @folkertdev
@beetrees
beetrees deleted the va-arg-avr branch November 8, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. O-AVR Target: AVR processors (ATtiny, ATmega, etc.) S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants