Protecting control dependencies with volatile_if()
Protecting control dependencies with volatile_if()
Posted Jun 19, 2021 23:37 UTC (Sat) by tialaramex (subscriber, #21167)In reply to: Protecting control dependencies with volatile_if() by alison
Parent article: Protecting control dependencies with volatile_if()
#define barrer() __asm__ __volatile__("":::"memory")
What that says is: Here are some assembly instructions you must execute at this point in my code. The instruction somehow don't need any of my variables in CPU registers, nor do the CPU registers need to be copied into any of my variables. However, these instructions touch memory somehow. Also, that list of instruction is empty.
This is a very common trick, the compiler is used to seeing this, and won't go "Huh? But your list of instructions is empty". Since it claims to touch memory in an unspecified way, no optimisations may move reads or writes past this line, as that would alter the meaning of the program. This is of course exactly what everybody who writes this wanted to achieve and the compiler authors know that.
Since the list of instructions is empty you don't even need to know the assembly language of the target CPU.