Skip to content

Commit

Permalink
Support reading metallib v1.2.8 files from macOS 15. (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt authored Jun 17, 2024
1 parent 01f246b commit 9472078
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 4 deletions.
41 changes: 38 additions & 3 deletions src/compiler/library.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,44 @@ tag_value_io["SARC"] = (
id = String(readuntil(io, UInt8(0)))
compressed = read(io, nb - sizeof(id) - 1)

# unpad and decompress the archive
i = findlast(!iszero, compressed)
archive = transcode(Bzip2Decompressor, compressed[1:i])
# Bzip2Decompressor doesn't support padding (JuliaIO/CodecBzip2.jl#31),
# so find the end marker and truncate the archive (in a naive way,
# but these source code archives are expected to be small)
compressed[1:2] == [0x42, 0x5a] || error("Not a BZip2 archive")
trailing_bytes = 10 # end marker + checksum
end_marker = 0x177245385090
i = 1
while i < length(compressed) - trailing_bytes
# aligned match
current_value = UInt64(compressed[i]) << 40 |
UInt64(compressed[i+1]) << 32 |
UInt64(compressed[i+2]) << 24 |
UInt64(compressed[i+3]) << 16 |
UInt64(compressed[i+4]) << 8 |
UInt64(compressed[i+5])
if current_value == end_marker
break
end

# misaligned match
current_value = UInt64(current_value) << 8 | compressed[i+6]
if end_marker in ((current_value >> 1) & 0xffffffffffff,
(current_value >> 2) & 0xffffffffffff,
(current_value >> 3) & 0xffffffffffff,
(current_value >> 4) & 0xffffffffffff,
(current_value >> 5) & 0xffffffffffff,
(current_value >> 6) & 0xffffffffffff,
(current_value >> 7) & 0xffffffffffff)
i += 1
break
end

i += 1
end
compressed = compressed[1:i+trailing_bytes-1]

# decompress the archive
archive = transcode(Bzip2Decompressor, compressed)

(; id, archive)
end,
Expand Down
2 changes: 1 addition & 1 deletion test/metallib/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
METAL := xcrun -sdk macosx metal
MACOSX_VERSIONS := 11 12 13 14
MACOSX_VERSIONS := 11 12 13 14 15

SOURCES := $(wildcard *.metal)
METALLIBS := $(foreach ver,$(MACOSX_VERSIONS),$(SOURCES:.metal=.$(ver).metallib))
Expand Down
Binary file added test/metallib/constants.15.metallib
Binary file not shown.
Binary file added test/metallib/debuginfo.15.metallib
Binary file not shown.
Binary file added test/metallib/kernel.15.metallib
Binary file not shown.
Binary file added test/metallib/kernels.15.metallib
Binary file not shown.
Binary file added test/metallib/sources.15.metallib
Binary file not shown.

0 comments on commit 9472078

Please sign in to comment.