[compiler-rt] [llvm] [compiler-rt][xray] porting to sunos platforms. (PR #90362)

David CARLIER via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 30 01:32:19 PDT 2024


https://github.com/devnexen updated https://github.com/llvm/llvm-project/pull/90362

>From 92ce82434f7968eddf688d045bbca40ebcc19f87 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Sat, 27 Apr 2024 23:37:45 +0100
Subject: [PATCH 1/2] [compiler-rt][xray] porting to sunos platforms.

---
 compiler-rt/cmake/config-ix.cmake             |  2 +-
 compiler-rt/lib/xray/CMakeLists.txt           |  4 +++
 compiler-rt/lib/xray/xray_x86_64.cpp          | 33 +++++++++++++++++++
 .../TestCases/Posix/fork_basic_logging.cpp    |  4 +++
 compiler-rt/test/xray/lit.cfg.py              |  2 +-
 5 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/cmake/config-ix.cmake b/compiler-rt/cmake/config-ix.cmake
index 7037bf89d6532..2cbce3b01c0ee 100644
--- a/compiler-rt/cmake/config-ix.cmake
+++ b/compiler-rt/cmake/config-ix.cmake
@@ -861,7 +861,7 @@ else()
 endif()
 
 if (COMPILER_RT_HAS_SANITIZER_COMMON AND XRAY_SUPPORTED_ARCH AND
-    OS_NAME MATCHES "Darwin|Linux|FreeBSD|NetBSD|Fuchsia")
+    OS_NAME MATCHES "Darwin|Linux|FreeBSD|NetBSD|Fuchsia|SunOS")
   set(COMPILER_RT_HAS_XRAY TRUE)
 else()
   set(COMPILER_RT_HAS_XRAY FALSE)
diff --git a/compiler-rt/lib/xray/CMakeLists.txt b/compiler-rt/lib/xray/CMakeLists.txt
index cf7b5062aae32..a67fca1b96a29 100644
--- a/compiler-rt/lib/xray/CMakeLists.txt
+++ b/compiler-rt/lib/xray/CMakeLists.txt
@@ -189,6 +189,10 @@ if (TARGET cxx-headers OR HAVE_LIBCXX)
   set(XRAY_DEPS cxx-headers)
 endif()
 
+if (CMAKE_SYSTEM_NAME STREQUAL "SunOS")
+  set(XRAY_LINK_LIBS ${XRAY_LINK_LIBS} kstat)
+endif()
+
 if (APPLE)
   add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)
   add_weak_symbols("xray" WEAK_SYMBOL_LINK_FLAGS)
diff --git a/compiler-rt/lib/xray/xray_x86_64.cpp b/compiler-rt/lib/xray/xray_x86_64.cpp
index b9666a40861d4..a8b59b8a2eb9a 100644
--- a/compiler-rt/lib/xray/xray_x86_64.cpp
+++ b/compiler-rt/lib/xray/xray_x86_64.cpp
@@ -9,6 +9,8 @@
 #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_APPLE
 #include <sys/types.h>
 #include <sys/sysctl.h>
+#elif SANITIZER_SOLARIS
+#include <kstat.h>
 #elif SANITIZER_FUCHSIA
 #include <zircon/syscalls.h>
 #endif
@@ -101,6 +103,37 @@ uint64_t getTSCFrequency() XRAY_NEVER_INSTRUMENT {
 
     return 0;
 }
+#elif SANITIZER_SOLARIS
+uint64_t getTSCFrequency() XRAY_NEVER_INSTRUMENT {
+  kstat_named_t *nm;
+  kstat_ctl_t *ctl;
+  kstat_t *s;
+  uint64_t TSCFrequency;
+
+  ctl = kstat_open();
+  if (!ctl) {
+    return 0;
+  }
+  s = kstat_lookup(ctl, "cpu_info", 0, NULL);
+  if (!s) {
+    kstat_close(ctl);
+    return 0;
+  }
+
+  if (kstat_read(ctl, s, NULL) == -1) {
+    kstat_close(ctl);
+    return 0;
+  }
+
+  nm = reinterpret_cast<kstat_named_t *>(kstat_data_lookup(s, "clock_MHz"));
+
+  auto clock =
+      nm->data_type == KSTAT_DATA_INT32 ? nm->value.i32 : nm->value.i64;
+  TSCFrequency = static_cast<uint64_t>(clock) * 1000000;
+  kstat_close(ctl);
+
+  return TSCFrequency;
+}
 #elif !SANITIZER_FUCHSIA
 uint64_t getTSCFrequency() XRAY_NEVER_INSTRUMENT {
     /* Not supported */
diff --git a/compiler-rt/test/xray/TestCases/Posix/fork_basic_logging.cpp b/compiler-rt/test/xray/TestCases/Posix/fork_basic_logging.cpp
index 58f310e3a1083..8c4095466354a 100644
--- a/compiler-rt/test/xray/TestCases/Posix/fork_basic_logging.cpp
+++ b/compiler-rt/test/xray/TestCases/Posix/fork_basic_logging.cpp
@@ -13,6 +13,10 @@
 
 // Not ported.
 // UNSUPPORTED: target={{.*netbsd.*}}
+// Not ported.
+// UNSUPPORTED: target={{.*solaris.*}}
+// Not ported.
+// UNSUPPORTED: target={{.*illumos.*}}
 
 #include "xray/xray_log_interface.h"
 #include <stdio.h>
diff --git a/compiler-rt/test/xray/lit.cfg.py b/compiler-rt/test/xray/lit.cfg.py
index f73ae3acd7715..491d5c1ff4e57 100644
--- a/compiler-rt/test/xray/lit.cfg.py
+++ b/compiler-rt/test/xray/lit.cfg.py
@@ -56,7 +56,7 @@ def build_invocation(compile_flags):
 # Default test suffixes.
 config.suffixes = [".c", ".cpp"]
 
-if config.host_os not in ["FreeBSD", "Linux", "NetBSD", "OpenBSD"]:
+if config.host_os not in ["FreeBSD", "Linux", "NetBSD", "OpenBSD", "SunOS"]:
     config.unsupported = True
 elif "64" not in config.host_arch:
     if "arm" in config.host_arch:

>From d5aa01797f91e0082b7479b246d575ee3569fe86 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Sun, 30 Jun 2024 09:20:51 +0100
Subject: [PATCH 2/2] fix solaris build

---
 third-party/benchmark/src/sysinfo.cc | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/third-party/benchmark/src/sysinfo.cc b/third-party/benchmark/src/sysinfo.cc
index 46df973b069a7..d02a9009c8b0b 100644
--- a/third-party/benchmark/src/sysinfo.cc
+++ b/third-party/benchmark/src/sysinfo.cc
@@ -502,7 +502,7 @@ int GetNumCPUsImpl() {
     PrintErrorAndDie("sysconf(_SC_NPROCESSORS_ONLN) failed with error: ",
                      strerror(errno));
   }
-  return (int)num_cpu;
+  return static_cast<int>(num_cpu);
 #elif defined(BENCHMARK_OS_QNX)
   return static_cast<int>(_syspage_ptr->num_cpu);
 #elif defined(BENCHMARK_OS_QURT)
@@ -779,8 +779,8 @@ double GetCPUCyclesPerSecond(CPUInfo::Scaling scaling) {
     std::cerr << "failed to read from /dev/kstat\n";
     return -1;
   }
-  kstat_named_t* knp = (kstat_named_t*)kstat_data_lookup(
-      ksp, const_cast<char*>("current_clock_Hz"));
+  kstat_named_t* knp = reinterpret_cast<kstat_named_t*>(kstat_data_lookup(
+      ksp, const_cast<char*>("current_clock_Hz")));
   if (!knp) {
     std::cerr << "failed to lookup data in /dev/kstat\n";
     return -1;



More information about the llvm-commits mailing list