Skip to content

Commit aa17d19

Browse files
Implement GlobalObject::updateSectionPrefix and add a unit test
1 parent 0b9dc8d commit aa17d19

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

llvm/lib/IR/Globals.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,17 @@ void GlobalObject::setSectionPrefix(StringRef Prefix) {
293293
MDB.createGlobalObjectSectionPrefix(Prefix));
294294
}
295295

296+
void GlobalObject::updateSectionPrefix(StringRef Prefix,
297+
std::optional<StringRef> KeepPrefix) {
298+
auto SectionPrefix = getSectionPrefix();
299+
if (SectionPrefix && (*SectionPrefix == Prefix ||
300+
(KeepPrefix && *SectionPrefix == *KeepPrefix)))
301+
return;
302+
303+
setSectionPrefix(Prefix);
304+
return;
305+
}
306+
296307
std::optional<StringRef> GlobalObject::getSectionPrefix() const {
297308
if (MDNode *MD = getMetadata(LLVMContext::MD_section_prefix)) {
298309
[[maybe_unused]] StringRef MDName =

llvm/unittests/IR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ add_llvm_unittest(IRTests
2828
DominatorTreeBatchUpdatesTest.cpp
2929
DroppedVariableStatsIRTest.cpp
3030
FunctionTest.cpp
31+
GlobalObjectTest.cpp
3132
PassBuilderCallbacksTest.cpp
3233
IRBuilderTest.cpp
3334
InstructionsTest.cpp
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===- GlobalObjectTest.cpp - Global Object unit tests
2+
//-----------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "llvm-c/Core.h"
11+
#include "llvm/AsmParser/Parser.h"
12+
#include "llvm/IR/Function.h"
13+
#include "llvm/IR/Module.h"
14+
#include "llvm/Support/SourceMgr.h"
15+
#include "gtest/gtest.h"
16+
using namespace llvm;
17+
18+
namespace {
19+
20+
static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
21+
SMDiagnostic Err;
22+
std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
23+
if (!Mod)
24+
Err.print("GlobalObjectTest", errs());
25+
return Mod;
26+
}
27+
28+
} // namespace
29+
30+
TEST(GlobalObjectTest, updateSectionPrefix) {
31+
LLVMContext C;
32+
33+
std::unique_ptr<Module> M = parseIR(C, R"(
34+
@foo = global i32 1, !section_prefix !0
35+
@bar = global i32 2
36+
@baz = global i32 0, !section_prefix !1
37+
38+
!0 = !{!"section_prefix", !"hot"}
39+
!1 = !{!"section_prefix", !"unlikely"}
40+
)");
41+
42+
GlobalVariable *Foo = M->getGlobalVariable("foo");
43+
EXPECT_EQ(Foo->getSectionPrefix(), "hot");
44+
Foo->updateSectionPrefix("unlikely", std::make_optional(StringRef("hot")));
45+
EXPECT_EQ(Foo->getSectionPrefix(), "hot");
46+
47+
GlobalVariable *Bar = M->getGlobalVariable("bar");
48+
EXPECT_EQ(Bar->getSectionPrefix(), std::nullopt);
49+
Bar->setSectionPrefix("unlikely");
50+
EXPECT_EQ(Bar->getSectionPrefix(), "unlikely");
51+
52+
GlobalVariable *Baz = M->getGlobalVariable("baz");
53+
EXPECT_EQ(Baz->getSectionPrefix(), "unlikely");
54+
Baz->updateSectionPrefix("hot");
55+
EXPECT_EQ(Baz->getSectionPrefix(), "hot");
56+
}

0 commit comments

Comments
 (0)