Repository navigation
Constant evaluation of GNU vector objects [GH #224] #448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
daveedvdv-nvidia
wants to merge
1
commit into
main
Choose a base branch
from
fix/224
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+205
−17
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
22 changes: 22 additions & 0 deletions
22
src/pending_changes/gh-224-constexpr-vector-init-bitmap.txt
This file contains hidden or 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,22 @@ | ||
| [GH #224] | ||
| Constant evaluation of GNU vector objects | ||
|
|
||
| The constexpr interpreter tracks the initialization of each element of a GNU | ||
| vector, but did not reserve space for that bookkeeping in complete objects of | ||
| vector type. Evaluating such an object (e.g., zero-initializing a 64-byte | ||
| vector) could therefore corrupt memory and crash the front end. In addition, | ||
| initializing (by assignment or as part of aggregate initialization) or | ||
| destroying a whole vector subobject only updated the state of its first | ||
| element, so that reading another element could give a spurious "access to | ||
| uninitialized object" error (or fail to diagnose an access after | ||
| destruction). For example, with --g++ --c++20: | ||
|
|
||
| typedef char V64 __attribute__((vector_size(64))); | ||
| typedef int V4 __attribute__((vector_size(16))); | ||
| int main() { | ||
| V64 a = V64(); // Could previously crash. | ||
| return (int)a[0]; | ||
| } | ||
| struct T { V4 v; }; | ||
| constexpr int f() { T t; t.v = V4{1, 2, 3, 4}; return t.v[3]; } | ||
| static_assert(f() == 4, ""); // Previously a spurious error. |
This file contains hidden or 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,23 @@ | ||
| fe_only -DTEST_NUMBER=1 --c++20 --g++ --gnu_version=150200 Test_name.c | ||
| "Test_name.c", line 12: error: expression must have a constant value | ||
| static_assert(d1() == 4, ""); // Error. | ||
| ^ | ||
| "Test_name.c", line 11: note: access to uninitialized object | ||
| constexpr int d1() { vi4 a = {1, 2, 3, 4}; a.~vi4(); return a[3]; } | ||
| ^ | ||
|
|
||
| "Test_name.c", line 15: error: expression must have a constant value | ||
| static_assert(d2() == 0, ""); // Error. | ||
| ^ | ||
| "Test_name.c", line 14: note: access to uninitialized object | ||
| constexpr int d2() { vl8 a = vl8(); a.~vl8(); return (int)a[7]; } | ||
| ^ | ||
|
|
||
| "Test_name.c", line 19: error: expression must have a constant value | ||
| static_assert(d3() == 0, ""); // Error. | ||
| ^ | ||
| "Test_name.c", line 18: note: access to uninitialized object | ||
| constexpr int d3() { S s{}; s.v.~vl8(); return (int)s.v[7]; } | ||
| ^ | ||
|
|
||
| 3 errors detected in the compilation of "Test_name.c". |
This file contains hidden or 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,23 @@ | ||
| fe_only -DTEST_NUMBER=2 --c++20 --clang --clang_version=220100 Test_name.c | ||
| "Test_name.c", line 12: error: expression must have a constant value | ||
| static_assert(d1() == 4, ""); // Error. | ||
| ^ | ||
| "Test_name.c", line 11: note: access to uninitialized object | ||
| constexpr int d1() { vi4 a = {1, 2, 3, 4}; a.~vi4(); return a[3]; } | ||
| ^ | ||
|
|
||
| "Test_name.c", line 15: error: expression must have a constant value | ||
| static_assert(d2() == 0, ""); // Error. | ||
| ^ | ||
| "Test_name.c", line 14: note: access to uninitialized object | ||
| constexpr int d2() { vl8 a = vl8(); a.~vl8(); return (int)a[7]; } | ||
| ^ | ||
|
|
||
| "Test_name.c", line 19: error: expression must have a constant value | ||
| static_assert(d3() == 0, ""); // Error. | ||
| ^ | ||
| "Test_name.c", line 18: note: access to uninitialized object | ||
| constexpr int d3() { S s{}; s.v.~vl8(); return (int)s.v[7]; } | ||
| ^ | ||
|
|
||
| 3 errors detected in the compilation of "Test_name.c". |
This file contains hidden or 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 @@ | ||
| fe_only -DTEST_NUMBER=1 --c++20 --g++ --gnu_version=150200 Test_name.c |
This file contains hidden or 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 @@ | ||
| fe_only -DTEST_NUMBER=2 --c++20 --clang --clang_version=220100 Test_name.c |
This file contains hidden or 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,19 @@ | ||
| //type:fn | ||
| //options:--c++20 --g++ --gnu_version=150200:--c++20 --clang --clang_version=220100 | ||
| //remark:Reading an element of a destroyed GNU vector is not constant. | ||
|
|
||
| // Destroying a vector subobject used to mark only its first element as | ||
| // uninitialized, so reading a later element was accepted (d3). | ||
|
|
||
| typedef int vi4 __attribute__((vector_size(16))); | ||
| typedef long long vl8 __attribute__((vector_size(64))); | ||
|
|
||
| constexpr int d1() { vi4 a = {1, 2, 3, 4}; a.~vi4(); return a[3]; } | ||
| static_assert(d1() == 4, ""); // Error. | ||
|
|
||
| constexpr int d2() { vl8 a = vl8(); a.~vl8(); return (int)a[7]; } | ||
| static_assert(d2() == 0, ""); // Error. | ||
|
|
||
| struct S { vl8 v; }; | ||
| constexpr int d3() { S s{}; s.v.~vl8(); return (int)s.v[7]; } | ||
| static_assert(d3() == 0, ""); // Error. |
This file contains hidden or 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,81 @@ | ||
| //type:fp | ||
| //options:--c++20 --g++ --gnu_version=150200:--c++20 --clang --clang_version=220100 | ||
| //remark:Constant evaluation of GNU vector objects and their elements. | ||
|
|
||
| // The interpreter did not reserve an initialization bitmap for complete | ||
| // objects of GNU vector type, so tracking the initialization of their elements | ||
| // wrote past the object's bookkeeping prefix (a 64-byte vector suffices). | ||
| // Assigning a whole vector subobject also marked only its first element as | ||
| // initialized. | ||
|
|
||
| typedef char vc64 __attribute__((vector_size(64))); | ||
| typedef int vi4 __attribute__((vector_size(16))); | ||
| typedef long long vl8 __attribute__((vector_size(64))); | ||
|
|
||
| int main() { | ||
| vc64 a = vc64(); | ||
| return (int)a[0]; | ||
| } | ||
|
|
||
| constexpr int f1() { vc64 a = vc64(); return a[63]; } | ||
| static_assert(f1() == 0, ""); | ||
|
|
||
| constexpr int f2() { vc64 a{}; return a[0] + a[63]; } | ||
| static_assert(f2() == 0, ""); | ||
|
|
||
| constexpr int f3() { vl8 a = vl8(); vl8 b = a; return (int)b[7]; } | ||
| static_assert(f3() == 0, ""); | ||
|
|
||
| constexpr int f4() { | ||
| vl8 a = vl8(); | ||
| a[7] = 3; | ||
| vl8 b; | ||
| b = a; | ||
| return (int)b[7]; | ||
| } | ||
| static_assert(f4() == 3, ""); | ||
|
|
||
| constexpr vc64 g5() { return vc64(); } | ||
| constexpr int f5() { return g5()[63]; } | ||
| static_assert(f5() == 0, ""); | ||
|
|
||
| constexpr vl8 v6 = vl8(); | ||
| static_assert(v6[7] == 0, ""); | ||
|
|
||
| struct S { vl8 v; }; | ||
| struct T { vi4 v; int i; }; | ||
|
|
||
| constexpr int f7() { S s{}; return (int)s.v[7]; } | ||
| static_assert(f7() == 0, ""); | ||
|
|
||
| constexpr int f8() { S s; s.v = vl8(); return (int)s.v[7]; } | ||
| static_assert(f8() == 0, ""); | ||
|
|
||
| constexpr int f9() { T t; t.v = vi4{1, 2, 3, 4}; t.i = 0; return t.v[3]; } | ||
| static_assert(f9() == 4, ""); | ||
|
|
||
| constexpr int f10() { vi4 a[2]; a[1] = vi4{1, 2, 3, 4}; return a[1][3]; } | ||
| static_assert(f10() == 4, ""); | ||
|
|
||
| // Aggregate initialization of a vector subobject. | ||
| constexpr int f11() { T t{{1, 2, 3, 4}, 5}; return t.v[3]; } | ||
| static_assert(f11() == 4, ""); | ||
|
|
||
| constexpr int f12() { T t{vi4{1, 2, 3, 4}, 5}; return t.v[3]; } | ||
| static_assert(f12() == 4, ""); | ||
|
|
||
| constexpr int f13() { | ||
| constexpr vi4 w{1, 2, 3, 4}; | ||
| T t{w, 5}; | ||
| return t.v[3]; | ||
| } | ||
| static_assert(f13() == 4, ""); | ||
|
|
||
| constexpr int f14() { T t{{1, 2}, 5}; return t.v[1] + t.v[3]; } | ||
| static_assert(f14() == 2, ""); | ||
|
|
||
| constexpr T t15{{1, 2, 3, 4}, 5}; | ||
| static_assert(t15.v[3] == 4, ""); | ||
|
|
||
| constexpr int f16() { vi4 a[2] = {{1, 2, 3, 4}, {5, 6, 7, 8}}; return a[1][3]; } | ||
| static_assert(f16() == 8, ""); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: edgcpp/compiler
Length of output: 38518
Script executed:
Repository: edgcpp/compiler
Length of output: 34160
Script executed:
Repository: edgcpp/compiler
Length of output: 42864
Check every source element before marking a vector initialized.
do_glvalue_to_prvaluechecks only the initialization bit at the vector start. Theeok_assignpath then copies the complete vector withmemcpyand marks the complete destination vector initialized. If onlys.v[0]is initialized,s.v = s.vcan mark later indeterminate elements initialized and allow a later read of one of those elements.Validate every vector source element before the copy and only mark the destination vector initialized after that validation succeeds.