As LabVIEW evolves more and more, the compiler takes over an awful lot of code optimisation for us. This leads us to situations where relatively large and important pieces of code can be evaluated at compile time and constant folded which can greatly aid execution speed. This is good.
Constant folding can be a great aid when programming but at the moment, it's usage is a bit "hit and miss" due to the opaqueness of the process. We already have constant folding highlighting, which really helps things (even if the feedback is sometimes very hard to understand). But this doesn't always give us enough feedback.
What I would like is the option to declare a portion of code as "Requires constant folding" (like a "Precompile" structure). In this way, I can, as a programmer, designate some code which is meant to be evaluated at compile time. If the compiler is unable to constant fold this code, then the VI should be broken. My motivations are three-fold.
- Sometimes we want to specifically make use of the constant folding capabilities of the compiler, but a small change can result in the code no longer being constant folded. I would like explicit feedback when code I want constant folded is not constant foldable.
- I have no idea whether code complexity has an effect on the ability to constant fold. Other compiler optimisations (Like unbundle-unbundle inplaceness) are dependent on code complexity. Explicit declaration is not code complexity dependent.
- When looking at FPGA designs, the ability to perform constant folding of data otherwise requiring resources or affecting performance is very powerful. In such a "Constant folding" code, we could also allow mathematical functions to be used which are otherwise not supported on the target (max/min of an array in a timed loop for example), or creating default data for an array (to be used as Block RAM) based on an existing equation where constants are defined as DBL.
One example of FPGA code is automatic latency balancing of several parameter pathways into a process where the code accepts abstract parameter objects whose latency is queried via a dynamic dispatch VI which simply returns a constant. I use dependency injection to tell the sub-VIs which communication pathways they are being given and they can then query the latency and do some static timing calculations for the delays required on different pathways. Tests have shown that this is constant folded and that it is thus possible to write very robust FPGA code which auto-adjusts request indices for parameters in multiplexed code. At the moment, things seem to work but the ability to specifically designate such code as being constant folded would be welcome to make sure I don't accidentally produce a version which doesn't actually return a constant (and my compiles fail, I get timing errors, or just over-use resources).... In the code below, all of the code circled in blue is constant-folded when compiling the FPGA code. In the sub-VIs I have to do some awkward calculations because certain functioanlity is not available on FPGA. By defining this code as requiring explicit constant folding, I could theoretically utilise the full palette of LV functions and also be guaranteed a compile error (LabVIEW error, not Xilinx) if the code thus designated can not be constant folded.

So in a way, it's similar to the In place element structure which, when all goes well, should not be needed but there are cases (I've run into some myself) where either small changes in code can make the desired operation impossible or where the code complexity can cause the optimisation to not be performed. As such, it is still required at times to explicitly designate some code paths as being In-Place. I would like to have the same functionality for "Constant folding".