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

[XRay][SystemZ] Use stckf for non-clang compilers #125289

Merged
merged 2 commits into from
Feb 6, 2025

Conversation

redstar
Copy link
Member

@redstar redstar commented Jan 31, 2025

Turns out there are users who use gcc to compile compiler-rt. Using the clang-specific builtin function __builtin_readcyclecounter() does not work in this case.
Solution is to use inline assembly using the stckf instruction in case the compiler is not clang.

Turns out there are users who use gcc to compile compiler-rt. Using the clang-specific builtin function `__builtin_readcyclecounter()` does not work in this case.
Solution is to use inline assembly using the stckf instruction in case the compiler is not clang.
@llvmbot
Copy link
Member

llvmbot commented Jan 31, 2025

@llvm/pr-subscribers-xray

Author: Kai Nacke (redstar)

Changes

Turns out there are users who use gcc to compile compiler-rt. Using the clang-specific builtin function __builtin_readcyclecounter() does not work in this case.
Solution is to use inline assembly using the stckf instruction in case the compiler is not clang.


Full diff: https://github.com/llvm/llvm-project/pull/125289.diff

1 Files Affected:

  • (modified) compiler-rt/lib/xray/xray_tsc.h (+6)
diff --git a/compiler-rt/lib/xray/xray_tsc.h b/compiler-rt/lib/xray/xray_tsc.h
index 17e06c7035d85c..06f509cd1b09b9 100644
--- a/compiler-rt/lib/xray/xray_tsc.h
+++ b/compiler-rt/lib/xray/xray_tsc.h
@@ -96,7 +96,13 @@ namespace __xray {
 inline bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }
 
 ALWAYS_INLINE uint64_t readTSC(uint8_t &CPU) XRAY_NEVER_INSTRUMENT {
+#if defined(__clang__)
   return __builtin_readcyclecounter();
+#else
+  uint64_t Cycles;
+  asm volatile("stckf %0" : /* No output. */ : "Q"(Cycles) : "cc");
+  return Cycles;
+#endif
 }
 
 inline uint64_t getTSCFrequency() XRAY_NEVER_INSTRUMENT {

@@ -96,7 +96,13 @@ namespace __xray {
inline bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }

ALWAYS_INLINE uint64_t readTSC(uint8_t &CPU) XRAY_NEVER_INSTRUMENT {
#if defined(__clang__)
Copy link
Member

Choose a reason for hiding this comment

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

__has_builtin(__builtin_readcyclecounter) ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks.

return __builtin_readcyclecounter();
#else
uint64_t Cycles;
asm volatile("stckf %0" : /* No output. */ : "Q"(Cycles) : "cc");
Copy link
Member

Choose a reason for hiding this comment

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

remove the unnecessary comment /* No output. */. In the majority of places we assume that the user is aware of the : : syntax

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed.

@redstar redstar merged commit d905c7e into llvm:main Feb 6, 2025
5 of 6 checks passed
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
Turns out there are users who use gcc to compile compiler-rt. Using the
clang-specific builtin function `__builtin_readcyclecounter()` does not
work in this case.
Solution is to use inline assembly using the stckf instruction in case
the compiler is not clang.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants