[clang] 7aef8b3 - [OpenMP] Make section variable external to prevent collisions
Joseph Huber via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 24 07:57:30 PST 2022
Author: Joseph Huber
Date: 2022-02-24T10:57:09-05:00
New Revision: 7aef8b3754a28e0856a8300980786a45bf7b4cd4
URL: https://github.com/llvm/llvm-project/commit/7aef8b3754a28e0856a8300980786a45bf7b4cd4
DIFF: https://github.com/llvm/llvm-project/commit/7aef8b3754a28e0856a8300980786a45bf7b4cd4.diff
LOG: [OpenMP] Make section variable external to prevent collisions
Summary:
We use a section to embed offloading code into the host for later
linking. This is normally unique to the translation unit as it is thrown
away during linking. However, if the user performs a relocatable link
the sections will be merged and we won't be able to access the files
stored inside. This patch changes the section variables to have external
linkage and a name defined by the section name, so if two sections are
combined during linking we get an error.
Added:
Modified:
clang/lib/CodeGen/BackendUtil.cpp
clang/test/Frontend/embed-object.ll
llvm/lib/Transforms/Utils/ModuleUtils.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index ebfbe991881c6..490f5b3de1ff3 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -1759,24 +1759,22 @@ void clang::EmbedObject(llvm::Module *M, const CodeGenOptions &CGOpts,
return;
for (StringRef OffloadObject : CGOpts.OffloadObjects) {
- if (OffloadObject.count(',') != 1) {
+ if (OffloadObject.count(',') != 1)
Diags.Report(Diags.getCustomDiagID(
DiagnosticsEngine::Error, "Invalid string pair for embedding '%0'"))
<< OffloadObject;
- return;
- }
auto FilenameAndSection = OffloadObject.split(',');
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ObjectOrErr =
- llvm::MemoryBuffer::getFileOrSTDIN(std::get<0>(FilenameAndSection));
+ llvm::MemoryBuffer::getFileOrSTDIN(FilenameAndSection.first);
if (std::error_code EC = ObjectOrErr.getError()) {
auto DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
"could not open '%0' for embedding");
- Diags.Report(DiagID) << std::get<0>(FilenameAndSection);
+ Diags.Report(DiagID) << FilenameAndSection.first;
return;
}
SmallString<128> SectionName(
- {".llvm.offloading.", std::get<1>(FilenameAndSection)});
+ {".llvm.offloading.", FilenameAndSection.second});
llvm::embedBufferInModule(*M, **ObjectOrErr, SectionName);
}
}
diff --git a/clang/test/Frontend/embed-object.ll b/clang/test/Frontend/embed-object.ll
index 49b477d4a7be0..3270a7806210b 100644
--- a/clang/test/Frontend/embed-object.ll
+++ b/clang/test/Frontend/embed-object.ll
@@ -3,8 +3,8 @@
; RUN: -fembed-offload-object=%S/Inputs/empty.h,section2 -x ir %s -o - \
; RUN: | FileCheck %s -check-prefix=CHECK
-; CHECK: @[[OBJECT1:.+]] = private constant [0 x i8] zeroinitializer, section ".llvm.offloading.section1"
-; CHECK: @[[OBJECT2:.+]] = private constant [0 x i8] zeroinitializer, section ".llvm.offloading.section2"
+; CHECK: @[[OBJECT1:.+]] = hidden constant [0 x i8] zeroinitializer, section ".llvm.offloading.section1"
+; CHECK: @[[OBJECT2:.+]] = hidden constant [0 x i8] zeroinitializer, section ".llvm.offloading.section2"
; CHECK: @llvm.compiler.used = appending global [3 x i8*] [i8* @x, i8* getelementptr inbounds ([0 x i8], [0 x i8]* @[[OBJECT1]], i32 0, i32 0), i8* getelementptr inbounds ([0 x i8], [0 x i8]* @[[OBJECT2]], i32 0, i32 0)], section "llvm.metadata"
@x = private constant i8 1
diff --git a/llvm/lib/Transforms/Utils/ModuleUtils.cpp b/llvm/lib/Transforms/Utils/ModuleUtils.cpp
index 61cf42db4a755..032ea80397977 100644
--- a/llvm/lib/Transforms/Utils/ModuleUtils.cpp
+++ b/llvm/lib/Transforms/Utils/ModuleUtils.cpp
@@ -271,9 +271,10 @@ void llvm::embedBufferInModule(Module &M, MemoryBufferRef Buf,
Constant *ModuleConstant = ConstantDataArray::get(
M.getContext(), makeArrayRef(Buf.getBufferStart(), Buf.getBufferSize()));
GlobalVariable *GV = new GlobalVariable(
- M, ModuleConstant->getType(), true, GlobalValue::PrivateLinkage,
- ModuleConstant, "llvm.embedded.object");
+ M, ModuleConstant->getType(), true, GlobalValue::ExternalLinkage,
+ ModuleConstant, SectionName.drop_front());
GV->setSection(SectionName);
+ GV->setVisibility(GlobalValue::HiddenVisibility);
appendToCompilerUsed(M, GV);
}
More information about the cfe-commits
mailing list