llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

wrong results with union and strict-aliasing

Open brunodefraine opened this issue 12 years ago • 28 comments

Bugzilla Link 21725
Version 3.4
OS Linux
CC @aelovikov-intel,@compnerd,@hfinkel,@kosarev,@dobbelaj-snps,@sunfishcode,@RalfJung,@regehr,@zygoloid,@sanjoy,@seanm

Extended Description

The following testcase produces wrong results with '-O1' and higher (tested with llvm-3.4 and llvm-3.5).

#include <stdio.h>

union U {
	short s;
	int i;
} u;

int v = 987;
union U* up = &u;

int main()
{
	for (int i = 0; i < 2; ++i) {
		u.s = v;         // 987
		up->i = 123;     // 123
	}
	printf("%d\n", up->i);   // 123     clang: 123
	printf("%d\n", u.i);     // 123     clang: 987 ******
	printf("%d\n", up->i);   // 123     clang: 987 ******
	printf("%d\n", u.i);     // 123     clang: 987 ******
	return 0;
}

Disabling 'strict-aliasing' makes the problem go away. We do believe that the behavior should be the same with and without strict-aliasing.

It seems that initially, the write to 'u.s' is moved out of the loop. (more exactly, after the loop) Later on the loop is removed, with only the write to 'up->i' remaining. As the store to 'u.s' is still following, the end result is not what we expect to see.

brunodefraine avatar Dec 03 '14 15:12 brunodefraine

mentioned in issue llvm/llvm-bugzilla-archive#31928

DimitryAndric avatar Nov 26 '21 22:11 DimitryAndric

So gcc assumes that even differently-typed accesses may overlap, but it seems to have little impact on optimizations as in most cases when accesses are not meant to overlap, it can prove it. It also means gcc is able to compile a substantially larger amount of existing code in strict aliasing mode. Does this sound correct?

Yes, I think so but I'm not familiar with gcc implementation, I've only tested it from outside.

If so, then what gcc does is very clever and supporting this model looks like a huge potential improvement to LLVM. And from this perspective all the mentioned cases should, indeed, be considered bugs.

I think it could also simplify treatment of unions and placement news. unions are mostly just a more limited case of the same, plus type-punning. The situation with placement news is probably even more interesting. I don't know what plans clang community has in regard to them but it's possible that optimization around placement news will require the same kind of analysis as described above. (I've posted an example with placement news to bug 26603 which I believe is valid according to stricter C++ rules and which illustrates that placement new should serve as a quite strong optimization barrier if the discussed analysis is not employed.)

llvmbot avatar Oct 31 '17 20:10 llvmbot

So gcc assumes that even differently-typed accesses may overlap, but it seems to have little impact on optimizations as in most cases when accesses are not meant to overlap, it can prove it. It also means gcc is able to compile a substantially larger amount of existing code in strict aliasing mode. Does this sound correct?

If so, then what gcc does is very clever and supporting this model looks like a huge potential improvement to LLVM. And from this perspective all the mentioned cases should, indeed, be considered bugs.

Thanks a lot for the pointers.

kosarev avatar Oct 30 '17 09:10 kosarev

What I see is that gcc requires differently-types access to not overlap in strict aliasing mode.

$ cat x.c void test(short *ps, int *pi) { *pi = 5; *ps = 7; *pi = *pi + 1; }

$ gcc -O1 -fstrict-aliasing -S x.c $ cat x.s ... test: .LFB0: .cfi_startproc movw $7, (%rdi) movl $6, (%rsi) ret

Yeah, this is a good example of TBAA in action even in those, "harder" conditions -- the read of int via *pi in the last line immediately follows the read of short via *ps which is only allowed if they overlap and gcc Oh, as it's written it's total garbage. Other parts seem fine but in this line I've managed to mix words two times. Please read the last line as following:

the write of short via *ps which is only allowed if they don't overlap and gcc

rightly optimizes based on that. An example of case when accesses are allowed to overlap and this interacts badly with optimization you can find in the first gcc bug referenced above and it looks less natural. Hopefully this means that such situations are quite rare and supporting them will not hamper optimizations too much.

To reiterate, writes and reads are not equal, only writes may change effective type, reads have to have a matching type.

llvmbot avatar Oct 27 '17 08:10 llvmbot

A bit more simplified:

#include <stdlib.h>
#include <stdio.h>

static void test(short *ps, int *pi)
{
  for (int i = 0; i < 1; ++i) {
    *ps = 0; // moved out of loop by "Loop Invariant Code Motion"
    pi[i] = 1;
  }
}

int main()
{
  int *p = malloc(10);

  test((void *)p, p);

  printf("%d\n", *p);
}

Results:

$ clang -std=c11 -Weverything test.c && ./a.out
1
$ clang -std=c11 -Weverything -O3 test.c && ./a.out
0

clang version: clang version 6.0.0 (https://llvm.org/git/clang.git 5f17d5ca46b48156b96f81e7c32d381a0ca65b41) (https://llvm.org/git/llvm.git 4ff96e5fbb1d79afb90f25698c4bf1696e2d63b9)

The same bug in gcc -- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81028 and https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57359 .

llvmbot avatar Oct 27 '17 00:10 llvmbot

Implementing TBAA in such conditions is harder but it's possible as evidenced by gcc.

Can you please elaborate on this?

For example, I've reported a couple of bugs in gcc in this area:

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

and they were promptly fixed. BTW the former one affects clang too -- bug 26603.

Another one is a bug I've reported just a couple of days ago (not found by me; I've just reported it):

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

I'm not idealizing gcc though, there are some old bugs that are not yet fixed (possibly for unrelated reasons).

What I see is that gcc requires differently-types access to not overlap in strict aliasing mode.

$ cat x.c void test(short *ps, int *pi) { *pi = 5; *ps = 7; *pi = *pi + 1; }

$ gcc -O1 -fstrict-aliasing -S x.c $ cat x.s ... test: .LFB0: .cfi_startproc movw $7, (%rdi) movl $6, (%rsi) ret

Yeah, this is a good example of TBAA in action even in those, "harder" conditions -- the read of int via *pi in the last line immediately follows the read of short via *ps which is only allowed if they overlap and gcc rightly optimizes based on that. An example of case when accesses are allowed to overlap and this interacts badly with optimization you can find in the first gcc bug referenced above and it looks less natural. Hopefully this means that such situations are quite rare and supporting them will not hamper optimizations too much.

To reiterate, writes and reads are not equal, only writes may change effective type, reads have to have a matching type.

llvmbot avatar Oct 26 '17 18:10 llvmbot

The problem is it's allowed by the C standard.

Yes, I'm aware of that. In real life, however, there are cases when the compiler cannot deduce effective types and active union members, meaning we are either forced to consider differently-typed accesses to be potentially overlapping (relaxed aliasing) or forced to require them to be non-overlapping (strict aliasing). The latter comes with the exception for unions that we can support provided all accesses to union members are written in a way that makes it possible for the compiler to recognize them as such.

Implementing TBAA in such conditions is harder but it's possible as evidenced by gcc.

Can you please elaborate on this?

What I see is that gcc requires differently-types access to not overlap in strict aliasing mode.

$ cat x.c void test(short *ps, int *pi) { *pi = 5; *ps = 7; *pi = *pi + 1; }

$ gcc -O1 -fstrict-aliasing -S x.c $ cat x.s ... test: .LFB0: .cfi_startproc movw $7, (%rdi) movl $6, (%rsi) ret

kosarev avatar Oct 26 '17 16:10 kosarev

Unions are a bit more tricky -- a read could be done with a "wrong" type if it's done explicitly through the union (type punning). (That's why I prefer examples with allocated memory instead of unions.)

Yes, accesses to direct and indirect union members may overlap regardless of the final access types. That's why in strict aliasing mode all such accesses are required to be performed through an explicit lvalue of the most enclosing union type. This is how the compiler knows these accesses may overlap with same- or differently-typed accesses to direct or indirect members of that union type.

We are in agreement about reads of inactive members of unions but writes differ. AIUI C++ requires writes to inactive members of unions to go through unions (this undoubtly has some benefits, e.g. it allows to disciminate between several members of the same type). OTOH C doesn't have such restriction or at least it's so terse/moot about this issue that you can read it any way you. But it's not that important given the freedom allowed with allocated memory.

In your example, there are no clues the compiler could use to deduce that the short and int pointers the test() function takes may point to overlapping objects. With TBAA enabled, dereferences of these pointers are considered to be non-overlapping accesses as they have different types. If you want the compiler to consider them as potentially overlapping, you have to disable TBAA.

The problem is it's allowed by the C standard. C11, 6.5p6, reads: "If a value is stored into an object having no declared type through an lvalue having a type that is not a character type, then the type of the lvalue becomes the effective type of the object for that access and for subsequent accesses that do not modify the stored value."

I'm not aware of serious attempts to abandon the possibility to reuse allocated memory in C. But maybe I've missed them?

Implementing TBAA in such conditions is harder but it's possible as evidenced by gcc.

llvmbot avatar Oct 26 '17 16:10 llvmbot

Unions are a bit more tricky -- a read could be done with a "wrong" type if it's done explicitly through the union (type punning). (That's why I prefer examples with allocated memory instead of unions.)

Yes, accesses to direct and indirect union members may overlap regardless of the final access types. That's why in strict aliasing mode all such accesses are required to be performed through an explicit lvalue of the most enclosing union type. This is how the compiler knows these accesses may overlap with same- or differently-typed accesses to direct or indirect members of that union type.

In your example, there are no clues the compiler could use to deduce that the short and int pointers the test() function takes may point to overlapping objects. With TBAA enabled, dereferences of these pointers are considered to be non-overlapping accesses as they have different types. If you want the compiler to consider them as potentially overlapping, you have to disable TBAA.

kosarev avatar Oct 25 '17 08:10 kosarev

I mean that if you see two consecutive reads of different types than you can deduce that they don't overlap and you can reorder all accesses (writes and reads) done through these lvalues. The same if you see consecutive write and read of different types.

Unions are a bit more tricky -- a read could be done with a "wrong" type if it's done explicitly through the union (type punning). (That's why I prefer examples with allocated memory instead of unions.)

llvmbot avatar Oct 24 '17 21:10 llvmbot

Well, if there are only non-volatile read accesses, then I guess we can reorder them regardless of whether they may overlap. We don't need TBAA to respond in a specific way to do this.

In presence of write accesses, however, overlapping accesses do matter. Since the purpose of TBAA is to tell us whether a couple of accesses are allowed to overlap by the rules of the input language, permitting accesses of different types to overlap, like in your test case, effectively means disabling TBAA.

kosarev avatar Oct 23 '17 13:10 kosarev

As your modified example, it seems there is no practical way for TBAA to support such cases. If we permit accesses of different types to alias, then there is no work to do for TBAA.

It depends on type of accesses. Based on types information only, you cannot reorder writes as they can change effective type of memory but you can reorder reads etc.

llvmbot avatar Oct 23 '17 09:10 llvmbot

It's not really fixed, just "pessimized" accesses to union members. See

https://reviews.llvm.org/D33328

for more details. I would suggest that we do not close any bug reports of this kind until we have a proper solution.

As your modified example, it seems there is no practical way for TBAA to support such cases. If we permit accesses of different types to alias, then there is no work to do for TBAA.

kosarev avatar Oct 23 '17 07:10 kosarev

The original testcase seems to be fixed now but replacing the union by allocated memory makes the problem come back.

Source code:

#include <stdlib.h>
#include <stdio.h>

int v = 987;

void test(short *ps, int *pi, int *pi2)
{
  for (int i = 0; i < 2; ++i) {
    *ps = v;
    *pi = 123;
  }
  printf("%d\n", *pi);
  printf("%d\n", *pi2);
  printf("%d\n", *pi);
  printf("%d\n", *pi2);
}

int main()
{
  void *p = malloc(10);

  test(p, p, p);
}

Results:

$ clang -std=c11 -Wall -O3 test.c && ./a.out
123
987
987
987

clang version: clang version 6.0.0 (https://llvm.org/git/clang.git f027325999d75572cbdb4dda2e475bd27bcc74da) (https://llvm.org/git/llvm.git f7bb38ca4e1c9eb290f7c4c9dbfcb9593f1b140f)

llvmbot avatar Oct 22 '17 20:10 llvmbot

Sounds good. Jeroen, can you make a patch with a test case (or just add it to test/CodeGen/tbaa-struct.cpp) and send it to the cfe-commits list for review?

This was posted to https://reviews.llvm.org/D8056 but then abandoned because of other bugs uncovered. Please see this thread for more information: http://lists.llvm.org/pipermail/cfe-dev/2015-March/042015.html

hfinkel avatar Dec 03 '16 03:12 hfinkel

Sounds good. Jeroen, can you make a patch with a test case (or just add it to test/CodeGen/tbaa-struct.cpp) and send it to the cfe-commits list for review?

This was posted to https://reviews.llvm.org/D8056 but then abandoned because of other bugs uncovered. Please see this thread for more information: http://lists.llvm.org/pipermail/cfe-dev/2015-March/042015.html

hfinkel avatar Dec 03 '16 03:12 hfinkel

Sounds good. Jeroen, can you make a patch with a test case (or just add it to test/CodeGen/tbaa-struct.cpp) and send it to the cfe-commits list for review?

hfinkel avatar Mar 04 '15 08:03 hfinkel

So the after metadata looks correct after, so i'm on board with this.

I think it's the right path.

I don't know if there are other bugs lurking that caused this disablement in the first place though.

(I also think Chris is correct in the bug that was duped to this one. There is nothing you can do in the general case, because you can always play games with where the union occurs. But this will at least get us "as right as other compilers generally are" :P)

llvmbot avatar Mar 03 '15 05:03 llvmbot

Output for 'clang -g -O1 --emit-llvm'. With the change The main difference is that we now (with the patch) have a member access (to !39):

!37 = !{!"int", !32, i64 0}
!39 = !{#40, !41, i64 0}
!40 = !{!"U", !41, i64 0, !37, i64 0}
!41 = !{!"short", !32, i64 0}


in stead of basic type access (to !41) that we had before (without the patch):
!41 = !{#42, !42, i64 0}
!42 = !{!"short", !32, i64 0}

dobbelaj-snps avatar Mar 02 '15 12:03 dobbelaj-snps