[llvm-branch-commits] [NFC][ModuleUtils] Test appendToGlobalCtors/appendToGlobalDtors (PR #101753)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Aug 2 14:14:02 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Vitaly Buka (vitalybuka)

<details>
<summary>Changes</summary>



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


1 Files Affected:

- (modified) llvm/unittests/Transforms/Utils/ModuleUtilsTest.cpp (+55) 


``````````diff
diff --git a/llvm/unittests/Transforms/Utils/ModuleUtilsTest.cpp b/llvm/unittests/Transforms/Utils/ModuleUtilsTest.cpp
index faca3bc03f120..5e712ca223628 100644
--- a/llvm/unittests/Transforms/Utils/ModuleUtilsTest.cpp
+++ b/llvm/unittests/Transforms/Utils/ModuleUtilsTest.cpp
@@ -9,6 +9,7 @@
 #include "llvm/Transforms/Utils/ModuleUtils.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/AsmParser/Parser.h"
+#include "llvm/IR/Constants.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/Support/SourceMgr.h"
@@ -67,3 +68,57 @@ TEST(ModuleUtils, AppendToUsedList2) {
   appendToUsed(*M, Globals);
   EXPECT_EQ(1, getListSize(*M, "llvm.used"));
 }
+
+using ParamType = std::pair<StringRef, decltype(&appendToGlobalCtors)>;
+class ModuleUtilsTest : public testing::TestWithParam<ParamType> {};
+
+INSTANTIATE_TEST_SUITE_P(
+    ModuleUtilsTestCtors, ModuleUtilsTest,
+    ::testing::Values(ParamType{"llvm.global_ctors", &appendToGlobalCtors},
+                      ParamType{"llvm.global_dtors", &appendToGlobalDtors}));
+
+TEST_P(ModuleUtilsTest, AppendToMissingArray) {
+  auto [ArrayName, Fn] = GetParam();
+
+  LLVMContext C;
+
+  std::unique_ptr<Module> M = parseIR(C, "");
+
+  EXPECT_EQ(0, getListSize(*M, ArrayName));
+  Function *F = cast<Function>(
+      M->getOrInsertFunction("ctor", Type::getVoidTy(C)).getCallee());
+  Fn(*M, F, 11, F);
+  ASSERT_EQ(1, getListSize(*M, ArrayName));
+
+  ConstantArray *CA = dyn_cast<ConstantArray>(
+      M->getGlobalVariable(ArrayName)->getInitializer());
+  ASSERT_NE(nullptr, CA);
+  ConstantStruct *CS = dyn_cast<ConstantStruct>(CA->getOperand(0));
+  ASSERT_NE(nullptr, CS);
+  ConstantInt * Pri = dyn_cast<ConstantInt>(CS->getOperand(0));
+  ASSERT_NE(nullptr, Pri);
+  EXPECT_EQ(11u, Pri->getLimitedValue());
+  EXPECT_EQ(F, dyn_cast<Function>(CS->getOperand(1)));
+  EXPECT_EQ(F, CS->getOperand(2));
+}
+
+TEST_P(ModuleUtilsTest, AppendToArray) {
+  auto [ArrayName, Fn] = GetParam();
+
+  LLVMContext C;
+
+  std::unique_ptr<Module> M = parseIR(
+      C, (R"(@)" + ArrayName + R"( = appending global [2 x { i32, ptr, ptr }] [
+            { i32, ptr, ptr } { i32 65535, ptr  null, ptr null },
+            { i32, ptr, ptr } { i32 0, ptr  null, ptr null }]
+      )")
+             .str()
+             .c_str());
+
+  EXPECT_EQ(2, getListSize(*M, ArrayName));
+  Fn(*M,
+     cast<Function>(
+         M->getOrInsertFunction("ctor", Type::getVoidTy(C)).getCallee()),
+     11, nullptr);
+  EXPECT_EQ(3, getListSize(*M, ArrayName));
+}

``````````

</details>


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


More information about the llvm-branch-commits mailing list