shfty-elysian / elysian-legacy

Field function compiler
0 stars 0 forks source link

Reduce generated code duplication and definition footguns for field wrappers #114

Open Shfty opened 1 year ago

Shfty commented 1 year ago

Currently, wrappers that compose other fields (ex. Raymarch, ElongateBasis, etc.) inline a call to their child field's function, and have to use FunctionIdentifier::new_dynamic inside AsIR::entry_point to forcibly avoid deduplication.

This causes a lot of duplication in generated code - which scales poorly for things like shader compilation time - and creates the footgun of accidentally tagging a wrapper with a static entrypoint identifier, thus causing all calls to use one set of parameters and creating unexpected behaviour.

Need to come up with a strongly-encoded way to reuse wrapper logic. Some duplication (i.e. of calls to reusable functions with specific params) will be necessary to avoid function pointer usage, but much of it should be avoidable.

The simplest solution is to split each wrapper's business logic out into its own statically-named reusable function, then call it from a unique entrypoint.

Hash-based deduplication should also be considered. Even with proper segmentation, identical functions can be generated if a given wrapper is constructed multiple times with the same parameters. As these have the same end effect, they can be combined.