iaca.h (1920B)
1 // Copyright (c) the JPEG XL Project Authors. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 #ifndef LIB_JXL_BASE_IACA_H_ 7 #define LIB_JXL_BASE_IACA_H_ 8 9 #include "lib/jxl/base/compiler_specific.h" 10 11 // IACA (Intel's Code Analyzer) analyzes instruction latencies, but only for 12 // code between special markers. These functions embed such markers in an 13 // executable, but only for reading via IACA - they deliberately trigger a 14 // crash if executed to ensure they are removed in normal builds. 15 16 #ifndef JXL_IACA_ENABLED 17 #define JXL_IACA_ENABLED 0 18 #endif 19 20 namespace jxl { 21 22 // Call before the region of interest. 23 static JXL_INLINE void BeginIACA() { 24 #if JXL_IACA_ENABLED && (JXL_COMPILER_GCC || JXL_COMPILER_CLANG) 25 asm volatile( 26 // UD2 "instruction" raises an invalid opcode exception. 27 ".byte 0x0F, 0x0B\n\t" 28 // Magic sequence recognized by IACA (MOV + addr32 fs:NOP). This actually 29 // clobbers EBX, but we don't care because the code won't be run, and we 30 // want IACA to observe the same code the compiler would have generated 31 // without this marker. 32 "movl $111, %%ebx\n\t" 33 ".byte 0x64, 0x67, 0x90\n\t" 34 : 35 : 36 // (Allegedly) clobbering memory may prevent reordering. 37 : "memory"); 38 #endif 39 } 40 41 // Call after the region of interest. 42 static JXL_INLINE void EndIACA() { 43 #if JXL_IACA_ENABLED && (JXL_COMPILER_GCC || JXL_COMPILER_CLANG) 44 asm volatile( 45 // See above. 46 "movl $222, %%ebx\n\t" 47 ".byte 0x64, 0x67, 0x90\n\t" 48 // UD2 49 ".byte 0x0F, 0x0B\n\t" 50 : 51 : 52 // (Allegedly) clobbering memory may prevent reordering. 53 : "memory"); 54 #endif 55 } 56 57 // Add to a scope to mark a region. 58 struct ScopeIACA { 59 JXL_INLINE ScopeIACA() { BeginIACA(); } 60 JXL_INLINE ~ScopeIACA() { EndIACA(); } 61 }; 62 63 } // namespace jxl 64 65 #endif // LIB_JXL_BASE_IACA_H_