[clang] [clang][CodeGen] Add range metadata for atomic load of boolean type. #131476 (PR #133546)

Jan Górski via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 10 12:49:00 PDT 2025


janagor wrote:

> I don't really understand what the new version of the patch is doing... instcombine can infer `nuw` on `trunc` if the operand has range metadata. There isn't any reason to duplicate that code.

The unoptimized version of IR is which is generated is the following:

```
  %atomic-temp = alloca i8, align 1
  %0 = load atomic i8, ptr @t1 monotonic, align 1
  store i8 %0, ptr %atomic-temp, align 1
  %1 = load i8, ptr %atomic-temp, align 1
  %loadedv = trunc i8 %1 to i1
  ret i1 %loadedv
```

Lines 2-3 are generated by 'EmitAtomicOp' in CGAtomic, while `CodeGenFunction::EmitLoadOfScalar` adds `range!` only to line 4 when -O1. Lack of adding `range!` to 2nd line does not generate nuw at the end in trunc. From what I saw there were 2 ways to solve it: either add `nuw` the way I did it there, or add `range!` in EmitAtomicOp. I think in such case in order to no longer duplicate code would be to just make `getRangeForLoadFromType` a public method and use it for atomic load as well. 


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


More information about the cfe-commits mailing list