Jump to page: 1 2
Thread overview
Strict aliasing in D
Jan 29, 2012
Denis Shelomovskij
Jan 29, 2012
Daniel Murphy
Jan 29, 2012
Peter Alexander
Jan 29, 2012
Daniel Murphy
Jul 26, 2013
monarch_dodra
Jul 27, 2013
Walter Bright
Jul 27, 2013
monarch_dodra
Jul 27, 2013
deadalnix
Jul 27, 2013
monarch_dodra
Jul 27, 2013
Walter Bright
Jul 27, 2013
monarch_dodra
Jul 28, 2013
deadalnix
Jul 27, 2013
David Nadlinger
Jul 27, 2013
Walter Bright
Jul 29, 2013
Denis Shelomovskij
Aug 03, 2013
David Nadlinger
Aug 03, 2013
monarch_dodra
Jul 27, 2013
ponce
Jul 27, 2013
ponce
Jul 27, 2013
bearophile
January 29, 2012
It was originally posted to D.learn but there was no reply. So:

Is there a strict aliasing rule in D?

I just saw https://bitbucket.org/goshawk/gdc/changeset/b44331053062
January 29, 2012
"Denis Shelomovskij" <verylonglogin.reg@gmail.com> wrote in message news:jg3f21$1jqa$1@digitalmars.com...
> It was originally posted to D.learn but there was no reply. So:
>
> Is there a strict aliasing rule in D?
>
> I just saw https://bitbucket.org/goshawk/gdc/changeset/b44331053062

Struct aliasing is required when doing array operations.

eg.
int[] a, b, c;
a[] = b[] + c[];

The arrays used must not overlap.
I'm pretty sure that's what that commit was about.


January 29, 2012
On Sunday, 29 January 2012 at 14:05:25 UTC, Daniel Murphy wrote:
> "Denis Shelomovskij" <verylonglogin.reg@gmail.com> wrote in message news:jg3f21$1jqa$1@digitalmars.com...
>> It was originally posted to D.learn but there was no reply. So:
>>
>> Is there a strict aliasing rule in D?
>>
>> I just saw https://bitbucket.org/goshawk/gdc/changeset/b44331053062
>
> Struct aliasing is required when doing array operations.
>
> eg.
> int[] a, b, c;
> a[] = b[] + c[];
>
> The arrays used must not overlap.
> I'm pretty sure that's what that commit was about.

That's not strict aliasing, that's just a language rule about aliasing for vector ops.

Strict aliasing is the assumption that no two pointers of different types point to the same location (char is an exception). For example, with -fstrict-aliasing in gcc, the following could generate unexpected code:

int* i = new int;
float* f = (float*)i;
*i = 0;
*f = 1.0f;
printf("%d\n", *i); // could still write 0

Because of strict aliasing, the compiler may assume that the assignment *f = 1.0f can't affect the int, so it could just print out 0 instead of whatever 1.0f is as an int. To get around it, you would have to either use a union (which is still implementation defined, but works around aliasing), or make i volatile.

As for D, I can't see anything in the standard that prevents two pointers of different types from pointing to the same location, but I suspect it is an assumption that is being made.
January 29, 2012
"Peter Alexander" <peter.alexander.au@gmail.com> wrote in message news:ggzqksxaiccnkvztmsql@dfeed.kimsufi.thecybershadow.net...
>
> That's not strict aliasing, that's just a language rule about aliasing for vector ops.
>
Yeah, you're right.  It's just an aliasing rule.


July 26, 2013
On Sunday, 29 January 2012 at 16:25:33 UTC, Peter Alexander wrote:
> As for D, I can't see anything in the standard that prevents two pointers of different types from pointing to the same location, but I suspect it is an assumption that is being made.

Resurrecting this old thread, maybe we'll get a better answer this time. I too am interested in knowing how D deals with pointer aliasing.

I'd like a bit more of an "official" or "factual" answer.
July 27, 2013
On 7/26/2013 12:45 PM, monarch_dodra wrote:
> On Sunday, 29 January 2012 at 16:25:33 UTC, Peter Alexander wrote:
>> As for D, I can't see anything in the standard that prevents two pointers of
>> different types from pointing to the same location, but I suspect it is an
>> assumption that is being made.
>
> Resurrecting this old thread, maybe we'll get a better answer this time. I too
> am interested in knowing how D deals with pointer aliasing.
>
> I'd like a bit more of an "official" or "factual" answer.

Although it isn't in the spec, D should be "strict aliasing". This is because:

1. it enables better code generation

2. there are ways, such as unions, to get the other aliasing that doesn't break strict aliasing
July 27, 2013
On Saturday, 27 July 2013 at 06:58:04 UTC, Walter Bright wrote:
> On 7/26/2013 12:45 PM, monarch_dodra wrote:
>> On Sunday, 29 January 2012 at 16:25:33 UTC, Peter Alexander wrote:
>>> As for D, I can't see anything in the standard that prevents two pointers of
>>> different types from pointing to the same location, but I suspect it is an
>>> assumption that is being made.
>>
>> Resurrecting this old thread, maybe we'll get a better answer this time. I too
>> am interested in knowing how D deals with pointer aliasing.
>>
>> I'd like a bit more of an "official" or "factual" answer.
>
> Although it isn't in the spec, D should be "strict aliasing". This is because:
>
> 1. it enables better code generation
>
> 2. there are ways, such as unions, to get the other aliasing that doesn't break strict aliasing

Thank you for the answer. I expected D to do strict aliasing for the reasons you mentioned. This does come up with two follow up question though:

1. Does strict aliasing apply to slices?
2. C++ uses 'char' as a 'neutral' type that can alias to anything. What about D? Does char fill that role? Does ubyte?
July 27, 2013
On Saturday, 27 July 2013 at 08:08:01 UTC, monarch_dodra wrote:
> Thank you for the answer. I expected D to do strict aliasing for the reasons you mentioned. This does come up with two follow up question though:
>
> 1. Does strict aliasing apply to slices?
> 2. C++ uses 'char' as a 'neutral' type that can alias to anything. What about D? Does char fill that role? Does ubyte?

We have void* and void[], I think they should have that role.
July 27, 2013
On Saturday, 27 July 2013 at 06:58:04 UTC, Walter Bright wrote:
> Although it isn't in the spec, D should be "strict aliasing". This is because:
>
> 1. it enables better code generation
>
> 2. there are ways, such as unions, to get the other aliasing that doesn't break strict aliasing

We need to carefully formalize this then, and quickly. The problem GCC, Clang and others are facing is that (as you are probably aware) 2. isn't guaranteed to work for type-casting pointers either by the specs, but people want to be able to do this nonetheless.

Thus, they both accept pointer aliasing through union types, trying to optimize as much as possible while avoiding to break people's expectations and existing code. This is a very unfortunate situation for both compiler developers and users; just search for something like "gcc strict aliasing" on StackOverflow for examples.

There is already quite a lot of D code out there that violates the C-style strict aliasing rules.

David
July 27, 2013
On 7/27/2013 1:08 AM, monarch_dodra wrote:
> 1. Does strict aliasing apply to slices?

I don't know what you mean.

> 2. C++ uses 'char' as a 'neutral' type that can alias to anything. What about D?
> Does char fill that role? Does ubyte?

I'll go with deadalnix's answer.
« First   ‹ Prev
1 2