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

[DSE] Stores to union are incorrectly merged

Open dtcxzyw opened this issue 2 years ago • 12 comments

Reproducer: https://godbolt.org/z/KGTExzTKc

#include <stdio.h>
#include <stdint.h>
union d {
  int16_t e;
  uint64_t f;
  int32_t g;
} i;
int32_t *k = &i.g;
void o() {
  int16_t *r = &i.e;
  i.f = 0;
  *k = 0;
  *r = 2;
}
int main() {
  o();
  printf("%d\n", i.e);
}

i.f = 0 and *r = 2 are merged into a store i64 2 to ptr @i before *k = 0.

dtcxzyw avatar Feb 24 '24 18:02 dtcxzyw

Another reproducer: https://godbolt.org/z/zrKWEG7sc

dtcxzyw avatar Feb 25 '24 13:02 dtcxzyw

If we apply -fno-strict-aliasing flag, we get the expected result. I'm not sure whether this is a TBAA problem or an UB now, just for you information.

XChy avatar Feb 26 '24 13:02 XChy

cc @fhahn

dtcxzyw avatar Feb 29 '24 07:02 dtcxzyw

Why is this a miscompile? You are only allowed to type pun through unions if you directly read/write union members, not if you use a pointer to them. If you do that, you violate strict aliasing.

nikic avatar Mar 01 '24 12:03 nikic

Why is this a miscompile? You are only allowed to type pun through unions if you directly read/write union members, not if you use a pointer to them. If you do that, you violate strict aliasing.

It is not a type punning because printf("%d\n", i.e); accesses the same as the member last used to store a value.

dtcxzyw avatar Mar 01 '24 12:03 dtcxzyw

BTW gcc doesn't give any warning with -Wstrict-aliasing=3.

dtcxzyw avatar Mar 01 '24 12:03 dtcxzyw

cc @shafik

dtcxzyw avatar Mar 01 '24 12:03 dtcxzyw

Why is this a miscompile? You are only allowed to type pun through unions if you directly read/write union members, not if you use a pointer to them. If you do that, you violate strict aliasing.

It is not a type punning because printf("%d\n", i.e); accesses the same as the member last used to store a value.

Thanks, I see what you mean now.

nikic avatar Mar 01 '24 13:03 nikic

k and r are not allowed to alias, one is int32_t and the other int16_t and so reordering the stores is allowed.

shafik avatar Mar 02 '24 01:03 shafik

k and r are not allowed to alias, one is int32_t and the other int16_t and so reordering the stores is allowed.

IIUC this is true in C++ but not so that true in C. The rules of union in C defeat TBAA in some cases.

frederick-vs-ja avatar Mar 02 '24 04:03 frederick-vs-ja

k and r are not allowed to alias, one is int32_t and the other int16_t and so reordering the stores is allowed.

IIUC this is true in C++ but not so that true in C. The rules of union in C defeat TBAA in some cases.

Yes, but neither k nor r are a aggregate or union type so

an aggregate or union type that includes one of the aforementioned types among its members (including, recursively, a member of a subaggregate or contained union), or

does not apply here.

shafik avatar Mar 04 '24 22:03 shafik

Still reproduces on current releases: clang 22.1.8, 23.1.4, and Apple clang 21.0.0 all print 0 instead of 2 at -O2/-O3 (arm64-apple-macosx, and x86_64 under Rosetta), while -O0/-O1 print the correct 2. This also explains the -fno-strict-aliasing observation upthread — that flag removes exactly the enabler identified below.

Pass attribution via opt -print-changed on the -O0-emitted IR: the merge is introduced by DSEPass, and it reduces to a four-instruction testcase where DSE alone performs the bad merge:

define void @o() {
  store i64 0, ptr @i, align 8, !tbaa !0   ; omnipotent char (direct union member access i.f = 0)
  %1 = load ptr, ptr @k, align 8, !tbaa !8
  store i32 0, ptr %1, align 4, !tbaa !3   ; scalar "int" tag (access through int32_t *)
  store i16 2, ptr @i, align 2, !tbaa !6   ; scalar "short" tag (access through int16_t *)
  ret void
}

opt -passes='dse' rewrites this to store i64 2, ptr @i placed before the intervening store i32 0, so the low half ends up 0. Two flips isolate the enabler: with the TBAA tags deleted, DSE leaves the module untouched (and end-to-end -O2 returns 2); giving the intervening store the same scalar tag as the merged-in store also fixes it — TBAA can only report NoAlias between two distinct scalar types. So the partial-overlapping-store merge is consulting TBAA when checking intervening clobbers, and the distinct int/short tags — both rooted at omnipotent char — let the merge proceed despite the address overlap.

Quantified error surface (intervening width W2 × merged-in width W1, both at offset 0; sequential semantics always requires low 16 bits == 2; clang 23.1.4 at -O2) — 9 of 16 combinations miscompile, exactly W1 != W2 && W1 < 8:

W2\W1   1    2    4    8
  1      ok   BAD  BAD  ok
  2      BAD  ok   BAD  ok
  4      BAD  BAD  ok   ok
  8      BAD  BAD  BAD  ok

Same-width pairs are safe (no provable NoAlias); full-width final stores are safe (taken by the kill path, not the merge path).

On the validity question debated upthread: whether this is a miscompile hinges exactly on whether the pointer-based accesses may pun through the union — under the C reading argued above (C11 6.5.2.3p3 + fn 95, frederick-vs-ja's point) they may, and then the TBAA NoAlias assertion does not hold. One clang-side datum that either way seems worth recording: a direct union member access (i.f = 0) is tagged omnipotent char, while dereferencing a pointer whose static type is the member type (*k, *r) gets the member's scalar tag — the latter is what TBAA uses to prove NoAlias here.

If this stands as a bug, two candidate directions: (a) in the DSE partial-merge clobber check, do not let TBAA refinement overrule the address overlap the merge itself already knows about; or (b) extend the union-aware TBAA path (currently applied to direct member accesses) to pointer accesses derived from union member lvalues.

jjdiong0-ui avatar Dec 01 '26 04:12 jjdiong0-ui