[llvm-bugs] [Bug 24698] New: Unnamed struct inside union doesn't initialize entire aggregate
via llvm-bugs
llvm-bugs at lists.llvm.org
Thu Sep 3 11:03:46 PDT 2015
https://llvm.org/bugs/show_bug.cgi?id=24698
Bug ID: 24698
Summary: Unnamed struct inside union doesn't initialize entire
aggregate
Product: clang
Version: trunk
Hardware: PC
OS: Linux
Status: NEW
Severity: normal
Priority: P
Component: -New Bugs
Assignee: unassignedclangbugs at nondot.org
Reporter: bblanco at gmail.com
CC: llvm-bugs at lists.llvm.org
Classification: Unclassified
I have a piece of code which initializes a struct, nested in a union. However,
the remainder part of the union is left uninitialized. My understanding of the
spec may be wrong, but I would have expected the entire outer union to be
initialized as well.
$ cat init.c
union U {
struct {
int a;
int b;
int c;
};
struct {
int s1;
int s2;
int s3;
int s4;
};
};
void foo(union U *);
void bar(int a, int b)
{
union U u = {
.a = a,
.b = b
};
foo(&u);
}
$ clang -O2 -S init.c -emit-llvm
$ cat init.ll
; ModuleID = 'init.c'
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
%union.U = type { %struct.anon.0 }
%struct.anon.0 = type { i32, i32, i32, i32 }
; Function Attrs: nounwind uwtable
define void @bar(i32 %a, i32 %b) #0 {
%u = alloca %union.U, align 4
%1 = bitcast %union.U* %u to i8*
call void @llvm.lifetime.start(i64 16, i8* %1) #1
%2 = getelementptr inbounds %union.U, %union.U* %u, i64 0, i32 0, i32 0
store i32 %a, i32* %2, align 4, !tbaa !1
%3 = getelementptr inbounds %union.U, %union.U* %u, i64 0, i32 0, i32 1
store i32 %b, i32* %3, align 4, !tbaa !6
%4 = getelementptr inbounds %union.U, %union.U* %u, i64 0, i32 0, i32 2
store i32 0, i32* %4, align 4, !tbaa !7
call void @foo(%union.U* nonnull %u) #1
call void @llvm.lifetime.end(i64 16, i8* %1) #1
ret void
}
; Function Attrs: nounwind
declare void @llvm.lifetime.start(i64, i8* nocapture) #1
declare void @foo(%union.U*) #2
...
As the IR shows, only the 3rd struct field is added to the initialized list,
rather than all 4.
Anecdotally, this was tested on clang 3.3 and also trunk. The behavior is the
same in both places. In the case of gcc, the full size of the union is
initialized.
--
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20150903/669c552a/attachment-0001.html>
More information about the llvm-bugs
mailing list