Codebox in Jit.Gen : code generation issue - essential code gets optimised away

soundyi's icon

Hi there,
I stumbled upon this weirdness of code generation issue, where an essential else block is dropped … most probably as the value set inside the else block is not directly part of the jit.gen codebox outputs.

If you run the attached patcher it works as expected, as I added a workaround via a variable called dummy.

If you open the jit.gen patch and also the „code“ sidebar, watch what happens if you comment out line 41 with the else block surrounding it : it disappears aka its optimised away.

And executing the patch leads to wrong results - as the logic depends on the boolean flag reset in this else block.

It took me quite a long time, to encircle the cause … and by the way I also stumbled upon the issue that assigning 1-plane vectors to the outputs via „out1 = vec(42)“ leads to a non interpretable compiler error and has to be done in a scalar fashion „out1 = 42“ … weird too, especially as this error is not hinted in the codebox, but only if one executes the parent patcher.

I can image some „code work“ to substitute the dropped boolean logic … but why shall I ???

To some extent its recognisable that the else block gets optimised away, as there is only 1 variable changed inside and this does not directly contribute to the output … BUT if this kind of thinking really rules, we end up in a assembler programming style … and if I am forced to develop software (and especially pieces of art, which should be a joy !) in this mind boggling kind of thinking, I am better off beaming myself back into stone age ;-).

Why do you drop into the darkness of codebox in the first place, you may ask? Stay in the joyful field of operators patching in a „normal“ jit.gen patcher and everything is fine, you may argue ;-).

Yes, I really would like to do so … but I need the sweet sugar of „loops“.

As an example : scanning matrices (or textures with jit.gl.pix) in rows or lines, or the surrounding area of a pixel is very easy to do with a for loop.

So patching generative art in the style of „nature of code“ (flow fields, boids flocking and alike), its a great option to have the codebox and its for & while loop features.

cheers
soundyi

JitGen Code Generation Optimization Issue.maxpat
Max Patch
comment out line 41 and be surprised ;-)