newLibraryWithURL:error: read its URL through Foundation's URL loading, which
refuses a URL of no scheme (NSURLErrorDomain -1002), and answered nil and
NSCocoaErrorDomain 256. Espresso names each of its kernels' libraries by such
a URL (-[EspressoMetalKernelsCache lazySetup]), so on the device it had no
kernel library and every GPU Vision and Core ML request threw: Geekbench 7's
workload 302, Photos, FaceTime's and Continuity Camera's effects, on every
host GPU.
The device now opens the file at the URL's path, whatever its scheme, as Metal
does, and answers Metal's errors for a file it cannot read
(src/RgfxLibraryFile.*). Measured with a .metallib of our own at each URL form,
alike on macOS 26.7.1's AMD Radeon Pro 5300M and Intel UHD 630 and macOS
27.0.1's M3 Max (jobs m3-max-20286, 20287, 20288):
- a URL of no scheme, absolute, relative to the current directory or with
percent escapes, a file URL, one relative to a file URL, and an http or
other scheme's URL of a file's path: the file's library;
- no file there: MTLLibraryErrorDomain 6, "library not found", for a file
URL too, which answered Foundation's read error before;
- a URL of no path (data:): MTLLibraryErrorDomain 6, "Invalid file url";
- a file it may not open: MTLLibraryErrorDomain 1, "metal library file PATH
failed to open with error: Permission denied";
- a directory or an empty file: MTLLibraryErrorDomain 1, "Invalid library
file (unexpected end of file)".
A URL of another scheme is no longer fetched through URL loading.
The other URL readers do not read this way: a binary archive's descriptor URL
must be a file URL, the IO handles take Metal's own check of a file URL, and
dynamic libraries are not supported. The shell tests read each URL form, and
compare each answer with this Mac's Metal's.
93 lines
2.5 KiB
Plaintext
93 lines
2.5 KiB
Plaintext
/*
|
|
* RgfxLibraryFile: see the header. Our own ObjC++.
|
|
*/
|
|
|
|
#import "RgfxLibraryFile.h"
|
|
|
|
#import <Metal/Metal.h>
|
|
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
|
|
namespace {
|
|
|
|
NSError *libraryError(MTLLibraryError code, NSString *description)
|
|
{
|
|
return [NSError errorWithDomain:MTLLibraryErrorDomain
|
|
code:code
|
|
userInfo:@{NSLocalizedDescriptionKey : description}];
|
|
}
|
|
|
|
NSError *notFound()
|
|
{
|
|
return libraryError(MTLLibraryErrorFileNotFound, @"library not found");
|
|
}
|
|
|
|
NSError *endOfFile()
|
|
{
|
|
return libraryError(MTLLibraryErrorUnsupported, @"Invalid library file (unexpected end of file)");
|
|
}
|
|
|
|
// Metal's error for a file it could not open, `reason` strerror's words for the open's error.
|
|
NSError *notOpened(NSString *path, NSString *reason)
|
|
{
|
|
return libraryError(MTLLibraryErrorUnsupported,
|
|
[NSString stringWithFormat:@"metal library file %@ failed to open with error: %@", path,
|
|
reason]);
|
|
}
|
|
|
|
NSString *reasonOf(int code)
|
|
{
|
|
return @(strerror(code));
|
|
}
|
|
|
|
// The POSIX error under Foundation's error of a read, 0 where it names none.
|
|
int posixCodeOf(NSError *error)
|
|
{
|
|
for (NSError *e = error; e != nil; e = e.userInfo[NSUnderlyingErrorKey]) {
|
|
if ([e.domain isEqualToString:NSPOSIXErrorDomain])
|
|
return static_cast<int>(e.code);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
NSString *RgfxLibraryFilePath(NSURL *url)
|
|
{
|
|
NSString *path = url.path;
|
|
return path.length != 0 ? path : nil;
|
|
}
|
|
|
|
NSData *RgfxLibraryFileData(NSString *path, NSError *__autoreleasing *error)
|
|
{
|
|
NSError *answer = nil;
|
|
NSData *data = nil;
|
|
struct stat st = {};
|
|
if (path == nil) {
|
|
answer = libraryError(MTLLibraryErrorFileNotFound, @"Invalid file url");
|
|
} else if (stat(path.fileSystemRepresentation, &st) != 0) {
|
|
const int code = errno;
|
|
answer = code == ENOENT ? notFound() : notOpened(path, reasonOf(code));
|
|
} else if (S_ISDIR(st.st_mode)) {
|
|
answer = endOfFile();
|
|
} else {
|
|
NSError *readError = nil;
|
|
data = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:&readError];
|
|
if (data == nil) {
|
|
// Foundation's error names the read's POSIX error; where it names none, its own words
|
|
// stand for strerror's.
|
|
const int code = posixCodeOf(readError);
|
|
answer = code == ENOENT ? notFound()
|
|
: notOpened(path, code != 0 ? reasonOf(code) : readError.localizedDescription);
|
|
} else if (data.length == 0) {
|
|
data = nil;
|
|
answer = endOfFile();
|
|
}
|
|
}
|
|
if (data == nil && error != nullptr)
|
|
*error = answer;
|
|
return data;
|
|
}
|