Issue 241441
Summary [SPIR-V] MachineCSE drops FPRoundingMode of a constrained fdiv that differs only in rounding mode
Labels new issue
Assignees
Reporter Qi-Ye-079
    ## Description

With the default `llc` pipeline, MachineCSE eliminates the second of two constrained `fdiv`s that have the same operands but different rounding modes, as if they computed the same value. In the SPIR-V backend the rounding mode is not an operand of `OpFDiv`; it is carried by a separate `OpDecorate ... FPRoundingMode` that uses the result register, so MachineCSE cannot tell the two divides apart.

After MachineCSE, the 2nd `OpStrictFDivS` is eliminated, and both decorations target the surviving `OpFDiv`:

```
# Before machine-cse
%9:fid = OpStrictFDivS %4:type, %0:fid, %1:fid
OpDecorate %9:fid, 39, 2        ; FPRoundingMode RTP
%10:fid = OpStrictFDivS %4:type, %0:fid, %1:fid
OpDecorate %10:fid, 39, 3       ; FPRoundingMode RTN
OpStore %2:pid, %9:fid, 2, 8
OpStore %3:pid, %10:fid, 2, 8

# After machine-cse
%9:fid = OpStrictFDivS %4:type, %0:fid, %1:fid
OpDecorate %9:fid, 39, 2        ; FPRoundingMode RTP
OpDecorate %9:fid, 39, 3        ; FPRoundingMode RTN
OpStore %2:pid, %9:fid, 2, 8
OpStore %3:pid, %9:fid, 2, 8
```

As a result, `SPIRVModuleAnalysis` then treats the second `FPRoundingMode` on `%9` as a duplicate decoration and skips it, so the RTN division silently becomes an RTP division.

Notes:
- `-O0` is not affected (MachineCSE does not run), and neither is `-disable-machine-cse`.
- OpenCL.std `sqrt` (`OpExtInst`) and rounding-mode conversions such as `__spirv_ConvertUToF_Rfloat_rtz` / `_rte` are not affected: those opcodes have `UnmodeledSideEffects` (no ISel pattern), so MachineCSE skips them. Opcodes with ISel patterns, such as `OpFDiv`, are affected.
- Constrained `fdiv`/`sqrt` get `FPRoundingMode` with `SPV_INTEL_rounded_divide_sqrt`, added in llvm/llvm-project#238403. Before that, the rounding mode was not valid on `OpFDiv` anyway, so this merge didn't matter in practice. A reproducer is checked in as an XFAIL test: `llvm/test/CodeGen/SPIRV/extensions/SPV_INTEL_rounded_divide_sqrt/fdiv_cse.ll`.

## Reproducer

`repro.ll`:

```llvm
define spir_kernel void @test(double %a, double %b, ptr addrspace(1) %p0, ptr addrspace(1) %p1) strictfp {
entry:
  %rtp = call double @llvm.experimental.constrained.fdiv.f64(double %a, double %b, metadata !"round.upward", metadata !"fpexcept.strict") strictfp
  %rtn = call double @llvm.experimental.constrained.fdiv.f64(double %a, double %b, metadata !"round.downward", metadata !"fpexcept.strict") strictfp
  store double %rtp, ptr addrspace(1) %p0
  store double %rtn, ptr addrspace(1) %p1
  ret void
}

declare double @llvm.experimental.constrained.fdiv.f64(double, double, metadata, metadata)
```

Command:

```sh
llc -mtriple=spirv64-unknown-unknown --spirv-ext=+SPV_INTEL_rounded_divide_sqrt repro.ll -o -
```

To see the MIR change:

```sh
llc -mtriple=spirv64-unknown-unknown --spirv-ext=+SPV_INTEL_rounded_divide_sqrt repro.ll \
    -print-before=machine-cse -print-after=machine-cse -o /dev/null
```

## Result

One `OpFDiv` with only the RTP decoration; both stores use its result:

```
OpDecorate %12 FPRoundingMode RTP
...
%12 = OpFDiv %3 %7 %8
OpStore %9 %12 Aligned 8
OpStore %10 %12 Aligned 8
```

## Expected behavior

Two `OpFDiv`s, each with its own rounding mode, as produced with `-O0`:

```
OpDecorate %12 FPRoundingMode RTP
OpDecorate %13 FPRoundingMode RTN
...
%12 = OpFDiv %3 %7 %8
%13 = OpFDiv %3 %7 %8
OpStore %9 %12 Aligned 8
OpStore %10 %13 Aligned 8
```

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to