<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>244188</bug_id>
          
          <creation_ts>2022-08-22 02:13:07 -0700</creation_ts>
          <short_desc>[DFG] Usekind of Div result turns to Int32 causing different answer</short_desc>
          <delta_ts>2022-08-29 02:14:14 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>JavaScriptCore</component>
          <version>WebKit Local Build</version>
          <rep_platform>PC</rep_platform>
          <op_sys>Linux</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Yue Sun">sunyue20z</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>saam</cc>
    
    <cc>webkit-bug-importer</cc>
    
    <cc>ysuzuki</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1892914</commentid>
    <comment_count>0</comment_count>
    <who name="Yue Sun">sunyue20z</who>
    <bug_when>2022-08-22 02:13:07 -0700</bug_when>
    <thetext>./path/to/jsc test.js --useConcurrentJIT=0 --forceWeakRandomSeed=1 --jitPolicyScale=0
./path/to/jsc test.js --useConcurrentJIT=0 --forceWeakRandomSeed=1 --jitPolicyScale=0.1

var CreateBaseline = false; 

var debugTestNum = -1; 

var test_values = [-5, 248, 654, -1026];

function rem3(x) {
  x = x | 0;
  return (x | debugTestNum / x + x) % 3 | 0;
}

function testSignedDivStrengthReduction() {
  var i = 0;
  test_values.forEach(function (value) {
    print(&quot;Test# &quot; + i + &quot;(&quot; + value + &quot;) :\t\t Found &quot; + rem3(value));
    print(&quot;Test# &quot; + i + &quot;(&quot; + value + &quot;) :\t\t Found &quot; + rem3(value));
    ++i;
  });
}

testSignedDivStrengthReduction();

We run the script using options listed above. In this PoC, function rem3() calculate an expression. Since the operand type of operation &quot;or&quot; and &quot;mod&quot; should be Int32, JSC label the usekind of operands as Int32 during the DFG backward propagation phase, which lead to problem. In Low Level Interpreter, when x=-5, JSC would calculate div first, which is 0.2, then plus -5(x) to get -4.8, then turn -4.8 to -4 use truncation, finally calculate -5 | -4. However, in DFG, after div, the 0.2 is first truncate to 0 before adding -5, leading to inconsistent behavior. The PoC below also have this problem.

function f(v) {
  var result;
  result = ((v) % 0 + 1)|0;
  return result;
}
noInline(f);
var x;
for (i = 0; i &lt; 2; ++i) {
  x = f(1);
  print(x);
}

In DFGSpeculativeJIT, when lowering ArithMod, if the demoninator is 0, they directely put 0 as result, instead of NaN. FTL also has the same issue. 

Our suggestion is that, in DFG backward propagation, label the usekind of operation div and mod itself as &quot;use as number&quot;. In this case, the ArithAdd following div and mod operation would turn to DoubleAdd, which make sense.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1894449</commentid>
    <comment_count>1</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2022-08-29 02:14:14 -0700</bug_when>
    <thetext>&lt;rdar://problem/99268137&gt;</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>