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

Insufficient Optimization of Variadic Functions for Some Targets #120355

Open
jonathan-gruber-jg opened this issue Dec 18, 2024 · 0 comments
Open

Comments

@jonathan-gruber-jg
Copy link

jonathan-gruber-jg commented Dec 18, 2024

Clang insufficiently optimizes variadic functions for some target architectures. I only tested the target architectures aarch64 and riscv64, so I'm unsure about other target architectures.

Below is a minimal test case:

#include <stdarg.h>

void yes_ap(int dummy, ...) {
	va_list ap;

	va_start(ap, dummy);

	va_end(ap);
}

void no_ap(int dummy, ...) {
	/* Nothing. */
}

Host system: Arch Linux, x86_64.

Clang version: official Arch Linux package of clang, version 18.1.8-4.

Command line to reproduce results: clang -c test.c -O<opt-level>.

aarch64 assembly with -O2, -O3, -Os, or -Oz is along the lines of this:

yes_ap:
	sub	sp, sp, #192
	stp	x1, x2, [sp, #136]
	stp	x3, x4, [sp, #152]
	stp	x5, x6, [sp, #168]
	str	x7, [sp, #184]
	stp	q0, q1, [sp]
	stp	q2, q3, [sp, #32]
	stp	q4, q5, [sp, #64]
	stp	q6, q7, [sp, #96]
	add	sp, sp, #192
	ret

no_ap:
	sub	sp, sp, #192
	stp	x1, x2, [sp, #136]
	stp	x3, x4, [sp, #152]
	stp	x5, x6, [sp, #168]
	str	x7, [sp, #184]
	stp	q0, q1, [sp]
	stp	q2, q3, [sp, #32]
	stp	q4, q5, [sp, #64]
	stp	q6, q7, [sp, #96]
	add	sp, sp, #192
	ret

riscv64 assembly with -O2, -O3, -Os, or -Oz is along the lines of this:

yes_ap:
	addi	sp, sp, -64
	sd	a7, 56(sp)
	sd	a6, 48(sp)
	sd	a5, 40(sp)
	sd	a4, 32(sp)
	sd	a3, 24(sp)
	sd	a2, 16(sp)
	sd	a1, 8(sp)
	addi	sp, sp, 64
	ret

no_ap:
	addi	sp, sp, -64
	sd	a7, 56(sp)
	sd	a6, 48(sp)
	sd	a5, 40(sp)
	sd	a4, 32(sp)
	sd	a3, 24(sp)
	sd	a2, 16(sp)
	sd	a1, 8(sp)
	addi	sp, sp, 64
	ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant