function argument declared volatile double broken

Post Reply

Topic author
jonesd
Valued Contributor
Posts: 90
Joined: Mon Aug 09, 2021 7:59 pm
Reputation: 0
Status: Offline

function argument declared volatile double broken

Post by jonesd » Thu Jun 20, 2024 7:32 pm

Some code I'm using declares a function argument with the volatile type attribute, for reasons according to the comments:

Code: Select all

/*
** Do one step of the Kahan-Babushka-Neumaier summation.
**
** https://en.wikipedia.org/wiki/Kahan_summation_algorithm
**
** Variables are marked "volatile" to defeat c89 x86 floating point
** optimizations can mess up this algorithm.
*/
I've discovered that on X86 this code is broken but appears to work if the volatile attribute is removed.
I've attached a reproducer program. I'm using VSI C x86-64 V7.5-009.
Attachments
volatile_fail.c
(3.83 KiB) Downloaded 23 times


Topic author
jonesd
Valued Contributor
Posts: 90
Joined: Mon Aug 09, 2021 7:59 pm
Reputation: 0
Status: Offline

Re: function argument declared volatile double broken

Post by jonesd » Fri Jun 21, 2024 9:33 am

Looking at the generated code in the debugger, it looks like the caller puts the argument to kbn_step in %xmm0 while the called procedure thinks that scalar argument should be read from %rsi when it's declared volatile.


jreagan
VSI Expert
Master
Posts: 174
Joined: Tue Dec 01, 2020 8:40 am
Reputation: 0
Status: Offline

Re: function argument declared volatile double broken

Post by jreagan » Mon Jun 24, 2024 11:09 am

We'll look at it


Topic author
jonesd
Valued Contributor
Posts: 90
Joined: Mon Aug 09, 2021 7:59 pm
Reputation: 0
Status: Offline

Re: function argument declared volatile double broken

Post by jonesd » Mon Jun 24, 2024 1:30 pm

jreagan wrote:
Mon Jun 24, 2024 11:09 am
We'll look at it
Thanks.

The code that exposed this bug was added to SQLite 11 months ago. At the time, there was discussion on their forum whether to use pragmas or the volatile keyword to suppress optimization and they went with the 'simpler' solution. I confirmed that left on its own the VSI compiler will optimize away the add/subtract pair of the same value that is the heart of the algorithm.

As it is easy to override built-in SQL functions, I have a workaround for the time being.

Post Reply