<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/55151>55151</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[aarch64] clang miscompilation of struct initializer in inline function (not properly zeroed)
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
rsms
</td>
</tr>
</table>
<pre>
Initializing a struct with certain union fields inside an inlined function does not properly zero out non-named fields in the initializer.
Minimal repro case attached:
[clang-init-bug.c.zip](https://github.com/llvm/llvm-project/files/8576446/clang-init-bug.c.zip)
Here's a summary demonstration:
```c
typedef struct Attr {
union { u8 a; u8 b[3]; }; u8 c[3];
} Attr;
typedef struct Foo {
Attr a;
} Foo;
inline static Foo make_foo(Attr a) { return (Foo){a}; }
int fail() {
Foo foo = make_foo( ((Attr){ .a = 7 }) );
return foo.a.b[2] != 0; // bug: foo.a.b[2] is not zero initialized
}
int pass() {
Foo foo = (Foo){ ((Attr){ .a = 7 }) };
return foo.a.b[2] != 0;
}
```
Compiling this with `-std=c11`, the `fail` function which uses the `make_foo` helper will not fully initialize foo, while the `pass` function which "manually inlines" the same code from `make_foo` correctly and fully initializes foo.
Note that compiling with optimizations yields correct code. Looking at the IR produced _with_ optimizations the `pass` and `fail` functions ends up doing the exact same thing, but not when disabling (`-O0`) optimizations.
Versions:
- LLVM: 14.0.0 (329fda39c507e8740978d10458451dcdb21563be)
- Target: arm64-apple-darwin21.3.0
- Host: `uname -srm => Darwin 21.3.0 arm64` macOS 12.2.1 (21D62)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyVVduO2zYQ_Rr5hbAgUfcHP2TXDRJg0wBtkdfFmKQsNhQpkFS33q_vkJIv6w2CBjAskZo5c-ZwZngw_LT7rKWXoOSr1EcCxHk7M09epB8IE9aD1GTW0mjSS6G4I1I7yQUBjW9KasFJP2vmgwU3whFtP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