🐛 Answer stage input attributes from Metal's own function

A function specialised by constant values answered as active an attribute that
Metal's compiler leaves inactive once it folds the values: the bundle read the
AIR itself, and its reading keeps reads that are dead only after folding.
RealityKit's vsRealityPbr, under its usdz preview's constants, answered color
active where the AMD 5300M answers it inactive; unspecialised, it answered the
15 attributes its metadata declares where Metal answers none.

Where a function's library has Metal's own library (the one Metal compiled,
linked or stitched, or one Metal makes over the bytes of a library of a file,
of data or of a bundle), its attributes are now those of Metal's own function
of the same name and constant values. The metadata and the AIR's
specialisation remain where there is no such library. oracle_fnreflect gains
v_deadfold, whose attributes' reads are dead only after folding, and the
shadow test checks that every function asks Metal's own.
This commit is contained in:
Anees Iqbal committed 2026-11-16 11:41:25 -08:00
1 parent 7338abf05a
commit 5afdd60765
5 files changed
+109 -48

No files matched your search

+8 -6
View File
@@ -224,12 +224,14 @@ id _Nullable RgfxRenderPipelineReflectionOf(id<MTLFunction> _Nullable vertex,
// Metal has those; nil goes back to Metal's own.
void RgfxPipelineReflectionUseClassesForTests(NSDictionary<NSString *, id> *_Nullable standIns);
// The stage input attributes `function`, one of our libraries' (RgfxFunction), answers: those its
// library declares for it, as its constant values specialise them (RgfxSpecialiseStageAttributes),
// or where its library's metadata declares none, those of Metal's own function of the same values
// in the library Metal compiled, linked or stitched (the library's adopted shadow); in Metal's
// objects (RgfxStageAttributesMake, of MTLVertexAttributeInternal for a vertex function); nil
// where neither declares any, and nil, logged once per reason, where they are not made.
// The stage input attributes `function`, one of our libraries' (RgfxFunction), answers: those of
// Metal's own function of the same name and constant values, where its library has Metal's own
// library (the one Metal compiled, linked or stitched, or one made over the library's bytes) and
// Metal makes the function; else those its library declares for it, or where it declares none
// those of Metal's own unspecialised function in the library Metal compiled, as its constant values
// specialise them (RgfxSpecialiseStageAttributes). In Metal's objects (RgfxStageAttributesMake, of
// MTLVertexAttributeInternal for a vertex function); nil where Metal's function answers nil or
// none are declared, and nil, logged once per reason, where they are not made.
NSArray *_Nullable RgfxStageAttributesOf(id<MTLFunction> function);
// The reflection a function answers by itself (RgfxFunctionReflectionMake): nil, logged once per
+58 -29
View File
@@ -139,28 +139,11 @@ id RgfxFunctionReflectionOf(id<MTLFunction> function)
return r;
}
// The stage input attributes Metal's own function declares, where `f`'s library was given Metal's
// own library of the compile, link or stitching that made it as its shadow: each attribute's name,
// index, type, use and patch flags, for a vertex function from vertexAttributes and for a kernel
// from stageInputAttributes. The function is Metal's own of the same name and constant values
// (rgfxShadowOrMake), so `specialised` is set where Metal specialised them; where `f` does not hold
// the values it was specialised by, Metal's own unspecialised function, for its AIR to specialise.
// None where its library was given no shadow or the shadow holds no such function.
// measured: on x86-64 macOS 11.7.11 the metadata of oracle_fnreflect's library compiled from
// source declares no function's attributes (the bundle reads no VATT or VATY tag of it), and
// Metal's own unspecialised function there leaves out an attribute under a function constant,
// where on 26.7.2 the metadata declares them and Metal's function answers that one too.
static void shadowStageAttributes(RgfxFunction *f, std::vector<RgfxStageAttributeDecl> &out, bool &specialised)
// Each attribute of `list`, Metal's attribute objects, as a declaration: its name, index, type, use
// and patch flags.
static std::vector<RgfxStageAttributeDecl> declarationsOf(NSArray *list)
{
RgfxResource *library = [f rgfxLibrary];
if (![library isKindOfClass:[RgfxLibrary class]] || [(RgfxLibrary *)library rgfxAdoptedShadow] == nil)
return;
id<MTLFunction> genuine = [f rgfxShadowOrMake];
specialised = genuine != nil && [f rgfxConstantCount] != 0;
if (genuine == nil && f.name != nil)
genuine = [[(RgfxLibrary *)library rgfxAdoptedShadow] newFunctionWithName:f.name];
NSArray *list = f.functionType == MTLFunctionTypeVertex ? (NSArray *)genuine.vertexAttributes
: (NSArray *)genuine.stageInputAttributes;
std::vector<RgfxStageAttributeDecl> out;
for (MTLAttribute *a in list) {
RgfxStageAttributeDecl d;
d.name = a.name.UTF8String ?: "";
@@ -171,26 +154,72 @@ static void shadowStageAttributes(RgfxFunction *f, std::vector<RgfxStageAttribut
d.patchData = a.patchData;
out.push_back(std::move(d));
}
return out;
}
// The stage input attributes a function of Metal's answers: a vertex function its vertexAttributes,
// a kernel its stageInputAttributes.
static NSArray *attributesOf(id<MTLFunction> genuine, bool vertex)
{
return vertex ? (NSArray *)genuine.vertexAttributes : (NSArray *)genuine.stageInputAttributes;
}
// Metal's own function of `f`, where `f`'s library has Metal's own library: the one of the compile,
// link or stitching that made it (rgfxAdoptShadow:), or one Metal makes over the bytes it was made
// from (a library of a file, of data or of a framework's bundle, rgfxShadow). It is Metal's own
// function of the same name and constant values (rgfxShadowOrMake), specialised by Metal's compiler
// where `f` was; nil where the library has no such library, Metal made none, or `f` was specialised
// by values it does not hold.
static id<MTLFunction> metalFunctionOf(RgfxFunction *f)
{
RgfxResource *library = [f rgfxLibrary];
if (![library isKindOfClass:[RgfxLibrary class]] || [(RgfxLibrary *)library rgfxShadow] == nil)
return nil;
return [f rgfxShadowOrMake];
}
// measured: on x86-64 macOS 11.7.11 the metadata of oracle_fnreflect's library compiled from
// source declares no function's attributes (the bundle reads no VATT or VATY tag of it), and
// Metal's own unspecialised function there leaves out an attribute under a function constant,
// where on 26.7.2 the metadata declares them and Metal's function answers that one too.
// measured: RealityKit's vsRealityPbr (CoreRE.framework's library) specialised by the constants of
// its usdz preview's pipeline answers its attribute 1 inactive on the AMD 5300M under macOS 26.7.1,
// where the attribute's reads are dead only after Metal's compiler folds the values (the bundle's
// own reading of the AIR kept it active); unspecialised, it answers none of the 15 attributes its
// metadata declares.
NSArray *RgfxStageAttributesOf(id<MTLFunction> function)
{
if (![(id)function isKindOfClass:[RgfxFunction class]])
return nil;
RgfxFunction *f = (RgfxFunction *)function;
std::vector<RgfxStageAttributeDecl> attributes;
const bool vertex = f.functionType == MTLFunctionTypeVertex;
NSString *why = nil;
// Metal's own function's answer, where there is one to ask: its compiler decides which reads the
// values leave, and an unspecialised function's answer is not always its metadata's.
if (id<MTLFunction> genuine = metalFunctionOf(f)) {
NSArray *theirs = attributesOf(genuine, vertex);
if (theirs == nil)
return nil;
NSArray *list = RgfxStageAttributesMake(declarationsOf(theirs), vertex, &why);
if (list == nil)
logOnce([NSString stringWithFormat:@"stage input attributes of %@", f.name], why);
return list;
}
// Else those its library's metadata declares, or where it declares none those of Metal's own
// unspecialised function in the library Metal compiled, as its values specialise them in its AIR.
std::vector<RgfxStageAttributeDecl> attributes;
const bool read = [f rgfxDeclaredStageAttributes:attributes];
// Where the metadata declares none, Metal's own function's, where its library has Metal's own
// library (a function of no stage input declares none there either).
bool specialised = false;
if (read && attributes.empty())
shadowStageAttributes(f, attributes, specialised);
if (read && attributes.empty() && f.name != nil) {
RgfxResource *library = [f rgfxLibrary];
id<MTLLibrary> adopted =
[library isKindOfClass:[RgfxLibrary class]] ? [(RgfxLibrary *)library rgfxAdoptedShadow] : nil;
attributes = declarationsOf(attributesOf([adopted newFunctionWithName:f.name], vertex));
}
if (!read) {
why = @"its library's metadata of it was not read";
} else if (attributes.empty()) {
return nil;
} else if ([f rgfxConstantCount] != 0 && !specialised) {
} else if ([f rgfxConstantCount] != 0) {
RgfxResource *library = [f rgfxLibrary];
std::shared_ptr<const RgfxAirEntries> module =
[library isKindOfClass:[RgfxLibrary class]]
@@ -202,7 +231,7 @@ NSArray *RgfxStageAttributesOf(id<MTLFunction> function)
RgfxSpecialiseStageAttributes(*module, module->entries[0], [f rgfxConstants], [f rgfxConstantCount],
attributes, &why);
}
NSArray *list = why == nil ? RgfxStageAttributesMake(attributes, f.functionType == MTLFunctionTypeVertex, &why) : nil;
NSArray *list = why == nil ? RgfxStageAttributesMake(attributes, vertex, &why) : nil;
if (list == nil)
logOnce([NSString stringWithFormat:@"stage input attributes of %@", f.name], why);
return list;
+16 -12
View File
@@ -9782,13 +9782,13 @@ void test_function_stage_attributes_as_metal()
CHECK([fn.ours newFunctionWithName:@"v_order"].vertexAttributes.count == 4);
}
// A function whose library's metadata declares no stage input attributes answers those of Metal's
// own function of the same constant values in the library Metal compiled, its library's adopted
// shadow (RgfxStageAttributesOf): the libraries Metal compiles from tests/stage_attributes.metal and
// the oracle probe's oracle_fnreflect.metal, their bytes made ours without their VATT and VATY
// tags, as macOS 11.7.11 on x86-64 writes none for a library it compiles, answer Metal's own for
// every function and for values that specialise them, each specialisation asked of the shadow.
// With the tags, the shadow is never asked of a function that declares attributes.
// A function whose library has Metal's own library answers the stage input attributes of Metal's
// own function of the same constant values, whether or not its library's metadata declares them
// (RgfxStageAttributesOf): the libraries Metal compiles from tests/stage_attributes.metal and the
// oracle probe's oracle_fnreflect.metal, their bytes made ours with their VATT and VATY tags and
// without them, as macOS 11.7.11 on x86-64 writes none for a library it compiles, answer Metal's own
// for every function and for values that specialise them, each asked of the shadow; v_deadfold's
// attributes whose reads only Metal's folding of the values makes dead among them.
void test_function_stage_attributes_from_the_shadow()
{
id<MTLDevice> metal = MTLCreateSystemDefaultDevice();
@@ -9808,7 +9808,9 @@ void test_function_stage_attributes_from_the_shadow()
{{"v_gated", {{"kOffset", MTLDataTypeBool, 1}, {"kColor", MTLDataTypeBool, 0}}},
{"v_gated", {{"kOffset", MTLDataTypeBool, 0}, {"kColor", MTLDataTypeBool, 0}}},
{"v_deadread", {{"kColor", MTLDataTypeBool, 0}}},
{"v_deadread", {{"kColor", MTLDataTypeBool, 1}}}}},
{"v_deadread", {{"kColor", MTLDataTypeBool, 1}}},
{"v_deadfold", {{"kColor", MTLDataTypeBool, 0}}},
{"v_deadfold", {{"kColor", MTLDataTypeBool, 1}}}}},
};
size_t attributes = 0, functions = 0, renamed = 0, declaring = 0;
for (const Source &src : sources) {
@@ -9870,10 +9872,12 @@ void test_function_stage_attributes_from_the_shadow()
[NSString stringWithFormat:@"%s %@%s", src.env, name, strip ? " untagged" : ""],
attributes);
functions++;
if (!strip && theirs.stageInputAttributes.count > 0) {
CHECK(![shadow.asked containsObject:name]);
// A vertex function or a kernel asks; a function of another stage answers none.
const MTLFunctionType type = theirs.functionType;
CHECK([shadow.asked containsObject:name] ==
(type == MTLFunctionTypeVertex || type == MTLFunctionTypeKernel));
if (!strip && theirs.stageInputAttributes.count > 0)
declaring++;
}
}
for (const auto &[function, values] : src.cases) {
NSMutableString *what = [NSMutableString stringWithFormat:@"%s%s", function, strip ? " untagged" : ""];
@@ -9885,7 +9889,7 @@ void test_function_stage_attributes_from_the_shadow()
[shadow.asked removeAllObjects];
sameStageAttributes(mine, theirs, what, attributes);
functions++;
CHECK([shadow.asked containsObject:@(function)] == (strip != 0));
CHECK([shadow.asked containsObject:@(function)]);
}
}
}
+7 -1
View File
@@ -22,7 +22,7 @@
// and for a name the library does not hold, `LIB/nosuch.reflection = VALUE`. Then functions
// specialised by constant values, the same getters under `S NAME`: v_gated of kOffset true and
// kColor false (`v_gated.offset`), of both true (`v_gated.both`) and of both false
// (`v_gated.none`), v_deadread of kColor false and true (`.off`, `.on`), and oracle_reflection's
// (`v_gated.none`), v_deadread and v_deadfold of kColor false and true (`.off`, `.on`), and oracle_reflection's
// k_constants of kOn true, kOff false and kIndex 5 and v_specialised of kMode 1, 0 and 2.
// Then pipeline states, `P CASE made|nil error=...`, then each property of the state's protocol
// and of the protocols it adopts (allocatedSize is MTLAllocation's) that gives an object or a
@@ -266,6 +266,12 @@ static void specialisations(id<MTLLibrary> fnreflect, id<MTLLibrary> reflection)
[v setConstantValue:&yes type:MTLDataTypeBool withName:@"kColor"];
specialised(fnreflect, @"v_deadread.on", @"v_deadread", v);
v = [MTLFunctionConstantValues new];
[v setConstantValue:&no type:MTLDataTypeBool withName:@"kColor"];
specialised(fnreflect, @"v_deadfold.off", @"v_deadfold", v);
v = [MTLFunctionConstantValues new];
[v setConstantValue:&yes type:MTLDataTypeBool withName:@"kColor"];
specialised(fnreflect, @"v_deadfold.on", @"v_deadfold", v);
v = [MTLFunctionConstantValues new];
int index = 5;
[v setConstantValue:&yes type:MTLDataTypeBool withName:@"kOn"];
[v setConstantValue:&no type:MTLDataTypeBool withName:@"kOff"];
@@ -135,6 +135,26 @@ vertex Out v_deadread(DeadRead in [[stage_in]])
return o;
}
// Attributes read under no function constant whose reads are dead only once the values are folded
// as Metal's compiler folds them: one read through a select on a constant, one through a loop the
// values give no trips.
struct DeadFold {
float4 position [[attribute(0)]];
float4 tint [[attribute(5)]];
float4 extra [[attribute(6)]];
};
vertex Out v_deadfold(DeadFold in [[stage_in]])
{
Out o;
const float4 tint = in.tint * in.position.w;
o.position = in.position;
for (int i = 0; i < (kColor ? 3 : 0); i++)
o.position += in.extra * float(i);
o.color = kColor ? tint : float4(1);
return o;
}
// A stage input beside built-ins and buffers.
struct Mixed {
float3 position [[attribute(0)]];