Bug 110303 - With -O0, _mm_shuffle_epi32 with a constexpr function argument does not compile
Summary: With -O0, _mm_shuffle_epi32 with a constexpr function argument does not compile
Status: RESOLVED DUPLICATE of bug 80353
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 13.2.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-06-19 00:03 UTC by Denis Yaroshevskiy
Modified: 2023-06-19 12:51 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Denis Yaroshevskiy 2023-06-19 00:03:45 UTC
Minimum example:

The following code

```
#include "immintrin.h"

constexpr int f() { return 0; }
__m128i shuffle(__m128i x) { return _mm_shuffle_epi32(x, f()); }
```

Does not compile in debug

Godbolt: https://godbolt.org/z/x7bfhr4vY
Comment 1 Andrew Pinski 2023-06-19 00:17:59 UTC
I think this is correct behavior really.
Comment 2 Andrew Pinski 2023-06-19 00:19:32 UTC
so _mm_shuffle_epi32 requires a constant but since it is an argument, the argument, it is not a constant expression requirement.
Comment 3 Andrew Pinski 2023-06-19 00:25:01 UTC
Dup of bug 80353. constexpr function does not have to be a full constexpression in many cases.

*** This bug has been marked as a duplicate of bug 80353 ***
Comment 4 Denis Yaroshevskiy 2023-06-19 07:40:12 UTC
> so _mm_shuffle_epi32 requires a constant but since it is an argument, the argument, it is not a constant expression requirement.

The function is marked constexpr. So it can be a constant if you ask it.
Comment 5 Xi Ruoyao 2023-06-19 12:30:04 UTC
(In reply to Denis Yaroshevskiy from comment #4)
> > so _mm_shuffle_epi32 requires a constant but since it is an argument, the argument, it is not a constant expression requirement.
> 
> The function is marked constexpr. So it can be a constant if you ask it.

The problem is -O0 means "don't ask it".  Constant propagation is an optimization, but -O0 means don't do optimization.
Comment 6 m.cencora 2023-06-19 12:51:58 UTC
(In reply to Denis Yaroshevskiy from comment #4)
> > so _mm_shuffle_epi32 requires a constant but since it is an argument, the argument, it is not a constant expression requirement.
> 
> The function is marked constexpr. So it can be a constant if you ask it.

constexpr at function declaration means that it COULD be evaluated in compile time, but doesn't force it.

To force it either invoke it in a context that requires a compile-time evaluation (e.g. static_assert, or initializer of constexpr variable), or mark it as consteval instead.