From 925d8749e030c9dec49a5e27080000a4f950d625 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Thu, 7 Nov 2024 12:56:49 +0100 Subject: [PATCH 1/3] Suggest `-microarch:native` if `popcnt` instruction is missing. Fixes #4453. --- src/bug_report.cpp | 21 --------------------- src/build_cpuid.cpp | 35 +++++++++++++++++++++++++++++++++++ src/build_settings.cpp | 1 + src/main.cpp | 13 +++++++++++++ 4 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 src/build_cpuid.cpp diff --git a/src/bug_report.cpp b/src/bug_report.cpp index c5a8adea3..3f58ebd24 100644 --- a/src/bug_report.cpp +++ b/src/bug_report.cpp @@ -2,12 +2,6 @@ Gather and print platform and version info to help with reporting Odin bugs. */ -#if !defined(GB_COMPILER_MSVC) - #if defined(GB_CPU_X86) - #include - #endif -#endif - #if defined(GB_SYSTEM_LINUX) #include #include @@ -154,21 +148,6 @@ gb_internal void report_windows_product_type(DWORD ProductType) { } #endif -gb_internal void odin_cpuid(int leaf, int result[]) { - #if defined(GB_CPU_ARM) || defined(GB_CPU_RISCV) - return; - - #elif defined(GB_CPU_X86) - - #if defined(GB_COMPILER_MSVC) - __cpuid(result, leaf); - #else - __get_cpuid(leaf, (unsigned int*)&result[0], (unsigned int*)&result[1], (unsigned int*)&result[2], (unsigned int*)&result[3]); - #endif - - #endif -} - gb_internal void report_cpu_info() { gb_printf("\tCPU: "); diff --git a/src/build_cpuid.cpp b/src/build_cpuid.cpp new file mode 100644 index 000000000..a4e44e7f1 --- /dev/null +++ b/src/build_cpuid.cpp @@ -0,0 +1,35 @@ +#if !defined(GB_COMPILER_MSVC) + #if defined(GB_CPU_X86) + #include + #endif +#endif + +gb_internal void odin_cpuid(int leaf, int result[]) { + #if defined(GB_CPU_ARM) || defined(GB_CPU_RISCV) + return; + + #elif defined(GB_CPU_X86) + + #if defined(GB_COMPILER_MSVC) + __cpuid(result, leaf); + #else + __get_cpuid(leaf, (unsigned int*)&result[0], (unsigned int*)&result[1], (unsigned int*)&result[2], (unsigned int*)&result[3]); + #endif + + #endif +} + +gb_internal bool should_use_march_native() { + #if !defined(GB_CPU_X86) + return false; + + #else + + int cpu[4]; + odin_cpuid(0x1, &cpu[0]); // Get feature information in ECX + EDX + + bool have_popcnt = cpu[2] && (1 << 23); // bit 23 in ECX = popcnt + return !have_popcnt; + + #endif +} \ No newline at end of file diff --git a/src/build_settings.cpp b/src/build_settings.cpp index e4413b1fe..142076a70 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -2,6 +2,7 @@ #include #include #endif +#include "build_cpuid.cpp" // #if defined(GB_SYSTEM_WINDOWS) // #define DEFAULT_TO_THREADED_CHECKER diff --git a/src/main.cpp b/src/main.cpp index b04ee90f1..e65f866a1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3321,6 +3321,19 @@ int main(int arg_count, char const **arg_ptr) { } } + #if defined(GB_CPU_X86) + // We've detected that the CPU doesn't support popcnt, or another reason to use `-microarch:native`, + // and that no custom microarch was chosen. + if (should_use_march_native() && march == get_default_microarchitecture()) { + if (command == "run" || command == "test") { + gb_printf_err("Error: Try using '-microarch:native' as Odin defaults to %.*s (close to Nehalem) by default and your CPU seems to be a much older CPU.\n", LIT(march)); + gb_exit(1); + } else if (command == "build") { + gb_printf("Suggestion: Try using '-microarch:native' as Odin defaults to %.*s (close to Nehalem) by default and your CPU seems to be a much older CPU.\n", LIT(march)); + } + } + #endif + if (build_context.target_features_string.len != 0) { String_Iterator target_it = {build_context.target_features_string, 0}; for (;;) { From deb562613f5229e2e3881bd92b7a58e13813ea86 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Thu, 7 Nov 2024 13:13:58 +0100 Subject: [PATCH 2/3] Phrasing! --- src/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index e65f866a1..92d03b47a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3326,10 +3326,10 @@ int main(int arg_count, char const **arg_ptr) { // and that no custom microarch was chosen. if (should_use_march_native() && march == get_default_microarchitecture()) { if (command == "run" || command == "test") { - gb_printf_err("Error: Try using '-microarch:native' as Odin defaults to %.*s (close to Nehalem) by default and your CPU seems to be a much older CPU.\n", LIT(march)); + gb_printf_err("Error: Try using '-microarch:native' as Odin defaults to %.*s (close to Nehalem), and your CPU seems to be older.\n", LIT(march)); gb_exit(1); } else if (command == "build") { - gb_printf("Suggestion: Try using '-microarch:native' as Odin defaults to %.*s (close to Nehalem) by default and your CPU seems to be a much older CPU.\n", LIT(march)); + gb_printf("Suggestion: Try using '-microarch:native' as Odin defaults to %.*s (close to Nehalem), and your CPU seems to be older.\n", LIT(march)); } } #endif From 3bfe675a68ad8448195d3b4729ee293b547d8761 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Thu, 7 Nov 2024 15:02:19 +0100 Subject: [PATCH 3/3] && --- src/build_cpuid.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/build_cpuid.cpp b/src/build_cpuid.cpp index a4e44e7f1..b7ba5dcdf 100644 --- a/src/build_cpuid.cpp +++ b/src/build_cpuid.cpp @@ -28,7 +28,7 @@ gb_internal bool should_use_march_native() { int cpu[4]; odin_cpuid(0x1, &cpu[0]); // Get feature information in ECX + EDX - bool have_popcnt = cpu[2] && (1 << 23); // bit 23 in ECX = popcnt + bool have_popcnt = cpu[2] & (1 << 23); // bit 23 in ECX = popcnt return !have_popcnt; #endif