Skip to content

Commit 0efcf62

Browse files
Momen-G-Ardaveedvdv-nvidia
authored andcommitted
Subscript a GNU vector xvalue in constant evaluation [GH #90]
With Core issue 616, a GNU vector member of a class prvalue (e.g., "X{...}.v") is an xvalue, but eok_vector_subscript took the address path only for lvalues, so it read the operand's address as the vector value: "X{{10, 20, 30, 40}}.v[3]" evaluated to 3 instead of 40. Xvalues now take the address path too. The element is then read through its address, so it must be marked initialized. When a class member of vector type was initialized (extract_value_from_constant), only the bit for its first element was set, so "X{a * a}.v[3]" (and "X x{a}; x.v[3]") gave "access to uninitialized object". Each element of such a member is now marked. Tests: regressions/gh-90-vector.
1 parent 78f95ec commit 0efcf62

4 files changed

Lines changed: 33 additions & 2 deletions

File tree

‎src/interpret.c‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7483,6 +7483,22 @@ END_DISABLE_GCC_WARNING_STR_OVERFLOW
74837483
unmap_stack_bytes(ips, dst_addr.address);
74847484
} /* if */
74857485
mark_subobject_initialized(dst_addr.address, complete_object);
7486+
#if GNU_VECTOR_TYPES_ALLOWED
7487+
if (type_is(ftp, tk_vector)) {
7488+
/* A vector element can be accessed by itself (e.g., by
7489+
subscripting the member), so mark each element
7490+
initialized. */
7491+
a_type_ptr etp = skip_typerefs(
7492+
ftp->variant.vector.element_type);
7493+
a_targ_size_t k, n_elems = num_vector_elements(ftp);
7494+
a_byte_count elem_size = value_bytes_for_type(ips, etp,
7495+
&result);
7496+
for (k = 0; result && k<n_elems; k += 1) {
7497+
mark_subobject_initialized(dst_addr.address+k*elem_size,
7498+
complete_object);
7499+
} /* for */
7500+
} /* if */
7501+
#endif /* GNU_VECTOR_TYPES_ALLOWED */
74867502
elem_con = elem_con->next;
74877503
} /* if */
74887504
if (this_bytes != NULL) {
@@ -32215,7 +32231,7 @@ the value representation of the integer value.
3221532231
break;
3221632232
#if GNU_VECTOR_TYPES_ALLOWED
3221732233
case eok_vector_subscript:
32218-
/* The first operand is a vector (lvalue or rvalue), and the
32234+
/* The first operand is a vector (glvalue or prvalue), and the
3221932235
second operand is an integer. */
3222032236
{ a_host_large_integer len = (a_host_large_integer)
3222132237
num_vector_elements(opnd1_type);
@@ -32234,7 +32250,7 @@ the value representation of the integer value.
3223432250
etp = skip_typerefs(opnd1_type->variant.vector.element_type);
3223532251
esize = value_bytes_for_type(ips, etp, &result);
3223632252
if (!result) break;
32237-
if (opnd1->is_lvalue) {
32253+
if (opnd1->is_lvalue || opnd1->is_xvalue) {
3223832254
a_constexpr_address
3223932255
result_addr = *(a_constexpr_address*)opnd1_value;
3224032256
result_addr.address += host_int_val*esize;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=1 --c++20 --g++ --gnu_version=150200 Test_name.c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=2 --c++20 --clang --clang_version=190100 Test_name.c
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//type:fp
2+
//options_all:--c++20
3+
//options:--g++ --gnu_version=150200:--clang --clang_version=190100
4+
// With Core issue 616, a GNU vector member of a class prvalue is an xvalue.
5+
// Subscripting it in a constant expression must read the element.
6+
typedef int v4 __attribute__((vector_size(16)));
7+
struct X { v4 v; };
8+
constexpr int f() { return X{{10, 20, 30, 40}}.v[3]; }
9+
static_assert(f() == 40);
10+
constexpr X mk() { return X{{1, 2, 3, 4}}; }
11+
static_assert(mk().v[1] == 2);
12+
constexpr int g() { v4 a = {1, 2, 3, 4}; return X{a * a}.v[3]; }
13+
static_assert(g() == 16);

0 commit comments

Comments
 (0)