Bug 101651 - vector extension and element access vs C++17 constexpr functions
Summary: vector extension and element access vs C++17 constexpr functions
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2021-07-27 20:46 UTC by Marc Glisse
Modified: 2024-11-07 18:26 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-07-27 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Marc Glisse 2021-07-27 20:46:17 UTC
(adapted from https://stackoverflow.com/q/68517921/1918193)

#ifdef WORK
 #include <array>
 typedef std::array<char,16> vec;
#else
 typedef char vec __attribute__((vector_size(16)));
#endif
constexpr auto gen () {
    vec ret{};
    for (int i = 0; i < sizeof(vec); ++i) {
        ret[i] = 2;
    }
    return ret;
};
constexpr auto m = gen();


c.cc:9:23:   in 'constexpr' expansion of 'gen()'
c.cc:9:24: error: modification of '(char [16])ret' is not a constant expression
    9 | constexpr auto m = gen();
      |                        ^

However, with -DWORK to use std::array instead of the vector extension, it compiles just fine, so there shouldn't be any strong obstacle to implement this.
Comment 1 Drea Pinski 2021-07-27 20:52:12 UTC
Confirmed.
I really doubt any of the vector extensions are really supported with constexpr.
Comment 2 Drea Pinski 2021-07-27 20:59:10 UTC
So it is any vector array that matters:
#ifdef WORK
 #include <array>
 typedef std::array<char,2> vec;
#else
 typedef char vec __attribute__((vector_size(2)));
#endif
constexpr auto gen () {
    vec ret{1,2};
    vec ret1{1,2};
    for (int i = 0; i < sizeof(vec); ++i) {
        ret[i] = ret1[0];
    }
 //   ret1[0] =3;
 //   ret1[0] =ret[0];
    return ret1;
};
 auto m = gen();

Note clang rejects the above for non -DWORK case.
Comment 3 Drea Pinski 2021-07-27 21:09:05 UTC
(In reply to Andrew Pinski from comment #2)
Here is clang's error message which is 10000% wrong.

<source>:7:16: error: constexpr function never produces a constant expression [-Winvalid-constexpr]
constexpr auto gen () {
               ^
<source>:11:18: note: subexpression not valid in a constant expression
        ret[i] = ret1[0];
                 ^
1 error generated.
Compiler returned: 1