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

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 25 19:58:10 PDT 2025


================
@@ -5187,6 +5187,58 @@ struct AADereferenceableCallSiteReturned final
 // ------------------------ Align Argument Attribute ------------------------
 
 namespace {
+
+Align getKnownAlignForIntrinsic(Attributor &A, AAAlign &QueryingAA,
+                                const IntrinsicInst &II) {
+  switch (II.getIntrinsicID()) {
+  case Intrinsic::ptrmask: {
+    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(), (unsigned)63);
+      Align ConstAlign(UINT64_C(1) << ShiftValue);
+      if (ConstAlign >= AlignAA->getKnownAlign())
+        return Align(1);
+    }
+    if (AlignAA)
+      return AlignAA->getKnownAlign();
+    break;
+  }
+  default:
+    break;
+  }
+  return Align(1);
+}
+
+Align getAssumedAlignForIntrinsic(Attributor &A, AAAlign &QueryingAA,
----------------
Shoreshen wrote:

Hi @arsenm , one is for known and one is for assumed.

Known is the maximum alignment that is required from its use. For example:
```
define ptr @test(ptr %x) {
  %p = load ptr, ptr %x, align 8
  ret ptr %p
}
```
Since `load` require `%x` to align at 8, the alignment of `%x` is at least 8.

Assumed is the minimum alignment calculated from source. For example:
```
define ptr @test(i1 %cmp, ptr align 8 %x, ptr align 16 %y) {
  %p = select i1 %cmp, ptr %x, ptr %y
  %r = load ptr, ptr %p
  ret ptr %r
}
```
The alignment from `%p` should be 8 as calculated from `%x` and `%y`.

The final alignment will be max of assumed and known.

Back to `%p = llvm.ptrmask(%x, %mask)`, the first is to calculate the known alignment for `%x`, the second is used to calculate the assumed alignment for `%p`. They have different functionalities.

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


More information about the llvm-commits mailing list