aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/IR/ConstantsTest.cpp
diff options
context:
space:
mode:
authorMatt Arsenault <Matthew.Arsenault@amd.com>2025-05-06 17:23:59 +0200
committerGitHub <noreply@github.com>2025-05-06 17:23:59 +0200
commit51a3bd919d68a8fb1b026377d6e86b1523d37433 (patch)
treed91abafdcc9b31d730a28fbbc5d4a33b5f57d6ac /llvm/unittests/IR/ConstantsTest.cpp
parent87f312aad6ede636cd2de5d18f3058bf2caf5651 (diff)
downloadllvm-51a3bd919d68a8fb1b026377d6e86b1523d37433.zip
llvm-51a3bd919d68a8fb1b026377d6e86b1523d37433.tar.gz
llvm-51a3bd919d68a8fb1b026377d6e86b1523d37433.tar.bz2
IR: Remove reference counts from ConstantData (#137314)
This is a follow up change to eliminating uselists for ConstantData. In the previous revision, ConstantData had a replacement reference count instead of a uselist. This reference count was misleading, and not useful in the same way as it would be for another value. The references may not have even been in the current module, since these are shared throughout the LLVMContext. This doesn't space leak any more than we previously did; nothing was attempting to garbage collect unused constants. Previously the use_empty, and hasNUses type of APIs were supported through the reference count. These now behave as if the uses are always empty. Ideally it would be illegal to inspect these, but this forces API complexity into quite a few places. It may be doable to make it illegal to check these counts, but I would like there to be a targeted fuzzing effort to make sure every transform properly deals with a constant in every operand position. All tests pass if I turn the hasNUses* and getNumUses queries into assertions, only hasOneUse in particular appears to hit in some set of contexts. I've added unit tests to ensure logical consistency between these cases
Diffstat (limited to 'llvm/unittests/IR/ConstantsTest.cpp')
-rw-r--r--llvm/unittests/IR/ConstantsTest.cpp38
1 files changed, 38 insertions, 0 deletions
diff --git a/llvm/unittests/IR/ConstantsTest.cpp b/llvm/unittests/IR/ConstantsTest.cpp
index a46178a..41cc212 100644
--- a/llvm/unittests/IR/ConstantsTest.cpp
+++ b/llvm/unittests/IR/ConstantsTest.cpp
@@ -21,6 +21,44 @@
namespace llvm {
namespace {
+// Check that use count checks treat ConstantData like they have no uses.
+TEST(ConstantsTest, UseCounts) {
+ LLVMContext Context;
+ Type *Int32Ty = Type::getInt32Ty(Context);
+ Constant *Zero = ConstantInt::get(Int32Ty, 0);
+
+ EXPECT_TRUE(Zero->use_empty());
+ EXPECT_EQ(Zero->getNumUses(), 0u);
+ EXPECT_TRUE(Zero->hasNUses(0));
+ EXPECT_FALSE(Zero->hasOneUse());
+ EXPECT_FALSE(Zero->hasOneUser());
+ EXPECT_FALSE(Zero->hasNUses(1));
+ EXPECT_FALSE(Zero->hasNUsesOrMore(1));
+ EXPECT_FALSE(Zero->hasNUses(2));
+ EXPECT_FALSE(Zero->hasNUsesOrMore(2));
+
+ std::unique_ptr<Module> M(new Module("MyModule", Context));
+
+ // Introduce some uses
+ new GlobalVariable(*M, Int32Ty, /*isConstant=*/false,
+ GlobalValue::ExternalLinkage, /*Initializer=*/Zero,
+ "gv_user0");
+ new GlobalVariable(*M, Int32Ty, /*isConstant=*/false,
+ GlobalValue::ExternalLinkage, /*Initializer=*/Zero,
+ "gv_user1");
+
+ // Still looks like use_empty with uses.
+ EXPECT_TRUE(Zero->use_empty());
+ EXPECT_EQ(Zero->getNumUses(), 0u);
+ EXPECT_TRUE(Zero->hasNUses(0));
+ EXPECT_FALSE(Zero->hasOneUse());
+ EXPECT_FALSE(Zero->hasOneUser());
+ EXPECT_FALSE(Zero->hasNUses(1));
+ EXPECT_FALSE(Zero->hasNUsesOrMore(1));
+ EXPECT_FALSE(Zero->hasNUses(2));
+ EXPECT_FALSE(Zero->hasNUsesOrMore(2));
+}
+
TEST(ConstantsTest, Integer_i1) {
LLVMContext Context;
IntegerType *Int1 = IntegerType::get(Context, 1);