Skip to content
Draft
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
4 changes: 4 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -18513,3 +18513,7 @@ ctest --preset dev --no-tests=error -j1 -R '^native_pixel_atomic_(warp|hardware)
```

Repeat with the release preset. The source-built fixture verifies 13 actual draws and four refusal controls per adapter/configuration, including contention, guard words, active out-of-view, odd raster edges and real WQM quad-neighbor reads with and without GLC. WQM+GLC records returned old values through a second exact atomic in a private lane slot and checks a valid serialization of the varying addends. Local RX 7900 XT/WARP Debug/Release integration over main 4b0350e passed 52 draws, 3328 DWORD and 1664 pixel comparisons, and 16 refusal controls. The debug layer was unavailable; actual title rendering is not established.

## Lane-local operations in masked pixel regions

[Native DXIL lowering and 8×2 readback fixture](experiments/native-pixel-masked-region-alu.md)
13 changes: 13 additions & 0 deletions docs/experiments/native-pixel-masked-region-alu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Lane-local operations in masked pixel regions

The native pixel translator can flatten a proven EXEC partition and merge each VGPR write with its incoming bits. The supported additions cover `s_clause`, the three `v_fma_mix*` forms, and texture loads and samples with explicit LOD or gradients through fixed bindings. Fixed table buffer loads redirect inactive addresses to the start of the bound view. The binding records the minimum size for that read.

An arm can use a VCC comparison as the lane bit for `v_cndmask_b32`, and can use numeric scalar arithmetic or fixed-offset scalar buffer reads. The translator expires the arm's SGPR, VCC and SCC results at the corresponding complement or restore. It rejects saved EXEC clobbers. Implicit derivatives, cross-lane operations and new memory writes are outside the supported set. `image_sample_d_cl`, `image_sample_c_l` and graphics MTBUF forms have no lowering in this path.

```powershell
cmake --build out/build/dev --target compile_dxil offscreen_check native_pixel_ir_dump -- -j 6
ctest --preset pr --no-tests=error --output-on-failure -R '^native_pixel_masked_region_alu_controls$'
ctest --test-dir out/build/dev --no-tests=error --output-on-failure -R '^native_pixel_masked_region_alu_(warp|hardware)$'
```

Repeat with `out/build/release` for Release. The controls validate the generated DXIL and check refusals for implicit sampling, stores, wave operations, escaped scalar results and saved EXEC clobbers. The GPU fixture checks every pixel of an 8×2 draw. Its wrong reference uses the THEN result in the six inactive pixels and must first differ at pixel `(0,0)`. The fixture also gives inactive lanes a deliberately out-of-view raw buffer address.
9 changes: 8 additions & 1 deletion src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -1056,27 +1056,34 @@ struct DirectCompute {
valid = ir.value("and i1 " + valid + ", " + cmp("ule", within, to_string(stride - offset - 4*n)));
start = ir.binary("add", start, within);
}
if (pixel_stage && !graphics_predicate.empty())
start = select(graphics_predicate, start, "0");
auto result = load_buffer(h, start, true, n);
for (unsigned j=0; j<n; ++j) {
auto word = ir.value("extractvalue %dx.types.ResRet.i32 " + result + ", " + to_string(j));
vector_write(data + j, select(valid, word, "0"));
}
return;
}
const bool masked_read = !store && pixel_stage && !graphics_predicate.empty() &&
s.at(base).type == Symbol::Type::descriptor;
if (!store && raw && s.at(base).type == Symbol::Type::root_address) {
const auto &low = s.at(base);
if (!raw_root_descriptor(s, base) || !(i.a & 0x1000) || (i.b >> 24) != 128)
refuse(i.pc, "unsupported raw root buffer descriptor or offset");
h = root_buffer_handle(low.value, 4*n);
} else {
h = resource(i, s, base, kind, raw ? 4 : n * (half ? 2 : 4));
h = resource(i, s, base, kind, raw && !masked_read ? 4 : n * (half ? 2 : 4));
}
auto addr = (i.a & (raw ? 0x1000 : 0x2000)) ? read(256 + (i.b & 255)) : "0";
if (raw) {
addr = ir.binary("add", addr, to_string(i.a & 4095));
addr = ir.binary("and", ir.binary("add", addr, operand32(i, i.b >> 24)), "4294967292");
}
if (!store) {
// Inactive lanes may carry offsets outside the view. Use its
// first element, whose minimum size is recorded in the binding.
if (masked_read) addr = select(graphics_predicate, addr, "0");
auto ret = load_buffer(h, addr, raw, n);
for (unsigned k = 0; k < n; ++k)
vector_write(data + k, ir.value("extractvalue %dx.types.ResRet.i32 " + ret + ", " + to_string(k)));
Expand Down
97 changes: 87 additions & 10 deletions src/gfx1013_direct_graphics.inl
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,70 @@ struct GraphicsMaskRegions {
std::multimap<unsigned, unsigned> entries;
for (const auto &i : program.code)
if (i.branch()) entries.emplace(i.target(), i.pc);
auto flattened_op = [&](size_t index, unsigned saved, std::set<unsigned> &writes) {
const auto &i = program.code[index];
const auto &state = program.before[index];
if (i.dpp_control || i.dpp8_selectors || carry_destination(i)) return false;
if (local_alu(i) || i.name == "s_waitcnt" || i.name == "s_nop" || i.name == "s_clause")
return true;
if (vertex) return false;
if (i.enc == Encoding::Mix)
return i.name == "v_fma_mix_f32" || i.name == "v_fma_mixlo_f16" || i.name == "v_fma_mixhi_f16";
if (i.enc == Encoding::Image) {
// Explicit LOD/gradients and texel loads do not observe the other lanes.
// Speculation must also use a fixed resource and sampler binding.
const auto &texture = state[((i.b >> 16) & 31) * 4];
if (texture.type != Symbol::Type::descriptor || texture.value >= 16) return false;
if (i.op == 0 || i.op == 1) return true;
return (i.op == 0x22 || i.op == 0x24 || i.op == 0x27 || i.op == 0x2f) &&
state[((i.b >> 21) & 31) * 4].type == Symbol::Type::constant;
}
if (i.enc == Encoding::Mubuf) {
const auto &buffer = state[((i.b >> 16) & 31) * 4];
return (i.name == "buffer_load_dword" || i.name == "buffer_load_format") &&
buffer.type == Symbol::Type::descriptor && buffer.value < 16;
}
if (i.enc == Encoding::Vopc && i.dst == 106 && i.name.substr(0,6) == "v_cmp_" &&
!i.clamp && !i.scale && !i.subword && i.name.find("class") == Name::npos) {
// Only CNDMASK can consume this invocation bit in the arm.
writes.insert(106);
if (!wave32) writes.insert(107);
return true;
}
unsigned width = 1;
if (i.enc == Encoding::Smem) {
const auto &buffer = state[i.src[0]];
// Fixed-offset scalar buffer reads carry a minimum view size
// in the binding. Their results are numeric, not descriptors.
if (i.op < 8 || i.op > 12 || (i.b >> 25) != 125 || (i.b & 0x100003) ||
buffer.type != Symbol::Type::descriptor || buffer.value >= 16) return false;
width = memory_count(i);
} else {
auto numeric = [&](unsigned src) {
if (src >= 106) return constant32(i, src).has_value();
const auto type = state[src].type;
return type == Symbol::Type::unknown || type == Symbol::Type::constant || type == Symbol::Type::masked;
};
const bool sets_scc = i.name == "s_add_u32" || i.name == "s_lshl_b32";
const bool binary = sets_scc || i.name == "s_cselect_b32";
if (i.name != "s_movk_i32" && !((binary || i.name == "s_mov_b32") &&
numeric(i.src[0]) && (!binary || numeric(i.src[1])))) return false;
if (sets_scc) writes.insert(128);
}
if (i.dst + width > 106) return false;
if (i.dst < saved + (wave32 ? 1u : 2u) && saved < i.dst + width)
refuse(i.pc, "masked graphics arm overwrites its saved EXEC");
for (unsigned r = i.dst; r < i.dst + width; ++r) writes.insert(r);
return true;
};
auto expire_arms = [&](unsigned middle, unsigned end, const std::set<unsigned> &first,
const std::set<unsigned> &second) {
// A skipped wave may never have made these definitions. For VCC
// only the invocation bit of an active lane was available.
if (middle != end) expired[middle].insert(first.begin(), first.end());
expired[end].insert(first.begin(), first.end());
expired[end].insert(second.begin(), second.end());
};
// Only carry SCC into a full numeric mask if its reaching producer
// compares ordinary scalar operands, with no join or intervening
// scalar writer. Per-lane predicates cannot establish those 64 bits.
Expand Down Expand Up @@ -1269,15 +1333,18 @@ struct GraphicsMaskRegions {
for (auto it = entries.upper_bound(save.pc); it != entries.end() && it->first <= restore.pc; ++it)
if (it->second != skip.pc && (!two_arms || it->second != else_skip.pc))
refuse(it->first, "branch enters a masked graphics region");
for (size_t j = k + 2 + split; j < end; ++j) {
std::set<unsigned> first_writes, second_writes;
for (size_t j = begin; j < end; ++j) {
const auto &body = program.code[j];
if (two_arms && (j == middle || j == middle + 1)) continue;
auto &writes = two_arms && j > middle ? second_writes : first_writes;
if (body.pc != program.code[j - 1].next || body.next != program.code[j + 1].pc ||
!(local_alu(body) || body.name == "s_waitcnt" || body.name == "s_nop" ||
(!vertex && body.name == "buffer_atomic_add" && !(body.a & 0x2000)) ||
(body.enc == Encoding::Image && body.op == 0x27)))
!(flattened_op(j, save.dst, writes) ||
(body.enc == Encoding::Image && body.op == 0x27) ||
(!vertex && body.name == "buffer_atomic_add" && !(body.a & 0x2000))))
refuse(body.pc, "masked graphics region requires lane-local vector arithmetic");
}
expire_arms(inverse.pc, restore.pc, first_writes, second_writes);
saves.insert(save.pc);
restores.insert(restore.pc);
if (split) skip_proven(program.code[k + 1]);
Expand Down Expand Up @@ -1316,13 +1383,16 @@ struct GraphicsMaskRegions {
const auto &cmp = code[k];
if (cmp.enc != Encoding::Vopc || cmp.name.substr(0,7) != "v_cmpx_" ||
cmp.dst != 126 || cmp.scale || cmp.clamp || cmp.subword) continue;
auto local = [&](const Inst &i) {
std::set<unsigned> first_writes, second_writes;
auto local = [&](size_t j, std::set<unsigned> &writes) {
const auto &i = code[j];
if (!vertex) return flattened_op(j, 126, writes);
return local_alu(i) || (i.enc == Encoding::Vopc && i.dst == 106 &&
i.name.substr(0,6) == "v_cmp_" && !i.clamp && !i.scale && !i.subword &&
i.name.find("class") == Name::npos);
};
size_t mid = k + 1;
while (mid < code.size() && local(code[mid])) ++mid;
while (mid < code.size() && local(mid, first_writes)) ++mid;
if (mid == code.size()) continue;
const auto &inverse = code[mid];
const bool two_arms=inverse.name=="s_andn2_b64" && inverse.src[1]==126;
Expand All @@ -1331,7 +1401,7 @@ struct GraphicsMaskRegions {
size_t end=mid;
if (two_arms) {
++end;
while (end < code.size() && local(code[end])) ++end;
while (end < code.size() && local(end, second_writes)) ++end;
}
if (end == code.size()) continue;
const auto &restore = code[end];
Expand All @@ -1357,6 +1427,10 @@ struct GraphicsMaskRegions {
}
}
if ((vertex || one_arm) && !local_save) continue;
if (first_writes.count(inverse.src[0]) || first_writes.count(inverse.src[0]+1) ||
second_writes.count(inverse.src[0]) || second_writes.count(inverse.src[0]+1))
refuse(cmp.pc, "masked graphics arm overwrites its saved EXEC");
expire_arms(inverse.pc, restore.pc, first_writes, second_writes);
cmpx_starts.insert(cmp.pc);
enclosing_mask.emplace(cmp.pc,inverse.src[0]);
if (two_arms) complements.insert(inverse.pc);
Expand Down Expand Up @@ -2078,8 +2152,11 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv =
}
}
}
string coverage_predicate;
alu.helper_result_unobserved = [&](unsigned r) {
return straight && r >= 256 && r < 512 && alu.graphics_pc >= last_helper_touch[r-256];
// A flattened arm can also leave covered lanes inactive.
return straight && alu.graphics_predicate == coverage_predicate &&
r >= 256 && r < 512 && alu.graphics_pc >= last_helper_touch[r-256];
};
bool whole_quad = false;
bool exact_after_wqm = false;
Expand Down Expand Up @@ -2548,7 +2625,7 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv =
if (begin) {
// Run read-only sampling with the quad intact for derivatives,
// then merge only the writes made under the guest coverage.
alu.graphics_predicate = covered_lane();
alu.graphics_predicate = coverage_predicate = covered_lane();
} else alu.graphics_predicate.clear();
return;
}
Expand Down Expand Up @@ -2601,7 +2678,7 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv =
if (covered.empty()) {
covered = covered_lane();
}
alu.graphics_predicate = covered;
alu.graphics_predicate = coverage_predicate = covered;
alu.exact_pixel_entry = true;
} else {
alu.graphics_predicate.clear();
Expand Down
14 changes: 14 additions & 0 deletions tests/cmake/native_pixel_masked_region_alu.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
if(TARGET compile_dxil AND TARGET native_pixel_ir_dump AND EXISTS "${INTAKE_PYTHON}")
set(masked_region_check "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_pixel_masked_region_alu_check.py")
add_test(NAME native_pixel_masked_region_alu_controls COMMAND "${INTAKE_PYTHON}" -B "${masked_region_check}"
"$<TARGET_FILE:compile_dxil>" "$<TARGET_FILE:native_pixel_ir_dump>" "${DXC_BIN_DIR}" controls)
set_tests_properties(native_pixel_masked_region_alu_controls PROPERTIES TIMEOUT 90)
if(TARGET offscreen_check)
foreach(backend warp hardware)
add_test(NAME native_pixel_masked_region_alu_${backend} COMMAND "${INTAKE_PYTHON}" -B "${masked_region_check}"
"$<TARGET_FILE:compile_dxil>" "$<TARGET_FILE:native_pixel_ir_dump>" "${DXC_BIN_DIR}"
${backend} "$<TARGET_FILE:offscreen_check>")
set_tests_properties(native_pixel_masked_region_alu_${backend} PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 120)
endforeach()
endif()
endif()
3 changes: 2 additions & 1 deletion tests/desktop_gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ void check_startup_image_3d(ID3D12Device* device, const char* vertex_path, const
bool one_dimensional = false, bool two_dimensional = false,
UINT first_mip = 0, UINT view_levels = 0, UINT mip_base = 4, bool cube = false);
void check_startup_image_load_d16(ID3D12Device* device, const char* vertex_path, const char* pixel_path,
bool depth16, std::uint32_t words[64], bool array = false);
bool depth16, std::uint32_t words[64], bool array = false,
const char* sampler = nullptr, bool raw = false);
int check_startup_image_store(ID3D12Device* device, const char* vertex_path, const char* pixel_path, UINT x, UINT y,
const std::uint32_t expected[4], unsigned coverage = 1);
// Queries colour formats and returns "typed_uav_load_refused" when one has no typed UAV load, before any draw.
Expand Down
9 changes: 5 additions & 4 deletions tests/native_direct_container_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,10 +1050,11 @@ def waterfall_pixel(byte_key=False, modifier=None, *, sample_x=None):
foreign_mask[12] = 0xbe962408
check("unproven-inner-coverage", "gfx1013-container-ps", patched_pixel(len(foreign_mask), foreign_mask,
input_address=1 << 8, input_signature=position_input), "inner EXEC requires saved entry coverage")
unsafe = masked.copy()
unsafe[3] = 0xb0000002 # scalar state changes for the whole guest wave
check("masked-scalar", "gfx1013-container-ps", patched_pixel(len(unsafe), unsafe,
input_address=1 << 8, input_signature=position_input), "lane-local vector arithmetic")
unsafe = [0xbe800381, *masked[:3], 0xb0000002, masked[4], 0x7e000200]
# The masked s_movk_i32 may be skipped wave-wide. The later v_mov
# cannot use its speculative result even though s0 was defined at entry.
check("masked-scalar-escape", "gfx1013-container-ps", patched_pixel(len(unsafe), unsafe,
input_address=1 << 8, input_signature=position_input), "graphics register undefined on an incoming path: 0")
full_mask = lanes + [0xbe80036a]
check("full-mask", "gfx1013-container-ps", patched_pixel(len(full_mask), full_mask),
"consumes a full wave mask")
Expand Down
Loading
Loading