Skip to content

[AA] Assert that alias() arguments are pointers #138242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2025
Merged

Conversation

nikic
Copy link
Contributor

@nikic nikic commented May 2, 2025

Assert instead of returning NoAlias for non-pointers. This makes sure that people don't confuse alias (working on locations) with getModRefInfo (working on instructions).

Assert instead of returning NoAlias for non-pointers. This makes
sure that people don't confused alias (working on locations) with
getModRefInfo (working on instructions).
@nikic nikic requested review from fhahn and dtcxzyw May 2, 2025 09:05
@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label May 2, 2025
@llvmbot
Copy link
Member

llvmbot commented May 2, 2025

@llvm/pr-subscribers-llvm-analysis

Author: Nikita Popov (nikic)

Changes

Assert instead of returning NoAlias for non-pointers. This makes sure that people don't confuse alias (working on locations) with getModRefInfo (working on instructions).


Full diff: https://github.com/llvm/llvm-project/pull/138242.diff

2 Files Affected:

  • (modified) llvm/lib/Analysis/AliasAnalysis.cpp (+3)
  • (modified) llvm/lib/Analysis/BasicAliasAnalysis.cpp (-3)
diff --git a/llvm/lib/Analysis/AliasAnalysis.cpp b/llvm/lib/Analysis/AliasAnalysis.cpp
index efabf69b06047..f4946c30de9bc 100644
--- a/llvm/lib/Analysis/AliasAnalysis.cpp
+++ b/llvm/lib/Analysis/AliasAnalysis.cpp
@@ -110,6 +110,9 @@ AliasResult AAResults::alias(const MemoryLocation &LocA,
 AliasResult AAResults::alias(const MemoryLocation &LocA,
                              const MemoryLocation &LocB, AAQueryInfo &AAQI,
                              const Instruction *CtxI) {
+  assert(LocA.Ptr->getType()->isPointerTy() &&
+         LocB.Ptr->getType()->isPointerTy() &&
+         "Can only call alias() on pointers");
   AliasResult Result = AliasResult::MayAlias;
 
   if (EnableAATrace) {
diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
index a46edc0b75f54..2de9bb502baf4 100644
--- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -1574,9 +1574,6 @@ AliasResult BasicAAResult::aliasCheck(const Value *V1, LocationSize V1Size,
   if (isValueEqualInPotentialCycles(V1, V2, AAQI))
     return AliasResult::MustAlias;
 
-  if (!V1->getType()->isPointerTy() || !V2->getType()->isPointerTy())
-    return AliasResult::NoAlias; // Scalars cannot alias each other
-
   // Figure out what objects these things are pointing to if we can.
   const Value *O1 = getUnderlyingObject(V1, MaxLookupSearchDepth);
   const Value *O2 = getUnderlyingObject(V2, MaxLookupSearchDepth);

Copy link
Member

@dtcxzyw dtcxzyw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks.

might be good to also update the doc comment, if it isn’t already

@nikic nikic merged commit 3416d4f into llvm:main May 5, 2025
13 checks passed
@nikic nikic deleted the aa-pointers branch May 5, 2025 10:08
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Assert instead of returning NoAlias for non-pointers. This makes sure
that people don't confuse alias (working on locations) with
getModRefInfo (working on instructions).
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Assert instead of returning NoAlias for non-pointers. This makes sure
that people don't confuse alias (working on locations) with
getModRefInfo (working on instructions).
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Assert instead of returning NoAlias for non-pointers. This makes sure
that people don't confuse alias (working on locations) with
getModRefInfo (working on instructions).
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
Assert instead of returning NoAlias for non-pointers. This makes sure
that people don't confuse alias (working on locations) with
getModRefInfo (working on instructions).
@slackito
Copy link
Collaborator

Hi, I'm debugging a problem after this commit with one of the Enzyme (https://github.com/EnzymeAD/) tests.

There's some code that doesn't call alias directly:

        if (llvm::isModSet(AA.getModRefInfo(
                inst2, MemoryLocation::getForArgument(callsite_op, i, TLI)))) {
                    ...

the call to getModRefInfo ends up here:

  if (Loc.Ptr) {
    AliasResult AR = alias(MemoryLocation::get(S), Loc, AAQI, S);

and running the test case under lldb, Loc.Ptr is not null (so the check passes) but doesn't have pointer type (so the assert inside alias() fails):

(lldb) p Loc.Ptr
(const llvm::Value *const) 0x00005092ffe57460
(lldb) p Loc.Ptr->getType()->dump()
i32

should the check in getModRefInfo be more strict and also check the type of Loc.Ptr before calling alias()?

@nikic
Copy link
Contributor Author

nikic commented May 13, 2025

@slackito Most likely the code is calling MemoryLocation::getForArgument on a non-pointer argument.

@slackito
Copy link
Collaborator

Thanks for the advice! I'll fix it on my end then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:analysis Includes value tracking, cost tables and constant folding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants