[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.
This commit is contained in:
Joseph Huber 2022-02-24 10:24:39 -05:00
parent 5379f76e63
commit 7aef8b3754
3 changed files with 9 additions and 10 deletions

View File

@ -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);
}
}

View File

@ -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

View File

@ -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);
}