Skip to content

Add unit tests for getUnderlyingObject #89120

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

Closed
wants to merge 1 commit into from

Conversation

MatzeB
Copy link
Contributor

@MatzeB MatzeB commented Apr 17, 2024

No description provided.

@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Apr 17, 2024
@llvmbot
Copy link
Member

llvmbot commented Apr 17, 2024

@llvm/pr-subscribers-llvm-analysis

Author: Matthias Braun (MatzeB)

Changes

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

1 Files Affected:

  • (modified) llvm/unittests/Analysis/ValueTrackingTest.cpp (+72)
diff --git a/llvm/unittests/Analysis/ValueTrackingTest.cpp b/llvm/unittests/Analysis/ValueTrackingTest.cpp
index 8738af91b652b8..c59ed041373001 100644
--- a/llvm/unittests/Analysis/ValueTrackingTest.cpp
+++ b/llvm/unittests/Analysis/ValueTrackingTest.cpp
@@ -1247,6 +1247,78 @@ TEST_F(ValueTrackingTest, computePtrAlignment) {
   EXPECT_EQ(getKnownAlignment(A, DL, CxtI3, &AC, &DT), Align(16));
 }
 
+TEST_F(ValueTrackingTest, getUnderlyingObjectCastsAliases) {
+  parseAssembly(R"IR(
+    @gvar = global i32 0
+    @alias = alias i32, i32* @gvar
+    @alias_interposable = weak alias i32, i32* @gvar
+    define void @test() {
+      %A = call ptr @llvm.ssa.copy.p0(ptr @gvar)
+      %A2 = call ptr @llvm.ssa.copy.p0(ptr bitcast(ptr @gvar to ptr))
+      %A3 = call ptr addrspace(42) @llvm.ssa.copy.p42(ptr addrspace(42) addrspacecast(ptr @gvar to ptr addrspace(42)))
+      %A4 = call ptr @llvm.ssa.copy.p0(ptr @alias)
+      %A5 = call ptr @llvm.ssa.copy.p0(ptr @alias_interposable)
+      ret void
+    }
+)IR");
+  Value *gvar = A->getOperand(0);
+  EXPECT_EQ(getUnderlyingObject(A->getOperand(0)), gvar);
+  EXPECT_EQ(getUnderlyingObject(A2->getOperand(0)), gvar);
+  EXPECT_EQ(getUnderlyingObject(A3->getOperand(0)), gvar);
+  EXPECT_EQ(getUnderlyingObject(A4->getOperand(0)), gvar);
+  EXPECT_EQ(getUnderlyingObject(A4->getOperand(0)), gvar);
+  // should not skip interposable alias
+  EXPECT_EQ(getUnderlyingObject(A5->getOperand(0)), A5->getOperand(0));
+}
+
+TEST_F(ValueTrackingTest, getUnderlyingObjectIntrinsics) {
+  parseAssembly(R"IR(
+    define void @test(ptr %arg) {
+      ; intrinsic with Return<> arg attribute
+      %A = call ptr @llvm.objc.retain(ptr %arg)
+      %A2 = call ptr @llvm.ssa.copy(ptr %arg)
+      ; special cased intrinsics
+      %A3 = call ptr @llvm.launder.invariant.group(ptr %arg)
+      ret void
+    }
+)IR");
+  Value *arg = F->getArg(0);
+  EXPECT_EQ(getUnderlyingObject(A), arg);
+  EXPECT_EQ(getUnderlyingObject(A2), arg);
+  EXPECT_EQ(getUnderlyingObject(A3), arg);
+}
+
+TEST_F(ValueTrackingTest, getUnderlyingObjectPtrInt) {
+  parseAssembly(R"IR(
+    define void @test(i64 %arg, ptr %arg1) {
+      %A = inttoptr i64 %arg to ptr
+      %t0 = ptrtoint ptr %arg1 to i64
+      %A2 = inttoptr i64 %t0 to ptr
+      ret void
+    }
+)IR");
+  // Should not skip anything here
+  EXPECT_EQ(getUnderlyingObject(A->getOperand(0)), A->getOperand(0));
+  EXPECT_EQ(getUnderlyingObject(A2->getOperand(0)), A2->getOperand(0));
+}
+
+TEST_F(ValueTrackingTest, getUnderlyingObjectPhi) {
+  parseAssembly(R"IR(
+    @gvar = global i32 0
+    define void @test() {
+    entry:
+      %A = call ptr @llvm.ssa.copy.p0(ptr @gvar)
+      br label %block2
+
+    block2:
+      %A2 = phi ptr [ @gvar, %entry ]
+      ret void
+    }
+)IR");
+  Value *gvar = A->getOperand(0);
+  EXPECT_EQ(getUnderlyingObject(A2->getOperand(0)), gvar);
+}
+
 TEST_F(ComputeKnownBitsTest, ComputeKnownBits) {
   parseAssembly(
       "define i32 @test(i32 %a, i32 %b) {\n"

@MatzeB
Copy link
Contributor Author

MatzeB commented Apr 17, 2024

Would be good to have some unittests for getUnderlyingObject; that way I can easily add a new one in #88418

@MatzeB MatzeB changed the title Add some tests for getUnderlyingObject Add unit tests for getUnderlyingObject Apr 17, 2024
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

TBH I'd rather not have (IR) unit tests unless lit tests are uniquely hard to write for some reason. They are very annoying to update if they ever break. getUnderlyingObject() can be (and is) tested through its impact on alias analysis etc.

@MatzeB
Copy link
Contributor Author

MatzeB commented Apr 18, 2024

No problem, this was mostly because I somehow expected reviewers to ask for more tests. Will gladly drop them.

@MatzeB MatzeB closed this Apr 18, 2024
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.

3 participants