-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[IR] Generalize Function's {set,get}SectionPrefix to GlobalObjects, the base class of {Function, GlobalVariable, IFunc} #125757
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
Changes from 3 commits
0b9dc8d
aa17d19
39fe9af
bc54eb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| #include "llvm/IR/GlobalAlias.h" | ||
| #include "llvm/IR/GlobalValue.h" | ||
| #include "llvm/IR/GlobalVariable.h" | ||
| #include "llvm/IR/MDBuilder.h" | ||
| #include "llvm/IR/Module.h" | ||
| #include "llvm/Support/Error.h" | ||
| #include "llvm/Support/ErrorHandling.h" | ||
|
|
@@ -286,6 +287,35 @@ void GlobalObject::setSection(StringRef S) { | |
| setGlobalObjectFlag(HasSectionHashEntryBit, !S.empty()); | ||
| } | ||
|
|
||
| void GlobalObject::setSectionPrefix(StringRef Prefix) { | ||
| MDBuilder MDB(getContext()); | ||
| setMetadata(LLVMContext::MD_section_prefix, | ||
| MDB.createGlobalObjectSectionPrefix(Prefix)); | ||
| } | ||
|
|
||
| void GlobalObject::updateSectionPrefix(StringRef Prefix, | ||
|
||
| std::optional<StringRef> KeepPrefix) { | ||
| auto SectionPrefix = getSectionPrefix(); | ||
| if (SectionPrefix && (*SectionPrefix == Prefix || | ||
| (KeepPrefix && *SectionPrefix == *KeepPrefix))) | ||
| return; | ||
|
|
||
| setSectionPrefix(Prefix); | ||
| return; | ||
| } | ||
|
|
||
| std::optional<StringRef> GlobalObject::getSectionPrefix() const { | ||
| if (MDNode *MD = getMetadata(LLVMContext::MD_section_prefix)) { | ||
| [[maybe_unused]] StringRef MDName = | ||
| cast<MDString>(MD->getOperand(0))->getString(); | ||
| assert((MDName == "section_prefix" || | ||
| (isa<Function>(this) && MDName == "function_section_prefix")) && | ||
| "Metadata not match"); | ||
| return cast<MDString>(MD->getOperand(1))->getString(); | ||
| } | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| bool GlobalValue::isNobuiltinFnDef() const { | ||
| const Function *F = dyn_cast<Function>(this); | ||
| if (!F || F->empty()) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| ; RUN: llc -mtriple x86_64-linux-gnu -data-sections %s -o - | FileCheck %s --check-prefix=ELF | ||
| ; RUN: llc -mtriple x86_64-linux-gnu -unique-section-names=0 -data-sections %s -o - | FileCheck %s --check-prefix=ELF-NOUNIQ | ||
|
|
||
| ; RUN: llc -mtriple x86_64-windows-msvc -data-sections %s -o - | FileCheck %s --check-prefix=COFF-MSVC | ||
|
|
||
| ; ELF: .section .data.hot.foo, | ||
| ; ELF: .section .data.bar, | ||
| ; ELF: .section .bss.unlikely.baz, | ||
| ; ELF: .section .bss.quz, | ||
|
|
||
| ; ELF-NOUNIQ: .section .data.hot.,"aw",@progbits,unique,1 | ||
| ; ELF-NOUNIQ: .section .data,"aw",@progbits,unique,2 | ||
| ; ELF-NOUNIQ: .section .bss.unlikely.,"aw",@nobits,unique,3 | ||
| ; ELF-NOUNIQ: .section .bss,"aw",@nobits,unique,4 | ||
|
|
||
| ; COFF-MSVC: .section .data,"dw",one_only,foo | ||
| ; COFF-MSVC: .section .data,"dw",one_only,bar | ||
| ; COFF-MSVC: .section .bss,"bw",one_only,baz | ||
| ; COFF-MSVC: .section .bss,"bw",one_only,quz | ||
|
|
||
| @foo = global i32 1, !section_prefix !0 | ||
| @bar = global i32 2 | ||
| @baz = global i32 0, !section_prefix !1 | ||
| @quz = global i32 0 | ||
|
|
||
| !0 = !{!"section_prefix", !"hot"} | ||
| !1 = !{!"section_prefix", !"unlikely"} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| //===- 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-c/Core.h" | ||
| #include "llvm/AsmParser/Parser.h" | ||
| #include "llvm/IR/Function.h" | ||
| #include "llvm/IR/Module.h" | ||
| #include "llvm/Support/SourceMgr.h" | ||
| #include "gtest/gtest.h" | ||
| using namespace llvm; | ||
|
|
||
| namespace { | ||
|
|
||
| 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("GlobalObjectTest", errs()); | ||
| return Mod; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| TEST(GlobalObjectTest, updateSectionPrefix) { | ||
| LLVMContext C; | ||
|
|
||
| std::unique_ptr<Module> M = parseIR(C, R"( | ||
| @foo = global i32 1, !section_prefix !0 | ||
| @bar = global i32 2 | ||
| @baz = global i32 0, !section_prefix !1 | ||
|
|
||
| !0 = !{!"section_prefix", !"hot"} | ||
| !1 = !{!"section_prefix", !"unlikely"} | ||
| )"); | ||
|
|
||
| GlobalVariable *Foo = M->getGlobalVariable("foo"); | ||
| EXPECT_EQ(Foo->getSectionPrefix(), "hot"); | ||
| Foo->updateSectionPrefix("unlikely", std::make_optional(StringRef("hot"))); | ||
| EXPECT_EQ(Foo->getSectionPrefix(), "hot"); | ||
|
|
||
| GlobalVariable *Bar = M->getGlobalVariable("bar"); | ||
| EXPECT_EQ(Bar->getSectionPrefix(), std::nullopt); | ||
| Bar->setSectionPrefix("unlikely"); | ||
| EXPECT_EQ(Bar->getSectionPrefix(), "unlikely"); | ||
|
|
||
| GlobalVariable *Baz = M->getGlobalVariable("baz"); | ||
| EXPECT_EQ(Baz->getSectionPrefix(), "unlikely"); | ||
| Baz->updateSectionPrefix("hot"); | ||
| EXPECT_EQ(Baz->getSectionPrefix(), "hot"); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.