Issue 234098
Summary [CodeGenPrepare] `optimizeExtUses` makes a sign check read `poison` through `zext nneg`
Labels new issue
Assignees
Reporter dyslexicsteak
    ## Summary

`optimizeExtUses` in `CodeGenPrepare` can make a sign check on a value read the `poison` of that value's `zext nneg` instead of the value itself. On `x86_64-pc-windows-msvc` this becomes a miscompile, since `WinEHPrepare`'s cleanup then folds the check to true and lets negative values through. I ran into this and found it reported previously at rust-lang/rust#163734, but the IR is language- and target-independent and also shows up on i686 with narrower types.

## Reproduction

The reproducer below loads a 32-bit `%id`, checks that it isn't negative, and only then widens it with `zext nneg`, which is valid because the check guards the block.

```llvm
define i64 @f(ptr %p, i1 %c) {
entry:
  %id = load i32, ptr %p, align 4
  br i1 %c, label %test, label %exit

direct:
  %z = zext nneg i32 %id to i64
  ret i64 %z

test:
 %nonneg = icmp sgt i32 %id, -1
  br i1 %nonneg, label %direct, label %exit

exit:
  ret i64 -1
}
```

Running it through `opt` like so:

```
opt -S -mtriple=x86_64-unknown-linux-gnu -passes='require<profile-summary>,function(codegenprepare)' f.ll
```

gives this:

```llvm
define i64 @f(ptr %p, i1 %c) {
entry:
 %id = load i32, ptr %p, align 4
  %z = zext nneg i32 %id to i64          ; <-- moved up, still nneg
  br i1 %c, label %test, label %exit

direct:
 ret i64 %z

test:
  %0 = trunc i64 %z to i32
  %nonneg = icmp sgt i32 %0, -1          ; <-- reads poison when %id < 0
  br i1 %nonneg, label %direct, label %exit

exit:
  ret i64 -1
}
```

On `x86_64-pc-windows-msvc`, this variant with an MSVC personality turns the `poison` into wrong code:

```llvm
define i64 @f(ptr %p, i1 %c) personality ptr @__CxxFrameHandler3 {
entry:
  %id = load i32, ptr %p, align 4
  br i1 %c, label %test, label %exit

direct:
  %z = zext nneg i32 %id to i64
  ret i64 %z

use:
  br i1 %nonneg, label %direct, label %exit

test:
 %nonneg = icmp sgt i32 %id, -1
  br i1 %c, label %use, label %exit

exit:
  ret i64 -1
}

declare i32 @__CxxFrameHandler3(...)
```

Running it through `opt` like so:

```
opt -S -mtriple=x86_64-pc-windows-msvc -passes='require<profile-summary>,function(codegenprepare,win-eh-prepare)' g.ll
```

gives this:

```llvm
define i64 @f(ptr %p, i1 %c) personality ptr @__CxxFrameHandler3 {
entry:
  %id = load i32, ptr %p, align 4
  %z = zext nneg i32 %id to i64
  br i1 %c, label %test, label %exit

direct:
 ret i64 %z

use:
  br label %direct                       ; <-- the check was folded to true

test:
  br i1 %c, label %use, label %exit

exit:
 ret i64 -1
}
```

The check is gone, and `use` is just `br label %direct`, so `f` ends up returning 2147483648 instead of -1 for an `%id` of `0x80000000`.

This reproduces on 23.1.4 and on `main` at 12b3aded3fc0.

## Diagnosis

What happens:
- `optimizeExt` first moves the `zext nneg` up next to the load so the two can become one extending load, and keeps `nneg`. That's ok on its own, since nothing outside `direct` uses `%z` yet.
- `optimizeExtUses` then comes along and rewrites the other uses of `%id` to read `trunc %z`, including the sign check. When `%id` is negative, `%z` is `poison`, so the check reads `poison`.

`optimizeExtUses` goes wrong even without the move, when the `zext nneg` already sits in the load's block. I'd expect it to either drop `nneg` from `%z` when it rewrites the check or just leave the check reading `%id`.

The fold in the second example comes from `WinEHPrepare`, which runs `SimplifyInstructionsInBlock` from `cleanupPreparedFunclets` for functions with an MSVC personality, and InstSimplify uses `nneg` to fold the check to true. Without `codegenprepare` the check survives. `-disable-cleanups` only hides the fold, because while the check survives, it still reads the poisoned `trunc`.

These examples run correctly everywhere else, including the first one on `x86_64-pc-windows-msvc`, because while they get the same `poison`, no pass in the pipeline folds the check and triggers a miscompile, but any pass that trusts `nneg` could break them. It isn't limited to 64-bit either. On `i686-unknown-linux-gnu`, a `zext nneg i8` to `i32` is moved beside the load the same way, and the sign check reads it through `trunc`.

## Suggested fix

I found 2 prior fixes that dropped poison-generating flags in `CodeGenPrepare` in similar situations:
- #72103, which dropped `nneg` when type promotion moved a `zext`
- #90382, which dropped flags after hoisting in `optimizeBranch`

I think the simplest fix is to do the same here, and call `dropPoisonGeneratingFlags()` on the extension once `optimizeExtUses` has rewritten a use.

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

Reply via email to