<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/66290>66290</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Add f32.nearest and f64.nearest intrinsics to BuiltinsWebAssembly.def
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          loxcel
      </td>
    </tr>
</table>

<pre>
    We've identified which CLANG/GCC intrinsics correspond to most -- but not all -- WASM numeric instructions from section 2.4.1 of

https://webassembly.github.io/spec/core/_download/WebAssembly.pdf

![wasm-numerics](https://github.com/llvm/llvm-project/assets/40441828/a0c00832-eda0-4786-b259-9b70f0cf91cc)

Some example include

       Intrinsic          WASM instruction
--------------------      ----------------
__builtin_clz() i32.clz
__builtin_clzll()         i64.clz
__builtin_popcount() i32.popcnt
__builtin_sqrt()          f64.sqrt

(We verified these intrinsics emit these WASM instructions by disassembling the wasm file produced from invocation of these intrinsics.)

Oddly, some WASM instructions like min & max appear to have no intrinsic equivalent, but instead are accessed through builtins documented by BuiltinsWebAssembly.def:

__builtin_wasm_min_f32() f32.min
__builtin_wasm_max_f64()  f64.max


Unfortunately, we cannot identify if there's an intrinsic or WASM builtin that corresponds to these WASM instructions:

????                      f32.nearest
???? f64.nearest

Now, BuiltinsWebAssembly.def defines "nearest" for the SIMD versions of WASM's nearest instruction:

TARGET_BUILTIN(__builtin_wasm_nearest_f32x4, "V4fV4f", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_nearest_f64x2, "V2dV2d", "nc", "simd128")

However, BuiltinsWebAssembly.def doesn't include definitions for its scalar versions:

TARGET_BUILTIN(__builtin_wasm_nearest_f32, ... ???
TARGET_BUILTIN(__builtin_wasm_nearest_f64, ... ???

My request is for a maintainer to
a. add intrinsics to CLANG/GCC, or
2. supplement BuiltinsWebAssembly.def

so that we can directly invoke WASM f32.nearest and f64.nearest from C++.

Thanks

Kevin

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVttu2zgTfhr6ZmBBpg6RLnzhJL_7B9t2gW2bXBoUObK5pUiVpHzYp19QkhPZSYp2iYAxR6M5fN_MUMw5udWIS5Ldkux-xjq_M3apzJGjmlVGnJZPSOjNHkEK1F7WEgUcdpLv4O7j6vMHQtcf7u5Aam-ldpI74MZadK3RAryBxjgP8zlUnQdtPDClwvFp9eUT6K5BKzlI7bztuJdGO6itacBhfwIapdECTE3iexKvhn3nfetIsiJ0Tej6gBVzDptKnaKt9LuuiqQhdO1a5ISuubFI6HojzEErwwSh6yesVuc3WnFhmtAFyW4PzDXzMTZHsntCi0ufox9uGkLXSu3P_-atNX8j94SuQ0zeEbpO4zRdFLQIspjHcZHQOQoWz9ObIp9XNCvnZXUT1zGvywXnhJbTgL6YBgGPrGkVgtRcdQKnz2FYD2f04Xn1AE-AHfTnb6xB_1o66G82VSeVl3rD1T8kZFGCTGgUTq8VlBpVzkvm6VuqrWm56bSfGAwi7a8V3Q_rr0xCnadRL7_grXhC2KMdytPv0OG0JLGRfpRe4-KgOoGQbiwiqbdBEUINQC0VQmuN6DiKoTCl3hvO-to09StH0RV9fwqhToTegQs8vnat5HeERmogNIeGHYG1LTIb-mbH9gjavBgH_NHJPVMYcLvr-ynYQiaAWQTGOTrXJ29Nt93BiKEDYXjXoPYoQqq3o3jaBQLrUNuTwF8oCEBsGqk3dUJHKuqERo3Ub2uy46bO0zNpgayGHS-46vdvujbWd5p5HBA6IHCmw4QY58wJZA9w6N8bB0xPoDB2AHN0DX7H_GTuuADgO3RfJUqS9fMfvLlCthqZReffeCUkePm03z-bQ0jqHbBBYC01OiCUnl-mFGpj-9r78vDpPhSz62vE1H0SPQij8kVfX-bzdfXXh_993dx-e_j49eEzocUVP6OFwOYxDSESSh_T-jGtCaXjWfOX3042YhHIpM-l_csu8vR4NvNIxSMVv-xi2P9vDrhH-1MgDTpN6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