[llvm] [orc-rt] Fold the default clear_icache into sys/CacheControl.h. (PR #219758)

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 29 21:50:18 PDT 2026


https://github.com/lhames created https://github.com/llvm/llvm-project/pull/219758

It was in a sys/posix/ header, but the default belongs with the declaration: sys/CacheControl.h now selects on the builtin rather than on an OS, so a freestanding target -- which has no __unix__ but does have the builtin -- is served too. Darwin keeps its own header, to call libSystem directly rather than depend on __clear_cache being linked in.

>From 880b336777eccb05a9f524cf4d4578c02d7ac87f Mon Sep 17 00:00:00 2001
From: Lang Hames <lhames at gmail.com>
Date: Sun, 30 Aug 2026 14:46:33 +1000
Subject: [PATCH] [orc-rt] Fold the default clear_icache into
 sys/CacheControl.h.

It was in a sys/posix/ header, but the default belongs with the
declaration: sys/CacheControl.h now selects on the builtin rather than on
an OS, so a freestanding target -- which has no __unix__ but does have the
builtin -- is served too. Darwin keeps its own header, to call libSystem
directly rather than depend on __clear_cache being linked in.
---
 .../support/sys/CacheControl.h                | 30 ++++++++++++------
 .../support/sys/posix/CacheControl.h          | 31 -------------------
 2 files changed, 20 insertions(+), 41 deletions(-)
 delete mode 100644 orc-rt/include/orc-rt-internal/support/sys/posix/CacheControl.h

diff --git a/orc-rt/include/orc-rt-internal/support/sys/CacheControl.h b/orc-rt/include/orc-rt-internal/support/sys/CacheControl.h
index 3576f8b242dd8..82f97cd454c00 100644
--- a/orc-rt/include/orc-rt-internal/support/sys/CacheControl.h
+++ b/orc-rt/include/orc-rt-internal/support/sys/CacheControl.h
@@ -8,18 +8,13 @@
 //
 // Host cache management APIs.
 //
-// Unlike the rest of sys/, where the build compiles one implementation .cpp per
-// system, these operations want to inline: on targets where they lower to
-// cache-maintenance instructions rather than a call, a cross-TU call would cost
-// more than the operation itself. So the declarations live here and the
-// definitions come from a per-system header selected below. The conditionals
-// are confined to this file; each implementation header is unconditional.
-//
 //===----------------------------------------------------------------------===//
 
 #ifndef ORC_RT_INTERNAL_SUPPORT_SYS_CACHECONTROL_H
 #define ORC_RT_INTERNAL_SUPPORT_SYS_CACHECONTROL_H
 
+#include "orc-rt/support/Compiler.h"
+
 #include <cstddef>
 
 namespace orc_rt::sys {
@@ -33,12 +28,27 @@ inline void clear_icache(void *Addr, size_t Size);
 
 } // namespace orc_rt::sys
 
-// Definition of the above. Selected here rather than by the build system so
-// that a reader of this header can see which implementation applies.
+// Definition of the above: the default is here, and a system that can do better
+// than the compiler builtin gets a header of its own, selected below.
 #if defined(__APPLE__)
+
 #include "orc-rt-internal/support/sys/darwin/CacheControl.h"
+
+#elif __has_builtin(__builtin___clear_cache) || defined(__GNUC__)
+
+namespace orc_rt::sys {
+
+inline void clear_icache(void *Addr, size_t Size) {
+  char *Start = static_cast<char *>(Addr);
+  __builtin___clear_cache(Start, Start + Size);
+}
+
+} // namespace orc_rt::sys
+
 #else
-#include "orc-rt-internal/support/sys/posix/CacheControl.h"
+
+#error "No clear_icache implementation for this target"
+
 #endif
 
 #endif // ORC_RT_INTERNAL_SUPPORT_SYS_CACHECONTROL_H
diff --git a/orc-rt/include/orc-rt-internal/support/sys/posix/CacheControl.h b/orc-rt/include/orc-rt-internal/support/sys/posix/CacheControl.h
deleted file mode 100644
index 00fe332e68a00..0000000000000
--- a/orc-rt/include/orc-rt-internal/support/sys/posix/CacheControl.h
+++ /dev/null
@@ -1,31 +0,0 @@
-//===- CacheControl.h - Instruction cache maintenance, generic --*- C++ -*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-//
-// Generic definitions for the operations declared in sys/CacheControl.h, which
-// is the header callers should include.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef ORC_RT_INTERNAL_SUPPORT_SYS_POSIX_CACHECONTROL_H
-#define ORC_RT_INTERNAL_SUPPORT_SYS_POSIX_CACHECONTROL_H
-
-#include <cstddef>
-
-namespace orc_rt::sys {
-
-inline void clear_icache(void *Addr, size_t Size) {
-  // A builtin rather than a declaration of __clear_cache, so that the compiler
-  // can emit the cache-maintenance instructions inline where the target has
-  // them, instead of an opaque call.
-  char *Start = static_cast<char *>(Addr);
-  __builtin___clear_cache(Start, Start + Size);
-}
-
-} // namespace orc_rt::sys
-
-#endif // ORC_RT_INTERNAL_SUPPORT_SYS_POSIX_CACHECONTROL_H



More information about the llvm-commits mailing list