Skip to content

[CMake] Fix __builtin_thread_pointer check with LTO #70968

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

Merged
merged 1 commit into from
Nov 5, 2023

Conversation

thesamesam
Copy link
Member

With LTO, gcc's IPA passes might drop the foo() function and then the test will pass even on platforms where __builtin_thread_pointer is unavailable.

On PPC64, we get this as a result:

llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp:361:61: error: ‘__builtin_thread_pointer’ is not supported on this targ

Just mark the function in the CMake configure test with the 'used' attribute to avoid it being optimised out. The test then behaves correctly with -flto.

Reported-by: matoro

@llvmbot llvmbot added the cmake Build system in general and CMake in particular label Nov 1, 2023
@thesamesam thesamesam force-pushed the cmake-fix-builtin-check branch from bb5bf26 to 5cfd3bc Compare November 1, 2023 18:43
@thesamesam thesamesam requested a review from MaskRay November 2, 2023 10:22
@MaskRay
Copy link
Member

MaskRay commented Nov 4, 2023

With LTO, gcc's IPA passes might drop the foo() function and then the test will pass even on platforms where __builtin_thread_pointer is unavailable.

Interesting. powerpc64le-linux-gnu-gcc -O2 -flto a.cc incorrectly passes in the absence of used. used makes the command fail as expected.

Suggest: add something like powerpc64le-linux-gnu-gcc -O2 -flto a.cc to the description.

@MaskRay
Copy link
Member

MaskRay commented Nov 4, 2023

Alternative: int main(void) { return (long)__builtin_thread_pointer(); } (if the return value doesn't matter)

gcc lto seems smart enough to optimize out int main(void) { return (long)__builtin_thread_pointer() && 0; } and not emit an error.

With LTO, gcc's IPA passes might drop the foo() function and then the test
will pass even on platforms where __builtin_thread_pointer is unavailable.

On PPC64, we get this as a result:
```
llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp:361:61: error: ‘__builtin_thread_pointer’ is not supported on this targ
```

Just mark the function in the CMake configure test with the 'used' attribute to
avoid it being optimised out. The test then behaves correctly with -flto.

Tested with e.g. 'powerpc64le-linux-gnu-gcc -O2 -flto a.c'.

Reported-by: matoro
Reviewed-by: maskray
Closes: llvm#70968
Signed-off-by: Sam James <[email protected]>
@thesamesam thesamesam force-pushed the cmake-fix-builtin-check branch from 5cfd3bc to a54545b Compare November 5, 2023 14:55
@thesamesam thesamesam closed this in a54545b Nov 5, 2023
@thesamesam thesamesam merged commit a54545b into llvm:main Nov 5, 2023
@thesamesam
Copy link
Member Author

Thank you! I made that change to the commit message. I kept the approach the same though as I was worried about it optimising it better in future.

@thesamesam thesamesam deleted the cmake-fix-builtin-check branch November 5, 2023 14:55
qihangkong pushed a commit to rvgpu/llvm that referenced this pull request Apr 18, 2024
With LTO, gcc's IPA passes might drop the foo() function and then the test
will pass even on platforms where __builtin_thread_pointer is unavailable.

On PPC64, we get this as a result:
```
llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp:361:61: error: ‘__builtin_thread_pointer’ is not supported on this targ
```

Just mark the function in the CMake configure test with the 'used' attribute to
avoid it being optimised out. The test then behaves correctly with -flto.

Tested with e.g. 'powerpc64le-linux-gnu-gcc -O2 -flto a.c'.

Reported-by: matoro
Reviewed-by: maskray
Closes: llvm/llvm-project#70968
Signed-off-by: Sam James <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cmake Build system in general and CMake in particular
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants