Skip to content

Conversation

@Alcaro
Copy link
Contributor

@Alcaro Alcaro commented Feb 15, 2025

Fixes #127271

Testing mostly done in Compiler Explorer https://godbolt.org/z/q1h3ohxr7

@llvmbot llvmbot added clang Clang issues not falling into any other category backend:X86 clang:headers Headers provided by Clang, e.g. for intrinsics labels Feb 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 15, 2025

@llvm/pr-subscribers-backend-x86

Author: None (Alcaro)

Changes

Fixes #127271

Testing mostly done in Compiler Explorer https://godbolt.org/z/q1h3ohxr7


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

1 Files Affected:

  • (modified) clang/lib/Headers/cpuid.h (+15-15)
diff --git a/clang/lib/Headers/cpuid.h b/clang/lib/Headers/cpuid.h
index 2601aa5724f05..d8ace39d4e7d1 100644
--- a/clang/lib/Headers/cpuid.h
+++ b/clang/lib/Headers/cpuid.h
@@ -268,16 +268,16 @@
 #else
 /* x86-64 uses %rbx as the base register, so preserve it. */
 #define __cpuid(__leaf, __eax, __ebx, __ecx, __edx) \
-    __asm("  xchgq  %%rbx,%q1\n" \
+    __asm("  xchg{q|}  {%%|}rbx,%q1\n" \
           "  cpuid\n" \
-          "  xchgq  %%rbx,%q1" \
+          "  xchg{q|}  {%%|}rbx,%q1" \
         : "=a"(__eax), "=r" (__ebx), "=c"(__ecx), "=d"(__edx) \
         : "0"(__leaf))
 
 #define __cpuid_count(__leaf, __count, __eax, __ebx, __ecx, __edx) \
-    __asm("  xchgq  %%rbx,%q1\n" \
+    __asm("  xchg{q|}  {%%|}rbx,%q1\n" \
           "  cpuid\n" \
-          "  xchgq  %%rbx,%q1" \
+          "  xchg{q|}  {%%|}rbx,%q1" \
         : "=a"(__eax), "=r" (__ebx), "=c"(__ecx), "=d"(__edx) \
         : "0"(__leaf), "2"(__count))
 #endif
@@ -289,18 +289,18 @@ static __inline unsigned int __get_cpuid_max (unsigned int __leaf,
 #ifdef __i386__
     int __cpuid_supported;
 
-    __asm("  pushfl\n"
-          "  popl   %%eax\n"
-          "  movl   %%eax,%%ecx\n"
-          "  xorl   $0x00200000,%%eax\n"
-          "  pushl  %%eax\n"
-          "  popfl\n"
-          "  pushfl\n"
-          "  popl   %%eax\n"
-          "  movl   $0,%0\n"
-          "  cmpl   %%eax,%%ecx\n"
+    __asm("  pushf{l|d}\n"
+          "  pop{l|}   {%%|}eax\n"
+          "  mov{l|}   {%%eax,%%ecx|ecx,eax}\n"
+          "  xor{l|}   {$0x00200000,%%eax|eax,0x00200000}\n"
+          "  push{l|}  {%%|}eax\n"
+          "  popf{l|d}\n"
+          "  pushf{l|d}\n"
+          "  pop{l|}   {%%|}eax\n"
+          "  mov{l|}   {$0,%0|%0,0}\n"
+          "  cmp{l|}   {%%eax,%%ecx|ecx,eax}\n"
           "  je     1f\n"
-          "  movl   $1,%0\n"
+          "  mov{l|}   {$1,%0|%0,1}\n"
           "1:"
         : "=r" (__cpuid_supported) : : "eax", "ecx");
     if (!__cpuid_supported)

@llvmbot
Copy link
Member

llvmbot commented Feb 15, 2025

@llvm/pr-subscribers-clang

Author: None (Alcaro)

Changes

Fixes #127271

Testing mostly done in Compiler Explorer https://godbolt.org/z/q1h3ohxr7


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

1 Files Affected:

  • (modified) clang/lib/Headers/cpuid.h (+15-15)
diff --git a/clang/lib/Headers/cpuid.h b/clang/lib/Headers/cpuid.h
index 2601aa5724f05..d8ace39d4e7d1 100644
--- a/clang/lib/Headers/cpuid.h
+++ b/clang/lib/Headers/cpuid.h
@@ -268,16 +268,16 @@
 #else
 /* x86-64 uses %rbx as the base register, so preserve it. */
 #define __cpuid(__leaf, __eax, __ebx, __ecx, __edx) \
-    __asm("  xchgq  %%rbx,%q1\n" \
+    __asm("  xchg{q|}  {%%|}rbx,%q1\n" \
           "  cpuid\n" \
-          "  xchgq  %%rbx,%q1" \
+          "  xchg{q|}  {%%|}rbx,%q1" \
         : "=a"(__eax), "=r" (__ebx), "=c"(__ecx), "=d"(__edx) \
         : "0"(__leaf))
 
 #define __cpuid_count(__leaf, __count, __eax, __ebx, __ecx, __edx) \
-    __asm("  xchgq  %%rbx,%q1\n" \
+    __asm("  xchg{q|}  {%%|}rbx,%q1\n" \
           "  cpuid\n" \
-          "  xchgq  %%rbx,%q1" \
+          "  xchg{q|}  {%%|}rbx,%q1" \
         : "=a"(__eax), "=r" (__ebx), "=c"(__ecx), "=d"(__edx) \
         : "0"(__leaf), "2"(__count))
 #endif
@@ -289,18 +289,18 @@ static __inline unsigned int __get_cpuid_max (unsigned int __leaf,
 #ifdef __i386__
     int __cpuid_supported;
 
-    __asm("  pushfl\n"
-          "  popl   %%eax\n"
-          "  movl   %%eax,%%ecx\n"
-          "  xorl   $0x00200000,%%eax\n"
-          "  pushl  %%eax\n"
-          "  popfl\n"
-          "  pushfl\n"
-          "  popl   %%eax\n"
-          "  movl   $0,%0\n"
-          "  cmpl   %%eax,%%ecx\n"
+    __asm("  pushf{l|d}\n"
+          "  pop{l|}   {%%|}eax\n"
+          "  mov{l|}   {%%eax,%%ecx|ecx,eax}\n"
+          "  xor{l|}   {$0x00200000,%%eax|eax,0x00200000}\n"
+          "  push{l|}  {%%|}eax\n"
+          "  popf{l|d}\n"
+          "  pushf{l|d}\n"
+          "  pop{l|}   {%%|}eax\n"
+          "  mov{l|}   {$0,%0|%0,0}\n"
+          "  cmp{l|}   {%%eax,%%ecx|ecx,eax}\n"
           "  je     1f\n"
-          "  movl   $1,%0\n"
+          "  mov{l|}   {$1,%0|%0,1}\n"
           "1:"
         : "=r" (__cpuid_supported) : : "eax", "ecx");
     if (!__cpuid_supported)

@github-actions
Copy link

github-actions bot commented Feb 15, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@Alcaro
Copy link
Contributor Author

Alcaro commented Feb 15, 2025

Oh come on, format bot, it was like that when I found it.

@phoebewang
Copy link
Contributor

Please update test in clang\test\Headers\cpuid.c

Copy link
Contributor

@phoebewang phoebewang left a comment

Choose a reason for hiding this comment

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

LGTM.

@Alcaro
Copy link
Contributor Author

Alcaro commented Feb 25, 2025

So what's going on here? What's the next step? Who's waiting for who?

@abhishek-kaushik22 abhishek-kaushik22 merged commit 547a8bc into llvm:main Feb 25, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend:X86 clang:headers Headers provided by Clang, e.g. for intrinsics clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cpuid.h doesn't like -masm=intel

4 participants