ldc
ldc copied to clipboard
Attribute/pragma to mark CTFE only code?
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.
@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.
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)
you can do pragma(whatever): and have the same effect of applying to everything in a scope as @attribute:.
I've created https://github.com/dlang/dmd/pull/20858 with the front end part of it. Adding codegen skip on top is trivial.
If dmd people say it's a no-go, I guess for LDC-specific solution I'll have to switch back to UDA instead of using a full-fledged function attribute...
I've posted my proposal to the dip.ideas forum: https://forum.dlang.org/post/[email protected]
We'll see how it goes.