0

As I understand it, currently there is no equivalent in GDC of GCC's asm volatile (to prevent optimisation-to-nothing, or motion). Is this correct?

If you wish to prevent certain nasties, such as either optimisation away to nothing, or code motion, in certain situations when using calls to GDC extended asm blocks, then what are the possibilities for a workaround? (I've read about tricks such as introducing a fake output from an asm block plus a dependency on an ‘anchor’ in the rest of the code, which might or might not work.) Such things seem flaky and are rather unattractive if the code needs to be ultra-high-performance, but maybe that's just life.

I suppose there is a general question: never mind asm, how do you prevent compilers from just deleting code altogether if it is actually needed because of its side-effects, eg i/o? Is it a really stupid question to ask why a compiler doesn't just delete calls to eg printf(), it would certainly speed the code up, and the rest of the code doesn't depend on it. Is it all down to where inlining, whole-program optimisation / LTO fails and beyond that a compiler has to be conservative and assume that calls to functions that have no outputs actually do something useful?

Cecil Ward
  • 597
  • 2
  • 13
  • I just noticed this [earlier thread](http://stackoverflow.com/questions/19965076/gcc-memory-barrier-sync-synchronize-vs-asm-volatile-memory) – Cecil Ward Feb 27 '17 at 03:38
  • I just noticed this [earlier thread](http://stackoverflow.com/questions/19965076/gcc-memory-barrier-sync-synchronize-vs-asm-volatile-memory) about 's/w barriers' to prevent code of loads/stores motion above / below the asm block. That's interesting, but it isn't the same thing as preventing the asm block itself moving relative to control structures, for example moving out of a loop to be before a loop in order to make the code (genuinely) much faster, but lose some side effect you need. – Cecil Ward Feb 27 '17 at 03:46
  • Maybe modern programming languages need something new and explicit, a new control structure - a ‘temporal sequence iterator’. – Cecil Ward Feb 27 '17 at 03:47
  • If GDC doesn't let you apply the volatile attribute to asm statements then I would suggest not using inline assembly in cases where the attribute is needed. Instead just use plain D code, or where that's not possible, ordinary assembly. – Ross Ridge Feb 27 '17 at 04:10
  • 1
    I'd add that with GCC (but not necessarily GDC) using `volatile` with a asm statement that has nothing listed as an output operands is redundant, GCC will assume it must have some unspecified side effect. You only need to use `volatile` with asm statements that have output operands and have side effects beyond those. Also the post you linked in a comment doesn't seem to be relevant as it's about an obsolete trick to prevent compiler reordering of loads and stores made by normal C/C++ code, not inline assembly. Modern code should use the modern C/C++ language support for memory ordering. – Ross Ridge Feb 27 '17 at 04:34
  • Thanks for that. Useful to know about the no-outputs case. And agreed, I said that the thread linked to wasn't the thing I needed. – Cecil Ward Feb 27 '17 at 05:32
  • So Ross, if that is equally true for the D member of the GCC family, then it is part of an answer. In one particular case I did have know outputs, so I was wary. D still needs volatile for the has-outputs case, for the same reason that C needs it. – Cecil Ward Feb 27 '17 at 05:36
  • I *strongly* suspect that in all current D implementations, `asm` blocks are assumed to be volatile. I don't have time right now to dig through the sources and confirm this though, and can't find any mention in the spec of how `asm` interacts with the optimiser. The only case I can think of where 'volatile' might not be assumed is `asm pure {…}`. – Cauterite Feb 28 '17 at 15:18
  • In GDC, AFAICS only extended asm blocks without output arguments are automatically marked volatile: `// Asm statements without outputs are treated as volatile. ASM_VOLATILE_P (exp) = (s->outputargs == 0);`. But AFAIK there's no way to explicitly mark asm blocks with outputs as volatile. – jpf Mar 01 '17 at 23:39
  • Anyway, I would think it’s safe to assume that whatever feature has been found necessary in GCC in a case such as this will be just as necessary in GDC. And of course I have noted Ross Ridge’s very helpful insight. I wouldn’t be able to find my way round the innards of GDC to find the necessary answer about optimisers and asm statements. If anyone is able to help me out there then I will be very grateful. – Cecil Ward May 24 '23 at 04:03

0 Answers0