-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ClangIR][CIRGen] Handle nested union in arrays of struct (#1007)
Reproducer: ``` struct nested { union { const char *single; const char *const *multi; } output; }; static const char * const test[] = { "test", }; const struct nested data[] = { { { .multi = test, }, }, { { .single = "hello", }, }, }; ``` ClangIR now failed to recognize `data` as an array since it failed to recognize the initializer for union. This comes from a fundamental difference between CIR and LLVM IR. In LLVM IR, the union is simply a struct with the largest member. So it is fine to have only one init element. But in CIR, the union has the information for all members. So if we only pass a single init element, we may be in trouble. We solve the problem by appending placeholder attribute for the uninitialized fields.
- Loading branch information
1 parent
d2df0f6
commit 6f15a2e
Showing
6 changed files
with
94 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir | ||
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR | ||
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll | ||
// RUN: FileCheck --input-file=%t.ll %s --check-prefix=LLVM | ||
|
||
struct nested | ||
{ | ||
union { | ||
const char *single; | ||
const char *const *multi; | ||
} output; | ||
}; | ||
static const char * const test[] = { | ||
"test", | ||
}; | ||
const struct nested data[] = | ||
{ | ||
{ | ||
{ | ||
.multi = test, | ||
}, | ||
}, | ||
{ | ||
{ | ||
.single = "hello", | ||
}, | ||
}, | ||
}; | ||
|
||
// CIR: ![[ANON_TY:.+]] = !cir.struct<union "anon.0" {!cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>> | ||
// CIR: ![[NESTED_TY:.+]] = !cir.struct<struct "nested" {![[ANON_TY]] | ||
// CIR: cir.global constant external @data = #cir.const_array<[#cir.const_struct<{#cir.const_struct<{#cir.inactive_field : !cir.ptr<!s8i>, #cir.global_view<@test> : !cir.ptr<!cir.ptr<!s8i>>}> : ![[ANON_TY]]}> : ![[NESTED_TY:.+]], #cir.const_struct<{#cir.const_struct<{#cir.global_view<@".str"> : !cir.ptr<!s8i>, #cir.inactive_field : !cir.ptr<!cir.ptr<!s8i>>}> : ![[ANON_TY]]}> : ![[NESTED_TY:.+]]]> : !cir.array<![[NESTED_TY:.+]] x 2> | ||
// LLVM: @data = constant [2 x {{.*}}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters