x86: extend coverage of -mlfence-after-load
Discussion on extending the x86 -mlfence-after-load option to cover more instructions that involve memory reads.
This email discusses extending the x86 assembler’s -mlfence-after-load option to include more instructions that perform memory reads, beyond just explicit loads. The sender, Haochen Jiang, is responding to Jan Beulich’s detailed analysis of various x86 instructions (like PUSH, LTR, LLDT, VERR, VERW, BT, SHLD/SHRD, BOUND, ARPL, CMPXCHG, VMXON, VMCLEAR, XSAVE) that involve memory reads or read-modify-write operations. The conversation aims to accurately identify and cover all relevant instructions to ensure correct memory ordering semantics when this option is used.
- proposer
Responds to detailed analysis of x86 instructions, indicating a need to extend the `-mlfence-after-load` option's coverage to include more memory-accessing instructions.
“Without even considering ISA extensions which appeared only in newer hardware, there are several issues: - PUSH with a memory operand is both a read and a write (much like POP is), - LTR, LLDT, VERR, and VERW are memory reads (first explicit, then implicit), - BT{,C,R,S} with register operand aren't any different from ones with immediate operand, - SHLD/SHRD aren't any different from SHL/SH…”
- reviewer
Provides a comprehensive list of x86 instructions that perform memory reads or read-modify-write operations, questioning the current coverage of the `-mlfence-after-load` option and suggesting simplifications.
“Sadly the Intel page referenced by the original commit doesn't provide any technical details, and the onwards reference from there now redirects to something generic (i.e. no technical details there either). For a few vector insns the situation is unclear: Pretty likely MASKMOV{Q,DQU}, V{,P}MASKMOV*, and masked AVX512 stores (V{,P}COMPRESS* possibly even when not masked) are read-modify-write op…”
Technical Tradeoffs
- Ensuring correct memory ordering semantics for performance-critical code vs. potentially adding overhead with unnecessary fence instructions.
- The complexity of accurately classifying all x86 instructions' memory access behavior.
- Balancing the need for compiler-generated fences with the programmer's explicit control over memory ordering.
In Details
The -mlfence-after-load compiler flag aims to insert memory fence instructions (like LFENCE) after certain memory load instructions to enforce memory ordering. This discussion focuses on identifying all x86 instructions that implicitly perform memory reads or read-modify-write operations, which should also be candidates for having an LFENCE inserted after them by the compiler when this flag is enabled. The thread analyzes numerous instructions to ensure comprehensive and correct coverage, as the documentation for some is unclear.
- LFENCE
- A specific x86 instruction that acts as a serializing instruction, preventing processors from reordering memory reads around it. It ensures that all prior loads are completed before any subsequent loads are initiated.
- memory ordering
- The rules governing the order in which memory operations (reads and writes) appear to execute from the perspective of different processors or cores in a multiprocessor system. Compilers and processors may reorder operations for performance, but fences are used to enforce specific ordering when necessary.
- instruction
- A basic command or operation that a CPU can execute. Examples include ADD, MOV, JMP, and LFENCE.
- ISA extensions
- Instruction Set Architecture extensions, which add new instructions to a processor's capabilities. Examples include SSE, AVX, and AMX for x86.
- read-modify-write
- An operation that reads a value from memory, modifies it, and then writes the modified value back to memory. Some instructions perform this atomically, while others may not.