3 ms·
How/when will we ever be able to confidently tell Gcc to generate these instructions, when we generally only know the code will be expected to run on some or ot
by ncmncm 5y ago
How/when will we ever be able to confidently tell Gcc to generate these instructions, when we generally only know the code will be expected to run on some or other Aaargh64?
It is the same problem as POPCNT on Amd64, and practically everything on RISC-V. Checking some status flag at program start is OK for choosing computation kernels that will run for microseconds or longer, but for things that take only a few cycles anyway, at best, checking first makes them take much longer.
I imagine monkeypatching at startup, the way link relocations used to get patched in the days before we had ISAs that didn't support PIC. But that is miserable.
- floatboth 5y agoAll implementation selection should be done with ifuncs. Sadly lots of programs still do it with just function pointers.
- th3typh00n 5y agoifuncs is a non-standard compiler extension that only works on certain operating systems. Developers that cares about portability are obviously going to stay far away from such things.
- Unklejoe 5y agoI guess the fist step could be to handle it in the C library using some capability check and function pointers, then perhaps later on in the compiler if some mcpu flag or something is provided.
- ndesaulniers 5y agoGood questions. For computer support, generally you would pass a -mcpu= flag (or maybe -mattr=, but that might be a compiler internal flag, I forget). Obviously then that's not portable and has implications on the ABI. I didn't read the article but I suspect they might be in ARMv9.0, hopefully, otherwise "better luck next major revision." For monkey patching, the Linux kernel already does this aggressively since it generally has permission to read the relevant machine specific registers (MSRs). Doesn't help userspace, but userspace can do something similar with hwcaps and ifuncs.
- hawk_ 5y agosorry i am out of loop here. what specifically is the problem with POPCNT on AMD64?
- ncmncm 5y agoPOPCNT was added to amd64 in 2003. Because there are still amd64 machines from before then, compilers don't produce POPCNT instructions unless directed to target a later chip. MSVC emits them to implement extension _popcount, but does not use that in its stdlib. Gcc, without a directive, expands __builtin_popcount to much slower code. You can check for a "capability", but testing and branching before a POPCNT instruction adds latency and burns a precious branch prediction slot. Most of the useful instructions on RISC-V are in optional extensions. Sometimes this is OK because you can put a whole loop behind a feature test. But some of these instructions would tend to be isolated and appear all over. That is the case for memcpy and memset, too, which often operate over very small blocks.
- deleted 5y ago[deleted]