<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/75543>75543</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[Codegen][RISCV][X86] Emit float instructions with static rounding mode for constrained intrinsics.
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
boomshroom
</td>
</tr>
</table>
<pre>
IEEE 754 section 4.1 describes floating point attributes with
> An attribute is logically associated with a program block to modify its numerical and exception semantics. A user can specify a constant value for an attribute parameter.
LLVM's constrained floating point intrinsics are the only thing in LLVM to provide the constant attribute behavior without constant-folding from the environment manipulation intrinsics. Some modern ISA extensions also allow specifying the rounding mode statically within the instruction, both better matching the desired semantics of the constrained intrinsics and saving instructions setting and restoring the global floating point environment.
However, LLVM does not currently use them without target-specific intrinsics completely detached from the target-independent intrinsics which take the same information. For RISC-V, these intrinsics are paradoxically only available for vector operations, which _don't_ have the option of a static rounding mode in the instructions, but there aren't any intrinsics for scalar operations which _do_ have that option. X86 on the other hand does have intrinsics for both vector and scalar operations, but that seems to be because they're provided by the same extensions.
Currently, constrained intrinsics are lowered to strict opcodes with rounding mode striped completely before reaching the target-specific code generation, preventing the desired behavior.
Source (note that static rounding mode in AVX-512 only supports suppressing exceptions, hence specifying `!"fpexcept.ignore"`):
```llvm
define float @div(float %x, float %y) {
entry:
%div = call float @llvm.experimental.constrained.fdiv.f32(float %x, float %y, metadata !"round.downward", metadata !"fpexcept.ignore")
ret float %div
}
declare float @llvm.experimental.constrained.fdiv.f32(float, float, metadata, metadata)
```
Output with llvm 17.0.1
On X86 with AVX-512F:
```asm
div: # @div
vdivss xmm0, xmm0, xmm1
ret
```
On Riscv with F and hard-float ABI:
```asm
div: # @div
fdiv.s fa0, fa0, fa1
ret
```
Expected output
On X86 with AVX-512F:
```asm
div: # @div
vdivss xmm0, xmm0, xmm1, {rd-sae}
ret
```
On RISC-V with F and hard-float ABI:
```asm
div: # @div
fdiv.s fa0, fa0, fa1, rdn
ret
```
Godbolt link: https://godbolt.org/z/7fPoEoxs8
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEV8uO47oR_Rp5U7Bg0--FF_1y0sANEtwGGrO7oMiSxQzFEsiSbOfrA1Ly8850BlkkRqMlt4pVp845RaplCGbvELfZ4jlbvI5kyxX5bUFUh8oT1aOC9Gn7_vb2BqvFHAIqNuRgnk9BY1DeFBigtCTZuD00ZByDZPamaBkDHAxXkE1es8nT8Hv2Bk_uGgImgKW9UdLaE8gQSBnJqPuVEhpPey9rKCyp78AENWlTnsBwANfW6ONKkE4DHhU2CVzAWjo2KuTwBG1AD0o6CA2quFKCIhdYOoZO2hahJA_yFlIjvayR0ee3yH_77fNvmViFfrmXxqF-7Nw49sYFowJIj8AVAjl7Aq5ijHEQk8QuGk-d0X3EBc4VQYGV7Az5xAK1fIkZl2R1zFV6qtNqdJ3x5Gp0DLV0pmmtTCxcseTwQTVG5tA7eP94AjwyumDIBZA2EEhr6XBmKKaPmT21LtWKCyGw5EGlCMq4FGMiFW3yRCZeoCCuoEBm9FBLVtU5l8ZgPOqrNEDltfmBzFv2nIYgu561S4kAATnRHZ97DEz-XGFvqZD2UZAbdu7E_CsdsEMfMSdJNGEARwyq9R4d21M0TkxcXzRg6ffI454lo27hKqobi4z2BBpZqipa46zQsM44jQ06jfc-OVRGVcDye2-GIOvIakm-TjLmsCMPv79_vIw_I1quMOCjz6JhNR0HeZLjZCeNlYXt7d2hYvJADfqUNcRUfeU_dJRuxX9AJbvBsv0YUQlykP3BC38WPyUsIkkVeoygUlKQ7nQLNmIJSlp5i-UK5IJB8gAih2_rJVBfj2JyqKL2Sa8U_JA9OXBoN5nosdoVqGQIiHWI81jEmVNy0PyUiVWktR9SDcXpqs11dO4M9XL2Tcz_M1N7BEsHjIPABIG9UbFRRfq8VT7OnDcN6lt7FViSR_Aor8P1aMyYD_bohpYj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