diff options
Diffstat (limited to 'src/shader_recompiler/frontend/ir/structured_control_flow.cpp')
| -rw-r--r-- | src/shader_recompiler/frontend/ir/structured_control_flow.cpp | 742 |
1 files changed, 742 insertions, 0 deletions
diff --git a/src/shader_recompiler/frontend/ir/structured_control_flow.cpp b/src/shader_recompiler/frontend/ir/structured_control_flow.cpp new file mode 100644 index 000000000..2e9ce2525 --- /dev/null +++ b/src/shader_recompiler/frontend/ir/structured_control_flow.cpp | |||
| @@ -0,0 +1,742 @@ | |||
| 1 | // Copyright 2021 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <memory> | ||
| 7 | #include <ranges> | ||
| 8 | #include <string> | ||
| 9 | #include <unordered_map> | ||
| 10 | #include <utility> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | #include <fmt/format.h> | ||
| 14 | |||
| 15 | #include <boost/intrusive/list.hpp> | ||
| 16 | |||
| 17 | #include "shader_recompiler/frontend/ir/basic_block.h" | ||
| 18 | #include "shader_recompiler/frontend/ir/ir_emitter.h" | ||
| 19 | #include "shader_recompiler/object_pool.h" | ||
| 20 | |||
| 21 | namespace Shader::IR { | ||
| 22 | namespace { | ||
| 23 | struct Statement; | ||
| 24 | |||
| 25 | // Use normal_link because we are not guaranteed to destroy the tree in order | ||
| 26 | using ListBaseHook = | ||
| 27 | boost::intrusive::list_base_hook<boost::intrusive::link_mode<boost::intrusive::normal_link>>; | ||
| 28 | |||
| 29 | using Tree = boost::intrusive::list<Statement, | ||
| 30 | // Allow using Statement without a definition | ||
| 31 | boost::intrusive::base_hook<ListBaseHook>, | ||
| 32 | // Avoid linear complexity on splice, size is never called | ||
| 33 | boost::intrusive::constant_time_size<false>>; | ||
| 34 | using Node = Tree::iterator; | ||
| 35 | using ConstNode = Tree::const_iterator; | ||
| 36 | |||
| 37 | enum class StatementType { | ||
| 38 | Code, | ||
| 39 | Goto, | ||
| 40 | Label, | ||
| 41 | If, | ||
| 42 | Loop, | ||
| 43 | Break, | ||
| 44 | Return, | ||
| 45 | Function, | ||
| 46 | Identity, | ||
| 47 | Not, | ||
| 48 | Or, | ||
| 49 | SetVariable, | ||
| 50 | Variable, | ||
| 51 | }; | ||
| 52 | |||
| 53 | bool HasChildren(StatementType type) { | ||
| 54 | switch (type) { | ||
| 55 | case StatementType::If: | ||
| 56 | case StatementType::Loop: | ||
| 57 | case StatementType::Function: | ||
| 58 | return true; | ||
| 59 | default: | ||
| 60 | return false; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | struct Goto {}; | ||
| 65 | struct Label {}; | ||
| 66 | struct If {}; | ||
| 67 | struct Loop {}; | ||
| 68 | struct Break {}; | ||
| 69 | struct Return {}; | ||
| 70 | struct FunctionTag {}; | ||
| 71 | struct Identity {}; | ||
| 72 | struct Not {}; | ||
| 73 | struct Or {}; | ||
| 74 | struct SetVariable {}; | ||
| 75 | struct Variable {}; | ||
| 76 | |||
| 77 | #ifdef _MSC_VER | ||
| 78 | #pragma warning(push) | ||
| 79 | #pragma warning(disable : 26495) // Always initialize a member variable, expected in Statement | ||
| 80 | #endif | ||
| 81 | struct Statement : ListBaseHook { | ||
| 82 | Statement(Block* code_, Statement* up_) : code{code_}, up{up_}, type{StatementType::Code} {} | ||
| 83 | Statement(Goto, Statement* cond_, Node label_, Statement* up_) | ||
| 84 | : label{label_}, cond{cond_}, up{up_}, type{StatementType::Goto} {} | ||
| 85 | Statement(Label, u32 id_, Statement* up_) : id{id_}, up{up_}, type{StatementType::Label} {} | ||
| 86 | Statement(If, Statement* cond_, Tree&& children_, Statement* up_) | ||
| 87 | : children{std::move(children_)}, cond{cond_}, up{up_}, type{StatementType::If} {} | ||
| 88 | Statement(Loop, Statement* cond_, Tree&& children_, Statement* up_) | ||
| 89 | : children{std::move(children_)}, cond{cond_}, up{up_}, type{StatementType::Loop} {} | ||
| 90 | Statement(Break, Statement* cond_, Statement* up_) | ||
| 91 | : cond{cond_}, up{up_}, type{StatementType::Break} {} | ||
| 92 | Statement(Return) : type{StatementType::Return} {} | ||
| 93 | Statement(FunctionTag) : children{}, type{StatementType::Function} {} | ||
| 94 | Statement(Identity, Condition cond_) : guest_cond{cond_}, type{StatementType::Identity} {} | ||
| 95 | Statement(Not, Statement* op_) : op{op_}, type{StatementType::Not} {} | ||
| 96 | Statement(Or, Statement* op_a_, Statement* op_b_) | ||
| 97 | : op_a{op_a_}, op_b{op_b_}, type{StatementType::Or} {} | ||
| 98 | Statement(SetVariable, u32 id_, Statement* op_, Statement* up_) | ||
| 99 | : op{op_}, id{id_}, up{up_}, type{StatementType::SetVariable} {} | ||
| 100 | Statement(Variable, u32 id_) : id{id_}, type{StatementType::Variable} {} | ||
| 101 | |||
| 102 | ~Statement() { | ||
| 103 | if (HasChildren(type)) { | ||
| 104 | std::destroy_at(&children); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | union { | ||
| 109 | Block* code; | ||
| 110 | Node label; | ||
| 111 | Tree children; | ||
| 112 | Condition guest_cond; | ||
| 113 | Statement* op; | ||
| 114 | Statement* op_a; | ||
| 115 | }; | ||
| 116 | union { | ||
| 117 | Statement* cond; | ||
| 118 | Statement* op_b; | ||
| 119 | u32 id; | ||
| 120 | }; | ||
| 121 | Statement* up{}; | ||
| 122 | StatementType type; | ||
| 123 | }; | ||
| 124 | #ifdef _MSC_VER | ||
| 125 | #pragma warning(pop) | ||
| 126 | #endif | ||
| 127 | |||
| 128 | std::string DumpExpr(const Statement* stmt) { | ||
| 129 | switch (stmt->type) { | ||
| 130 | case StatementType::Identity: | ||
| 131 | return fmt::format("{}", stmt->guest_cond); | ||
| 132 | case StatementType::Not: | ||
| 133 | return fmt::format("!{}", DumpExpr(stmt->op)); | ||
| 134 | case StatementType::Or: | ||
| 135 | return fmt::format("{} || {}", DumpExpr(stmt->op_a), DumpExpr(stmt->op_b)); | ||
| 136 | case StatementType::Variable: | ||
| 137 | return fmt::format("goto_L{}", stmt->id); | ||
| 138 | default: | ||
| 139 | return "<invalid type>"; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | std::string DumpTree(const Tree& tree, u32 indentation = 0) { | ||
| 144 | std::string ret; | ||
| 145 | std::string indent(indentation, ' '); | ||
| 146 | for (auto stmt = tree.begin(); stmt != tree.end(); ++stmt) { | ||
| 147 | switch (stmt->type) { | ||
| 148 | case StatementType::Code: | ||
| 149 | ret += fmt::format("{} Block {:04x};\n", indent, stmt->code->LocationBegin()); | ||
| 150 | break; | ||
| 151 | case StatementType::Goto: | ||
| 152 | ret += fmt::format("{} if ({}) goto L{};\n", indent, DumpExpr(stmt->cond), | ||
| 153 | stmt->label->id); | ||
| 154 | break; | ||
| 155 | case StatementType::Label: | ||
| 156 | ret += fmt::format("{}L{}:\n", indent, stmt->id); | ||
| 157 | break; | ||
| 158 | case StatementType::If: | ||
| 159 | ret += fmt::format("{} if ({}) {{\n", indent, DumpExpr(stmt->cond)); | ||
| 160 | ret += DumpTree(stmt->children, indentation + 4); | ||
| 161 | ret += fmt::format("{} }}\n", indent); | ||
| 162 | break; | ||
| 163 | case StatementType::Loop: | ||
| 164 | ret += fmt::format("{} do {{\n", indent); | ||
| 165 | ret += DumpTree(stmt->children, indentation + 4); | ||
| 166 | ret += fmt::format("{} }} while ({});\n", indent, DumpExpr(stmt->cond)); | ||
| 167 | break; | ||
| 168 | case StatementType::Break: | ||
| 169 | ret += fmt::format("{} if ({}) break;\n", indent, DumpExpr(stmt->cond)); | ||
| 170 | break; | ||
| 171 | case StatementType::Return: | ||
| 172 | ret += fmt::format("{} return;\n", indent); | ||
| 173 | break; | ||
| 174 | case StatementType::SetVariable: | ||
| 175 | ret += fmt::format("{} goto_L{} = {};\n", indent, stmt->id, DumpExpr(stmt->op)); | ||
| 176 | break; | ||
| 177 | case StatementType::Function: | ||
| 178 | case StatementType::Identity: | ||
| 179 | case StatementType::Not: | ||
| 180 | case StatementType::Or: | ||
| 181 | case StatementType::Variable: | ||
| 182 | throw LogicError("Statement can't be printed"); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | return ret; | ||
| 186 | } | ||
| 187 | |||
| 188 | bool HasNode(const Tree& tree, ConstNode stmt) { | ||
| 189 | const auto end{tree.end()}; | ||
| 190 | for (auto it = tree.begin(); it != end; ++it) { | ||
| 191 | if (it == stmt || (HasChildren(it->type) && HasNode(it->children, stmt))) { | ||
| 192 | return true; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | return false; | ||
| 196 | } | ||
| 197 | |||
| 198 | Node FindStatementWithLabel(Tree& tree, ConstNode goto_stmt) { | ||
| 199 | const ConstNode label_stmt{goto_stmt->label}; | ||
| 200 | const ConstNode end{tree.end()}; | ||
| 201 | for (auto it = tree.begin(); it != end; ++it) { | ||
| 202 | if (it == label_stmt || (HasChildren(it->type) && HasNode(it->children, label_stmt))) { | ||
| 203 | return it; | ||
| 204 | } | ||
| 205 | } | ||
| 206 | throw LogicError("Lift label not in tree"); | ||
| 207 | } | ||
| 208 | |||
| 209 | void SanitizeNoBreaks(const Tree& tree) { | ||
| 210 | if (std::ranges::find(tree, StatementType::Break, &Statement::type) != tree.end()) { | ||
| 211 | throw NotImplementedException("Capturing statement with break nodes"); | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | size_t Level(Node stmt) { | ||
| 216 | size_t level{0}; | ||
| 217 | Statement* node{stmt->up}; | ||
| 218 | while (node) { | ||
| 219 | ++level; | ||
| 220 | node = node->up; | ||
| 221 | } | ||
| 222 | return level; | ||
| 223 | } | ||
| 224 | |||
| 225 | bool IsDirectlyRelated(Node goto_stmt, Node label_stmt) { | ||
| 226 | const size_t goto_level{Level(goto_stmt)}; | ||
| 227 | const size_t label_level{Level(label_stmt)}; | ||
| 228 | size_t min_level; | ||
| 229 | size_t max_level; | ||
| 230 | Node min; | ||
| 231 | Node max; | ||
| 232 | if (label_level < goto_level) { | ||
| 233 | min_level = label_level; | ||
| 234 | max_level = goto_level; | ||
| 235 | min = label_stmt; | ||
| 236 | max = goto_stmt; | ||
| 237 | } else { // goto_level < label_level | ||
| 238 | min_level = goto_level; | ||
| 239 | max_level = label_level; | ||
| 240 | min = goto_stmt; | ||
| 241 | max = label_stmt; | ||
| 242 | } | ||
| 243 | while (max_level > min_level) { | ||
| 244 | --max_level; | ||
| 245 | max = max->up; | ||
| 246 | } | ||
| 247 | return min->up == max->up; | ||
| 248 | } | ||
| 249 | |||
| 250 | bool IsIndirectlyRelated(Node goto_stmt, Node label_stmt) { | ||
| 251 | return goto_stmt->up != label_stmt->up && !IsDirectlyRelated(goto_stmt, label_stmt); | ||
| 252 | } | ||
| 253 | |||
| 254 | bool SearchNode(const Tree& tree, ConstNode stmt, size_t& offset) { | ||
| 255 | ++offset; | ||
| 256 | |||
| 257 | const auto end = tree.end(); | ||
| 258 | for (ConstNode it = tree.begin(); it != end; ++it) { | ||
| 259 | ++offset; | ||
| 260 | if (stmt == it) { | ||
| 261 | return true; | ||
| 262 | } | ||
| 263 | if (HasChildren(it->type) && SearchNode(it->children, stmt, offset)) { | ||
| 264 | return true; | ||
| 265 | } | ||
| 266 | } | ||
| 267 | return false; | ||
| 268 | } | ||
| 269 | |||
| 270 | class GotoPass { | ||
| 271 | public: | ||
| 272 | explicit GotoPass(std::span<Block* const> blocks, ObjectPool<Statement, 64>& stmt_pool) | ||
| 273 | : pool{stmt_pool} { | ||
| 274 | std::vector gotos{BuildUnorderedTreeGetGotos(blocks)}; | ||
| 275 | fmt::print(stdout, "BEFORE\n{}\n", DumpTree(root_stmt.children)); | ||
| 276 | for (const Node& goto_stmt : gotos | std::views::reverse) { | ||
| 277 | RemoveGoto(goto_stmt); | ||
| 278 | } | ||
| 279 | fmt::print(stdout, "AFTER\n{}\n", DumpTree(root_stmt.children)); | ||
| 280 | } | ||
| 281 | |||
| 282 | Statement& RootStatement() noexcept { | ||
| 283 | return root_stmt; | ||
| 284 | } | ||
| 285 | |||
| 286 | private: | ||
| 287 | void RemoveGoto(Node goto_stmt) { | ||
| 288 | // Force goto_stmt and label_stmt to be directly related | ||
| 289 | const Node label_stmt{goto_stmt->label}; | ||
| 290 | if (IsIndirectlyRelated(goto_stmt, label_stmt)) { | ||
| 291 | // Move goto_stmt out using outward-movement transformation until it becomes | ||
| 292 | // directly related to label_stmt | ||
| 293 | while (!IsDirectlyRelated(goto_stmt, label_stmt)) { | ||
| 294 | goto_stmt = MoveOutward(goto_stmt); | ||
| 295 | } | ||
| 296 | } | ||
| 297 | // Force goto_stmt and label_stmt to be siblings | ||
| 298 | if (IsDirectlyRelated(goto_stmt, label_stmt)) { | ||
| 299 | const size_t label_level{Level(label_stmt)}; | ||
| 300 | size_t goto_level{Level(goto_stmt)}; | ||
| 301 | if (goto_level > label_level) { | ||
| 302 | // Move goto_stmt out of its level using outward-movement transformations | ||
| 303 | while (goto_level > label_level) { | ||
| 304 | goto_stmt = MoveOutward(goto_stmt); | ||
| 305 | --goto_level; | ||
| 306 | } | ||
| 307 | } else { // Level(goto_stmt) < Level(label_stmt) | ||
| 308 | if (Offset(goto_stmt) > Offset(label_stmt)) { | ||
| 309 | // Lift goto_stmt to above stmt containing label_stmt using goto-lifting | ||
| 310 | // transformations | ||
| 311 | goto_stmt = Lift(goto_stmt); | ||
| 312 | } | ||
| 313 | // Move goto_stmt into label_stmt's level using inward-movement transformation | ||
| 314 | while (goto_level < label_level) { | ||
| 315 | goto_stmt = MoveInward(goto_stmt); | ||
| 316 | ++goto_level; | ||
| 317 | } | ||
| 318 | } | ||
| 319 | } | ||
| 320 | // TODO: Remove this | ||
| 321 | Node it{goto_stmt}; | ||
| 322 | bool sibling{false}; | ||
| 323 | do { | ||
| 324 | sibling |= it == label_stmt; | ||
| 325 | --it; | ||
| 326 | } while (it != goto_stmt->up->children.begin()); | ||
| 327 | while (it != goto_stmt->up->children.end()) { | ||
| 328 | sibling |= it == label_stmt; | ||
| 329 | ++it; | ||
| 330 | } | ||
| 331 | if (!sibling) { | ||
| 332 | throw LogicError("Not siblings"); | ||
| 333 | } | ||
| 334 | |||
| 335 | // goto_stmt and label_stmt are guaranteed to be siblings, eliminate | ||
| 336 | if (std::next(goto_stmt) == label_stmt) { | ||
| 337 | // Simply eliminate the goto if the label is next to it | ||
| 338 | goto_stmt->up->children.erase(goto_stmt); | ||
| 339 | } else if (Offset(goto_stmt) < Offset(label_stmt)) { | ||
| 340 | // Eliminate goto_stmt with a conditional | ||
| 341 | EliminateAsConditional(goto_stmt, label_stmt); | ||
| 342 | } else { | ||
| 343 | // Eliminate goto_stmt with a loop | ||
| 344 | EliminateAsLoop(goto_stmt, label_stmt); | ||
| 345 | } | ||
| 346 | } | ||
| 347 | |||
| 348 | std::vector<Node> BuildUnorderedTreeGetGotos(std::span<Block* const> blocks) { | ||
| 349 | // Assume all blocks have two branches | ||
| 350 | std::vector<Node> gotos; | ||
| 351 | gotos.reserve(blocks.size() * 2); | ||
| 352 | |||
| 353 | const std::unordered_map labels_map{BuildLabels(blocks)}; | ||
| 354 | Tree& root{root_stmt.children}; | ||
| 355 | auto insert_point{root.begin()}; | ||
| 356 | for (Block* const block : blocks) { | ||
| 357 | ++insert_point; // Skip label | ||
| 358 | ++insert_point; // Skip set variable | ||
| 359 | root.insert(insert_point, *pool.Create(block, &root_stmt)); | ||
| 360 | |||
| 361 | if (block->IsTerminationBlock()) { | ||
| 362 | root.insert(insert_point, *pool.Create(Return{})); | ||
| 363 | continue; | ||
| 364 | } | ||
| 365 | const Condition cond{block->BranchCondition()}; | ||
| 366 | Statement* const true_cond{pool.Create(Identity{}, Condition{true})}; | ||
| 367 | if (cond == Condition{true} || cond == Condition{false}) { | ||
| 368 | const bool is_true{cond == Condition{true}}; | ||
| 369 | const Block* const branch{is_true ? block->TrueBranch() : block->FalseBranch()}; | ||
| 370 | const Node label{labels_map.at(branch)}; | ||
| 371 | Statement* const goto_stmt{pool.Create(Goto{}, true_cond, label, &root_stmt)}; | ||
| 372 | gotos.push_back(root.insert(insert_point, *goto_stmt)); | ||
| 373 | } else { | ||
| 374 | Statement* const ident_cond{pool.Create(Identity{}, cond)}; | ||
| 375 | const Node true_label{labels_map.at(block->TrueBranch())}; | ||
| 376 | const Node false_label{labels_map.at(block->FalseBranch())}; | ||
| 377 | Statement* goto_true{pool.Create(Goto{}, ident_cond, true_label, &root_stmt)}; | ||
| 378 | Statement* goto_false{pool.Create(Goto{}, true_cond, false_label, &root_stmt)}; | ||
| 379 | gotos.push_back(root.insert(insert_point, *goto_true)); | ||
| 380 | gotos.push_back(root.insert(insert_point, *goto_false)); | ||
| 381 | } | ||
| 382 | } | ||
| 383 | return gotos; | ||
| 384 | } | ||
| 385 | |||
| 386 | std::unordered_map<const Block*, Node> BuildLabels(std::span<Block* const> blocks) { | ||
| 387 | // TODO: Consider storing labels intrusively inside the block | ||
| 388 | std::unordered_map<const Block*, Node> labels_map; | ||
| 389 | Tree& root{root_stmt.children}; | ||
| 390 | u32 label_id{0}; | ||
| 391 | for (const Block* const block : blocks) { | ||
| 392 | Statement* const label{pool.Create(Label{}, label_id, &root_stmt)}; | ||
| 393 | labels_map.emplace(block, root.insert(root.end(), *label)); | ||
| 394 | Statement* const false_stmt{pool.Create(Identity{}, Condition{false})}; | ||
| 395 | root.push_back(*pool.Create(SetVariable{}, label_id, false_stmt, &root_stmt)); | ||
| 396 | ++label_id; | ||
| 397 | } | ||
| 398 | return labels_map; | ||
| 399 | } | ||
| 400 | |||
| 401 | void UpdateTreeUp(Statement* tree) { | ||
| 402 | for (Statement& stmt : tree->children) { | ||
| 403 | stmt.up = tree; | ||
| 404 | } | ||
| 405 | } | ||
| 406 | |||
| 407 | void EliminateAsConditional(Node goto_stmt, Node label_stmt) { | ||
| 408 | Tree& body{goto_stmt->up->children}; | ||
| 409 | Tree if_body; | ||
| 410 | if_body.splice(if_body.begin(), body, std::next(goto_stmt), label_stmt); | ||
| 411 | Statement* const cond{pool.Create(Not{}, goto_stmt->cond)}; | ||
| 412 | Statement* const if_stmt{pool.Create(If{}, cond, std::move(if_body), goto_stmt->up)}; | ||
| 413 | UpdateTreeUp(if_stmt); | ||
| 414 | body.insert(goto_stmt, *if_stmt); | ||
| 415 | body.erase(goto_stmt); | ||
| 416 | } | ||
| 417 | |||
| 418 | void EliminateAsLoop(Node goto_stmt, Node label_stmt) { | ||
| 419 | Tree& body{goto_stmt->up->children}; | ||
| 420 | Tree loop_body; | ||
| 421 | loop_body.splice(loop_body.begin(), body, label_stmt, goto_stmt); | ||
| 422 | Statement* const cond{goto_stmt->cond}; | ||
| 423 | Statement* const loop{pool.Create(Loop{}, cond, std::move(loop_body), goto_stmt->up)}; | ||
| 424 | UpdateTreeUp(loop); | ||
| 425 | body.insert(goto_stmt, *loop); | ||
| 426 | body.erase(goto_stmt); | ||
| 427 | } | ||
| 428 | |||
| 429 | [[nodiscard]] Node MoveOutward(Node goto_stmt) { | ||
| 430 | switch (goto_stmt->up->type) { | ||
| 431 | case StatementType::If: | ||
| 432 | return MoveOutwardIf(goto_stmt); | ||
| 433 | case StatementType::Loop: | ||
| 434 | return MoveOutwardLoop(goto_stmt); | ||
| 435 | default: | ||
| 436 | throw LogicError("Invalid outward movement"); | ||
| 437 | } | ||
| 438 | } | ||
| 439 | |||
| 440 | [[nodiscard]] Node MoveInward(Node goto_stmt) { | ||
| 441 | Statement* const parent{goto_stmt->up}; | ||
| 442 | Tree& body{parent->children}; | ||
| 443 | const Node label_nested_stmt{FindStatementWithLabel(body, goto_stmt)}; | ||
| 444 | const Node label{goto_stmt->label}; | ||
| 445 | const u32 label_id{label->id}; | ||
| 446 | |||
| 447 | Statement* const goto_cond{goto_stmt->cond}; | ||
| 448 | Statement* const set_var{pool.Create(SetVariable{}, label_id, goto_cond, parent)}; | ||
| 449 | body.insert(goto_stmt, *set_var); | ||
| 450 | |||
| 451 | Tree if_body; | ||
| 452 | if_body.splice(if_body.begin(), body, std::next(goto_stmt), label_nested_stmt); | ||
| 453 | Statement* const variable{pool.Create(Variable{}, label_id)}; | ||
| 454 | Statement* const neg_var{pool.Create(Not{}, variable)}; | ||
| 455 | if (!if_body.empty()) { | ||
| 456 | Statement* const if_stmt{pool.Create(If{}, neg_var, std::move(if_body), parent)}; | ||
| 457 | UpdateTreeUp(if_stmt); | ||
| 458 | body.insert(goto_stmt, *if_stmt); | ||
| 459 | } | ||
| 460 | body.erase(goto_stmt); | ||
| 461 | |||
| 462 | // Update nested if condition | ||
| 463 | switch (label_nested_stmt->type) { | ||
| 464 | case StatementType::If: | ||
| 465 | label_nested_stmt->cond = pool.Create(Or{}, neg_var, label_nested_stmt->cond); | ||
| 466 | break; | ||
| 467 | case StatementType::Loop: | ||
| 468 | break; | ||
| 469 | default: | ||
| 470 | throw LogicError("Invalid inward movement"); | ||
| 471 | } | ||
| 472 | Tree& nested_tree{label_nested_stmt->children}; | ||
| 473 | Statement* const new_goto{pool.Create(Goto{}, variable, label, &*label_nested_stmt)}; | ||
| 474 | return nested_tree.insert(nested_tree.begin(), *new_goto); | ||
| 475 | } | ||
| 476 | |||
| 477 | [[nodiscard]] Node Lift(Node goto_stmt) { | ||
| 478 | Statement* const parent{goto_stmt->up}; | ||
| 479 | Tree& body{parent->children}; | ||
| 480 | const Node label{goto_stmt->label}; | ||
| 481 | const u32 label_id{label->id}; | ||
| 482 | const Node label_nested_stmt{FindStatementWithLabel(body, goto_stmt)}; | ||
| 483 | const auto type{label_nested_stmt->type}; | ||
| 484 | |||
| 485 | Tree loop_body; | ||
| 486 | loop_body.splice(loop_body.begin(), body, label_nested_stmt, goto_stmt); | ||
| 487 | SanitizeNoBreaks(loop_body); | ||
| 488 | Statement* const variable{pool.Create(Variable{}, label_id)}; | ||
| 489 | Statement* const loop_stmt{pool.Create(Loop{}, variable, std::move(loop_body), parent)}; | ||
| 490 | UpdateTreeUp(loop_stmt); | ||
| 491 | const Node loop_node{body.insert(goto_stmt, *loop_stmt)}; | ||
| 492 | |||
| 493 | Statement* const new_goto{pool.Create(Goto{}, variable, label, loop_stmt)}; | ||
| 494 | loop_stmt->children.push_front(*new_goto); | ||
| 495 | const Node new_goto_node{loop_stmt->children.begin()}; | ||
| 496 | |||
| 497 | Statement* const set_var{pool.Create(SetVariable{}, label_id, goto_stmt->cond, loop_stmt)}; | ||
| 498 | loop_stmt->children.push_back(*set_var); | ||
| 499 | |||
| 500 | body.erase(goto_stmt); | ||
| 501 | return new_goto_node; | ||
| 502 | } | ||
| 503 | |||
| 504 | Node MoveOutwardIf(Node goto_stmt) { | ||
| 505 | const Node parent{Tree::s_iterator_to(*goto_stmt->up)}; | ||
| 506 | Tree& body{parent->children}; | ||
| 507 | const u32 label_id{goto_stmt->label->id}; | ||
| 508 | Statement* const goto_cond{goto_stmt->cond}; | ||
| 509 | Statement* const set_goto_var{pool.Create(SetVariable{}, label_id, goto_cond, &*parent)}; | ||
| 510 | body.insert(goto_stmt, *set_goto_var); | ||
| 511 | |||
| 512 | Tree if_body; | ||
| 513 | if_body.splice(if_body.begin(), body, std::next(goto_stmt), body.end()); | ||
| 514 | if_body.pop_front(); | ||
| 515 | Statement* const cond{pool.Create(Variable{}, label_id)}; | ||
| 516 | Statement* const neg_cond{pool.Create(Not{}, cond)}; | ||
| 517 | Statement* const if_stmt{pool.Create(If{}, neg_cond, std::move(if_body), &*parent)}; | ||
| 518 | UpdateTreeUp(if_stmt); | ||
| 519 | body.insert(goto_stmt, *if_stmt); | ||
| 520 | |||
| 521 | body.erase(goto_stmt); | ||
| 522 | |||
| 523 | Statement* const new_cond{pool.Create(Variable{}, label_id)}; | ||
| 524 | Statement* const new_goto{pool.Create(Goto{}, new_cond, goto_stmt->label, parent->up)}; | ||
| 525 | Tree& parent_tree{parent->up->children}; | ||
| 526 | return parent_tree.insert(std::next(parent), *new_goto); | ||
| 527 | } | ||
| 528 | |||
| 529 | Node MoveOutwardLoop(Node goto_stmt) { | ||
| 530 | Statement* const parent{goto_stmt->up}; | ||
| 531 | Tree& body{parent->children}; | ||
| 532 | const u32 label_id{goto_stmt->label->id}; | ||
| 533 | Statement* const goto_cond{goto_stmt->cond}; | ||
| 534 | Statement* const set_goto_var{pool.Create(SetVariable{}, label_id, goto_cond, parent)}; | ||
| 535 | Statement* const cond{pool.Create(Variable{}, label_id)}; | ||
| 536 | Statement* const break_stmt{pool.Create(Break{}, cond, parent)}; | ||
| 537 | body.insert(goto_stmt, *set_goto_var); | ||
| 538 | body.insert(goto_stmt, *break_stmt); | ||
| 539 | body.erase(goto_stmt); | ||
| 540 | |||
| 541 | const Node loop{Tree::s_iterator_to(*goto_stmt->up)}; | ||
| 542 | Statement* const new_goto_cond{pool.Create(Variable{}, label_id)}; | ||
| 543 | Statement* const new_goto{pool.Create(Goto{}, new_goto_cond, goto_stmt->label, loop->up)}; | ||
| 544 | Tree& parent_tree{loop->up->children}; | ||
| 545 | return parent_tree.insert(std::next(loop), *new_goto); | ||
| 546 | } | ||
| 547 | |||
| 548 | size_t Offset(ConstNode stmt) const { | ||
| 549 | size_t offset{0}; | ||
| 550 | if (!SearchNode(root_stmt.children, stmt, offset)) { | ||
| 551 | fmt::print(stdout, "{}\n", DumpTree(root_stmt.children)); | ||
| 552 | throw LogicError("Node not found in tree"); | ||
| 553 | } | ||
| 554 | return offset; | ||
| 555 | } | ||
| 556 | |||
| 557 | ObjectPool<Statement, 64>& pool; | ||
| 558 | Statement root_stmt{FunctionTag{}}; | ||
| 559 | }; | ||
| 560 | |||
| 561 | Block* TryFindForwardBlock(const Statement& stmt) { | ||
| 562 | const Tree& tree{stmt.up->children}; | ||
| 563 | const ConstNode end{tree.cend()}; | ||
| 564 | ConstNode forward_node{std::next(Tree::s_iterator_to(stmt))}; | ||
| 565 | while (forward_node != end && !HasChildren(forward_node->type)) { | ||
| 566 | if (forward_node->type == StatementType::Code) { | ||
| 567 | return forward_node->code; | ||
| 568 | } | ||
| 569 | ++forward_node; | ||
| 570 | } | ||
| 571 | return nullptr; | ||
| 572 | } | ||
| 573 | |||
| 574 | [[nodiscard]] U1 VisitExpr(IREmitter& ir, const Statement& stmt) { | ||
| 575 | switch (stmt.type) { | ||
| 576 | case StatementType::Identity: | ||
| 577 | return ir.Condition(stmt.guest_cond); | ||
| 578 | case StatementType::Not: | ||
| 579 | return ir.LogicalNot(U1{VisitExpr(ir, *stmt.op)}); | ||
| 580 | case StatementType::Or: | ||
| 581 | return ir.LogicalOr(VisitExpr(ir, *stmt.op_a), VisitExpr(ir, *stmt.op_b)); | ||
| 582 | case StatementType::Variable: | ||
| 583 | return ir.GetGotoVariable(stmt.id); | ||
| 584 | default: | ||
| 585 | throw NotImplementedException("Statement type {}", stmt.type); | ||
| 586 | } | ||
| 587 | } | ||
| 588 | |||
| 589 | class TranslatePass { | ||
| 590 | public: | ||
| 591 | TranslatePass(ObjectPool<Inst>& inst_pool_, ObjectPool<Block>& block_pool_, | ||
| 592 | ObjectPool<Statement, 64>& stmt_pool_, Statement& root_stmt, | ||
| 593 | const std::function<void(IR::Block*)>& func_, BlockList& block_list_) | ||
| 594 | : stmt_pool{stmt_pool_}, inst_pool{inst_pool_}, block_pool{block_pool_}, func{func_}, | ||
| 595 | block_list{block_list_} { | ||
| 596 | Visit(root_stmt, nullptr, nullptr); | ||
| 597 | } | ||
| 598 | |||
| 599 | private: | ||
| 600 | void Visit(Statement& parent, Block* continue_block, Block* break_block) { | ||
| 601 | Tree& tree{parent.children}; | ||
| 602 | Block* current_block{nullptr}; | ||
| 603 | |||
| 604 | for (auto it = tree.begin(); it != tree.end(); ++it) { | ||
| 605 | Statement& stmt{*it}; | ||
| 606 | switch (stmt.type) { | ||
| 607 | case StatementType::Label: | ||
| 608 | // Labels can be ignored | ||
| 609 | break; | ||
| 610 | case StatementType::Code: { | ||
| 611 | if (current_block && current_block != stmt.code) { | ||
| 612 | IREmitter ir{*current_block}; | ||
| 613 | ir.Branch(stmt.code); | ||
| 614 | } | ||
| 615 | current_block = stmt.code; | ||
| 616 | func(stmt.code); | ||
| 617 | block_list.push_back(stmt.code); | ||
| 618 | break; | ||
| 619 | } | ||
| 620 | case StatementType::SetVariable: { | ||
| 621 | if (!current_block) { | ||
| 622 | current_block = MergeBlock(parent, stmt); | ||
| 623 | } | ||
| 624 | IREmitter ir{*current_block}; | ||
| 625 | ir.SetGotoVariable(stmt.id, VisitExpr(ir, *stmt.op)); | ||
| 626 | break; | ||
| 627 | } | ||
| 628 | case StatementType::If: { | ||
| 629 | if (!current_block) { | ||
| 630 | current_block = block_pool.Create(inst_pool); | ||
| 631 | block_list.push_back(current_block); | ||
| 632 | } | ||
| 633 | Block* const merge_block{MergeBlock(parent, stmt)}; | ||
| 634 | |||
| 635 | // Visit children | ||
| 636 | const size_t first_block_index{block_list.size()}; | ||
| 637 | Visit(stmt, merge_block, break_block); | ||
| 638 | |||
| 639 | // Implement if header block | ||
| 640 | Block* const first_if_block{block_list.at(first_block_index)}; | ||
| 641 | IREmitter ir{*current_block}; | ||
| 642 | const U1 cond{VisitExpr(ir, *stmt.cond)}; | ||
| 643 | ir.SelectionMerge(merge_block); | ||
| 644 | ir.BranchConditional(cond, first_if_block, merge_block); | ||
| 645 | |||
| 646 | current_block = merge_block; | ||
| 647 | break; | ||
| 648 | } | ||
| 649 | case StatementType::Loop: { | ||
| 650 | Block* const loop_header_block{block_pool.Create(inst_pool)}; | ||
| 651 | if (current_block) { | ||
| 652 | IREmitter{*current_block}.Branch(loop_header_block); | ||
| 653 | } | ||
| 654 | block_list.push_back(loop_header_block); | ||
| 655 | |||
| 656 | Block* const new_continue_block{block_pool.Create(inst_pool)}; | ||
| 657 | Block* const merge_block{MergeBlock(parent, stmt)}; | ||
| 658 | |||
| 659 | // Visit children | ||
| 660 | const size_t first_block_index{block_list.size()}; | ||
| 661 | Visit(stmt, new_continue_block, merge_block); | ||
| 662 | |||
| 663 | // The continue block is located at the end of the loop | ||
| 664 | block_list.push_back(new_continue_block); | ||
| 665 | |||
| 666 | // Implement loop header block | ||
| 667 | Block* const first_loop_block{block_list.at(first_block_index)}; | ||
| 668 | IREmitter ir{*loop_header_block}; | ||
| 669 | ir.LoopMerge(merge_block, new_continue_block); | ||
| 670 | ir.Branch(first_loop_block); | ||
| 671 | |||
| 672 | // Implement continue block | ||
| 673 | IREmitter continue_ir{*new_continue_block}; | ||
| 674 | const U1 continue_cond{VisitExpr(continue_ir, *stmt.cond)}; | ||
| 675 | continue_ir.BranchConditional(continue_cond, ir.block, merge_block); | ||
| 676 | |||
| 677 | current_block = merge_block; | ||
| 678 | break; | ||
| 679 | } | ||
| 680 | case StatementType::Break: { | ||
| 681 | if (!current_block) { | ||
| 682 | current_block = block_pool.Create(inst_pool); | ||
| 683 | block_list.push_back(current_block); | ||
| 684 | } | ||
| 685 | Block* const skip_block{MergeBlock(parent, stmt)}; | ||
| 686 | |||
| 687 | IREmitter ir{*current_block}; | ||
| 688 | ir.BranchConditional(VisitExpr(ir, *stmt.cond), break_block, skip_block); | ||
| 689 | |||
| 690 | current_block = skip_block; | ||
| 691 | break; | ||
| 692 | } | ||
| 693 | case StatementType::Return: { | ||
| 694 | if (!current_block) { | ||
| 695 | current_block = block_pool.Create(inst_pool); | ||
| 696 | block_list.push_back(current_block); | ||
| 697 | } | ||
| 698 | IREmitter{*current_block}.Return(); | ||
| 699 | current_block = nullptr; | ||
| 700 | break; | ||
| 701 | } | ||
| 702 | default: | ||
| 703 | throw NotImplementedException("Statement type {}", stmt.type); | ||
| 704 | } | ||
| 705 | } | ||
| 706 | if (current_block && continue_block) { | ||
| 707 | IREmitter ir{*current_block}; | ||
| 708 | ir.Branch(continue_block); | ||
| 709 | } | ||
| 710 | } | ||
| 711 | |||
| 712 | Block* MergeBlock(Statement& parent, Statement& stmt) { | ||
| 713 | if (Block* const block{TryFindForwardBlock(stmt)}) { | ||
| 714 | return block; | ||
| 715 | } | ||
| 716 | // Create a merge block we can visit later | ||
| 717 | Block* const block{block_pool.Create(inst_pool)}; | ||
| 718 | Statement* const merge_stmt{stmt_pool.Create(block, &parent)}; | ||
| 719 | parent.children.insert(std::next(Tree::s_iterator_to(stmt)), *merge_stmt); | ||
| 720 | return block; | ||
| 721 | } | ||
| 722 | |||
| 723 | ObjectPool<Statement, 64>& stmt_pool; | ||
| 724 | ObjectPool<Inst>& inst_pool; | ||
| 725 | ObjectPool<Block>& block_pool; | ||
| 726 | const std::function<void(IR::Block*)>& func; | ||
| 727 | BlockList& block_list; | ||
| 728 | }; | ||
| 729 | } // Anonymous namespace | ||
| 730 | |||
| 731 | BlockList VisitAST(ObjectPool<Inst>& inst_pool, ObjectPool<Block>& block_pool, | ||
| 732 | std::span<Block* const> unordered_blocks, | ||
| 733 | const std::function<void(Block*)>& func) { | ||
| 734 | ObjectPool<Statement, 64> stmt_pool; | ||
| 735 | GotoPass goto_pass{unordered_blocks, stmt_pool}; | ||
| 736 | BlockList block_list; | ||
| 737 | TranslatePass translate_pass{inst_pool, block_pool, stmt_pool, goto_pass.RootStatement(), | ||
| 738 | func, block_list}; | ||
| 739 | return block_list; | ||
| 740 | } | ||
| 741 | |||
| 742 | } // namespace Shader::IR | ||