Opened 4 years ago

Closed 4 years ago

#8561 closed bug (fixed)

Clang/iOS: __builtin___clear_cache doesn't exist in clang, use __clear_cache instead

Reported by: lukexi Owned by:
Priority: high Milestone:
Component: Compiler Version: 7.6.3
Keywords: Cc: thoughtpolice, nkpart@…
Operating System: Other Architecture: Unknown/Multiple
Type of failure: Building GHC failed Test Case:
Blocked By: Blocking:
Related Tickets: #8562 Differential Rev(s):
Wiki Page:


The builtin_clear_cache from GCC is just called clear_cache with clang, and must be declared. Otherwise the build fails

(introduced in

Attachments (2)

clear_cache_support_for_clang.patch (1.5 KB) - added by lukexi 4 years ago.
clear_cache_support_for_clang_via_sys_icache_invalidate.patch (1.7 KB) - added by lukexi 4 years ago.

Download all attachments as: .zip

Change History (16)

Changed 4 years ago by lukexi

comment:1 Changed 4 years ago by lukexi

Actually, __clear_cache seems to be missing entirely on iOS, so I'm not sure what to do here.

(I found a reference to someone reimplementing it in assembly?

Last edited 4 years ago by lukexi (previous) (diff)

comment:2 Changed 4 years ago by thoughtpolice

A few years ago I helped Mike Pall (of LuaJIT) dig into using the JIT component on iOS, which involved finding a way to clear the icache.

I dug up the code. On Darwin, the trick seems to be sys_icache_invalidate:

I imagine this still works, but I can't test it (I no longer have a jailbroken iOS device where the JIT would work.)

comment:3 Changed 4 years ago by lukexi

OK, thanks for that! I'm running a build now with

#if defined(ios_HOST_OS) || defined (darwin_HOST_OS)
void sys_icache_invalidate(void *start, size_t len);

/* Synchronize data/instruction cache. */
void clearCache(void *start, void *end)
#if defined(ios_HOST_OS) || defined (darwin_HOST_OS)
  sys_icache_invalidate(start, (char *)end-(char *)start);
#elif defined(__GNUC__)
  __builtin___clear_cache(start, end);
#error "Missing builtin to flush instruction cache"

adapted from that to test

comment:4 Changed 4 years ago by carter

heatsink is likely to follow up on this, but GCC 4.2 lacks the builtin clear cache primitive, and thus any OS X system with XCode < 5 has a GCC old enough that builtin_clear_cache shoudn't be used.

(at least if we want to support working at all on older OS X systems, which i think is worth doing)

comment:5 Changed 4 years ago by carter

that is, on x86 architectures we perhaps shouldn't be calling clear cache at all.

comment:6 Changed 4 years ago by heatsink

comment:7 Changed 4 years ago by nkpart

Cc: nkpart@… added

comment:8 Changed 4 years ago by lukexi

(just the above in patch form, it's been working quite happily in intense use on iOS)

comment:9 Changed 4 years ago by amosrobinson

Thanks! I'm trying now on OSX10.8.5 & GCC 4.2.1. I got a warning (and error due to -Werror) that clearCache didn't have a prototype before it, so I made it static.

comment:10 Changed 4 years ago by lukexi

Thanks! updated.

comment:11 Changed 4 years ago by carter

i'm meh about that fixup patch, the real issue is we don't need to call any cache synchronization on x86/ x86_64 architectures.

I'll integrate the above patch into my fixup.

comment:12 Changed 4 years ago by Austin Seipp <austin@…>

In 9fbb8c788d4bdf091709778b42a1294bbdabef95/ghc:

Rejigger flushExec implementation (#8562, #8561)

Instead, just don't do anything on x86/amd64, and on !x86, use either A)
__clear_cache from libgcc, or B) sys_icache_invalidate for OS X (and

Signed-off-by: Austin Seipp <>

comment:13 Changed 4 years ago by thoughtpolice

This should now be fixed.

comment:14 Changed 4 years ago by thoughtpolice

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.