public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug d/94425] New: [D] Consider always settings ASM_VOLATILE_P on asm statements
@ 2020-03-31 14:46 ibuclaw at gdcproject dot org
  2020-04-01  6:59 ` [Bug d/94425] " rguenth at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: ibuclaw at gdcproject dot org @ 2020-03-31 14:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94425

            Bug ID: 94425
           Summary: [D] Consider always settings ASM_VOLATILE_P on asm
                    statements
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: d
          Assignee: ibuclaw at gdcproject dot org
          Reporter: ibuclaw at gdcproject dot org
  Target Milestone: ---

Currently, the following have equivalents with the c-family front-ends.

asm      { ""; } -> asm __volatile__ ("");
asm pure { ""; } -> asm ("");

In the case of 'asm pure' the result can be cached, with duplicated statements
being DCE'd.  This happens in the std.math unittests when compiling with -O2.

The example below distils what the test looks like after inlining.  The last
asm statement is removed, and the first result is reused, which produces wrong
run-time results.  The use of 'asm pure' is in line with expectations however,
particularly as the getters are 'pure' in the D sense.

Either will have to say that every asm statement is volatile, or will have to
apply a stricter condition.

In C, asm statements without output operands are automatically considered
volatile, but that won't help us here.

---
import core.stdc.fenv;

enum ROUNDING_MASK = FE_TONEAREST|FE_DOWNWARD|FE_UPWARD|FE_TOWARDZERO;

void main()
{
    uint newMode = FE_DOWNWARD;
    fexcept_t oldState;
    asm pure
    {
        "fstcw %0;" : "=m" (oldState);
    }
    fexcept_t newState = cast(fexcept_t)((oldState & (-1 - ROUNDING_MASK))
                                         | (newMode & ROUNDING_MASK));
    asm
    {
        "fclex; fldcw %0;" : : "m" (newState);
    }
    uint mxcsr;
    asm
    {
        "stmxcsr %0" : "=m" (mxcsr);
    }
    mxcsr &= ~(ROUNDING_MASK << 3);
    mxcsr |= (newState & ROUNDING_MASK) << 3;
    mxcsr &= ~(FE_ALL_EXCEPT << 7);
    mxcsr |= (newState & FE_ALL_EXCEPT) << 7;
    asm
    {
        "ldmxcsr %0" : : "m" (mxcsr);
    }
    fexcept_t contState;
    asm pure
    {
        "fstcw %0;" : "=m" (contState);
    }
    assert(cast(uint)(contState & ROUNDING_MASK) == FE_DOWNWARD);
}

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-04-07 16:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-31 14:46 [Bug d/94425] New: [D] Consider always settings ASM_VOLATILE_P on asm statements ibuclaw at gdcproject dot org
2020-04-01  6:59 ` [Bug d/94425] " rguenth at gcc dot gnu.org
2020-04-01 17:38 ` ibuclaw at gdcproject dot org
2020-04-06 13:27 ` ibuclaw at gdcproject dot org
2020-04-07  7:44 ` cvs-commit at gcc dot gnu.org
2020-04-07 16:32 ` ibuclaw at gdcproject dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).