[clang] [llvm] [InstCombine] Canonicalize `(sitofp x)` -> `(uitofp x)` if `x >= 0` (PR #82404)

via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 20 10:56:57 PDT 2024


asmok-g wrote:

Well, I'm not sure how proper that wold be as a reproducer, 

I extracted the mentioned test to a program:
```
#include <cmath>

#include "third_party/swiftshader/src/Reactor/Coroutine.hpp"
#include "third_party/swiftshader/src/Reactor/Reactor.hpp"

using float4 = float[4];
using int4 = int[4];

static std::string testName() { return "test name"; }

using namespace rr;

int main() {
  Function<Void(Pointer<Int4>, Pointer<Int4>)> function;
  {
    Pointer<Int4> x = function.Arg<0>();
    Pointer<Int4> y = function.Arg<1>();

    *y = Abs(*x);
  }

  auto routine = function(testName().c_str());
  auto callable = (void (*)(int4 *, int4 *))routine->getEntry();

  int input[] = {1, -1, 0, (int)0x80000000};

  for (int x : input) {
    int4 v_in = {x, x, x, x};
    int4 v_out = {0};

    callable(&v_in, &v_out);

    float expected = abs(x);
    if (v_out[0] != expected) {
      return 10;
    }
  }
}
```
Before the change program exits with 0, after it exists with 10.
More precisely: `v_out[0]` is: -2147483648, `expected` is: 2.14748365e+09

[out_at.txt](https://github.com/llvm/llvm-project/files/14670227/out_at.txt)
[out_before.txt](https://github.com/llvm/llvm-project/files/14670228/out_before.txt)

I attached the generated IR before and after the change. It's only one line diff:
```
   call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %9, i8 0, i64 16, i1 false)
   call void %49(ptr noundef nonnull %8, ptr noundef nonnull %9) #16
   %61 = call i32 @llvm.abs.i32(i32 %60, i1 true)
-  %62 = sitofp i32 %61 to float
+  %62 = uitofp i32 %61 to float
   %63 = load i32, ptr %9, align 16
   %64 = sitofp i32 %63 to float
   %65 = fcmp une float %64, %62
```

generation command is `clang -O1 -fsized-deallocation '-std=gnu++20' -c  pre.ii -emit-llvm -S -o /tmp/ir_before.ll`

I'm trying to reduce the preprocessed file, tho not sure i'll keep the semantics of the failure.


https://github.com/llvm/llvm-project/pull/82404


More information about the cfe-commits mailing list