Repository navigation
[Loads] Fix incorrect replacement of pointers with different provenance - #224281
Conversation
|
@llvm/pr-subscribers-llvm-analysis @llvm/pr-subscribers-llvm-transforms Author: Nikita Popov (nikic) ChangesisPointerAlwaysReplaceable() uses getUnderlyingObject() to check whether the two pointers have the same provenance, because in that case the replacement is always legal. However, getUnderlyingObject() does not actually guarantee that the provenance is the same: In particular, it can look through some intrinsics like strip.invariant.group and launder.invariant.group, which do change the provenance of the pointer. And replacing the result of those intrinsics with their argument is indeed incorrect. Fix this by adding a MustPreserveProvenance argument to the relevant APIs, and enable it in isPointerAlwaysReplaceable(). Noticed while working on #224222. 4 Files Affected:
|
| inline Value *getArgumentAliasingToReturnedPointer(CallBase *Call, | ||
| bool MustPreserveOffset) { |
There was a problem hiding this comment.
Minor nit: would update the non-const overload too, for consistency.
|
@aeubanks Is it possible that this fix removes the need for the llvm.strip.invariant.group intrinsic entirely? The purpose of this intrinsic is really unclear from LangRef, but based on usage in clang and some old discussions, it seems like this intrinsic was inserted on comparisons (and casts to int and similar) to make sure that x == launder(x) does not result in launder(x) being replaced with x, by making it strip(x) == strip(launder(x)) instead (which is safe to replace because the !invariant.groups will be on x or launder(x) only, not strip(x).) With this change, the replacement would get prevented in the first place instead. Does that sound about right? (I think you were involved with the invariant group stuff, sorry if I got the wrong person.) |
|
(yeah I was involved at some point trying to push things along but wasn't the initial designer) yes I think that's right, we shouldn't need strip anymore with this https://docs.google.com/document/d/16GVtCpzK8sIHNc2qZz6RN8amICNBtvjWUod2SujZVEo has the motivation for strip which is exactly what this PR solves |
Clang currently emits calls to llvm.strip.invariant.group under `-fstrict-vtable-pointers` whenever a pointer to a dynamic object is used in a comparison or might be used in a comparison through operations we cannot track. The purpose of the llvm.strip.invariant.group intrinsic is to make sure that if we have a dominating condition like `a == launder.invariant.group(a)`, we don't end up replacing the latter with the former, as that would allow the optimizer to assume that the memory stays invariant, despite going through a launder operation (e.g. on placement new). However, since this was introduced, we've come to the understanding that replacing pointers based on equality comparison is generally only legal if they have the same provenance, and stopped doing such replacements. The specific strip.invariant.group/launder.invariant.group case was still buggy due to an implementation bug, but this was fixed in llvm#224281. As such, I believe that we no longer need to emit llvm.strip.invariant.group in Clang. I plan to also remove the LLVM intrinsic in a followup.
Clang currently emits calls to llvm.strip.invariant.group under `-fstrict-vtable-pointers` whenever a pointer to a dynamic object is used in a comparison or might be used in a comparison through operations we cannot track. The purpose of the llvm.strip.invariant.group intrinsic is to make sure that if we have a dominating condition like `a == launder.invariant.group(a)`, we don't end up replacing the latter with the former, as that would allow the optimizer to assume that the memory stays invariant, despite going through a launder operation (e.g. on placement new). However, since this was introduced, we've come to the understanding that replacing pointers based on equality comparison is generally only legal if they have the same provenance, and stopped doing such replacements. The specific strip.invariant.group/launder.invariant.group case was still buggy due to an implementation bug, but this was fixed in llvm/llvm-project#224281. As such, I believe that we no longer need to emit llvm.strip.invariant.group in Clang. I plan to also remove the LLVM intrinsic in a followup.
Clang currently emits calls to llvm.strip.invariant.group under `-fstrict-vtable-pointers` whenever a pointer to a dynamic object is used in a comparison or might be used in a comparison through operations we cannot track. The purpose of the llvm.strip.invariant.group intrinsic is to make sure that if we have a dominating condition like `a == launder.invariant.group(a)`, we don't end up replacing the latter with the former, as that would allow the optimizer to assume that the memory stays invariant, despite going through a launder operation (e.g. on placement new). However, since this was introduced, we've come to the understanding that replacing pointers based on equality comparison is generally only legal if they have the same provenance, and stopped doing such replacements. The specific strip.invariant.group/launder.invariant.group case was still buggy due to an implementation bug, but this was fixed in llvm/llvm-project#224281. As such, I believe that we no longer need to emit llvm.strip.invariant.group in Clang. I plan to also remove the LLVM intrinsic in a followup.
This intrinsic was originally introduced to make sure that if we have a dominating condition like `a == launder.invariant.group(a)`, we don't end up replacing the latter with the former, as that would allow the optimizer to assume that the memory stays invariant, despite going through a launder operation. Wrapping both comparison operands in strip.invariant.group avoids the issue, because replacing one stripped pointer with another is safe. The underlying issue was fixed in #224281 (and prior work), which ensures that the problematic replacement cannot happen in the first place. #225072 stopped emitting llvm.strip.invariant.group on the clang side. This PR now removes the intrinsic entirely. To be on the safe side, it is upgraded to llvm.launder.invariant.group. For Clang's specific usage, just dropping it would be fine.
This intrinsic was originally introduced to make sure that if we have a dominating condition like `a == launder.invariant.group(a)`, we don't end up replacing the latter with the former, as that would allow the optimizer to assume that the memory stays invariant, despite going through a launder operation. Wrapping both comparison operands in strip.invariant.group avoids the issue, because replacing one stripped pointer with another is safe. The underlying issue was fixed in llvm/llvm-project#224281 (and prior work), which ensures that the problematic replacement cannot happen in the first place. llvm/llvm-project#225072 stopped emitting llvm.strip.invariant.group on the clang side. This PR now removes the intrinsic entirely. To be on the safe side, it is upgraded to llvm.launder.invariant.group. For Clang's specific usage, just dropping it would be fine.
This intrinsic was originally introduced to make sure that if we have a dominating condition like `a == launder.invariant.group(a)`, we don't end up replacing the latter with the former, as that would allow the optimizer to assume that the memory stays invariant, despite going through a launder operation. Wrapping both comparison operands in strip.invariant.group avoids the issue, because replacing one stripped pointer with another is safe. The underlying issue was fixed in llvm/llvm-project#224281 (and prior work), which ensures that the problematic replacement cannot happen in the first place. llvm/llvm-project#225072 stopped emitting llvm.strip.invariant.group on the clang side. This PR now removes the intrinsic entirely. To be on the safe side, it is upgraded to llvm.launder.invariant.group. For Clang's specific usage, just dropping it would be fine.
This intrinsic was originally introduced to make sure that if we have a dominating condition like `a == launder.invariant.group(a)`, we don't end up replacing the latter with the former, as that would allow the optimizer to assume that the memory stays invariant, despite going through a launder operation. Wrapping both comparison operands in strip.invariant.group avoids the issue, because replacing one stripped pointer with another is safe. The underlying issue was fixed in llvm#224281 (and prior work), which ensures that the problematic replacement cannot happen in the first place. llvm#225072 stopped emitting llvm.strip.invariant.group on the clang side. This PR now removes the intrinsic entirely. To be on the safe side, it is upgraded to llvm.launder.invariant.group. For Clang's specific usage, just dropping it would be fine.
isPointerAlwaysReplaceable() uses getUnderlyingObject() to check whether the two pointers have the same provenance, because in that case the replacement is always legal. However, getUnderlyingObject() does not actually guarantee that the provenance is the same: In particular, it can look through some intrinsics like strip.invariant.group and launder.invariant.group, which do change the provenance of the pointer. And replacing the result of those intrinsics with their argument is indeed incorrect.
Fix this by adding a MustPreserveProvenance argument to the relevant APIs, and enable it in isPointerAlwaysReplaceable().
Noticed while working on #224222.