[Tarantool-patches] [PATCH luajit 4/5] Fix pow() optimization inconsistencies.

Sergey Kaplun skaplun at tarantool.org
Mon Aug 21 12:31:06 MSK 2023


Hi, Maxim!
Thanks for the answers!
Fixed your comment and updated the branch.

On 21.08.23, Maxim Kokryashkin wrote:
> Hi, Sergey!
> Thanks for the fixes!
> LGTM now, see my answers below.
> On Mon, Aug 21, 2023 at 11:06:32AM +0300, Sergey Kaplun wrote:

<snipped>

> > 
> > > > +
> > > > +-- 2921 ^ 0.5 = 0x1.b05ec632536fap+5.
> > > We certainly need to add some explanation here about the precision, because
> > > it is not obvious why these magic numbers should cause any issues.
> > 
> > I suppose any really intererested in this reader may compare the
> > behaviour of the glibc implementation of `sqrt()` and `pow()`. Also, the
> > comment should mention this implementation, so it becomes too huge and
> > distracts the reader from the test case itself.
> Something like the comment below is sufficient:
> | This number has no special meaning and is used as one that gives different
> | results when its square root is obtained with glibc's `sqrt` and `power`
> | operations, thanks to their implementation nuances.
> 
> I strongly suggest adding it to make the test case more understandable.

Added. See the iterative patch below:

===================================================================
diff --git a/test/tarantool-tests/lj-684-pow-inconsistencies.test.lua b/test/tarantool-tests/lj-684-pow-inconsistencies.test.lua
index 418a1557..cfd4860d 100644
--- a/test/tarantool-tests/lj-684-pow-inconsistencies.test.lua
+++ b/test/tarantool-tests/lj-684-pow-inconsistencies.test.lua
@@ -50,6 +50,9 @@ test:samevalues(res, ('consistent results for folding (-inf) ^ 0.5'))
 
 -- 2921 ^ 0.5 = 0x1.b05ec632536fap+5.
 res = {}
+-- This number has no special meaning and is used as one that
+-- gives different results when its square root is obtained with
+-- glibc's `sqrt()` and `pow()` operations.
 -- XXX: use local variable to prevent folding via parser.
 -- XXX: use stack slot out of trace to prevent constant folding.
 local corner_case_pow_05 = 2921
===================================================================

> > 
> > Ignoring for now.
> > 
> > > > +res = {}
> > 
> > <snipped>
> > 
> > > > +test:samevalues(res, ('consistent results for folding 2921 ^ 0.5'))
> > > 
> > > I believe it is possible to make a single function with different
> > > parameters for all three cases above.
> > > Something like `test_power(value, power, extra_map)`, so you can do
> > > | res[i] = extra_map(value ^ power)
> > 
> > I afraid that this function doesn't give any improvement in readability,
> > also, it may change the trace semantics, so I prefer to leave it as is.
> > 
> > Ignoring for now.
> I've expressed my suggestion incomprehensively, sorry. Here is what I've meant
> someting like this:
> 
> | local function pow_test_case(value, power, extra_map)
> |   jit.on()
> |   res = {}
> |   jit.on()
> |   for i = 1, 4 do
> |     res[i] = extra_map(value ^ power)
> |   end
> |
> |   -- XXX: Prevent hotcount side effects.
> |   jit.off()
> |   jit.flush()
> |
> |   test:samevalues(res, ('consistent results for <...>'))
> | end
> 
> Anyway, I've checked the jit.dump by myself, and even for the simple
> cases traces are entirely different. With that in mind, I believe, this
> comment should be ignored, even though this is very sad.

Yes, also it changes the semantic of trace, since power isn't a
constant, fold optimization isn't taken.

> 
> > 
> > > 
> > > > +
> > 
> > <snipped>
> > 
> > > > +-- Need some value near 1, to avoid infinite result.
> > > Typo: s/Need/We need/
> > > Typo: s/avoid/avoid an/
> > 
> > Fixed.
> > 
> > See the iterative patch below.
> > 
> > ===================================================================
> > diff --git a/test/tarantool-tests/lj-684-pow-inconsistencies.test.lua b/test/tarantool-tests/lj-684-pow-inconsistencies.test.lua
> > index 5129fc45..003fe957 100644
> > --- a/test/tarantool-tests/lj-684-pow-inconsistencies.test.lua
> > +++ b/test/tarantool-tests/lj-684-pow-inconsistencies.test.lua
> > @@ -18,7 +18,7 @@ jit.off()
> >  jit.flush()
> >  
> >  local res = {}
> > --- -0 ^ 0.5 = 0. Test sign with `tostring()`.
> > +-- -0 ^ 0.5 = 0. Test the sign with `tostring()`.
> >  -- XXX: use local variable to prevent folding via parser.
> >  -- XXX: use stack slot out of trace to prevent constant folding.
> >  local minus_zero = -0
> > @@ -75,7 +75,7 @@ jit.on()
> >  pow(1, 2)
> >  pow(1, 2)
> >  
> > --- Need some value near 1, to avoid infinite result.
> > +-- We need some value near 1, to avoid an infinite result.
> >  local base = 1.0000000001
> >  local power = 65536 * 3
> >  local resulting_value = pow(base, power)
> > ===================================================================
> > 
> > > > +local base = 1.0000000001
> > 
> > <snipped>
> > 
> > > > -- 
> > > > 2.41.0
> > > > 
> > 
> > -- 
> > Best regards,
> > Sergey Kaplun

-- 
Best regards,
Sergey Kaplun


More information about the Tarantool-patches mailing list