[PATCH v2 11/20] util/cacheflush.c: Update cache flushing mechanism for Emscripten

Kohei Tokunaga posted 20 patches 6 months, 3 weeks ago
There is a newer version of this series
[PATCH v2 11/20] util/cacheflush.c: Update cache flushing mechanism for Emscripten
Posted by Kohei Tokunaga 6 months, 3 weeks ago
Although __builtin___clear_cache is used to flush the instruction cache for
a specified memory region[1], this operation doesn't apply to wasm, as its
memory isn't executable. Moreover, Emscripten does not support this builtin
and fails to compile it with the following error.

> fatal error: error in backend: llvm.clear_cache is not supported on wasm

To resolve this, this commit removes the call to __builtin___clear_cache for
Emscripten build.

[1] https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005f_005f_005fclear_005fcache

Signed-off-by: Kohei Tokunaga <ktokunaga.mail@gmail.com>
---
 include/qemu/cacheflush.h | 7 +++++++
 util/cacheflush.c         | 4 ++++
 2 files changed, 11 insertions(+)

V2:
- Added an explanation for the change to both the code comment and the
  commit message.

diff --git a/include/qemu/cacheflush.h b/include/qemu/cacheflush.h
index ae20bcda73..76eb55d818 100644
--- a/include/qemu/cacheflush.h
+++ b/include/qemu/cacheflush.h
@@ -26,6 +26,13 @@ static inline void flush_idcache_range(uintptr_t rx, uintptr_t rw, size_t len)
     /* icache is coherent and does not require flushing. */
 }
 
+#elif defined(EMSCRIPTEN)
+
+static inline void flush_idcache_range(uintptr_t rx, uintptr_t rw, size_t len)
+{
+    /* Wasm doesn't have executable region of memory. */
+}
+
 #else
 
 void flush_idcache_range(uintptr_t rx, uintptr_t rw, size_t len);
diff --git a/util/cacheflush.c b/util/cacheflush.c
index 1d12899a39..17c58918de 100644
--- a/util/cacheflush.c
+++ b/util/cacheflush.c
@@ -229,6 +229,10 @@ static void __attribute__((constructor)) init_cache_info(void)
 
 /* Caches are coherent and do not require flushing; symbol inline. */
 
+#elif defined(EMSCRIPTEN)
+
+/* Wasm doesn't have executable region of memory. */
+
 #elif defined(__aarch64__) && !defined(CONFIG_WIN32)
 /*
  * For Windows, we use generic implementation of flush_idcache_range, that
-- 
2.25.1
Re: [PATCH v2 11/20] util/cacheflush.c: Update cache flushing mechanism for Emscripten
Posted by Philippe Mathieu-Daudé 6 months, 3 weeks ago
On 22/4/25 07:27, Kohei Tokunaga wrote:
> Although __builtin___clear_cache is used to flush the instruction cache for
> a specified memory region[1], this operation doesn't apply to wasm, as its
> memory isn't executable. Moreover, Emscripten does not support this builtin
> and fails to compile it with the following error.
> 
>> fatal error: error in backend: llvm.clear_cache is not supported on wasm

Note, you mix LLVM error ...

> 
> To resolve this, this commit removes the call to __builtin___clear_cache for
> Emscripten build.
> 
> [1] https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005f_005f_005fclear_005fcache

... with this GCC documentation:

   If the target does not require instruction cache flushes,
   __builtin___clear_cache has no effect.

I'd expect __builtin___clear_cache() to be OK, having no effect on a GCC
WASM implementation.

> 
> Signed-off-by: Kohei Tokunaga <ktokunaga.mail@gmail.com>
> ---
>   include/qemu/cacheflush.h | 7 +++++++
>   util/cacheflush.c         | 4 ++++
>   2 files changed, 11 insertions(+)
Re: [PATCH v2 11/20] util/cacheflush.c: Update cache flushing mechanism for Emscripten
Posted by Kohei Tokunaga 6 months, 3 weeks ago
Hi Philippe,

>On 22/4/25 07:27, Kohei Tokunaga wrote:
>> Although __builtin___clear_cache is used to flush the instruction cache
for
>> a specified memory region[1], this operation doesn't apply to wasm, as
its
>> memory isn't executable. Moreover, Emscripten does not support this
builtin
>> and fails to compile it with the following error.
>>
>>> fatal error: error in backend: llvm.clear_cache is not supported on wasm
>
>Note, you mix LLVM error ...
>
>>
>> To resolve this, this commit removes the call to __builtin___clear_cache
for
>> Emscripten build.
>>
>> [1]
https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005f_005f_005fclear_005fcache
>
>... with this GCC documentation:
>
>   If the target does not require instruction cache flushes,
>   __builtin___clear_cache has no effect.
>
>I'd expect __builtin___clear_cache() to be OK, having no effect on a GCC
>WASM implementation.

Thank you for the feedback.
Although Emscripten supports built-in functions using GCC-compatible syntax,
the corner-case behavior looks different from what's described in the GCC
documentation, as shown in this patch. To avoid the confusion, I'll remove
that link in the next version of the patch series.
Re: [PATCH v2 11/20] util/cacheflush.c: Update cache flushing mechanism for Emscripten
Posted by Philippe Mathieu-Daudé 6 months, 3 weeks ago
On 22/4/25 07:27, Kohei Tokunaga wrote:
> Although __builtin___clear_cache is used to flush the instruction cache for
> a specified memory region[1], this operation doesn't apply to wasm, as its
> memory isn't executable. Moreover, Emscripten does not support this builtin
> and fails to compile it with the following error.
> 
>> fatal error: error in backend: llvm.clear_cache is not supported on wasm
> 
> To resolve this, this commit removes the call to __builtin___clear_cache for
> Emscripten build.
> 
> [1] https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005f_005f_005fclear_005fcache
> 
> Signed-off-by: Kohei Tokunaga <ktokunaga.mail@gmail.com>
> ---
>   include/qemu/cacheflush.h | 7 +++++++
>   util/cacheflush.c         | 4 ++++
>   2 files changed, 11 insertions(+)

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>