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

__builtin_nondeterministic_value is converted to a zero #101121

Open
dzaima opened this issue Jul 30, 2024 · 2 comments
Open

__builtin_nondeterministic_value is converted to a zero #101121

dzaima opened this issue Jul 30, 2024 · 2 comments

Comments

@dzaima
Copy link

dzaima commented Jul 30, 2024

The code:

void f(int x, int a, int b) {
    ext(__builtin_nondeterministic_value(5), a, b);
}

gets optimized to ext(0, a, b);, thus ending up with an unnecessary zeroing of a register. IR-wise, a freeze i32 poison becomes 0 during instcombine. https://godbolt.org/z/6rxsdr634

This means that __builtin_nondeterministic_value is unsuitable for efficiently calling a function where the caller knows that the callee won't use some argument(s). The only option that roughly works that I've found is to read an uninitialized variable, but that produces a warning and breaks under memorysanitizer.

Passing the pre-instcombine IR directly to the backend generates the desired assembly: https://godbolt.org/z/on6W17ex4

@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed new issue labels Jul 30, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 30, 2024

@llvm/issue-subscribers-clang-frontend

Author: dzaima (dzaima)

The code:
void f(int x, int a, int b) {
    ext(__builtin_nondeterministic_value(5), a, b);
}

gets optimized to ext(0, a, b);, thus ending up with an unnecessary zeroing of a register. IR-wise, a freeze i32 poison becomes 0 during instcombine. https://godbolt.org/z/6rxsdr634

This means that __builtin_nondeterministic_value is unsuitable for efficiently calling a function where the caller knows that the callee won't use some argument(s). The only option that roughly works that I've found is to read an uninitialized variable, but that produces a warning and breaks under memorysanitizer.

Passing the pre-instcombine IR directly to the backend generates the desired assembly: https://godbolt.org/z/on6W17ex4

@AaronBallman AaronBallman added llvm:instcombine and removed clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 30, 2024
@shafik
Copy link
Collaborator

shafik commented Jul 30, 2024

This is working as intended AFAICT: https://reviews.llvm.org/D142388

CC @ManuelJBrito

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

5 participants