<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - WebAssembly: float-to-int intrinsics aren't const-propagated"
   href="https://bugs.llvm.org/show_bug.cgi?id=46982">46982</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>WebAssembly: float-to-int intrinsics aren't const-propagated
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Backend: WebAssembly
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>alex@crichton.co
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>In the Rust compiler we've been updating the codegen for WebAssembly targets on
float-to-int casts recently. <a href="https://github.com/rust-lang/rust/issues/73591">https://github.com/rust-lang/rust/issues/73591</a> has
a high-level description of the problem, but the basic idea is that
rust-as-the-language defines our `as` operator as safe and saturating. This
causes quite a lot of codegen if we have our own branching and then LLVM has
its own set of branching for the `fptosi` and `fptoui` instructions. Put
another way, LLVM already emits quite a lot of instructions for `fptosi` and
`fptoui`, and we want to avoid that in codegen.

We recently switched to using the `llvm.wasm.trunc.signed.i32.f32` family of
intrinsics for implementing safe float-to-int casts. (this is when the
`+nontrapping-fptoint` argument is not provided, the default). In doing so,
though, we realized that LLVM won't optimize programs quite as much any more,
since the intrinsic is more opaque than the `fptosi` instruction itself. For
example this program:




define i32 @foo() {
start:
  %0 = tail call i32 @llvm.wasm.trunc.signed.i32.f32(float 5.000000e+00)
  ret i32 %0
}

declare i32 @llvm.wasm.trunc.signed.i32.f32(float)




won't get optimized any further, and the final wasm code has `i32.trunc_f32_s`,
generating this assembly:





foo:                                    # @foo
        f32.const       0x1.4p2
        i32.trunc_f32_s
        end_function




Would it be possible to teach the optimizer how to constant evaluate the wasm
float-to-int intrinsics? I'm not entirely sure how else `fptosi` interacts with
the optimizer but this is at least one case we're running into.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>