Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attribute/pragma to mark CTFE only code? #4789

Open
yanok opened this issue Nov 29, 2024 · 3 comments
Open

Attribute/pragma to mark CTFE only code? #4789

yanok opened this issue Nov 29, 2024 · 3 comments

Comments

@yanok
Copy link
Contributor

yanok commented Nov 29, 2024

Would it be possible to add a pragma or attribute to mark CTFE-only code, such that it is omitted from the code generation? Our project uses CTFE a lot, most of the functions are templated and many of them are instantiated widely. This results in non trivial codegen cost + LLVM compilation cost just to produce some code that is going to be later thrown away during linking.

I think the implementation could be damn straightforward (but I might be missing something): just skip codegen if we see that attribute/pragma. That could result in LLVM complaining about missing symbols, if some other function that references this one was codegened, so a better implementation would also enforce that CTFE-only functions could only be referenced from another CTFE-only functions.

I'm happy to work on the implementation, if there is an approval of the idea.

@kinke
Copy link
Member

kinke commented Nov 29, 2024

@thewilsonator proposed this 7 years ago in https://forum.dlang.org/thread/[email protected] - for dlang in general, no LDC-specific language extension. I don't know how that thread ended. And https://forum.dlang.org/thread/[email protected] proposed skipping codegen for assert(__ctfe);.

I'd prefer a general solution, but that's not a requirement I think - feel free to hack around and see if you can get it to work. If you can then demonstrate significant compile-time improvements for a (hopefully public) project, we'll probably adopt it. In the best case, the hard numbers then convince upstream to adopt it too.

@JohanEngelen
Copy link
Member

Would be nice indeed. In general, I think a magic attribute will be nicer to work with than a pragma. (for example, you can do @attribute: to apply it to everything in the scope it creates, e.g. a whole file)

@thewilsonator
Copy link
Contributor

you can do pragma(whatever): and have the same effect of applying to everything in a scope as @attribute:.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants