Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions runtime/src/subgroup_size.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,21 @@ inline SubgroupRequest decide_subgroup_size(const ShaderEntry& entry, const Subg
return r;
}
r.wanted = entry.subgroup_size ? entry.subgroup_size : (entry.wave64 ? 64 : 32);
if (!caps.default_size && !caps.max_size) {
// VkPhysicalDeviceSubgroupSizeControlProperties guarantees power-of-two bounds with 1 <= min <= max; a half-zero
// (min == 0, max > 0), inverted, or non-power-of-two range cannot prove requiredSubgroupSize is valid (-02761).
const bool valid_range = subgroup_is_power_of_two(caps.min_size) && subgroup_is_power_of_two(caps.max_size) &&
caps.min_size <= caps.max_size;
if (!caps.default_size && !valid_range) {
r.outcome = SubgroupOutcome::unknown_device;
return r;
}

const bool compute_like = r.stage == VK_SHADER_STAGE_COMPUTE_BIT || r.stage == VK_SHADER_STAGE_MESH_BIT_EXT;
const std::uint64_t max_invocations = std::uint64_t(r.wanted) * caps.max_compute_workgroup_subgroups;
const bool fixed = caps.min_size && caps.min_size == caps.max_size && caps.min_size == r.wanted;
const bool fixed = valid_range && caps.min_size == caps.max_size && caps.min_size == r.wanted;
const bool requirable =
caps.subgroup_size_control && (caps.required_stages & r.stage) && subgroup_is_power_of_two(r.wanted) &&
r.wanted >= caps.min_size && r.wanted <= caps.max_size &&
valid_range && caps.subgroup_size_control && (caps.required_stages & r.stage) &&
subgroup_is_power_of_two(r.wanted) && r.wanted >= caps.min_size && r.wanted <= caps.max_size &&
(!compute_like || subgroup_workgroup_fits(entry.local_size, max_invocations));

if (fixed) {
Expand Down
67 changes: 67 additions & 0 deletions runtime/test/subgroup_size_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,73 @@ int main() {
CHECK(decide_subgroup_size(entry("ps", false), caps).outcome == SubgroupOutcome::unavailable);
}

// --- Malformed or half-zero [min_size, max_size] ranges ---------------------------------------------------------
{
// Half-zero range (min_size == 0, max_size == 64): lower bound is unset, so requiredSubgroupSize must not be
// chained (VUID -requiredSubgroupSize-02761); fall back to default_size or unavailable.
SubgroupSizeCaps caps = amd();
caps.min_size = 0;
caps.max_size = 64;
caps.default_size = 64;
auto r = decide_subgroup_size(entry("cs", true), caps);
CHECK(r.outcome == SubgroupOutcome::device_default && r.size == 0 && r.flags == 0);
r = decide_subgroup_size(entry("cs", false), caps);
CHECK(r.outcome == SubgroupOutcome::unavailable && r.size == 0 && r.flags == 0);
r = decide_subgroup_size(entry("ps", false), caps);
CHECK(r.outcome == SubgroupOutcome::unavailable && r.size == 0 && r.flags == 0);

// Same half-zero range with default_size == 32: wave64 cannot be required, wave32 falls back to default.
caps.default_size = 32;
r = decide_subgroup_size(entry("cs", true), caps);
CHECK(r.outcome == SubgroupOutcome::unavailable && r.size == 0 && r.flags == 0);
r = decide_subgroup_size(entry("cs", false), caps);
CHECK(r.outcome == SubgroupOutcome::device_default && r.size == 0 && r.flags == 0);

// Half-zero range with default_size == 0: neither a default nor a valid [min, max] range is known.
caps.default_size = 0;
r = decide_subgroup_size(entry("cs", true), caps);
CHECK(r.outcome == SubgroupOutcome::unknown_device && r.size == 0 && r.flags == 0);
r = decide_subgroup_size(entry("cs", false), caps);
CHECK(r.outcome == SubgroupOutcome::unknown_device && r.size == 0 && r.flags == 0);

// Mirror half-zero (min_size == 32, max_size == 0) and inverted range (min_size > max_size).
caps = amd();
caps.default_size = 0;
caps.min_size = 32;
caps.max_size = 0;
CHECK(decide_subgroup_size(entry("cs", false), caps).outcome == SubgroupOutcome::unknown_device);
caps.min_size = 64;
caps.max_size = 32;
CHECK(decide_subgroup_size(entry("cs", true), caps).outcome == SubgroupOutcome::unknown_device);
caps.default_size = 64;
CHECK(decide_subgroup_size(entry("cs", true), caps).outcome == SubgroupOutcome::device_default);
CHECK(decide_subgroup_size(entry("cs", false), caps).outcome == SubgroupOutcome::unavailable);

// Non-power-of-two bounds violate VkPhysicalDeviceSubgroupSizeControlProperties and must not be trusted.
caps = amd();
caps.min_size = 24;
caps.max_size = 64;
caps.default_size = 64;
r = decide_subgroup_size(entry("cs", false), caps);
CHECK(r.outcome == SubgroupOutcome::unavailable && r.size == 0 && r.flags == 0);
r = decide_subgroup_size(entry("cs", true), caps);
CHECK(r.outcome == SubgroupOutcome::device_default && r.size == 0 && r.flags == 0);
caps.min_size = caps.max_size = 48;
ShaderEntry odd = entry("cs", true);
odd.subgroup_size = 48;
CHECK(decide_subgroup_size(odd, caps).outcome == SubgroupOutcome::unavailable);

// Control: a valid power-of-two range with default_size == 0 still requires sizes within [min, max].
caps = amd();
caps.default_size = 0;
r = decide_subgroup_size(entry("cs", true), caps);
CHECK(r.outcome == SubgroupOutcome::required && r.size == 64);
r = decide_subgroup_size(entry("cs", false), caps);
CHECK(r.outcome == SubgroupOutcome::required && r.size == 32);
odd.subgroup_size = 16;
CHECK(decide_subgroup_size(odd, caps).outcome == SubgroupOutcome::unavailable);
}

// --- The create-info chain -------------------------------------------------------------------------------------
{
const SubgroupSizeCaps caps = amd();
Expand Down
Loading