cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[SUGGESTION] Forwarding pragma directives

Open KaruroChori opened this issue 7 months ago • 7 comments

Pragma directives are crucial to access several compiler extensions.
In particular, OpenMP and OpenACC use them in C/C++ to map most of their features.
Is there a way to forward pragma directives inline as they appear in a *.cpp2 to downstream generations?

Sadly, C++ code cannot be nested inside Cpp2, so forwarding them is the only way to make use of compiler extensions and standard which are mapped on pragmas.

KaruroChori avatar May 23 '25 09:05 KaruroChori

Temporary fix I added to a branch to forward any preprocessor directive: https://github.com/KaruroChori/cppfront/tree/pragmas
In principle it could be slightly adjusted to filter in only pragmas.

KaruroChori avatar May 27 '25 16:05 KaruroChori

This could possibly be solved with user-defined metafunctions and allow metafunctions to be used in for loops and blocks.

related https://github.com/hsutter/cppfront/issues/1252

zaucy avatar May 28 '25 03:05 zaucy

This might not be sufficient. At times, pragmas for OpenMP and OpenACC are standalone or referred to variables. Also, recreating the whole omp specs just to rewrite pragma directive might be a lot more effort and hard to keep updated with new releases.

I think attributes are just as important, and can be good with metafunctions, but the pragma functionality cannot be easily replaced.

KaruroChori avatar May 28 '25 04:05 KaruroChori

OpenMP and OpenACC are standalone or referred to variables.

I'm not as familiar as you might be, but could you give an example of a standalone one?

For variables the user metafunctions could potentially be allowed on those.

Also, recreating the whole omp specs just to rewrite pragma directive might be a lot more effort and hard to keep updated with new releases.

Effort for sure, but it could have the added benefit of restricting what pragmas can go where which could be utilised by autocomplete and cpp2 related linting.

Part of the goal of cpp2 is to replace preprocessor usage with reflection and codegen. Currently metafunctions are the only way to do that. If metafunctions are inappropriate for replacing pragmas (I'm not convinced they are) then another feature should be suggested that's more in line with the goal of replacing the preprocessor.

zaucy avatar May 28 '25 17:05 zaucy

I'm not as familiar as you might be, but could you give an example of a standalone one?

#pragma omp requires unified_shared_memory for example.

Effort for sure, but it could have the added benefit of restricting what pragmas can go where which could be utilised by autocomplete and cpp2 related linting.

Sure, but most compilers use pragmas as the de-facto mechanisms to target extensions.
I cannot see how this project could (or should) realistically maintain all of them, even less so considering that some are proprietary.
I am not suggesting to generically enable the preprocessor, but to accept injection of pragmas, at least as a stopgap to promote interoperability. I think cpp2 should just be agnostic about their content.

That being said, I surely like the idea of a proper OpenMP/OpenACC implementation within the natural boundaries of cpp2, even if it falls outside the respective standards. At the same time, missing an important part of the interoperability with target compilers is not desirable in my opinion.

KaruroChori avatar May 28 '25 18:05 KaruroChori

#pragma omp requires unified_shared_memory for example.

Maybe metafunctions could be applied at file scope to support a pragma like this. Or maybe it could be required on main and then emitted at the top of the file. I think most others require some kind of statment for it to be attached to right?

I cannot see how this project could (or should) realistically maintain all of them, even less so considering that some are proprietary.

I agree it shouldn't be cppfronts job to maintain these. Metafunctions are intended to be user defined (they aren't currently, however).

zaucy avatar May 28 '25 21:05 zaucy

Part of the goal of cpp2 is to replace preprocessor usage with reflection and codegen.

I don't think pragmas fit the purpose there at all. There's nothing code generation and reflection can do to, e.g., turn off a compiler warning and then on again.

jcanizales avatar May 28 '25 22:05 jcanizales