[llvm] [NFCI][Globals]For GlobalObjects, add updateSectionPrefix and change setSectionPrefix to handle empty strings (PR #158460)
Mingming Liu via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 15 15:52:38 PDT 2025
================
@@ -0,0 +1,76 @@
+//===- GlobalObjectTest.cpp - Global object unit tests --------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/GlobalObject.h"
+#include "llvm-c/Core.h"
+#include "llvm/AsmParser/Parser.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Support/SourceMgr.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+using namespace llvm;
+namespace {
+using testing::Eq;
+using testing::Optional;
+using testing::StrEq;
+
+static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
+ SMDiagnostic Err;
+ std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
+ if (!Mod)
+ Err.print("GlobalObjectTests", errs());
+ return Mod;
+}
+
+static LLVMContext C;
+static std::unique_ptr<Module> M;
+
+class GlobalObjectTest : public testing::Test {
+public:
+ static void SetUpTestSuite() {
+ M = parseIR(C, R"(
+ at foo = global i32 3, !section_prefix !0
+ at bar = global i32 0
+
+!0 = !{!"section_prefix", !"hot"}
+)");
+ }
+};
+
+TEST_F(GlobalObjectTest, SectionPrefix) {
+ GlobalVariable *Foo = M->getGlobalVariable("foo");
+
+ // Initial section prefix is hot.
+ ASSERT_NE(Foo, nullptr);
+ ASSERT_THAT(Foo->getSectionPrefix(), Optional(StrEq("hot")));
+
+ // No actual update.
+ EXPECT_FALSE(Foo->updateSectionPrefix("hot"));
+
+ // Update prefix from hot to unlikely.
+ Foo->setSectionPrefix("unlikely");
+ EXPECT_THAT(Foo->getSectionPrefix(), Optional(StrEq("unlikely")));
+
+ // Update prefix to empty is the same as clear.
+ Foo->setSectionPrefix("");
+ EXPECT_THAT(Foo->getSectionPrefix(), Eq(std::nullopt));
+
+ GlobalVariable *Bar = M->getGlobalVariable("bar");
+
+ // Initial section prefix is empty.
+ ASSERT_NE(Bar, nullptr);
+ ASSERT_THAT(Bar->getSectionPrefix(), Eq(std::nullopt));
+
+ // No actual update.
+ EXPECT_FALSE(Bar->updateSectionPrefix(""));
----------------
mingmingl-llvm wrote:
> updateSectionPrefix has no impact since there is not section prefix metadata.
Correct, and this line means to check the return value of 'update' is false.
> Perhaps this check is more meaningful after L74?
I added two more `EXPECT` after L74, to test that update to non-empty prefix works.
> Similar to the check on L60.
Not sure if I follow this one. The check around L60 tests the effect of 'setSectionPrefix` to empty, after the unlikely check around L56. Let me know if it'd be better to have some other test coverage for 'Foo' above.
https://github.com/llvm/llvm-project/pull/158460
More information about the llvm-commits
mailing list