Skip to content

Commit f7b3559

Browse files
authored
[clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images (llvm#125737)
Add manual ELF packaging for `spirv64-intel` images as there is no SPIR-V linker available. This format will be expected by the runtime plugin we will submit in the future and is compatible with the format we already use downstream. --------- Signed-off-by: Sarnie, Nick <[email protected]>
1 parent 553f8e7 commit f7b3559

File tree

7 files changed

+131
-0
lines changed

7 files changed

+131
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Verify the ELF packaging of OpenMP SPIR-V device images.
2+
// REQUIRES: system-linux
3+
// REQUIRES: spirv-tools
4+
// RUN: mkdir -p %t_tmp
5+
// RUN: cd %t_tmp
6+
// RUN: %clangxx -fopenmp -fopenmp-targets=spirv64-intel -nogpulib -c -o %t_clang-linker-wrapper-spirv-elf.o %s
7+
// RUN: not clang-linker-wrapper -o a.out %t_clang-linker-wrapper-spirv-elf.o --save-temps --linker-path=ld
8+
// RUN: clang-offload-packager --image=triple=spirv64-intel,kind=openmp,file=%t.elf %t_tmp/a.out.openmp.image.wrapper.o
9+
// RUN: llvm-readelf -t %t.elf | FileCheck -check-prefix=CHECK-SECTION %s
10+
// RUN: llvm-readelf -n %t.elf | FileCheck -check-prefix=CHECK-NOTES %s
11+
12+
// CHECK-SECTION: .note.inteloneompoffload
13+
// CHECK-SECTION: __openmp_offload_spirv_0
14+
15+
// CHECK-NOTES-COUNT-3: INTELONEOMPOFFLOAD
16+
int main(int argc, char** argv) {
17+
return 0;
18+
}

clang/test/Tooling/lit.local.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
if not config.root.clang_staticanalyzer:
22
config.unsupported = True
3+
4+
if config.spirv_tools_tests:
5+
config.available_features.add("spirv-tools")
6+
config.substitutions.append(("spirv-dis", os.path.join(config.llvm_tools_dir, "spirv-dis")))
7+
config.substitutions.append(("spirv-val", os.path.join(config.llvm_tools_dir, "spirv-val")))
8+
config.substitutions.append(("spirv-as", os.path.join(config.llvm_tools_dir, "spirv-as")))

clang/test/lit.site.cfg.py.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ config.llvm_external_lit = path(r"@LLVM_EXTERNAL_LIT@")
4343
config.standalone_build = @CLANG_BUILT_STANDALONE@
4444
config.ppc_linux_default_ieeelongdouble = @PPC_LINUX_DEFAULT_IEEELONGDOUBLE@
4545
config.have_llvm_driver = @LLVM_TOOL_LLVM_DRIVER_BUILD@
46+
config.spirv_tools_tests = "@LLVM_INCLUDE_SPIRV_TOOLS_TESTS@"
4647
config.substitutions.append(("%llvm-version-major", "@LLVM_VERSION_MAJOR@"))
4748

4849
import lit.llvm

clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,15 @@ Expected<StringRef> linkDevice(ArrayRef<StringRef> InputFiles,
605605
}
606606
}
607607

608+
Error containerizeRawImage(std::unique_ptr<MemoryBuffer> &Img, OffloadKind Kind,
609+
const ArgList &Args) {
610+
llvm::Triple Triple(Args.getLastArgValue(OPT_triple_EQ));
611+
if (Kind != OFK_OpenMP || !Triple.isSPIRV() ||
612+
Triple.getVendor() != llvm::Triple::Intel)
613+
return Error::success();
614+
return offloading::intel::containerizeOpenMPSPIRVImage(Img);
615+
}
616+
608617
Expected<StringRef> writeOffloadFile(const OffloadFile &File) {
609618
const OffloadBinary &Binary = *File.getBinary();
610619

@@ -957,6 +966,10 @@ Expected<SmallVector<StringRef>> linkAndWrapDeviceFiles(
957966
return createFileError(*OutputOrErr, EC);
958967
}
959968

969+
// Manually containerize offloading images not in ELF format.
970+
if (Error E = containerizeRawImage(*FileOrErr, Kind, LinkerArgs))
971+
return E;
972+
960973
std::scoped_lock<decltype(ImageMtx)> Guard(ImageMtx);
961974
OffloadingImage TheImage{};
962975
TheImage.TheImageKind =

llvm/include/llvm/Frontend/Offloading/Utility.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_FRONTEND_OFFLOADING_UTILITY_H
1111

1212
#include <cstdint>
13+
#include <memory>
1314

1415
#include "llvm/ADT/StringMap.h"
1516
#include "llvm/ADT/StringRef.h"
@@ -152,6 +153,12 @@ Error getAMDGPUMetaDataFromImage(MemoryBufferRef MemBuffer,
152153
StringMap<AMDGPUKernelMetaData> &KernelInfoMap,
153154
uint16_t &ELFABIVersion);
154155
} // namespace amdgpu
156+
157+
namespace intel {
158+
/// Containerizes an offloading binary into the ELF binary format expected by
159+
/// the Intel runtime offload plugin.
160+
Error containerizeOpenMPSPIRVImage(std::unique_ptr<MemoryBuffer> &Binary);
161+
} // namespace intel
155162
} // namespace offloading
156163
} // namespace llvm
157164

llvm/lib/Frontend/Offloading/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_llvm_component_library(LLVMFrontendOffloading
1212
Core
1313
BinaryFormat
1414
Object
15+
ObjectYAML
1516
Support
1617
TransformUtils
1718
TargetParser

llvm/lib/Frontend/Offloading/Utility.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "llvm/IR/GlobalVariable.h"
1616
#include "llvm/IR/Value.h"
1717
#include "llvm/Object/ELFObjectFile.h"
18+
#include "llvm/ObjectYAML/ELFYAML.h"
19+
#include "llvm/ObjectYAML/yaml2obj.h"
1820
#include "llvm/Support/MemoryBufferRef.h"
1921
#include "llvm/Transforms/Utils/ModuleUtils.h"
2022

@@ -374,3 +376,86 @@ Error llvm::offloading::amdgpu::getAMDGPUMetaDataFromImage(
374376
}
375377
return Error::success();
376378
}
379+
Error offloading::intel::containerizeOpenMPSPIRVImage(
380+
std::unique_ptr<MemoryBuffer> &Img) {
381+
constexpr char INTEL_ONEOMP_OFFLOAD_VERSION[] = "1.0";
382+
constexpr int NT_INTEL_ONEOMP_OFFLOAD_VERSION = 1;
383+
constexpr int NT_INTEL_ONEOMP_OFFLOAD_IMAGE_COUNT = 2;
384+
constexpr int NT_INTEL_ONEOMP_OFFLOAD_IMAGE_AUX = 3;
385+
386+
// Start creating notes for the ELF container.
387+
std::vector<ELFYAML::NoteEntry> Notes;
388+
std::string Version = toHex(INTEL_ONEOMP_OFFLOAD_VERSION);
389+
Notes.emplace_back(ELFYAML::NoteEntry{"INTELONEOMPOFFLOAD",
390+
yaml::BinaryRef(Version),
391+
NT_INTEL_ONEOMP_OFFLOAD_VERSION});
392+
393+
// The AuxInfo string will hold auxiliary information for the image.
394+
// ELFYAML::NoteEntry structures will hold references to the
395+
// string, so we have to make sure the string is valid.
396+
std::string AuxInfo;
397+
398+
// TODO: Pass compile/link opts
399+
StringRef CompileOpts = "";
400+
StringRef LinkOpts = "";
401+
402+
unsigned ImageFmt = 1; // SPIR-V format
403+
404+
AuxInfo = toHex((Twine(0) + Twine('\0') + Twine(ImageFmt) + Twine('\0') +
405+
CompileOpts + Twine('\0') + LinkOpts)
406+
.str());
407+
Notes.emplace_back(ELFYAML::NoteEntry{"INTELONEOMPOFFLOAD",
408+
yaml::BinaryRef(AuxInfo),
409+
NT_INTEL_ONEOMP_OFFLOAD_IMAGE_AUX});
410+
411+
std::string ImgCount = toHex(Twine(1).str()); // always one image per ELF
412+
Notes.emplace_back(ELFYAML::NoteEntry{"INTELONEOMPOFFLOAD",
413+
yaml::BinaryRef(ImgCount),
414+
NT_INTEL_ONEOMP_OFFLOAD_IMAGE_COUNT});
415+
416+
std::string YamlFile;
417+
llvm::raw_string_ostream YamlFileStream(YamlFile);
418+
419+
// Write the YAML template file.
420+
421+
// We use 64-bit little-endian ELF currently.
422+
ELFYAML::FileHeader Header{};
423+
Header.Class = ELF::ELFCLASS64;
424+
Header.Data = ELF::ELFDATA2LSB;
425+
Header.Type = ELF::ET_DYN;
426+
// Use an existing Intel machine type as there is not one specifically for
427+
// Intel GPUs.
428+
Header.Machine = ELF::EM_IA_64;
429+
430+
// Create a section with notes.
431+
ELFYAML::NoteSection Section{};
432+
Section.Type = ELF::SHT_NOTE;
433+
Section.AddressAlign = 0;
434+
Section.Name = ".note.inteloneompoffload";
435+
Section.Notes.emplace(std::move(Notes));
436+
437+
ELFYAML::Object Object{};
438+
Object.Header = Header;
439+
Object.Chunks.push_back(
440+
std::make_unique<ELFYAML::NoteSection>(std::move(Section)));
441+
442+
// Create the section that will hold the image
443+
ELFYAML::RawContentSection ImageSection{};
444+
ImageSection.Type = ELF::SHT_PROGBITS;
445+
ImageSection.AddressAlign = 0;
446+
std::string Name = "__openmp_offload_spirv_0";
447+
ImageSection.Name = Name;
448+
ImageSection.Content =
449+
llvm::yaml::BinaryRef(arrayRefFromStringRef(Img->getBuffer()));
450+
Object.Chunks.push_back(
451+
std::make_unique<ELFYAML::RawContentSection>(std::move(ImageSection)));
452+
Error Err = Error::success();
453+
llvm::yaml::yaml2elf(
454+
Object, YamlFileStream,
455+
[&Err](const Twine &Msg) { Err = createStringError(Msg); }, UINT64_MAX);
456+
if (Err)
457+
return Err;
458+
459+
Img = MemoryBuffer::getMemBufferCopy(YamlFile);
460+
return Error::success();
461+
}

0 commit comments

Comments
 (0)