[llvm] Windows: use EcoQoS for ThreadPriority::Background (PR #148797)

Alexandre Ganea via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 16 06:39:24 PDT 2025


================
@@ -107,6 +107,39 @@ void llvm::get_thread_name(SmallVectorImpl<char> &Name) {
 }
 
 SetThreadPriorityResult llvm::set_thread_priority(ThreadPriority Priority) {
+
+  // SetThreadInformation is only available on Windows 8 and later. so we load
+  // it at run-time
+  typedef BOOL(WINAPI * SetThreadInformation_t)(
+      HANDLE hThread, THREAD_INFORMATION_CLASS ThreadInformationClass,
+      _In_reads_bytes_(ThreadInformationSize) PVOID ThreadInformation,
+      ULONG ThreadInformationSize);
+  static const auto pfnSetThreadInformation =
+      (SetThreadInformation_t)GetProcAddress(
+          GetModuleHandle(TEXT("kernel32.dll")), "SetThreadInformation");
+
+  if (pfnSetThreadInformation) {
+    auto setThreadInformation = [](ULONG ControlMask, ULONG StateMask) {
+      THREAD_POWER_THROTTLING_STATE state{};
+      state.Version = THREAD_POWER_THROTTLING_CURRENT_VERSION;
+      state.ControlMask = ControlMask;
+      state.StateMask = StateMask;
+      return pfnSetThreadInformation(GetCurrentThread(), ThreadPowerThrottling,
+                                     &state, sizeof(state));
+    };
+
+    // Use EcoQoS for ThreadPriority::Background available (running on most
+    // efficent cores at the most efficient cpu frequency):
+    // https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadinformation
+    // https://learn.microsoft.com/en-us/windows/win32/procthread/quality-of-service
+    if (Priority == ThreadPriority::Background) {
+      setThreadInformation(THREAD_POWER_THROTTLING_EXECUTION_SPEED,
----------------
aganea wrote:

For the current requirements, we don't need to be that verbose here, just one value would suffice here (and use it for both control mask and state mask above).

https://github.com/llvm/llvm-project/pull/148797


More information about the llvm-commits mailing list