Illegal instruction when running the CI test suite on Aarch64
Summary
We caught an issue in our downstream CI where test cases from ./tests/gtests/test_batch_normalization_f32 are failing with
[----------] 8 tests from Simple_nCdhw8c_CPU/bnorm_test
[ RUN ] Simple_nCdhw8c_CPU/bnorm_test.TestsBnormF32/0
[1] 63779 illegal hardware instruction (core dumped) ./tests/gtests/test_batch_normalization_f32
Version
Issue started at githash https://github.com/oneapi-src/oneDNN/commit/1249e4eb0cc0a307f41103d7debec31ba918821c
Environment
We only see this problem on some machines from the CI pool and I've not been able to reproduce it on other machines, pasting the output of lscpu below:
Architecture: aarch64
Byte Order: Little Endian
CPU(s): 64
On-line CPU(s) list: 0-63
Thread(s) per core: 1
Core(s) per socket: 16
Socket(s): 4
NUMA node(s): 4
Vendor ID: ARM
Model: 2
Model name: Cortex-A72
Stepping: r0p2
BogoMIPS: 100.00
NUMA node0 CPU(s): 0-15
NUMA node1 CPU(s): 16-31
NUMA node2 CPU(s): 32-47
NUMA node3 CPU(s): 48-63
Flags: fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid
Steps to reproduce
Checkout and build latest master, then run CI with ctest.
It is ./tests/gtests/test_batch_normalization_f32 that is failing.
Observed behavior
May be of interest to @kawakami-k:
I've investigated this behaviour a little and came to the conclusion that this illegal instruction is most likely caused by the by the lack of cache coherency in the architecture (https://community.arm.com/developer/ip-products/processors/b/processors-ip-blog/posts/caches-and-self-modifying-code).
From the article: On Arm, because the D-cache and I-cache are not coherent, the newly-written instructions might be masked by the existing contents of the I-cache, causing the processor to execute old (or possibly invalid) instructions. The major case outside of drivers where you need to worry about caches is with JIT code-generation on non-x86 architectures with non-coherent instruction caches. If you (or a JIT library) write some machine code into a char[] buffer and cast it to a function pointer, architectures like ARM don't guarantee that code-fetch will "see" that newly-written data.
The solution to this problem seems to be clearing the I-cache before loading the JITed code and I can confirm that this fixes the problem for me:
diff --git a/src/cpu/aarch64/jit_generator.hpp b/src/cpu/aarch64/jit_generator.hpp
index 445efe6de..8bbedabe7 100644
--- a/src/cpu/aarch64/jit_generator.hpp
+++ b/src/cpu/aarch64/jit_generator.hpp
@@ -372,6 +372,7 @@ private:
if (!is_initialized()) return nullptr;
const uint8_t *code
= reinterpret_cast<const uint8_t *>(CodeGenerator::getCode());
+ __builtin___clear_cache((void*)code, (void*)(code + getSize() * CSIZE));
register_jit_code(code, getSize() * CSIZE);
return code;
}
Obviously this is a bit hacky and there's probably a better place to put this, but I thought it's worth raising this issue.
Hi, @diaena Thank you for the report and solution.
Could you try the following? https://github.com/kawakami-k/oneDNN/commits/fix_clear_cache
Hi @kawakami-k, apologies for the delay in response, I read your comment before you edited to include the link and did not receive a notification for the edit.
I tried the fix provided in #995 and I can confirm that it works.
Thanks!
Fixed in v2.2, backported to v2.1 release branch.