Thread overview
How to defeat the optimizer in GDC
May 20, 2019
Mike Franklin
May 20, 2019
Iain Buclaw
May 20, 2019
Mike Franklin
May 20, 2019
Mike Franklin
May 20, 2019
I'm trying to benchmark some code, but the optimizer is basically removing all of it, so I'm benchmarking nothing.

I'd like to do something like what Chandler Carruth does here to defeat the optimizer:  https://www.youtube.com/watch?v=nXaxk27zwlk&feature=youtu.be&t=2446

Here presents the following inline asm function to tell the optimizer that `p` is being used (at least that's how I understand it):
```
void escape(void* p)
{
    asm volatile("" : : "g"(p) : memory);
}
```

I tried to do the same thing in D with this function:
```
void use(void* p)
{
    version(LDC)
    {
        import ldc.llvmasm;
         __asm("", "r,~{memory}", p);
    }
    version(GDC)
    {
        asm { "" : : "g"(p), "memory"; }
    }
}
```

The LDC version seems to work fine:  https://d.godbolt.org/z/qbg54J

But I can't get GDC to do the same:  https://explore.dgnu.org/z/quCjhU

Is this currently possible in GDC?

Mike
May 20, 2019
On Monday, 20 May 2019 at 08:11:19 UTC, Mike Franklin wrote:
> I'm trying to benchmark some code, but the optimizer is basically removing all of it, so I'm benchmarking nothing.
>
> I'd like to do something like what Chandler Carruth does here to defeat the optimizer:  https://www.youtube.com/watch?v=nXaxk27zwlk&feature=youtu.be&t=2446
>
> Here presents the following inline asm function to tell the optimizer that `p` is being used (at least that's how I understand it):
> ```
> void escape(void* p)
> {
>     asm volatile("" : : "g"(p) : memory);
> }
> ```
>
> I tried to do the same thing in D with this function:
> ```
> void use(void* p)
> {
>     version(LDC)
>     {
>         import ldc.llvmasm;
>          __asm("", "r,~{memory}", p);
>     }
>     version(GDC)
>     {
>         asm { "" : : "g"(p), "memory"; }
>     }
> }
> ```
>
> The LDC version seems to work fine:  https://d.godbolt.org/z/qbg54J
>
> But I can't get GDC to do the same:  https://explore.dgnu.org/z/quCjhU
>
> Is this currently possible in GDC?
>

Looks like you've done a typo to me. Memory should be a clobber, not an input operand.

--
Iain
May 20, 2019
On Monday, 20 May 2019 at 08:11:19 UTC, Mike Franklin wrote:

> But I can't get GDC to do the same:  https://explore.dgnu.org/z/quCjhU
>
> Is this currently possible in GDC?

Gah!! Ignore that.  `version (GNU)`, not `version(GDC)`.

This works:

void use(void* p)
{
    version(LDC)
    {
        import ldc.llvmasm;
         __asm("", "r,~{memory}", p);
    }
    version(GNU)
    {
        asm { "" : : "r" p : "memory"; };
    }
}

May 20, 2019
On Monday, 20 May 2019 at 08:21:19 UTC, Iain Buclaw wrote:

> Looks like you've done a typo to me. Memory should be a clobber, not an input operand.

Yes, that too.