llvm and flushing instruction cache

Hi. I have a very quick question. Is the instruction cache flushed by the llvm jit before code in memory is written to or executed? I’m assuming so, but I can’t find where in the source code this is happening. In particular I searched for clflush, invd, and wbinvd instructions, but couldn’t find them. And if it’s not necessary to do this, can you please explain why? (I’m assuming the target platform has a separate L1 i-cache). Thanks.

  • Shasank

In general, yes. It's possible there are cases we've missed (I fixed
one in the last couple months), in which case please let us know. The
actual code is in sys::Memory::InvalidateInstructionCache
(http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/System/Memory.cpp?view=markup),
called by, for example,
lib/Target/PowerPC/PPCJITInfo.cpp:PPCCompilationCallbackC().

On x86 in particular, explicitly flushing the instruction cache isn't
necessary; the processor transparently handles modifications to cached
code.

-Eli

It handles modifications properly as long as the execution trace passes through a branch before hitting the modified code. It's still possible to run into problems if you try to modify the currently executing basic block.

That said, it's correct that for the purposes of LLVM JIT (and most sane purposes I can imagine), you don't need to worry about flushing the icache on X86.

--Owen

Thanks, and thanks Jeffrey for your response as well. So I see in Memory.cpp in sys_icache_invalidate() that we only invalidate the cache for powerpc. Can you point me to any literature that describes how for x86 the processor handles these modifications automatically? Does this work, by the way, for both AMD and Intel chips? - on Linux and NT? Thanks.

  • Shasank