Skip to content

ObjCARCContract: Use stripPointerCastsAndAliases #134275

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

Conversation

arsenm
Copy link
Contributor

@arsenm arsenm commented Apr 3, 2025

No description provided.

@arsenm arsenm added the objective-c label Apr 3, 2025 — with Graphite App
@arsenm arsenm marked this pull request as ready for review April 3, 2025 16:53
@llvmbot
Copy link
Member

llvmbot commented Apr 3, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Matt Arsenault (arsenm)

Changes

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

1 Files Affected:

  • (modified) llvm/lib/Transforms/ObjCARC/ObjCARCContract.cpp (+10-24)
diff --git a/llvm/lib/Transforms/ObjCARC/ObjCARCContract.cpp b/llvm/lib/Transforms/ObjCARC/ObjCARCContract.cpp
index e11748b2c9dbb..ed7a235f5e2e3 100644
--- a/llvm/lib/Transforms/ObjCARC/ObjCARCContract.cpp
+++ b/llvm/lib/Transforms/ObjCARC/ObjCARCContract.cpp
@@ -661,30 +661,16 @@ bool ObjCARCContract::run(Function &F, AAResults *A, DominatorTree *D) {
 
     Value *Arg = cast<CallInst>(Inst)->getArgOperand(0);
 
-    // TODO: Change this to a do-while.
-    for (;;) {
-      ReplaceArgUses(Arg);
-
-      // If Arg is a no-op casted pointer, strip one level of casts and iterate.
-      if (const BitCastInst *BI = dyn_cast<BitCastInst>(Arg))
-        Arg = BI->getOperand(0);
-      else if (isa<GEPOperator>(Arg) &&
-               cast<GEPOperator>(Arg)->hasAllZeroIndices())
-        Arg = cast<GEPOperator>(Arg)->getPointerOperand();
-      else if (isa<GlobalAlias>(Arg) &&
-               !cast<GlobalAlias>(Arg)->isInterposable())
-        Arg = cast<GlobalAlias>(Arg)->getAliasee();
-      else {
-        // If Arg is a PHI node, get PHIs that are equivalent to it and replace
-        // their uses.
-        if (PHINode *PN = dyn_cast<PHINode>(Arg)) {
-          SmallVector<Value *, 1> PHIList;
-          getEquivalentPHIs(*PN, PHIList);
-          for (Value *PHI : PHIList)
-            ReplaceArgUses(PHI);
-        }
-        break;
-      }
+    ReplaceArgUses(Arg);
+
+    Arg = Arg->stripPointerCastsAndAliases();
+    // If Arg is a PHI node, get PHIs that are equivalent to it and replace
+    // their uses.
+    if (PHINode *PN = dyn_cast<PHINode>(Arg)) {
+      SmallVector<Value *, 1> PHIList;
+      getEquivalentPHIs(*PN, PHIList);
+      for (Value *PHI : PHIList)
+        ReplaceArgUses(PHI);
     }
   }
 

@arsenm arsenm force-pushed the users/arsenm/objc-arc-drop-pointer-bitcast-handling branch from 14866ad to f0c5f77 Compare April 7, 2025 16:56
@arsenm arsenm force-pushed the users/arsenm/objc-arc-contract-use-stripPointerCastsAndAliases branch from 94bafd4 to 981f435 Compare April 7, 2025 16:56
Base automatically changed from users/arsenm/objc-arc-drop-pointer-bitcast-handling to main April 8, 2025 00:23
@arsenm arsenm force-pushed the users/arsenm/objc-arc-contract-use-stripPointerCastsAndAliases branch from 981f435 to 6614a6b Compare April 8, 2025 00:24
@arsenm arsenm requested a review from fhahn April 8, 2025 11:30
Copy link
Contributor Author

@arsenm arsenm left a comment

Choose a reason for hiding this comment

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

ping

@ahatanak
Copy link
Collaborator

Is this supposed to be a NFC patch?

With the changes in this patch, the pass no longer rewrites the argument to the second call to use_pointer in the following IR:

define void @test0(ptr %x) {
  %y = getelementptr inbounds ptr, ptr %x, i32 0
  %v0 = call ptr @llvm.objc.retain(ptr %y) nounwind
  call void @use_pointer(ptr %x)
  call void @use_pointer(ptr %y)
  ret void
}

This is a contrived example and maybe it's okay if we know that clang/llvm never generates IR like this in practice. But I'd like to understand what assumptions you are making about the IR that's fed to this pass.

@ahatanak
Copy link
Collaborator

Correction: with the changes in this patch, the pass no longer rewrites the argument to the first call to use_pointer in the following IR:

@arsenm
Copy link
Contributor Author

arsenm commented Apr 11, 2025

Is this supposed to be a NFC patch?

Yes. This is code that's plainly reinventing a common helper function

With the changes in this patch, the pass no longer rewrites the argument to the second call to use_pointer in the following IR:

define void @test0(ptr %x) {
  %y = getelementptr inbounds ptr, ptr %x, i32 0
  %v0 = call ptr @llvm.objc.retain(ptr %y) nounwind
  call void @use_pointer(ptr %x)
  call void @use_pointer(ptr %y)
  ret void
}

Missing test coverage. I'm assuming this loop is not properly tested. Can you push this to fix the gaps?

This is a contrived example and maybe it's okay if we know that clang/llvm never generates IR like this in practice. But I'd like to understand what assumptions you are making about the IR that's fed to this pass.

My only assumption is the lit test suite is adequate to cover all of the functionality exercised in the code

@arsenm arsenm force-pushed the users/arsenm/objc-arc-contract-use-stripPointerCastsAndAliases branch from 6614a6b to 238597d Compare April 12, 2025 09:25
@arsenm arsenm force-pushed the users/arsenm/objc-arc-contract-use-stripPointerCastsAndAliases branch from 238597d to cfd2525 Compare April 13, 2025 10:16
@nikic
Copy link
Contributor

nikic commented Apr 13, 2025

I'm not convinced that the replacement with stripPointerCastsAndAliases() makes sense. Yes, this does something similar, but the important difference is that the current code performs the replacement at each instruction in the chain. Now you are only doing it at the first and last value in the chain, which is not the same.

arsenm added a commit that referenced this pull request Apr 13, 2025
@arsenm
Copy link
Contributor Author

arsenm commented Apr 13, 2025

Don't care about this to spend any time on it

@arsenm arsenm closed this Apr 13, 2025
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
@arsenm arsenm deleted the users/arsenm/objc-arc-contract-use-stripPointerCastsAndAliases branch April 25, 2025 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants