✅ Hold stage-in on a Mac's Intel GPU to the quotient

test_stage_in_matches_metal runs on every GPU of the Mac since the commit
before. On a UHD 630 under macOS 26.7.1 (a probe of all 65 cases, every thread
compared), 56 cases match Metal's [[stage_in]] kernel bit for bit and 9 do not:

- The unsigned 8-bit normalized formats (UCharNormalized to UChar4Normalized,
  UChar4Normalized_BGRA; 6 cases): Metal's lane is the code times f32(1/255)
  in every lane, where the M3's Metal answers the correctly rounded quotient,
  and ours is within 1 ULP of that quotient in every lane. On a Mac's own
  Intel GPU, a component lane of ours that differs from Metal's now passes
  within 1 ULP of the quotient the test computes on the CPU, the M3's answer,
  and each case that used this prints a line with the count of such lanes.
- Int1010102Normalized: on this GPU our lanes read as the unmasked word; the
  M3 and our device are exact.
- UInt1010102Normalized: far off on this GPU, cause not found; the M3 and our
  device are exact.
- FloatRG11B10: this GPU's Metal answers 2^31 for a NaN code; the M3's quiet
  NaN is the reference.

Those three formats are not compared on a Mac's own Intel GPU: each case still
makes and runs both pipelines, and prints a SKIP line with its reason. Every
other lane on every GPU stays bit for bit, and the lowering does not change.

The GPU is told apart as in the commit before: the PCI vendor-id, 0x8086,
read from the IORegistry upward from the device's registryID (the probe read
0x8086 on the UHD 630 and 0x1002 on the Radeon Pro 5300M), never on our
device. Test code only.
This commit is contained in:
Anees Iqbal committed 2026-12-05 03:26:16 -08:00
1 parent 9a5ad80ab7
commit e8cabf95a1
1 file changed
+99 -4
+99 -4
View File
@@ -1392,6 +1392,60 @@ uint32_t pciVendorOf(id<MTLDevice> metal)
return vendor;
}
// The components of an unsigned 8-bit normalized format (UCharNormalized to UChar4Normalized, and
// UChar4Normalized_BGRA); 0 for any other format.
unsigned unsignedByteNormalizedComponents(MTLAttributeFormat format)
{
switch (format) {
case MTLAttributeFormatUCharNormalized:
return 1;
case MTLAttributeFormatUChar2Normalized:
return 2;
case MTLAttributeFormatUChar3Normalized:
return 3;
case MTLAttributeFormatUChar4Normalized:
case MTLAttributeFormatUChar4Normalized_BGRA:
return 4;
default:
return 0;
}
}
// The element of the attribute's buffer that thread `t` of case `c` fetches, as its layout steps:
// by the thread's x or y over the step rate, by the index at its x, or the first for a constant.
size_t stageInElement(const StageInCase &c, const std::vector<uint8_t> &indices, unsigned t)
{
const unsigned x = t % c.width, y = t / c.width;
switch (c.step) {
case MTLStepFunctionThreadPositionInGridX:
return x / c.rate;
case MTLStepFunctionThreadPositionInGridY:
return y / c.rate;
case MTLStepFunctionThreadPositionInGridXIndexed:
if (c.indexType == MTLIndexTypeUInt32) {
uint32_t k;
std::memcpy(&k, &indices[4 * x], 4);
return k;
} else {
uint16_t k;
std::memcpy(&k, &indices[2 * x], 2);
return k;
}
default:
return 0;
}
}
// The ULPs between the floats of `a` and `b`, of either sign.
uint64_t ulpsBetween(uint32_t a, uint32_t b)
{
auto ordered = [](uint32_t v) {
return (v & 0x80000000u) ? -static_cast<int64_t>(v & 0x7fffffffu) : static_cast<int64_t>(v);
};
const int64_t d = ordered(a) - ordered(b);
return static_cast<uint64_t>(d < 0 ? -d : d);
}
// A GL kernel's control point input, specialized under a stage input descriptor
// (RgfxGLSpecializeStageIn), fetches what Metal's own [[stage_in]] kernel fetches under the same
// descriptor on the GPU `metal`, bit for bit: every MTLAttributeFormat into a field of four
@@ -1406,12 +1460,21 @@ uint32_t pciVendorOf(id<MTLDevice> metal)
// pciVendorOf) every case still makes both pipelines and runs, but the lanes are not compared, and
// the test says so on a SKIP line: there our specialized kernel's lanes 2 and 3 read 0 (measured on
// a Radeon Pro 5300M under macOS 26.7.1), where its IR stores every lane and the M3 matches Metal;
// the cause lies past our IR, and its row is parked.
// the cause lies past our IR, and its row is parked. On a Mac's own Intel GPU (PCI vendor 0x8086),
// measured on a UHD 630 under macOS 26.7.1, Metal's fetch of an unsigned 8-bit normalized format
// multiplies the code by f32(1/255) where the M3's answers the quotient: a component lane of ours
// that differs from Metal's there passes within 1 ULP of the correctly rounded quotient, computed
// here, the M3's answer, and the case says so on a line with the count of such lanes. Three formats
// are not compared there, each on a SKIP line with its reason: Int1010102Normalized and
// UInt1010102Normalized, whose lanes of ours read wrong on that GPU while the M3 and our device are
// exact, and FloatRG11B10, for which that GPU's Metal answers 2^31 for a NaN code. Every other lane
// stays bit for bit.
void stageInMatchesMetalOn(id<MTLDevice> metal, const char *path)
{
const bool guest = isOurDevice(metal);
const uint32_t vendor = guest ? 0 : pciVendorOf(metal);
const bool compare = vendor != 0x1002;
const bool intel = vendor == 0x8086;
if (!compare)
fprintf(stderr,
"SKIP test_stage_in_matches_metal's comparison on %s (PCI vendor 0x%04x): the 5300M zeroes lanes "
@@ -1550,16 +1613,43 @@ void stageInMatchesMetalOn(id<MTLDevice> metal, const char *path)
runStageInCase(metal, queue, ourPipeline, c, data, indices, got);
if (!compare)
continue;
const char *skip = nullptr;
if (intel && c.format == MTLAttributeFormatInt1010102Normalized)
skip = "on this GPU our lanes read as the unmasked word; the M3 and our device are exact";
else if (intel && c.format == MTLAttributeFormatUInt1010102Normalized)
skip = "far off on this GPU, cause not found; the M3 and our device are exact";
else if (intel && c.format == MTLAttributeFormatFloatRG11B10)
skip = "this GPU's Metal answers 2^31 for a NaN code; the M3's quiet NaN is the reference";
if (skip != nullptr) {
fprintf(stderr, "SKIP stage input oracle on %s: format %lu field %s: %s\n", metal.name.UTF8String,
static_cast<unsigned long>(c.format), c.typeName, skip);
continue;
}
compared++;
const bool anyNaN = guest && nanEncodingIsTheDevices(c.format);
unsigned wrong = 0;
const unsigned byteNormalized = intel ? unsignedByteNormalizedComponents(c.format) : 0;
unsigned wrong = 0, allowed = 0;
for (unsigned t = 0; t < c.width * c.height; t++) {
uint32_t e[4] = {}, g[4] = {};
std::memcpy(e, &expected[16 * t], 4 * c.lanes);
std::memcpy(g, &got[16 * t], 4 * c.lanes);
const uint8_t *element = &data[stageInElement(c, indices, t) * c.stride + c.offset];
bool same = true;
for (unsigned k = 0; k < c.lanes; k++)
same = same && (e[k] == g[k] || (anyNaN && isNaNBits(e[k]) && isNaNBits(g[k])));
for (unsigned k = 0; k < c.lanes; k++) {
if (e[k] == g[k] || (anyNaN && isNaNBits(e[k]) && isNaNBits(g[k])))
continue;
if (k < byteNormalized) {
const bool bgra = c.format == MTLAttributeFormatUChar4Normalized_BGRA;
const float quotient = static_cast<float>(element[bgra && k != 1 && k != 3 ? 2 - k : k]) / 255.0f;
uint32_t q;
std::memcpy(&q, &quotient, 4);
if (ulpsBetween(g[k], q) <= 1) {
allowed++;
continue;
}
}
same = false;
}
if (!same && wrong++ < 3)
fprintf(stderr,
"stage input oracle: format %lu field %s step %lu: thread %u: Metal %08x %08x %08x %08x, "
@@ -1568,6 +1658,11 @@ void stageInMatchesMetalOn(id<MTLDevice> metal, const char *path)
e[0], e[1], e[2], e[3], g[0], g[1], g[2], g[3]);
}
CHECK(wrong == 0);
if (allowed != 0)
fprintf(stderr,
"stage input oracle on %s: format %lu field %s: %u lanes passed within 1 ULP of the correctly "
"rounded quotient, not equal to this GPU's Metal, which multiplies by f32(1/255)\n",
metal.name.UTF8String, static_cast<unsigned long>(c.format), c.typeName, allowed);
}
fprintf(stderr, "stage input oracle: %s (PCI vendor 0x%04x): %u of %zu cases compared\n", metal.name.UTF8String,
vendor, compared, cases.size());