[PATCH 1010/1017] use a more generic solution for pocl_get_distro_kernellib_name()
authorAndreas Beckmann <anbe@debian.org>
Wed, 23 Mar 2022 13:24:18 +0000 (14:24 +0100)
committerAndreas Beckmann <anbe@debian.org>
Tue, 17 Jan 2023 13:11:18 +0000 (13:11 +0000)
Gbp-Pq: Name 1010-use-a-more-generic-solution-for-pocl_get_distro_kern.patch

lib/CL/pocl_llvm_build.cc
lib/CL/pocl_llvm_utils.cc

index 38d4541cb2d67a6d4c78e35842cce9edb38c1666..7bee43124140f927f614c0753d1d3af88d5ce7e8 100644 (file)
@@ -889,47 +889,6 @@ int pocl_llvm_link_program(cl_program program, unsigned device_i,
   return CL_SUCCESS;
 }
 
-/* for "distro" style kernel libs, return which kernellib to use, at runtime */
-#ifdef KERNELLIB_HOST_DISTRO_VARIANTS
-const char *pocl_get_distro_kernellib_name() {
-  StringMap<bool> Features;
-  const char *res = NULL;
-
-  if (!llvm::sys::getHostCPUFeatures(Features)) {
-    POCL_ABORT("LLVM can't get host CPU flags!\n");
-  }
-
-#if defined(__x86_64__)
-  if (Features["sse2"])
-    res = "sse2";
-  if (Features["ssse3"] && Features["cx16"])
-    res = "ssse3";
-  if (Features["sse4.1"] && Features["cx16"])
-    res = "sse41";
-  if (Features["avx"] && Features["cx16"] && Features["popcnt"])
-    res = "avx";
-  if (Features["avx"] && Features["cx16"] && Features["popcnt"] && Features["f16c"])
-    res = "avx_f16c";
-  if (Features["avx"] && Features["cx16"] && Features["popcnt"]
-      && Features["xop"] && Features["fma4"])
-    res = "avx_fma4";
-  if (Features["avx"] && Features["avx2"] && Features["cx16"]
-      && Features["popcnt"] && Features["lzcnt"] && Features["f16c"]
-      && Features["fma"] && Features["bmi"] && Features["bmi2"])
-    res = "avx2";
-  if (Features["avx512f"] )
-    res = "avx512";
-#endif
-
-  if (!res)
-    POCL_ABORT("Can't find a kernellib supported by the host CPU (%s)\n",
-               llvm::sys::getHostCPUName());
-
-  return res;
-}
-#endif
-
-
 /**
  * Return the OpenCL C built-in function library bitcode
  * for the given device.
index 3cc7fcc55ec330692329d6403f7d5d9848593f0f..c2012234c53e549ba7a84be77fbd6017c545b09f 100644 (file)
@@ -147,6 +147,61 @@ char *pocl_get_llvm_cpu_name() {
   return cpu_name;
 }
 
+#ifdef KERNELLIB_HOST_DISTRO_VARIANTS
+const struct kernellib_features {
+  const char *kernellib_name;
+  const char *features[12];
+} kernellib_feature_map[] = {
+// order the entries s.t. if a cpu matches multiple entries, the "best" match
+// comes last
+#if defined(__x86_64__)
+    "sse2",
+    {"sse2", NULL},
+    "ssse3",
+    {"sse2", "ssse3", "cx16", NULL},
+    "sse41",
+    {"sse2", "sse4.1", "cx16", NULL},
+    "avx",
+    {"sse2", "avx", "cx16", "popcnt", NULL},
+    "avx_f16c",
+    {"sse2", "avx", "cx16", "popcnt", "f16c", NULL},
+    "avx_fma4",
+    {"sse2", "avx", "cx16", "popcnt", "xop", "fma4", NULL},
+    "avx2",
+    {"sse2", "avx", "avx2", "cx16", "popcnt", "lzcnt", "f16c", "fma", "bmi",
+     "bmi2", NULL},
+    "avx512",
+    {"sse2", "avx512f", NULL},
+#endif
+    NULL,
+    {NULL}};
+
+/* for "distro" style kernel libs, return which kernellib to use, at runtime */
+const char *pocl_get_distro_kernellib_name() {
+  StringMap<bool> Features;
+
+  if (!llvm::sys::getHostCPUFeatures(Features))
+    POCL_ABORT("LLVM can't get host CPU flags!\n");
+
+  const kernellib_features *best_match = NULL;
+  for (const kernellib_features *kf = kernellib_feature_map; kf->kernellib_name;
+       ++kf) {
+    bool matches = true;
+    for (const char *const *f = kf->features; *f; ++f)
+      matches &= Features[*f];
+    if (matches) {
+      best_match = kf;
+    }
+  }
+
+  if (!best_match)
+    POCL_ABORT("Can't find a kernellib supported by the host CPU (%s)\n",
+               llvm::sys::getHostCPUName());
+
+  return best_match->kernellib_name;
+}
+#endif
+
 int bitcode_is_triple(const char *bitcode, size_t size, const char *triple) {
   std::string Triple;
   if (getModuleTriple(bitcode, size, Triple) == 0)