Browse Source

Merge pull request #4464 from Kelimion/should_use_native

Suggest `-microarch:native` if `popcnt` instruction is missing.
Jeroen van Rijn 9 months ago
parent
commit
1b16ddbb3e
4 changed files with 49 additions and 21 deletions
  1. 0 21
      src/bug_report.cpp
  2. 35 0
      src/build_cpuid.cpp
  3. 1 0
      src/build_settings.cpp
  4. 13 0
      src/main.cpp

+ 0 - 21
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 <cpuid.h>
-	#endif
-#endif
-
 #if defined(GB_SYSTEM_LINUX)
 	#include <sys/utsname.h>
 	#include <sys/sysinfo.h>
@@ -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:     ");
 

+ 35 - 0
src/build_cpuid.cpp

@@ -0,0 +1,35 @@
+#if !defined(GB_COMPILER_MSVC)
+	#if defined(GB_CPU_X86)
+		#include <cpuid.h>
+	#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
+}

+ 1 - 0
src/build_settings.cpp

@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <sys/sysctl.h>
 #endif
+#include "build_cpuid.cpp"
 
 // #if defined(GB_SYSTEM_WINDOWS)
 // #define DEFAULT_TO_THREADED_CHECKER

+ 13 - 0
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), 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), and your CPU seems to be older.\n", LIT(march));
+		}
+	}
+	#endif
+
 	if (build_context.target_features_string.len != 0) {
 		String_Iterator target_it = {build_context.target_features_string, 0};
 		for (;;) {