🐛 Read OpenCL kernels' by-value arguments, each function on its own
The bundle's AIR argument reader did not know `air.constant`, the node Metal's compiler writes for an OpenCL C kernel's argument passed by value (a scalar, a vector, a struct or a union), bound at an `air.location_index` among the buffers. It took the unknown binding as one that may be a texture, so the kernel's arguments did not read, and functionsOf() then dropped every entry point of the module. Nearly every OpenCL kernel takes a value, so the guest logged "no arguments read (its module did not read)" for each of them, and every texture with ShaderWrite usage it bound was taken as written: a cost in surface writes, never a wrong result. Now a by-value argument is read as a buffer at its index, of the access its node names (none): every kernel of the audit's clprobe reads, k_buffers and k_struct among them. And an entry point whose arguments do not read is listed with `argumentsRead` false beside the module's other entry points, which keep their own answers; the library logs "an argument of it is not one the reader follows" for it alone. Read from this Mac's compile of OpenCL C programs of our own (macOS 27.0.1, the M3 Max), as LLVM's text form prints the modules. The tests read every kernel of the CL mode tests' programs and of a new one of every value kind, and a module of every entry point of tests/air_arguments.metal (the compiler's .air) with one entry point spoilt.
This commit is contained in:
1 parent
b3fa81fa6b
commit
72503e9c5d
7 files changed
+300
-42
No files matched your search
+11
-8
@@ -18,8 +18,8 @@
|
||||
# make test build + run the checks: the serialiser check (x86_64, under
|
||||
# Rosetta), the AIR argument reader's check (x86_64, under Rosetta,
|
||||
# over .metallibs built from tests/air_arguments.metal,
|
||||
# tests/air_arguments_old.metal and tests/air_arguments_11.metal, and the
|
||||
# repo's committed metallibs), the
|
||||
# tests/air_arguments_old.metal and tests/air_arguments_11.metal, the module
|
||||
# of every function of the first, and the repo's committed metallibs), the
|
||||
# Metal-shell wrapper/routing check (this Mac's native arch; the
|
||||
# device shell is not in its binary), and the device's base-class
|
||||
# check (x86_64, under Rosetta), which loads the bundle
|
||||
@@ -246,8 +246,11 @@ $(PATCH_LIB): tests/patch.metal Makefile
|
||||
# --- the AIR argument reader's check (x86_64, Rosetta) ------------------------
|
||||
# The reader (src/RgfxAirArguments.*) over a .metallib Apple's Metal compiler builds here from
|
||||
# tests/air_arguments.metal, never committed; its function constants against the library's CNST
|
||||
# tags (src/RgfxFunctionConstants.*).
|
||||
# tags (src/RgfxFunctionConstants.*). AIR_MODULE is the compiler's module of every function of the
|
||||
# source, which `metallib` splits into the library's one module a function: the reader's answers
|
||||
# for a module of several entry points.
|
||||
AIR_LIB = $(BUILD)/air_arguments.metallib
|
||||
AIR_MODULE = $(BUILD)/air_arguments.air
|
||||
# The same library built for macOS 26, whose functions Metal's library reflection answers
|
||||
# (tests/protocol_tests.mm, test_argument_encoders_of_bindings).
|
||||
AIR_REFLECTED = $(BUILD)/air_arguments_reflected.metallib
|
||||
@@ -259,8 +262,8 @@ $(AIR_REFLECTED): tests/air_arguments.metal Makefile
|
||||
$(AIR_LIB): tests/air_arguments.metal Makefile
|
||||
@mkdir -p $(@D)
|
||||
xcrun -sdk macosx metal -c -std=metal3.1 -mmacosx-version-min=14.0 tests/air_arguments.metal \
|
||||
-o $(BUILD)/air_arguments.air
|
||||
xcrun -sdk macosx metallib $(BUILD)/air_arguments.air -o $@
|
||||
-o $(AIR_MODULE)
|
||||
xcrun -sdk macosx metallib $(AIR_MODULE) -o $@
|
||||
|
||||
# Pipelines' reflection (src/RgfxPipelineReflection.*) over the oracle probe's library
|
||||
# (tools/oracle/oracle_reflection.metal), built for macOS 26 and Metal 3.0 as build.sh builds it,
|
||||
@@ -366,9 +369,9 @@ test: $(SER_TESTBIN) $(AIR_TESTBIN) $(AIR_LIB) $(AIR_OLD_LIB) $(AIR_11_LIB) $(AI
|
||||
$(SHELL_TESTBIN) $(CI_KERNEL) $(STITCHABLE) $(STITCH_CONSTANTS) $(PATCH_LIB) $(INHERIT_TESTBIN) \
|
||||
$(MEMBERS_TESTBIN) $(VIDEO_TESTBIN) $(VIDEO_H265_TESTBIN) $(VIDEO_DRIVER_TESTBIN) $(BUNDLEBIN)
|
||||
arch -$(ARCH) $(SER_TESTBIN)
|
||||
RGFX_AIR_ARGUMENTS=$(AIR_LIB) RGFX_AIR_ARGUMENTS_OLD=$(AIR_OLD_LIB) RGFX_AIR_ARGUMENTS_11=$(AIR_11_LIB) \
|
||||
RGFX_ORACLE_REFLECTION=$(ORACLE_REFLECTION_LIB) RGFX_COMMITTED_METALLIBS=$(COMMITTED_METALLIBS) \
|
||||
arch -$(ARCH) $(AIR_TESTBIN)
|
||||
RGFX_AIR_ARGUMENTS=$(AIR_LIB) RGFX_AIR_MODULE=$(AIR_MODULE) RGFX_AIR_ARGUMENTS_OLD=$(AIR_OLD_LIB) \
|
||||
RGFX_AIR_ARGUMENTS_11=$(AIR_11_LIB) RGFX_ORACLE_REFLECTION=$(ORACLE_REFLECTION_LIB) \
|
||||
RGFX_COMMITTED_METALLIBS=$(COMMITTED_METALLIBS) arch -$(ARCH) $(AIR_TESTBIN)
|
||||
RGFX_CI_KERNEL=$(CI_KERNEL) RGFX_STITCHABLE=$(STITCHABLE) RGFX_STITCH_CONSTANTS=$(STITCH_CONSTANTS) \
|
||||
RGFX_AIR_ARGUMENTS=$(AIR_LIB) RGFX_AIR_ARGUMENTS_11=$(AIR_11_LIB) \
|
||||
RGFX_AIR_REFLECTED=$(AIR_REFLECTED) RGFX_ORACLE_REFLECTION=$(ORACLE_REFLECTION_LIB) \
|
||||
|
||||
@@ -1437,24 +1437,26 @@ std::string globalName(const Module &m, uint64_t v)
|
||||
return {};
|
||||
}
|
||||
|
||||
// One entry point's node: the function, then two nodes, the second its arguments.
|
||||
// One entry point's node: the function, then two nodes, the second its arguments. False where its
|
||||
// arguments do not read; `out.name` is then the function's name, or empty where the node names no
|
||||
// function of the module.
|
||||
bool entryPoint(const Module &m, uint64_t id, RgfxAirStage stage, RgfxAirFunction &out)
|
||||
{
|
||||
out.name.clear();
|
||||
out.stage = stage;
|
||||
out.arguments.clear();
|
||||
const Module::Md *entry = mdAt(m, id);
|
||||
if (entry == nullptr || entry->kind != Module::Md::kNode || entry->ops.size() < 3)
|
||||
return false;
|
||||
const Module::Md *fn = mdAt(m, entry->ops[0]);
|
||||
const Module::Md *args = mdAt(m, entry->ops[2]);
|
||||
if (fn == nullptr || fn->kind != Module::Md::kValue || args == nullptr ||
|
||||
args->kind != Module::Md::kNode)
|
||||
if (fn == nullptr || fn->kind != Module::Md::kValue)
|
||||
return false;
|
||||
const uint64_t v = fn->value;
|
||||
if (v >= m.globals.size() || !m.globals[static_cast<size_t>(v)].function)
|
||||
return false;
|
||||
out.name = globalName(m, v);
|
||||
out.stage = stage;
|
||||
out.arguments.clear();
|
||||
if (out.name.empty())
|
||||
const Module::Md *args = mdAt(m, entry->ops[2]);
|
||||
if (out.name.empty() || args == nullptr || args->kind != Module::Md::kNode)
|
||||
return false;
|
||||
for (uint64_t argId : args->ops) {
|
||||
const Module::Md *arg = mdAt(m, argId);
|
||||
@@ -1474,7 +1476,8 @@ bool entryPoint(const Module &m, uint64_t id, RgfxAirStage stage, RgfxAirFunctio
|
||||
continue; // an output or a built-in, of no binding
|
||||
RgfxAirArgument a = {};
|
||||
a.access = RgfxAirAccess::Unknown;
|
||||
if (*kind == "air.buffer")
|
||||
// An OpenCL C kernel's argument passed by value is a buffer of the value at its index.
|
||||
if (*kind == "air.buffer" || *kind == "air.constant")
|
||||
a.kind = RgfxAirKind::Buffer;
|
||||
else if (*kind == "air.indirect_buffer")
|
||||
a.kind = RgfxAirKind::IndirectBuffer;
|
||||
@@ -2807,7 +2810,8 @@ bool visibleOf(const Module &m, std::vector<RgfxAirVisible> &out)
|
||||
return true;
|
||||
}
|
||||
|
||||
// The entry points' arguments of the module `m` (rgfxReadAirFunctions).
|
||||
// The entry points' arguments of the module `m` (rgfxReadAirFunctions): each entry point's own
|
||||
// answer, one whose arguments do not read listed with none, one that names no function left out.
|
||||
bool functionsOf(const Module &m, std::vector<RgfxAirFunction> &out)
|
||||
{
|
||||
out.clear();
|
||||
@@ -2826,8 +2830,10 @@ bool functionsOf(const Module &m, std::vector<RgfxAirFunction> &out)
|
||||
for (uint64_t id : it->second) {
|
||||
RgfxAirFunction f;
|
||||
if (!entryPoint(m, id, s.stage, f)) {
|
||||
out.clear();
|
||||
return false;
|
||||
if (f.name.empty())
|
||||
continue;
|
||||
f.argumentsRead = false;
|
||||
f.arguments.clear();
|
||||
}
|
||||
f.constants = constants;
|
||||
f.constantsRead = constantsRead;
|
||||
|
||||
@@ -22,11 +22,17 @@
|
||||
// a post-tessellation vertex function's patch inputs, function tables and acceleration structures
|
||||
// are named, as bindings of no texture. Threadgroup memory is an `air.buffer` of
|
||||
// `air.address_space` 3, at an index of its own that no buffer is bound to: it is no argument
|
||||
// here. Read from modules that Apple's Metal compiler built from our own sources
|
||||
// (tests/air_arguments.metal), and as LLVM's text form of the same modules prints them; a count
|
||||
// set by function constants, which no source of ours declares, from macOS 27's frameworks'
|
||||
// modules on the M3 Max, where 709 entry points bind one (Metal Performance Shaders' kernels
|
||||
// among them).
|
||||
// here. An OpenCL C kernel's argument passed by value (a scalar, a vector, a struct or a union)
|
||||
// is an `air.constant`, bound at an `air.location_index` among the buffers, with no access key;
|
||||
// the kernel's function takes it as a pointer of address space 2 to the value. It is read as a
|
||||
// buffer at its index, of no access the reader knows (read: Metal's compiler on macOS 27.0.1, the
|
||||
// M3 Max, OpenCL C programs of our own compiled as the GL renderer compiles them, for each scalar
|
||||
// type, vectors of 2 to 16 components, half and half4, structs, a nested struct and a union, as
|
||||
// LLVM's text form prints the modules; tests/cl_mode_tests.mm). Read from modules that Apple's
|
||||
// Metal compiler built from our own sources (tests/air_arguments.metal), and as LLVM's text form
|
||||
// of the same modules prints them; a count set by function constants, which no source of ours
|
||||
// declares, from macOS 27's frameworks' modules on the M3 Max, where 709 entry points bind one
|
||||
// (Metal Performance Shaders' kernels among them).
|
||||
//
|
||||
// A module built for macOS 10.12 (-std=macos-metal1.2) holds LLVM 3.5's metadata nodes, each
|
||||
// operand a type and a value: a metadata where the type is metadata, else a value in place of a
|
||||
@@ -97,17 +103,24 @@ struct RgfxAirConstant {
|
||||
};
|
||||
|
||||
// One entry point: its name, stage and binding arguments, in the module's order, and the function
|
||||
// constants its module declares (`constantsRead` false where they did not read).
|
||||
// constants its module declares (`constantsRead` false where they did not read). `argumentsRead`
|
||||
// is false where an argument's node is not one the reader follows (a binding of a kind it does
|
||||
// not know, a texture of no access it knows, a binding it cannot read): which bindings the
|
||||
// function writes is then unknown, and `arguments` is empty.
|
||||
struct RgfxAirFunction {
|
||||
std::string name;
|
||||
RgfxAirStage stage;
|
||||
bool argumentsRead = true;
|
||||
std::vector<RgfxAirArgument> arguments;
|
||||
std::vector<RgfxAirConstant> constants;
|
||||
bool constantsRead = false;
|
||||
};
|
||||
|
||||
// The entry points of the AIR module at `bitcode` (raw bitcode, or in LLVM's wrapper header).
|
||||
// False where it is not one the reader can follow; `out` then holds nothing.
|
||||
// The entry points of the AIR module at `bitcode` (raw bitcode, or in LLVM's wrapper header),
|
||||
// each with its own answer: an entry point whose arguments the reader does not follow is listed,
|
||||
// `argumentsRead` false, beside the others, and one whose node names no function of the module is
|
||||
// left out. False where the bytes are not a module the reader can follow; `out` then holds
|
||||
// nothing.
|
||||
bool rgfxReadAirFunctions(const uint8_t *bitcode, size_t size, std::vector<RgfxAirFunction> &out);
|
||||
|
||||
// The signatures of a module's visible functions. The module lists them in the named metadata
|
||||
|
||||
@@ -644,7 +644,8 @@ bool rgfxAirUnreadFirstLog(const std::string &name)
|
||||
|
||||
// The arguments of the entry point `name`, read from its bitcode in the bytes the library was
|
||||
// made from (rgfxModuleOf:why:), once: null where the library kept no bytes (one Metal's link or
|
||||
// stitching made), or they do not read, so which textures the function writes is unknown.
|
||||
// stitching made), or its module or its own arguments do not read, so which textures the function
|
||||
// writes is unknown.
|
||||
- (std::shared_ptr<const RgfxAirFunction>)rgfxAirFunctionNamed:(const std::string &)name
|
||||
{
|
||||
std::lock_guard<std::mutex> hold(_airLock);
|
||||
@@ -657,14 +658,19 @@ bool rgfxAirUnreadFirstLog(const std::string &name)
|
||||
if (module != nullptr && !module->functionsRead) {
|
||||
why = "its module did not read";
|
||||
} else if (module != nullptr) {
|
||||
const RgfxAirFunction *named = nullptr;
|
||||
for (const RgfxAirFunction &f : module->functions) {
|
||||
if (f.name == name) {
|
||||
air = std::shared_ptr<const RgfxAirFunction>(module, &f);
|
||||
named = &f;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (air == nullptr)
|
||||
if (named == nullptr)
|
||||
why = "its module declares no such entry point";
|
||||
else if (!named->argumentsRead)
|
||||
why = "an argument of it is not one the reader follows";
|
||||
else
|
||||
air = std::shared_ptr<const RgfxAirFunction>(module, named);
|
||||
}
|
||||
if (air == nullptr && why != nullptr && rgfxAirUnreadFirstLog(name))
|
||||
NSLog(@"RGFX: %s: no arguments read (%s): the textures it binds are taken as written "
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
// compiler builds from tests/air_arguments.metal at test time (RGFX_AIR_ARGUMENTS names it):
|
||||
// every entry point's binding arguments, kind, index, count and access, as the source declares
|
||||
// them, those under function constants among them; which texture bindings each may write; no
|
||||
// answer for a binding of a kind the reader does not know; each stitchable function's signature,
|
||||
// answer for an entry point with a binding of a kind the reader does not know, beside the answers
|
||||
// of the others of its module, over the compiler's module of every entry point of the same source
|
||||
// (RGFX_AIR_MODULE, its .air); each stitchable function's signature,
|
||||
// type by type, and an unknown one where it has a type the reader does not know; each module's
|
||||
// function constants, as its function's CNST tag lists them; the same over the .metallib built
|
||||
// from tests/air_arguments_old.metal for macOS 10.12 (RGFX_AIR_ARGUMENTS_OLD); each buffer's
|
||||
@@ -62,8 +64,8 @@ static bool operator==(const RgfxAirConstant &a, const RgfxAirConstant &b)
|
||||
|
||||
static bool operator==(const RgfxAirFunction &a, const RgfxAirFunction &b)
|
||||
{
|
||||
return a.name == b.name && a.stage == b.stage && a.arguments == b.arguments && a.constants == b.constants &&
|
||||
a.constantsRead == b.constantsRead;
|
||||
return a.name == b.name && a.stage == b.stage && a.argumentsRead == b.argumentsRead && a.arguments == b.arguments &&
|
||||
a.constants == b.constants && a.constantsRead == b.constantsRead;
|
||||
}
|
||||
|
||||
static bool operator==(const RgfxAirType &a, const RgfxAirType &b)
|
||||
@@ -171,6 +173,7 @@ RgfxAirFunction entry(const std::vector<uint8_t> &lib, const std::string &name)
|
||||
std::vector<RgfxAirFunction> fns;
|
||||
check(rgfxReadAirFunctions(bitcode, size, fns), name + ": its module is read");
|
||||
check(fns.size() == 1 && fns[0].name == name, name + ": its module's one entry point is it");
|
||||
check(fns.empty() || fns[0].argumentsRead, name + ": its arguments are read");
|
||||
return fns.empty() ? RgfxAirFunction{} : fns[0];
|
||||
}
|
||||
|
||||
@@ -556,9 +559,11 @@ int main()
|
||||
unread++;
|
||||
continue;
|
||||
}
|
||||
for (const RgfxAirFunction &fn : fns)
|
||||
for (const RgfxAirFunction &fn : fns) {
|
||||
unread += fn.argumentsRead ? 0 : 1;
|
||||
for (const RgfxAirArgument &a : fn.arguments)
|
||||
anyCount += a.anyCount ? 1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::printf("air_arguments_test: the OS's MPS libraries: %zu, %zu entry points, %zu unread, "
|
||||
@@ -600,7 +605,8 @@ int main()
|
||||
expect(table, RgfxAirKind::Buffer, 0, 1, RgfxAirAccess::ReadWrite, "a device buffer at 0");
|
||||
expect(table, RgfxAirKind::Texture, 3, 1, RgfxAirAccess::Write, "access::write at 3");
|
||||
|
||||
// A binding of a kind the reader does not know may be a texture: the function has no answer.
|
||||
// A binding of a kind the reader does not know may be a texture: the function has no answer,
|
||||
// and its module still reads.
|
||||
{
|
||||
std::vector<uint8_t> other = lib;
|
||||
const char kind[] = "air.visible_function_table";
|
||||
@@ -611,13 +617,56 @@ int main()
|
||||
size_t n = 0;
|
||||
std::vector<RgfxAirFunction> fns;
|
||||
check(rgfxMetallibBitcode(other.data(), other.size(), "calls_through_table", &bc, &n) &&
|
||||
!rgfxReadAirFunctions(bc, n, fns) && fns.empty(),
|
||||
"calls_through_table, its table's kind unknown: no answer");
|
||||
rgfxReadAirFunctions(bc, n, fns) && fns.size() == 1 && fns[0].name == "calls_through_table" &&
|
||||
!fns[0].argumentsRead && fns[0].arguments.empty(),
|
||||
"calls_through_table, its table's kind unknown: listed, its arguments unread");
|
||||
check(rgfxMetallibBitcode(other.data(), other.size(), "conditional_planes", &bc, &n) &&
|
||||
rgfxReadAirFunctions(bc, n, fns),
|
||||
rgfxReadAirFunctions(bc, n, fns) && fns.size() == 1 && fns[0].argumentsRead,
|
||||
"conditional_planes is still read beside it");
|
||||
}
|
||||
|
||||
// A module of every entry point of tests/air_arguments.metal (the compiler's .air, before
|
||||
// `metallib` splits it into one module a function; RGFX_AIR_MODULE): each entry point's answer
|
||||
// is the one its own module gives, and one whose arguments do not read (its function table's
|
||||
// kind spoilt) leaves the others' answers as they were.
|
||||
if (const char *modulePath = std::getenv("RGFX_AIR_MODULE")) {
|
||||
const std::vector<uint8_t> whole = readFile(modulePath);
|
||||
std::vector<RgfxAirFunction> all;
|
||||
check(rgfxReadAirFunctions(whole.data(), whole.size(), all), "the whole module reads");
|
||||
size_t compared = 0;
|
||||
for (const RgfxAirFunction &f : all) {
|
||||
const RgfxAirFunction own = entry(lib, f.name);
|
||||
check(f.argumentsRead && own.stage == f.stage && own.arguments == f.arguments,
|
||||
f.name + ": the whole module's answer is its own module's");
|
||||
compared++;
|
||||
}
|
||||
check(compared >= 12, "the whole module lists every entry point (" + std::to_string(compared) + ")");
|
||||
// Each string that starts with the kind's spoilt: the kind's, and the stitching type's
|
||||
// (`air.visible_function_table_type`), which only visible functions' signatures name.
|
||||
std::vector<uint8_t> spoilt = whole;
|
||||
const char tableKind[] = "air.visible_function_table";
|
||||
const size_t length = sizeof(tableKind) - 1;
|
||||
size_t spoiled = 0;
|
||||
for (auto it = spoilt.begin(); (it = std::search(it, spoilt.end(), tableKind, tableKind + length)) != spoilt.end();
|
||||
it += length) {
|
||||
*(it + length - 1) = '_';
|
||||
spoiled++;
|
||||
}
|
||||
check(spoiled > 0, "the whole module names the function table's kind");
|
||||
std::vector<RgfxAirFunction> left;
|
||||
check(rgfxReadAirFunctions(spoilt.data(), spoilt.size(), left) && left.size() == all.size(),
|
||||
"the spoilt module reads, every entry point listed");
|
||||
for (size_t i = 0; i < left.size() && i < all.size(); i++) {
|
||||
if (left[i].name == "calls_through_table")
|
||||
check(!left[i].argumentsRead && left[i].arguments.empty(),
|
||||
"calls_through_table, its table's kind unknown: its arguments unread");
|
||||
else
|
||||
check(left[i] == all[i], left[i].name + ": its answer beside it is as it was");
|
||||
}
|
||||
} else {
|
||||
std::printf("air_arguments_test: RGFX_AIR_MODULE unset: no module of several entry points\n");
|
||||
}
|
||||
|
||||
const uint8_t *bitcode = nullptr;
|
||||
size_t size = 0;
|
||||
check(!rgfxMetallibBitcode(lib.data(), lib.size(), "no_such_function", &bitcode, &size),
|
||||
@@ -1554,7 +1603,10 @@ int main()
|
||||
case 0:
|
||||
case 1:
|
||||
case 2: {
|
||||
check(rgfxReadAirFunctions(bc.data(), bc.size(), args) && named(args), what + ": its arguments");
|
||||
check(rgfxReadAirFunctions(bc.data(), bc.size(), args) &&
|
||||
std::any_of(args.begin(), args.end(),
|
||||
[&](const RgfxAirFunction &x) { return x.name == f.name && x.argumentsRead; }),
|
||||
what + ": its arguments");
|
||||
const std::string listedAs = file.substr(file.rfind('/') + 1) + " " + f.name;
|
||||
const bool unread =
|
||||
std::find(nodesUnread.begin(), nodesUnread.end(), listedAs) != nodesUnread.end();
|
||||
|
||||
@@ -11,7 +11,11 @@
|
||||
// the host makes no pipeline of OpenCL mode, or of another plugin data or bitcodeType;
|
||||
// - the same of kernels of images and samplers, over this Mac's compile of a program of them, as
|
||||
// the x86 Macs answer them (measured: macOS 26.7.1, both Macs alike, Metal's OpenCL mode driven
|
||||
// as the renderer drives it).
|
||||
// as the renderer drives it);
|
||||
// - the AIR reader's binding arguments (rgfxReadAirFunctions) of each kernel of those programs,
|
||||
// and of kernels of values of every kind (scalars, vectors, half, structs, a union), over this
|
||||
// Mac's compile: each value a buffer at its index, its function's and its OpenCL-mode
|
||||
// variant's arguments read.
|
||||
// Nothing here runs on the GPU: tools/cl_probe.m runs the kernels on a device.
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <Metal/Metal.h>
|
||||
@@ -274,6 +278,10 @@ void test_cl_function_of_opencl_mode()
|
||||
for (const Expected &e : kExpected) {
|
||||
RgfxFunction *kernel = (RgfxFunction *)[program newFunctionWithName:@(e.name)];
|
||||
CHECK(kernel != nil && [kernel rgfxClBindings] == nullptr && [(id)kernel valueForKey:@"pluginData"] == nil);
|
||||
// Its binding arguments are read: a buffer for each pointer and each value (its __local
|
||||
// pointer, lsum's, is threadgroup memory, no argument).
|
||||
std::shared_ptr<const RgfxAirFunction> air = [kernel rgfxAirFunction];
|
||||
CHECK(air != nullptr && air->arguments.size() == e.buffers.size() + e.byValue.size());
|
||||
id<MTLFunction> variant = [kernel newFunctionWithPluginData:plugin bitcodeType:2];
|
||||
CHECK([(id)variant isKindOfClass:[RgfxFunction class]] && variant != kernel);
|
||||
if (![(id)variant isKindOfClass:[RgfxFunction class]])
|
||||
@@ -482,6 +490,13 @@ void checkImageKernels(id<MTLDevice> metal, NSString *source, const ImageKernel
|
||||
for (size_t k = 0; k < count; k++) {
|
||||
const ImageKernel &want = kernels[k];
|
||||
RgfxFunction *kernel = (RgfxFunction *)[program newFunctionWithName:@(want.name)];
|
||||
// Its binding arguments are read (its values' among them), and it writes each texture its
|
||||
// write-only images are bound to, and no other.
|
||||
std::shared_ptr<const RgfxAirFunction> air = [kernel rgfxAirFunction];
|
||||
CHECK(air != nullptr);
|
||||
for (const Argument &a : want.arguments)
|
||||
if (air != nullptr && a.type == MTLArgumentTypeTexture)
|
||||
CHECK(rgfxAirWritesTexture(*air, static_cast<uint32_t>(a.index)) == (a.access != MTLArgumentAccessReadOnly));
|
||||
id<MTLFunction> variant = [kernel newFunctionWithPluginData:plugin bitcodeType:2];
|
||||
if (![(id)variant isKindOfClass:[RgfxFunction class]])
|
||||
fprintf(stderr, "cl_mode_tests: %s: no function of OpenCL mode\n", want.name);
|
||||
@@ -532,6 +547,167 @@ void test_cl_images_of_opencl_mode()
|
||||
checkImageKernels(metal, kDepthSource, &kDepthKernel, 1);
|
||||
}
|
||||
|
||||
// Kernels of values passed by value of every kind: a float beside global pointers and a struct
|
||||
// (k_buffers and k_struct of the audit's probe, which the reader read none of before), each
|
||||
// scalar type, vectors of 2 to 16 components, half and half4 (cl_khr_fp16), a nested struct beside
|
||||
// a __constant and a __local pointer to structs, a union, and an int beside a written image.
|
||||
NSString *const kValueSource =
|
||||
@"#pragma OPENCL EXTENSION cl_khr_fp16 : enable\n"
|
||||
@"typedef struct { float m; float a; int n; } P;\n"
|
||||
@"typedef struct { float4 v; int k[3]; } In;\n"
|
||||
@"typedef struct { In in; float w; char c; } Out;\n"
|
||||
@"typedef union { float f; uint u; } U;\n"
|
||||
@"__kernel void v_float(__global const float *a, __global const float *b, __global float *o, float s) {\n"
|
||||
@" int i = get_global_id(0); o[i] = a[i] * s + b[i]; }\n"
|
||||
@"__kernel void v_struct(__global const float *x, __global float *o, P p) {\n"
|
||||
@" int i = get_global_id(0); o[i] = (i < p.n) ? x[i] * p.m + p.a : -1.0f; }\n"
|
||||
@"__kernel void v_scalars(__global float *o, char a, uchar b, short c, ushort d, int e, uint f, long g,\n"
|
||||
@" ulong h, float k) {\n"
|
||||
@" o[get_global_id(0)] = a + b + c + d + e + f + g + h + k; }\n"
|
||||
@"__kernel void v_vectors(__global float4 *o, float2 a, float3 b, float4 c, int4 d, uchar16 e, short8 f,\n"
|
||||
@" long2 g) {\n"
|
||||
@" o[get_global_id(0)] = (float4)(a.x, b.y, c.z, d.w) + (float)e.s7 + (float)f.s3 + (float)g.y; }\n"
|
||||
@"__kernel void v_half(__global float *o, half h, half4 v) { o[get_global_id(0)] = (float)h + (float)v.w; }\n"
|
||||
@"__kernel void v_nested(__global float *o, Out s, __constant Out *cs, __local In *ls) {\n"
|
||||
@" int i = get_global_id(0); ls[0] = s.in; barrier(CLK_LOCAL_MEM_FENCE);\n"
|
||||
@" o[i] = s.in.v.x + s.in.k[2] + s.w + s.c + cs->w + ls[0].v.y; }\n"
|
||||
@"__kernel void v_union(__global uint *o, U u) { o[get_global_id(0)] = u.u; }\n"
|
||||
@"__kernel void v_image(__global float *o, __write_only image2d_t im, int n) {\n"
|
||||
@" int i = get_global_id(0); o[i] = n; write_imagef(im, (int2)(i, 0), (float4)(n)); }\n";
|
||||
|
||||
// What the reader answers for one binding argument: its kind, index and access, of count 1 at a
|
||||
// fixed index.
|
||||
struct ReadArgument {
|
||||
RgfxAirKind kind;
|
||||
uint32_t index;
|
||||
RgfxAirAccess access;
|
||||
};
|
||||
|
||||
// Each kernel's binding arguments, as this Mac's compile of kValueSource declares them (read:
|
||||
// macOS 27.0.1, the M3 Max, its modules as LLVM's text form prints them): each value an
|
||||
// `air.constant` at its buffer index, of no access key, read as a buffer of no access; a __global
|
||||
// pointer `air.read` where it is const and `air.read_write` where it is not; v_nested's __local
|
||||
// pointer threadgroup memory, no argument.
|
||||
const struct {
|
||||
const char *name;
|
||||
std::vector<ReadArgument> arguments;
|
||||
} kValueKernels[] = {
|
||||
{"__entry_v_float",
|
||||
{{RgfxAirKind::Buffer, 0, RgfxAirAccess::Read}, {RgfxAirKind::Buffer, 1, RgfxAirAccess::Read},
|
||||
{RgfxAirKind::Buffer, 2, RgfxAirAccess::ReadWrite}, {RgfxAirKind::Buffer, 3, RgfxAirAccess::Unknown}}},
|
||||
{"__entry_v_struct",
|
||||
{{RgfxAirKind::Buffer, 0, RgfxAirAccess::Read}, {RgfxAirKind::Buffer, 1, RgfxAirAccess::ReadWrite},
|
||||
{RgfxAirKind::Buffer, 2, RgfxAirAccess::Unknown}}},
|
||||
{"__entry_v_scalars",
|
||||
{{RgfxAirKind::Buffer, 0, RgfxAirAccess::ReadWrite}, {RgfxAirKind::Buffer, 1, RgfxAirAccess::Unknown},
|
||||
{RgfxAirKind::Buffer, 2, RgfxAirAccess::Unknown}, {RgfxAirKind::Buffer, 3, RgfxAirAccess::Unknown},
|
||||
{RgfxAirKind::Buffer, 4, RgfxAirAccess::Unknown}, {RgfxAirKind::Buffer, 5, RgfxAirAccess::Unknown},
|
||||
{RgfxAirKind::Buffer, 6, RgfxAirAccess::Unknown}, {RgfxAirKind::Buffer, 7, RgfxAirAccess::Unknown},
|
||||
{RgfxAirKind::Buffer, 8, RgfxAirAccess::Unknown}, {RgfxAirKind::Buffer, 9, RgfxAirAccess::Unknown}}},
|
||||
{"__entry_v_vectors",
|
||||
{{RgfxAirKind::Buffer, 0, RgfxAirAccess::ReadWrite}, {RgfxAirKind::Buffer, 1, RgfxAirAccess::Unknown},
|
||||
{RgfxAirKind::Buffer, 2, RgfxAirAccess::Unknown}, {RgfxAirKind::Buffer, 3, RgfxAirAccess::Unknown},
|
||||
{RgfxAirKind::Buffer, 4, RgfxAirAccess::Unknown}, {RgfxAirKind::Buffer, 5, RgfxAirAccess::Unknown},
|
||||
{RgfxAirKind::Buffer, 6, RgfxAirAccess::Unknown}, {RgfxAirKind::Buffer, 7, RgfxAirAccess::Unknown}}},
|
||||
{"__entry_v_half",
|
||||
{{RgfxAirKind::Buffer, 0, RgfxAirAccess::ReadWrite}, {RgfxAirKind::Buffer, 1, RgfxAirAccess::Unknown},
|
||||
{RgfxAirKind::Buffer, 2, RgfxAirAccess::Unknown}}},
|
||||
{"__entry_v_nested",
|
||||
{{RgfxAirKind::Buffer, 0, RgfxAirAccess::ReadWrite}, {RgfxAirKind::Buffer, 1, RgfxAirAccess::Unknown},
|
||||
{RgfxAirKind::Buffer, 2, RgfxAirAccess::Read}}},
|
||||
{"__entry_v_union",
|
||||
{{RgfxAirKind::Buffer, 0, RgfxAirAccess::ReadWrite}, {RgfxAirKind::Buffer, 1, RgfxAirAccess::Unknown}}},
|
||||
{"__entry_v_image",
|
||||
{{RgfxAirKind::Buffer, 0, RgfxAirAccess::ReadWrite}, {RgfxAirKind::Texture, 0, RgfxAirAccess::Write},
|
||||
{RgfxAirKind::Buffer, 1, RgfxAirAccess::Unknown}}},
|
||||
};
|
||||
|
||||
// Whether `air` binds `want`, in its order, each of count 1 at a fixed index.
|
||||
bool bindsAsRead(const RgfxAirFunction &air, const std::vector<ReadArgument> &want)
|
||||
{
|
||||
if (!air.argumentsRead || air.arguments.size() != want.size())
|
||||
return false;
|
||||
for (size_t i = 0; i < want.size(); i++) {
|
||||
const RgfxAirArgument &a = air.arguments[i];
|
||||
if (a.kind != want[i].kind || a.index != want[i].index || a.access != want[i].access || a.count != 1 ||
|
||||
a.anyIndex || a.anyCount || a.writesTextures)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void test_cl_values_read()
|
||||
{
|
||||
id<MTLDevice> metal = MTLCreateSystemDefaultDevice();
|
||||
NSArray<NSString *> *names = nil;
|
||||
NSData *container = clLibraryBytes(metal, kValueSource, &names);
|
||||
std::vector<uint8_t> metallib;
|
||||
if (container == nil || !RgfxMetallibOfClLibrary(static_cast<const uint8_t *>(container.bytes), container.length,
|
||||
metallib)) {
|
||||
fprintf(stderr, "cl_mode_tests: this Mac's Metal answers no OpenCL container: no value kernels checked\n");
|
||||
return;
|
||||
}
|
||||
CHECK(names.count == std::size(kValueKernels));
|
||||
// Each kernel's module, as the reader reads it.
|
||||
for (const auto &want : kValueKernels) {
|
||||
const uint8_t *bc = nullptr;
|
||||
size_t n = 0;
|
||||
std::vector<RgfxAirFunction> fns;
|
||||
CHECK(rgfxMetallibBitcode(metallib.data(), metallib.size(), want.name, &bc, &n) &&
|
||||
rgfxReadAirFunctions(bc, n, fns));
|
||||
const bool one = fns.size() == 1 && fns[0].name == want.name && fns[0].stage == RgfxAirStage::Kernel;
|
||||
if (!one || !bindsAsRead(fns[0], want.arguments)) {
|
||||
fprintf(stderr, "cl_mode_tests: %s: %zu functions read\n", want.name, fns.size());
|
||||
for (const RgfxAirFunction &f : fns)
|
||||
for (const RgfxAirArgument &a : f.arguments)
|
||||
fprintf(stderr, "cl_mode_tests: %s: %s read %d: kind %d index %u count %u access %d\n", want.name,
|
||||
f.name.c_str(), f.argumentsRead, static_cast<int>(a.kind), a.index, a.count,
|
||||
static_cast<int>(a.access));
|
||||
}
|
||||
CHECK(one && bindsAsRead(fns[0], want.arguments));
|
||||
}
|
||||
// Through a library over the program, as the GL renderer makes its kernels: each function's
|
||||
// arguments, and its OpenCL-mode variant's, are the module's, logged as unread for none.
|
||||
TestRing &ring = *new TestRing(0x40000);
|
||||
RgfxRingWriter &writer =
|
||||
*new RgfxRingWriter(ring.base(), ring.dataSize, RGFX_CONTROL_RING_ID, TestRing::doorbell, nullptr);
|
||||
RgfxIdSpace &ids = *new RgfxIdSpace();
|
||||
RgfxSerializer &ser = *new RgfxSerializer(writer, ids);
|
||||
ring.setConsumerIdle(1);
|
||||
static RgfxClModeTestsDevice *device = [RgfxClModeTestsDevice new];
|
||||
device.openCLMode = YES;
|
||||
uint32_t status = 0;
|
||||
RgfxLibrary *program = [RgfxLibrary libraryWithData:metallib.data()
|
||||
length:metallib.size()
|
||||
key:nullptr
|
||||
device:(id<MTLDevice>)device
|
||||
serializer:&ser
|
||||
status:&status];
|
||||
[program rgfxSetSource:dispatch_data_create(container.bytes, container.length, nil,
|
||||
^{
|
||||
(void)container;
|
||||
})
|
||||
shadowMaker:^id<MTLLibrary>(dispatch_data_t) {
|
||||
return nil;
|
||||
}];
|
||||
drainRing(ring);
|
||||
CHECK(program != nil);
|
||||
const uint8_t pluginBytes[32] = {2};
|
||||
NSData *plugin = [NSData dataWithBytes:pluginBytes length:sizeof pluginBytes];
|
||||
for (const auto &want : kValueKernels) {
|
||||
RgfxFunction *kernel = (RgfxFunction *)[program newFunctionWithName:@(want.name)];
|
||||
std::shared_ptr<const RgfxAirFunction> air = [kernel rgfxAirFunction];
|
||||
CHECK(air != nullptr && bindsAsRead(*air, want.arguments));
|
||||
id<MTLFunction> variant = [kernel newFunctionWithPluginData:plugin bitcodeType:2];
|
||||
CHECK([(id)variant isKindOfClass:[RgfxFunction class]] && [(RgfxFunction *)variant rgfxAirFunction] == air);
|
||||
CHECK(rgfxAirUnreadFirstLog(want.name)); // not logged as unread
|
||||
}
|
||||
RgfxFunction *image = (RgfxFunction *)[program newFunctionWithName:@"__entry_v_image"];
|
||||
CHECK([image rgfxAirFunction] != nullptr && rgfxAirWritesTexture(*[image rgfxAirFunction], 0) &&
|
||||
!rgfxAirWritesTexture(*[image rgfxAirFunction], 1));
|
||||
drainRing(ring);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void rgfxClModeTests()
|
||||
@@ -540,5 +716,6 @@ void rgfxClModeTests()
|
||||
test_cl_bindings_of();
|
||||
test_cl_function_of_opencl_mode();
|
||||
test_cl_images_of_opencl_mode();
|
||||
test_cl_values_read();
|
||||
}
|
||||
}
|
||||
@@ -4657,8 +4657,9 @@ void test_library_air_arguments()
|
||||
CHECK([(RgfxFunction *)[bare newFunctionWithName:@"every_access"] rgfxAirFunction] == nullptr);
|
||||
drainRing(fx.ring);
|
||||
|
||||
// A function whose module does not read (its function table's kind spoilt), in two libraries
|
||||
// of the same bytes: unknown in each, its line logged once a process.
|
||||
// A function whose arguments do not read (its function table's kind spoilt), in two libraries
|
||||
// of the same bytes: unknown in each, its line logged once a process, and the library's other
|
||||
// functions read.
|
||||
std::vector<uint8_t> spoilt(static_cast<const uint8_t *>(bytes.bytes),
|
||||
static_cast<const uint8_t *>(bytes.bytes) + bytes.length);
|
||||
const char kind[] = "air.visible_function_table";
|
||||
|
||||
Reference in new issue
Block a user