[llvm] [orc-rt] Implement Windows CPU feature detection (PR #224516)

Jared Wyles via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 21 06:23:47 PDT 2026


https://github.com/jaredwy updated https://github.com/llvm/llvm-project/pull/224516

>From e5984bc3257c8c558d0b7876bcd536514e76be2b Mon Sep 17 00:00:00 2001
From: Jared Wyles <jared.wyles at gmail.com>
Date: Fri, 18 Sep 2026 14:59:24 +1000
Subject: [PATCH 1/2] [orc-rt] Implement Windows target triple detection

---
 orc-rt/lib/bedrock/sys/windows/TargetTriple.cpp   | 8 +++++++-
 orc-rt/test/unit/bedrock/sys/TargetTripleTest.cpp | 7 +++++--
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/orc-rt/lib/bedrock/sys/windows/TargetTriple.cpp b/orc-rt/lib/bedrock/sys/windows/TargetTriple.cpp
index 465a24afc82f8a..7a5db97b98f7b1 100644
--- a/orc-rt/lib/bedrock/sys/windows/TargetTriple.cpp
+++ b/orc-rt/lib/bedrock/sys/windows/TargetTriple.cpp
@@ -10,6 +10,12 @@
 
 namespace orc_rt::sys {
 
-std::string detectTargetTriple() noexcept { return {}; }
+std::string detectTargetTriple() noexcept {
+#if defined(_M_X64)
+  return "x86_64-pc-windows-msvc";
+#else
+#error "Unsupported Windows architecture"
+#endif
+}
 
 } // namespace orc_rt::sys
diff --git a/orc-rt/test/unit/bedrock/sys/TargetTripleTest.cpp b/orc-rt/test/unit/bedrock/sys/TargetTripleTest.cpp
index 65675d8db58d60..76e612415ebe11 100644
--- a/orc-rt/test/unit/bedrock/sys/TargetTripleTest.cpp
+++ b/orc-rt/test/unit/bedrock/sys/TargetTripleTest.cpp
@@ -16,7 +16,7 @@
 #include <algorithm>
 
 using namespace orc_rt;
-#ifndef _WIN32
+
 TEST(TargetTripleTest, NotEmpty) {
   EXPECT_FALSE(sys::detectTargetTriple().empty());
 }
@@ -46,13 +46,16 @@ TEST(TargetTripleTest, ArchMatchesCompileTarget) {
 
 TEST(TargetTripleTest, OSMatchesCompileTarget) {
   auto Triple = sys::detectTargetTriple();
+
 #if defined(__APPLE__)
   EXPECT_NE(Triple.find("-apple-"), std::string::npos);
 #elif defined(__linux__)
   EXPECT_NE(Triple.find("-linux-"), std::string::npos);
+#elif defined(_WIN32)
+  EXPECT_NE(Triple.find("-windows-"), std::string::npos);
 #endif
 }
-#endif
+
 TEST(TargetTripleTest, CachedResultIsIdempotent) {
   EXPECT_EQ(sys::detectTargetTriple(), sys::detectTargetTriple());
 }

>From 036dda2da682d9bab29dfbeb8787ab010f11247a Mon Sep 17 00:00:00 2001
From: Jared Wyles <jared.wyles at gmail.com>
Date: Fri, 18 Sep 2026 15:10:04 +1000
Subject: [PATCH 2/2] [orc-rt] Implement Windows CPU feature detection

---
 .../lib/bedrock/sys/windows/CPUFeatures.cpp   | 55 ++++++++++++++++++-
 .../test/unit/bedrock/sys/CPUFeaturesTest.cpp | 12 ++++
 2 files changed, 66 insertions(+), 1 deletion(-)

diff --git a/orc-rt/lib/bedrock/sys/windows/CPUFeatures.cpp b/orc-rt/lib/bedrock/sys/windows/CPUFeatures.cpp
index 8cb9bfa7d7b145..9928a978c53a2f 100644
--- a/orc-rt/lib/bedrock/sys/windows/CPUFeatures.cpp
+++ b/orc-rt/lib/bedrock/sys/windows/CPUFeatures.cpp
@@ -1,7 +1,60 @@
 #include "orc-rt-internal/bedrock/sys/CPUFeatures.h"
 
+#include <intrin.h>
+#include <windows.h>
+
 namespace orc_rt::sys {
 
-std::vector<std::string_view> detectTargetCPUFeatures() { return {}; }
+std::vector<std::string_view> detectTargetCPUFeatures() {
+  std::vector<std::string_view> Features;
+
+  auto A = [&](bool Present, std::string_view Feature) {
+    if (Present)
+      Features.push_back(Feature);
+  };
+
+  int Regs[4] = {};
+  __cpuid(Regs, 0);
+
+  const unsigned MaxLeaf = static_cast<unsigned>(Regs[0]);
+  if (MaxLeaf < 1)
+    return Features;
+
+  __cpuidex(Regs, 1, 0);
+
+  const unsigned ECX = static_cast<unsigned>(Regs[2]);
+  const unsigned EDX = static_cast<unsigned>(Regs[3]);
+
+  A(EDX & (1u << 25), "sse");
+  A(EDX & (1u << 26), "sse2");
+
+  A(ECX & (1u << 0), "sse3");
+  A(ECX & (1u << 9), "ssse3");
+  A(ECX & (1u << 19), "sse4.1");
+  A(ECX & (1u << 20), "sse4.2");
+
+  const bool HasXSAVE = ECX & (1u << 26);
+  const bool HasOSXSAVE = ECX & (1u << 27);
+  const bool HasAVXHardware = ECX & (1u << 28);
+
+  uint64_t XCR0 = 0;
+  if (HasXSAVE && HasOSXSAVE)
+    XCR0 = _xgetbv(0);
+
+  const bool HasAVXState = (XCR0 & 0x6) == 0x6;
+  const bool HasAVX = HasAVXHardware && HasAVXState;
+
+  A(HasAVX, "avx");
+
+  if (MaxLeaf >= 7) {
+    __cpuidex(Regs, 7, 0);
+
+    const unsigned EBX = static_cast<unsigned>(Regs[1]);
+
+    A(HasAVX && (EBX & (1u << 5)), "avx2");
+  }
+
+  return Features;
+}
 
 } // namespace orc_rt::sys
diff --git a/orc-rt/test/unit/bedrock/sys/CPUFeaturesTest.cpp b/orc-rt/test/unit/bedrock/sys/CPUFeaturesTest.cpp
index a4cbffdf1da2a3..fa5d89595f1a6a 100644
--- a/orc-rt/test/unit/bedrock/sys/CPUFeaturesTest.cpp
+++ b/orc-rt/test/unit/bedrock/sys/CPUFeaturesTest.cpp
@@ -25,3 +25,15 @@ TEST(CPUFeaturesTest, DetectDoesNotCrash) {
 TEST(CPUFeaturesTest, CachedResultIsIdempotent) {
   EXPECT_EQ(sys::detectTargetCPUFeatures(), sys::detectTargetCPUFeatures());
 }
+
+// A temporary test to tide us over until the next branch lands.
+// We we enable regression testing for EPI.
+#if defined(_WIN32) && defined(_M_X64)
+TEST(CPUFeaturesTest, DetectsBaselineX86Features) {
+  auto Features = sys::detectTargetCPUFeatures();
+
+  EXPECT_NE(std::find(Features.begin(), Features.end(), "sse"), Features.end());
+  EXPECT_NE(std::find(Features.begin(), Features.end(), "sse2"),
+            Features.end());
+}
+#endif



More information about the llvm-commits mailing list