compiler-optimization

Building backward compatible binaries with newer CPU instructions support

那年仲夏 提交于 2021-01-27 12:41:41
问题 What is the best way to implement multiple versions of the same function that uses a specific CPU instructions if available (tested at run time), or falls back to a slower implementation if not? For example, x86 BMI2 provides a very useful PDEP instruction. How would I write a C code such that it tests BMI2 availability of the executing CPU on startup, and uses one of the two implementations -- one that uses _pdep_u64 call (available with -mbmi2 ), and another that does bit manipulation "by

Does MSVC 2017 support automatic CPU dispatch?

一曲冷凌霜 提交于 2021-01-27 07:30:39
问题 I read on a few sites that MSVC can actually emit say AVX instructions, when SSE2 architecture is used and detect the AVX support runtime. Is it true? I tested various loops that would definitely benefit from AVX/AVX2 support, but when run in debugger I couldn't really find any AVX instructions. When /arch:AVX is used, then it emits AVX instructions, but it of course crashes on CPUs that doesn't support it (tested), so no runtime detection either. I could use AVX intrinsics though and it

Do compilers optimize out net zero bit shifts?

允我心安 提交于 2021-01-27 04:12:37
问题 I have some code like the following code block (I am not allowed to post the original code) inside a .cpp file that I think is being compiled by clang++ ( Ubuntu clang version 3.5.2-3ubuntu1 (tags/RELEASE_352/final) (based on LLVM 3.5.2) ). It looks like C code, because we are using GoogleTest for testing our C code. Anyways: size_t const SHIFT = 4; uint8_t var, var2; /* Omitted: Code that sets var to, say 00011000 (base 2) */ var2 = var; var = var << SHIFT >> SHIFT; // [1] result is 00011000

Do compilers optimize out net zero bit shifts?

主宰稳场 提交于 2021-01-27 04:11:11
问题 I have some code like the following code block (I am not allowed to post the original code) inside a .cpp file that I think is being compiled by clang++ ( Ubuntu clang version 3.5.2-3ubuntu1 (tags/RELEASE_352/final) (based on LLVM 3.5.2) ). It looks like C code, because we are using GoogleTest for testing our C code. Anyways: size_t const SHIFT = 4; uint8_t var, var2; /* Omitted: Code that sets var to, say 00011000 (base 2) */ var2 = var; var = var << SHIFT >> SHIFT; // [1] result is 00011000

How to get the gcc compiler to not optimize a standard library function call like printf?

非 Y 不嫁゛ 提交于 2021-01-26 19:28:44
问题 Out of curiosity, I was wondering if there is any way that gcc does not optimize any function calls? In the generated assembly code, the printf function is replaced by putchar. This happens even with the default -O0 minimal optimization flag. #include <stdio.h> int main(void) { printf("a"); return 0; } (Godbolt showing GCC9 doing it, clang8 keeping it unchanged.) 回答1: Use -fno-builtin to disable all replacement and inlining of standard C functions with equivalents. Or use -fno-builtin

How to get the gcc compiler to not optimize a standard library function call like printf?

血红的双手。 提交于 2021-01-26 19:27:25
问题 Out of curiosity, I was wondering if there is any way that gcc does not optimize any function calls? In the generated assembly code, the printf function is replaced by putchar. This happens even with the default -O0 minimal optimization flag. #include <stdio.h> int main(void) { printf("a"); return 0; } (Godbolt showing GCC9 doing it, clang8 keeping it unchanged.) 回答1: Use -fno-builtin to disable all replacement and inlining of standard C functions with equivalents. Or use -fno-builtin

What's the difference between a compiler's `-O0` option and `-Og` option?

元气小坏坏 提交于 2020-08-17 04:47:05
问题 When I want to do debugging of C or C++ programs, I've been taught to use -O0 to turn optimization OFF, and -ggdb to insert symbols into the executable which are optimized for using the GNU gdb debugger, which I use (or, you can use -glldb for LLVM/clang's lldb debugger, or just -g for general debugging symbols, but that won't be as good as -ggdb apparently...). However, I recently stumbled upon someone saying to use -Og (instead of -O0 ), and it caught me off-guard. Sure enough though, it's

What's the difference between a compiler's `-O0` option and `-Og` option?

∥☆過路亽.° 提交于 2020-08-17 04:45:29
问题 When I want to do debugging of C or C++ programs, I've been taught to use -O0 to turn optimization OFF, and -ggdb to insert symbols into the executable which are optimized for using the GNU gdb debugger, which I use (or, you can use -glldb for LLVM/clang's lldb debugger, or just -g for general debugging symbols, but that won't be as good as -ggdb apparently...). However, I recently stumbled upon someone saying to use -Og (instead of -O0 ), and it caught me off-guard. Sure enough though, it's