On Fri, Dec 13, 2024 at 01:15:36PM +0100, Tobias Burnus wrote: > OpenMP states for C++: > > "Directives may not appear in constexpr functions or in constant expressions." > > There is some support for this already in GCC, but not for [[omp::decl]]-type > of directives and it also doesn't work that well. For the example, for the > newly added testcase, the result with the patch is simple and clear: > > error: OpenMP directives may not appear in ‘constexpr’ functions > > without the patch: > > error: uninitialized variable ‘i’ in ‘constexpr’ function > error: uninitialized variable ‘i’ in ‘constexpr’ function > sorry, unimplemented: ‘#pragma omp allocate’ not yet supported > sorry, unimplemented: ‘#pragma omp allocate’ not yet supported > error: ‘constexpr int f()’ called in a constant expression > error: ‘constexpr int g()’ called in a constant expression > > Note: I think OpenACC has a similar issue but as the specification > is silent about it, the patch only handles OpenMP. > > * * * > > I have not touched the 'case OMP_...:' in constexpr.cc, added in > previous patches; in principle, those should be now unreachable > and could be removed. > I also have not included any OpenACC pragmas, even though they have > the same issue. (However, contrary to OpenMP, the OpenACC spec is > silent about constexpr.) > > * * * > > Comments, suggestions, concerns?
LGTM, though I think the restriction is against the direction of C++ development over the recent years. Pretty much no constructs are disallowed in constexpr functions, just with some exceptions what is still disallowed when encountering it. So, I think the above restriction should be replaced for OpenMP 6.1 or whatever version is planned next. Generally, we want to allow something like constexpr type foo (...) { if consteval { ... } else { #pragma omp ... ... } } or even just if (some_cond) { #pragma omp ... } in there, as long as some_cond doesn't evaluate to true at compile time. So, similar in how more recent C++ versions handle say asm statements in constexpr functions (or before C++26 e.g. throwing exceptions). If you encounter it while evaluating it, it is not constant expression, if you don't, it is not a bug. And, I don't see why declarative directives should be a problem in constexpr functions. Jakub