[llvm] [llvm][tools] Add support to llvm-offload-binary to unbundle spirv64-intel images (PR #184774)
James Henderson via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 6 01:27:13 PST 2026
================
@@ -135,6 +137,119 @@ static Error bundleImages() {
return Error::success();
}
+// Extract SPIR-V binaries from an ELF image with triple "spirv64-intel" or
+// "spirv32-intel". These ELF images contain SPIR-V binaries in sections named
+// "__openmp_offload_spirv_*".
+static Expected<SmallVector<StringRef>>
+extractSPIRVFromELF(StringRef ImageData) {
+ SmallVector<StringRef> SPIRVBinaries;
+
+ // Try to parse as ELF object file
+ Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
+ ObjectFile::createObjectFile(MemoryBufferRef(ImageData, "spirv-elf"));
+ if (!ObjOrErr)
+ return ObjOrErr.takeError();
+
+ ObjectFile &Obj = *ObjOrErr->get();
+ if (!Obj.isELF())
+ return createStringError(inconvertibleErrorCode(),
+ "Expected ELF format for Intel SPIR-V image");
+
+ // Extract all sections with name matching "__openmp_offload_spirv_*"
+ for (const SectionRef &Sec : Obj.sections()) {
+ Expected<StringRef> NameOrErr = Sec.getName();
+ if (!NameOrErr) {
+ // consume error and skip this section
+ consumeError(NameOrErr.takeError());
+ continue;
+ }
+
+ if (!NameOrErr->starts_with("__openmp_offload_spirv_"))
+ continue;
+
+ Expected<StringRef> ContentsOrErr = Sec.getContents();
+ if (!ContentsOrErr)
+ return ContentsOrErr.takeError();
+
+ SPIRVBinaries.push_back(*ContentsOrErr);
+ }
+
+ if (SPIRVBinaries.empty())
+ return createStringError(inconvertibleErrorCode(),
+ "No SPIR-V sections found in ELF image");
----------------
jh7370 wrote:
Same nit comments as before.
https://github.com/llvm/llvm-project/pull/184774
More information about the llvm-commits
mailing list