Local variables reordering and 'asm volatile("" ::: "memory");'

Alexander Monakov amonakov@ispras.ru
Mon Sep 9 04:23:00 GMT 2019


On Sun, 8 Sep 2019, Ayrat Gaskarov wrote:

> But it would be a real pain to add asm volatile for every possible
> variable and computations that were previously (and after) done (may
> be even from callers up a stacktrace). Could we somehow avoid this?

You can move the code-that-shall-not-be-disturbed in a separate function
and limit visibility of interprocedural analysis into that function,
for example by compiling it in a separate file without LTO,
or calling it via a pointer received from an asm, e.g.:

  int (*fptr)(void);
  asm("" : "=r"(fptr) : "0"(&critical_function));
  /* The above has the effect of assigning fptr = &critical_function,
     but the compiler cannot prove the equality. In principle it could
     emit a check and optimize speculatively. */
  fptr();

Alexander



More information about the Gcc-help mailing list