[llvm] [Attributor] Propagate alignment through ptrmask (PR #150158)

via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 3 17:00:20 PST 2025


================
@@ -5200,6 +5201,28 @@ static unsigned getKnownAlignForUse(Attributor &A, AAAlign &QueryingAA,
       TrackUse = true;
     return 0;
   }
+  if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
+    switch (II->getIntrinsicID()) {
+    case Intrinsic::ptrmask: {
+      // Is it appropriate to pull attribute in initialization?
+      const auto *ConstVals = A.getAAFor<AAPotentialConstantValues>(
+          QueryingAA, IRPosition::value(*II->getOperand(1)), DepClassTy::NONE);
+      const auto *AlignAA = A.getAAFor<AAAlign>(
+          QueryingAA, IRPosition::value(*II), DepClassTy::NONE);
+      if (ConstVals && ConstVals->isValidState() && ConstVals->isAtFixpoint()) {
+        unsigned ShiftValue = std::min(ConstVals->getAssumedMinTrailingZeros(),
+                                       Value::MaxAlignmentExponent);
+        Align ConstAlign(UINT64_C(1) << ShiftValue);
+        if (ConstAlign >= AlignAA->getKnownAlign())
----------------
Shoreshen wrote:

Hi this means the mask is going to "cancel out" the alignment requirement, say if we have:
```
define internal ptr @test(ptr align 4 %x) {
  %p = tail call ptr @llvm.ptrmask.p0.i64(ptr %x, i64 -16)
  store float 1.0, ptr %p, align 8
  ret ptr %p
}
```
And we are calculating the alignment of `%x`, since `%p` is masked by `-16`, it is aligned at 16. Then it doesn't matter what is the alignment of `%x`. So this use of `%x` returns 1, which means no alignment requirement of this use for `%x`. The final required alignment of `%x` depends on all of its use.

Otherwise, if we have:
```
define internal ptr @test(ptr align 4 %x) {
  %p = tail call ptr @llvm.ptrmask.p0.i64(ptr %x, i64 -4)
  store float 1.0, ptr %p, align 8
  ret ptr %p
}
```
Then if `%x` is aligned at 4, it must not meet the requirement for the `store` instruction, so the alignment of `%x` should be at least 8. Same, for this use of `%x` it requires `%x`'s alignment is at least 8


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


More information about the llvm-commits mailing list