• I wonder if there is a method to preserve the order of instructions from inline assembly relative to other statements. • According to Extended Asm (Using the GNU Compiler Collection (GCC)) : Note that the compiler can move even volatile asm instructions relative to other code, including across jump instructions. • For example, on many targets there is a system register that controls the rounding mode of floating-point operations. • Setting it with a volatile asm statement, as in the following PowerPC example, does not work reliably. • asm volatile(“mtfsf 255, %0” : : “f” (fpenv)); sum = x + y; The compiler may move the addition back before the volatile asm statement. • To make it work as expected, add an artificial dependency to the asm by referencing a variable in the subsequent code, for example: asm volatile (“mtfsf 255,%1” : “=X” (sum) : “f” (fpenv)); sum = x + y; I have the following testing code: float foo(float x, float y, float fpenv) { float sum; asm volatile (“mtfsf 255,%1” : “=X” (sum) : “f” (fpenv)); sum = x + y; return sum; } Compiling it with $ clang-21 -target powerpc t.c -S -o - -O2 And I get .file “t.c” .text .globl foo # – Begin function foo .p2align 2 .type foo,@function foo: # @foo .Lfunc_begin0: .cfi_startproc # %bb.0: stwu 1, -16(1) .cfi_def_cfa_offset 16 fadds 1, 1, 2 #APP mtfsf 255, 3 #NO_APP addi 1, 1, 16 blr .Lfunc_end0: .size foo, .Lfunc_end0-.Lfunc_begin0 .cfi_endproc # – End function .ident “Debian clang version 21.1.8 (3+b1)” .section “.note.GNU-stack”,"

Article Summaries:

  • I wonder if there is a method to preserve the order of instructions from inline assembly relative to other statements. According to Extended Asm (Using the GNU Compiler Collection (GCC)) : Note that the compiler can move even volatile asm instructions relative to other code, including across jump instructions. For example, on many targets there is a system register that controls the rounding mode of floating-point operations. Setting it with a volatile asm statement, as in the following PowerPC example, does not work reliably. asm volatile(“mtfsf 255, %0” : : “f” (fpenv)); sum = x + y; The com

Sources: