spin2cpp icon indicating copy to clipboard operation
spin2cpp copied to clipboard

CORDIC optimizer bug

Open electrodude opened this issue 2 years ago • 6 comments

The following code:

PUB main(a) : x, y | b
  asm
                qexp    a
                neg     b, a
                qexp    b
                getqx   x
                getqx   y
  endasm

compiles to the following, which is missing the first qexp instruction and therefore produces incorrect results:

_main
	neg	_var01, arg01
	qexp	_var01
'   asm
	getqx	result1
	getqx	result2
_main_ret
	ret

I'm running the latest commit e37a63f51defd82a38811e20c0f7819ca76b9fc2, invoked as flexspin -O2 -2 -l. The program I made this example from works correctly if I disable optimization with org/end but fails due to this bug with asm/endasm.

electrodude avatar Mar 30 '22 04:03 electrodude

Not a bug IMO, code like this (i.e. cordic interleave tricks in non-volatile inline ASM) was never guaranteed to work to begin with (try using the function in a context where it becomes inlined and only one result is used - that will result in brap in any version) It just happened to work most of the time. The change that breaks this isn't an optimization, it's the fix for bug #223

Wuerfel21 avatar Mar 30 '22 05:03 Wuerfel21

I agree, not a bug, but it would be nice to have a warning in there. I think the optimizer should be able to see these kind of cases and suggest to the user that an "asm const" or something similar is required.

totalspectrum avatar May 16 '22 13:05 totalspectrum

Yeah, that'd probably be good

Wuerfel21 avatar May 16 '22 14:05 Wuerfel21

Also, aren't we missing Spin syntax for volatile-but-not-cogexec flavour inline ASM?

Wuerfel21 avatar May 16 '22 14:05 Wuerfel21

Yes, Spin has no way to do volatile hubexec ASM. It'd be easy to add, but I don't know what the syntax should be. Any suggestions?

totalspectrum avatar May 17 '22 00:05 totalspectrum

ORGH is used now for volatile but not cogexec inline ASM. Still need to look at a warning for CORDIC optimizations though.

totalspectrum avatar Dec 02 '22 13:12 totalspectrum