[llvm] [RISCV][Support] Fix instruction-cache invalidation in Memory::InvalidateInstructionCache (PR #216059)

Fu-Chiang Chang via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 12 07:47:25 PDT 2026


https://github.com/Com1t updated https://github.com/llvm/llvm-project/pull/216059

>From cbd2e60558b06673ff399124539b4f4a8b11e1f7 Mon Sep 17 00:00:00 2001
From: Fu-Chiang Chang <fu-chiang.chang at sifive.com>
Date: Thu, 13 Aug 2026 15:41:54 +0800
Subject: [PATCH] Ensure icache is flushed after calling
 InvalidateInstructionCache on RISC-V

---
 llvm/lib/Support/Unix/Memory.inc | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/llvm/lib/Support/Unix/Memory.inc b/llvm/lib/Support/Unix/Memory.inc
index bac208a7d543ca..ccec5c575e1856 100644
--- a/llvm/lib/Support/Unix/Memory.inc
+++ b/llvm/lib/Support/Unix/Memory.inc
@@ -237,6 +237,12 @@ void Memory::InvalidateInstructionCache(const void *Addr, size_t Len) {
   for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
     asm volatile("icbi 0, %0" : : "r"(Line));
   asm volatile("isync");
+#elif defined(__riscv) && defined(__GNUC__)
+  // __clear_cache() is a no-op in libgcc on RISC-V and is not lowered
+  // by Clang; the builtin lowers to __riscv_flush_icache.
+  const char *Start = static_cast<const char *>(Addr);
+  const char *End = Start + Len;
+  __builtin___clear_cache(const_cast<char *>(Start), const_cast<char *>(End));
 #elif (defined(__arm__) || defined(__aarch64__) || defined(__loongarch__) ||   \
        defined(__mips__)) &&                                                   \
     defined(__GNUC__)



More information about the llvm-commits mailing list