Skip to content

[X86] Fix the implementation of __readcr[4,8]/__writecr[4,8] to work in 64-bit mode #122238

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
Jan 10, 2025

Conversation

phoebewang
Copy link
Contributor

According to MSVC, __readcr4/__writecr4 return/use unsigned __int64, and are supported on both x86 and x64. While __readcr8/__writecr8 are only supported on x64. So we use INTPTR_TYPE and __int64 respectively.

Following: 3cec2a1

Ref.:
https://learn.microsoft.com/en-us/cpp/intrinsics/readcr3?view=msvc-170
https://learn.microsoft.com/en-us/cpp/intrinsics/readcr8?view=msvc-170

…in 64-bit mode

According to MSVC, __readcr4/__writecr4 return/use `unsigned __int64`,
and are supported on both x86 and x64. While __readcr8/__writecr8 are
only supported on x64. So we use __INTPTR_TYPE__ and __int64
respectively.

Following llvm@3cec2a1

Ref.:
https://learn.microsoft.com/en-us/cpp/intrinsics/readcr3?view=msvc-170
https://learn.microsoft.com/en-us/cpp/intrinsics/readcr8?view=msvc-170
@phoebewang phoebewang requested review from rnk, RKSimon and topperc January 9, 2025 09:30
@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 Jan 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 9, 2025

@llvm/pr-subscribers-backend-x86

Author: Phoebe Wang (phoebewang)

Changes

According to MSVC, __readcr4/__writecr4 return/use unsigned __int64, and are supported on both x86 and x64. While __readcr8/__writecr8 are only supported on x64. So we use INTPTR_TYPE and __int64 respectively.

Following: 3cec2a1

Ref.:
https://learn.microsoft.com/en-us/cpp/intrinsics/readcr3?view=msvc-170
https://learn.microsoft.com/en-us/cpp/intrinsics/readcr8?view=msvc-170


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

1 Files Affected:

  • (modified) clang/lib/Headers/intrin.h (+4-4)
diff --git a/clang/lib/Headers/intrin.h b/clang/lib/Headers/intrin.h
index e8a01d1888026c..376046aeeaf5e9 100644
--- a/clang/lib/Headers/intrin.h
+++ b/clang/lib/Headers/intrin.h
@@ -94,8 +94,8 @@ void __outwordstring(unsigned short, unsigned short *, unsigned long);
 unsigned long __readcr0(void);
 unsigned long __readcr2(void);
 unsigned __LPTRINT_TYPE__ __readcr3(void);
-unsigned long __readcr4(void);
-unsigned long __readcr8(void);
+unsigned __LPTRINT_TYPE__ __readcr4(void);
+unsigned __int64 __readcr8(void);
 unsigned int __readdr(unsigned int);
 #ifdef __i386__
 unsigned char __readfsbyte(unsigned long);
@@ -124,8 +124,8 @@ void __vmx_vmptrst(unsigned __int64 *);
 void __wbinvd(void);
 void __writecr0(unsigned int);
 void __writecr3(unsigned __INTPTR_TYPE__);
-void __writecr4(unsigned int);
-void __writecr8(unsigned int);
+void __writecr4(unsigned __INTPTR_TYPE__);
+void __writecr8(unsigned __int64);
 void __writedr(unsigned int, unsigned int);
 void __writefsbyte(unsigned long, unsigned char);
 void __writefsdword(unsigned long, unsigned long);

@llvmbot
Copy link
Member

llvmbot commented Jan 9, 2025

@llvm/pr-subscribers-clang

Author: Phoebe Wang (phoebewang)

Changes

According to MSVC, __readcr4/__writecr4 return/use unsigned __int64, and are supported on both x86 and x64. While __readcr8/__writecr8 are only supported on x64. So we use INTPTR_TYPE and __int64 respectively.

Following: 3cec2a1

Ref.:
https://learn.microsoft.com/en-us/cpp/intrinsics/readcr3?view=msvc-170
https://learn.microsoft.com/en-us/cpp/intrinsics/readcr8?view=msvc-170


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

1 Files Affected:

  • (modified) clang/lib/Headers/intrin.h (+4-4)
diff --git a/clang/lib/Headers/intrin.h b/clang/lib/Headers/intrin.h
index e8a01d1888026c..376046aeeaf5e9 100644
--- a/clang/lib/Headers/intrin.h
+++ b/clang/lib/Headers/intrin.h
@@ -94,8 +94,8 @@ void __outwordstring(unsigned short, unsigned short *, unsigned long);
 unsigned long __readcr0(void);
 unsigned long __readcr2(void);
 unsigned __LPTRINT_TYPE__ __readcr3(void);
-unsigned long __readcr4(void);
-unsigned long __readcr8(void);
+unsigned __LPTRINT_TYPE__ __readcr4(void);
+unsigned __int64 __readcr8(void);
 unsigned int __readdr(unsigned int);
 #ifdef __i386__
 unsigned char __readfsbyte(unsigned long);
@@ -124,8 +124,8 @@ void __vmx_vmptrst(unsigned __int64 *);
 void __wbinvd(void);
 void __writecr0(unsigned int);
 void __writecr3(unsigned __INTPTR_TYPE__);
-void __writecr4(unsigned int);
-void __writecr8(unsigned int);
+void __writecr4(unsigned __INTPTR_TYPE__);
+void __writecr8(unsigned __int64);
 void __writedr(unsigned int, unsigned int);
 void __writefsbyte(unsigned long, unsigned char);
 void __writefsdword(unsigned long, unsigned long);

Copy link
Collaborator

@rnk rnk left a comment

Choose a reason for hiding this comment

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

Thanks!

I see we don't actually implement these intrinsics at this point, but fixing them to avoid redeclaration errors is reasonable.

@phoebewang phoebewang merged commit 5e92e8c into llvm:main Jan 10, 2025
12 checks passed
@phoebewang phoebewang deleted the intrin branch January 10, 2025 07:49
BaiXilin pushed a commit to BaiXilin/llvm-fix-vnni-instr-types that referenced this pull request Jan 12, 2025
…in 64-bit mode (llvm#122238)

According to MSVC, __readcr4/__writecr4 return/use `unsigned __int64`,
and are supported on both x86 and x64. While __readcr8/__writecr8 are
only supported on x64. So we use __INTPTR_TYPE__ and __int64
respectively.

Following:
llvm@3cec2a1

Ref.:
https://learn.microsoft.com/en-us/cpp/intrinsics/readcr3?view=msvc-170
https://learn.microsoft.com/en-us/cpp/intrinsics/readcr8?view=msvc-170
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.

3 participants