shader: let structurization own rollback on failure

This commit is contained in:
nmzik
2026-09-11 19:23:07 +04:00
parent 5a31de7955
commit e026bfc9a2
4 changed files with 62 additions and 18 deletions
@@ -552,19 +552,10 @@ TranslateResult TranslateProgram(std::span<const uint32_t> code, const CompileOp
if (cfg.irreducible) {
LogDispatcherFallback(options, cfg, "build");
} else {
const auto unstructured_cfg = cfg;
LOGF("%s phase begin: stage=%s hash=0x%016" PRIx64 " CFG Structurize\n",
GetDumpLabel(options), StageName(options.stage), options.shader_hash);
if (!CFG::Structurize(cfg)) {
const auto failure_kind = cfg.failure_kind;
const auto failure_block = cfg.failure_block;
LogDispatcherFallback(options, cfg, "structurize");
auto failure_reason = std::move(cfg.unsupported_reason);
cfg = unstructured_cfg;
cfg.unsupported = true;
cfg.failure_kind = failure_kind;
cfg.failure_block = failure_block;
cfg.unsupported_reason = std::move(failure_reason);
} else {
LOGF("%s structured CFG success: blocks=%" PRIu64 "\n", GetDumpLabel(options),
static_cast<uint64_t>(cfg.blocks.size()));
@@ -2193,27 +2193,38 @@ bool StructurizeImpl(Graph& graph) {
} // namespace
bool Structurize(Graph& graph) {
Graph original = graph;
if (StructurizeImpl(graph)) {
Graph structured = graph;
if (StructurizeImpl(structured)) {
graph = std::move(structured);
return true;
}
Graph failed_graph = std::move(graph);
graph = std::move(original);
const auto failure_kind = structured.failure_kind;
// Structurization inserts and renumbers blocks. Recover source identity for a
// semantic block; a synthetic block has no corresponding original diagnostic ID.
const auto* failed = structured.FindBlock(structured.failure_block);
const auto original = std::ranges::find_if(graph.blocks, [&](const BasicBlock& block) {
return failed != nullptr && failed->inst_begin != failed->inst_end &&
block.inst_begin == failed->inst_begin && block.inst_end == failed->inst_end &&
block.start_pc == failed->start_pc && block.end_pc == failed->end_pc;
});
const auto failure_block = original != graph.blocks.end() ? original->id : UINT32_MAX;
auto failure_reason = std::move(structured.unsupported_reason);
Graph routed = graph;
const auto route_budget = static_cast<uint32_t>(graph.blocks.size());
// Apply one route at a time and retry. Eagerly routing every matching diamond can
// rewrite unrelated selections that were already structurally valid.
for (uint32_t route_variable = 0; route_variable < route_budget; route_variable++) {
if (!RouteSharedSelectionArm(graph, route_variable)) {
if (!RouteSharedSelectionArm(routed, route_variable)) {
break;
}
Graph routed = graph;
if (StructurizeImpl(routed)) {
graph = std::move(routed);
structured = routed;
if (StructurizeImpl(structured)) {
graph = std::move(structured);
return true;
}
}
graph = std::move(failed_graph);
SetFailure(graph, failure_kind, failure_block, failure_reason);
return false;
}
@@ -110,6 +110,8 @@ struct Graph {
};
Graph BuildGraph(const Decoder::Program& program);
// Commits structured control flow on success; preserves the original graph with
// failure diagnostics on failure. failure_block is an original block ID or UINT32_MAX.
bool Structurize(Graph& graph);
std::string BranchConditionToString(BranchCondition condition);
std::string FailureKindToString(FailureKind kind);