<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/80402>80402</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Redundant range check in webassembly float to int conversion
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
uncleasm
</td>
</tr>
</table>
<pre>
Converting a float to int by static cast seems to produce the following code (webassembly-trunk)
(https://godbolt.org/z/WsW1MM995)
```
int f(float a) {
return a;
}
f(float): # @f(float)
block
local.get 0
f32.abs
f32.const 0x1p31
f32.lt
i32.eqz
br_if 0 # 0: down to label0
local.get 0
i32.trunc_f32_s
return
end_block # label0:
i32.const -2147483648
end_function
```
where the input range is first compared against `-(1<<31)`
I would have expected that the preclamping the input parameter to a smaller range would allow skipping this stage as in
```
int g(float a) {
if (a < 0) { a = 0; }
if (a > 55) { a = 55; }
return a;
}
```
But that doesn't seem to be the case:
```
g(float): # @g(float)
block
f32.const 0x1.b8p5
f32.const 0x0p0
local.get 0
local.get 0
f32.const 0x0p0
f32.lt
f32.select
local.tee 0
local.get 0
f32.const 0x1.b8p5
f32.gt
f32.select
local.tee 0
f32.abs
f32.const 0x1p31
f32.lt
i32.eqz
br_if 0 # 0: down to label1
local.get 0
i32.trunc_f32_s
return
end_block # label1:
i32.const -2147483648
end_function
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVk1z4ygQ_TX40hUXAsmSDjrkY121h7nsZY4phFoSGwRaQHEyv34LS_FXnE02NVQqNtDQr5t-zy28V51BrEh2R7KHlZhCb101GalR-GFV2-a1urfmGV1QpgMBrbYiQLCgTID6FXwQQUmQwgfwiIOPe6OzzSQRQo_QWq3tLh6WtkEgrNhhLbzHodavN8FN5omwktAHQm8JK_oQRk_4LWFbwradbWqrw9q6jrDtL8K2P_3P5MePssyOhzZ0-dtPI66WsGIGKggrgeR38x4AOA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