summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Fernando Sahmkow2019-09-21 13:07:02 -0400
committerGravatar FernandoS272019-10-04 18:52:53 -0400
commitb3c46d694846c8ea4fbdcfccda8a41a9f88622f9 (patch)
treeadc31c59c53a43539ce33fb75c5b8dd00736861b /src
parentvk_shader_compiler: Correct SPIR-V AST Decompiling (diff)
downloadyuzu-b3c46d694846c8ea4fbdcfccda8a41a9f88622f9.tar.gz
yuzu-b3c46d694846c8ea4fbdcfccda8a41a9f88622f9.tar.xz
yuzu-b3c46d694846c8ea4fbdcfccda8a41a9f88622f9.zip
Shader_IR: corrections and clang-format
Diffstat (limited to 'src')
-rw-r--r--src/video_core/shader/ast.cpp132
-rw-r--r--src/video_core/shader/expr.h2
2 files changed, 64 insertions, 70 deletions
diff --git a/src/video_core/shader/ast.cpp b/src/video_core/shader/ast.cpp
index 7e5e916ab..7c8507280 100644
--- a/src/video_core/shader/ast.cpp
+++ b/src/video_core/shader/ast.cpp
@@ -13,7 +13,7 @@ namespace VideoCommon::Shader {
13 13
14ASTZipper::ASTZipper() = default; 14ASTZipper::ASTZipper() = default;
15 15
16void ASTZipper::Init(ASTNode new_first, ASTNode parent) { 16void ASTZipper::Init(const ASTNode new_first, const ASTNode parent) {
17 ASSERT(new_first->manager == nullptr); 17 ASSERT(new_first->manager == nullptr);
18 first = new_first; 18 first = new_first;
19 last = new_first; 19 last = new_first;
@@ -26,7 +26,7 @@ void ASTZipper::Init(ASTNode new_first, ASTNode parent) {
26 } 26 }
27} 27}
28 28
29void ASTZipper::PushBack(ASTNode new_node) { 29void ASTZipper::PushBack(const ASTNode new_node) {
30 ASSERT(new_node->manager == nullptr); 30 ASSERT(new_node->manager == nullptr);
31 new_node->previous = last; 31 new_node->previous = last;
32 if (last) { 32 if (last) {
@@ -40,7 +40,7 @@ void ASTZipper::PushBack(ASTNode new_node) {
40 new_node->manager = this; 40 new_node->manager = this;
41} 41}
42 42
43void ASTZipper::PushFront(ASTNode new_node) { 43void ASTZipper::PushFront(const ASTNode new_node) {
44 ASSERT(new_node->manager == nullptr); 44 ASSERT(new_node->manager == nullptr);
45 new_node->previous.reset(); 45 new_node->previous.reset();
46 new_node->next = first; 46 new_node->next = first;
@@ -54,13 +54,13 @@ void ASTZipper::PushFront(ASTNode new_node) {
54 new_node->manager = this; 54 new_node->manager = this;
55} 55}
56 56
57void ASTZipper::InsertAfter(ASTNode new_node, ASTNode at_node) { 57void ASTZipper::InsertAfter(const ASTNode new_node, const ASTNode at_node) {
58 ASSERT(new_node->manager == nullptr); 58 ASSERT(new_node->manager == nullptr);
59 if (!at_node) { 59 if (!at_node) {
60 PushFront(new_node); 60 PushFront(new_node);
61 return; 61 return;
62 } 62 }
63 ASTNode next = at_node->next; 63 const ASTNode next = at_node->next;
64 if (next) { 64 if (next) {
65 next->previous = new_node; 65 next->previous = new_node;
66 } 66 }
@@ -73,13 +73,13 @@ void ASTZipper::InsertAfter(ASTNode new_node, ASTNode at_node) {
73 new_node->manager = this; 73 new_node->manager = this;
74} 74}
75 75
76void ASTZipper::InsertBefore(ASTNode new_node, ASTNode at_node) { 76void ASTZipper::InsertBefore(const ASTNode new_node, const ASTNode at_node) {
77 ASSERT(new_node->manager == nullptr); 77 ASSERT(new_node->manager == nullptr);
78 if (!at_node) { 78 if (!at_node) {
79 PushBack(new_node); 79 PushBack(new_node);
80 return; 80 return;
81 } 81 }
82 ASTNode previous = at_node->previous; 82 const ASTNode previous = at_node->previous;
83 if (previous) { 83 if (previous) {
84 previous->next = new_node; 84 previous->next = new_node;
85 } 85 }
@@ -92,7 +92,7 @@ void ASTZipper::InsertBefore(ASTNode new_node, ASTNode at_node) {
92 new_node->manager = this; 92 new_node->manager = this;
93} 93}
94 94
95void ASTZipper::DetachTail(ASTNode node) { 95void ASTZipper::DetachTail(const ASTNode node) {
96 ASSERT(node->manager == this); 96 ASSERT(node->manager == this);
97 if (node == first) { 97 if (node == first) {
98 first.reset(); 98 first.reset();
@@ -111,14 +111,14 @@ void ASTZipper::DetachTail(ASTNode node) {
111 } 111 }
112} 112}
113 113
114void ASTZipper::DetachSegment(ASTNode start, ASTNode end) { 114void ASTZipper::DetachSegment(const ASTNode start, const ASTNode end) {
115 ASSERT(start->manager == this && end->manager == this); 115 ASSERT(start->manager == this && end->manager == this);
116 if (start == end) { 116 if (start == end) {
117 DetachSingle(start); 117 DetachSingle(start);
118 return; 118 return;
119 } 119 }
120 ASTNode prev = start->previous; 120 const ASTNode prev = start->previous;
121 ASTNode post = end->next; 121 const ASTNode post = end->next;
122 if (!prev) { 122 if (!prev) {
123 first = post; 123 first = post;
124 } else { 124 } else {
@@ -142,10 +142,10 @@ void ASTZipper::DetachSegment(ASTNode start, ASTNode end) {
142 ASSERT(found); 142 ASSERT(found);
143} 143}
144 144
145void ASTZipper::DetachSingle(ASTNode node) { 145void ASTZipper::DetachSingle(const ASTNode node) {
146 ASSERT(node->manager == this); 146 ASSERT(node->manager == this);
147 ASTNode prev = node->previous; 147 const ASTNode prev = node->previous;
148 ASTNode post = node->next; 148 const ASTNode post = node->next;
149 node->previous.reset(); 149 node->previous.reset();
150 node->next.reset(); 150 node->next.reset();
151 if (!prev) { 151 if (!prev) {
@@ -163,10 +163,10 @@ void ASTZipper::DetachSingle(ASTNode node) {
163 node->parent.reset(); 163 node->parent.reset();
164} 164}
165 165
166void ASTZipper::Remove(ASTNode node) { 166void ASTZipper::Remove(const ASTNode node) {
167 ASSERT(node->manager == this); 167 ASSERT(node->manager == this);
168 ASTNode next = node->next; 168 const ASTNode next = node->next;
169 ASTNode previous = node->previous; 169 const ASTNode previous = node->previous;
170 if (previous) { 170 if (previous) {
171 previous->next = next; 171 previous->next = next;
172 } 172 }
@@ -409,35 +409,35 @@ void ASTManager::DeclareLabel(u32 address) {
409} 409}
410 410
411void ASTManager::InsertLabel(u32 address) { 411void ASTManager::InsertLabel(u32 address) {
412 u32 index = labels_map[address]; 412 const u32 index = labels_map[address];
413 ASTNode label = ASTBase::Make<ASTLabel>(main_node, index); 413 const ASTNode label = ASTBase::Make<ASTLabel>(main_node, index);
414 labels[index] = label; 414 labels[index] = label;
415 program->nodes.PushBack(label); 415 program->nodes.PushBack(label);
416} 416}
417 417
418void ASTManager::InsertGoto(Expr condition, u32 address) { 418void ASTManager::InsertGoto(Expr condition, u32 address) {
419 u32 index = labels_map[address]; 419 const u32 index = labels_map[address];
420 ASTNode goto_node = ASTBase::Make<ASTGoto>(main_node, condition, index); 420 const ASTNode goto_node = ASTBase::Make<ASTGoto>(main_node, condition, index);
421 gotos.push_back(goto_node); 421 gotos.push_back(goto_node);
422 program->nodes.PushBack(goto_node); 422 program->nodes.PushBack(goto_node);
423} 423}
424 424
425void ASTManager::InsertBlock(u32 start_address, u32 end_address) { 425void ASTManager::InsertBlock(u32 start_address, u32 end_address) {
426 ASTNode block = ASTBase::Make<ASTBlockEncoded>(main_node, start_address, end_address); 426 const ASTNode block = ASTBase::Make<ASTBlockEncoded>(main_node, start_address, end_address);
427 program->nodes.PushBack(block); 427 program->nodes.PushBack(block);
428} 428}
429 429
430void ASTManager::InsertReturn(Expr condition, bool kills) { 430void ASTManager::InsertReturn(Expr condition, bool kills) {
431 ASTNode node = ASTBase::Make<ASTReturn>(main_node, condition, kills); 431 const ASTNode node = ASTBase::Make<ASTReturn>(main_node, condition, kills);
432 program->nodes.PushBack(node); 432 program->nodes.PushBack(node);
433} 433}
434 434
435void ASTManager::Decompile() { 435void ASTManager::Decompile() {
436 auto it = gotos.begin(); 436 auto it = gotos.begin();
437 while (it != gotos.end()) { 437 while (it != gotos.end()) {
438 ASTNode goto_node = *it; 438 const ASTNode goto_node = *it;
439 u32 label_index = goto_node->GetGotoLabel(); 439 const u32 label_index = goto_node->GetGotoLabel();
440 ASTNode label = labels[label_index]; 440 const ASTNode label = labels[label_index];
441 if (!full_decompile) { 441 if (!full_decompile) {
442 // We only decompile backward jumps 442 // We only decompile backward jumps
443 if (!IsBackwardsJump(goto_node, label)) { 443 if (!IsBackwardsJump(goto_node, label)) {
@@ -452,7 +452,7 @@ void ASTManager::Decompile() {
452 } 452 }
453 if (DirectlyRelated(goto_node, label)) { 453 if (DirectlyRelated(goto_node, label)) {
454 u32 goto_level = goto_node->GetLevel(); 454 u32 goto_level = goto_node->GetLevel();
455 u32 label_level = label->GetLevel(); 455 const u32 label_level = label->GetLevel();
456 while (label_level < goto_level) { 456 while (label_level < goto_level) {
457 MoveOutward(goto_node); 457 MoveOutward(goto_node);
458 goto_level--; 458 goto_level--;
@@ -481,7 +481,7 @@ void ASTManager::Decompile() {
481 it++; 481 it++;
482 } 482 }
483 if (full_decompile) { 483 if (full_decompile) {
484 for (ASTNode label : labels) { 484 for (const ASTNode label : labels) {
485 auto& manager = label->GetManager(); 485 auto& manager = label->GetManager();
486 manager.Remove(label); 486 manager.Remove(label);
487 } 487 }
@@ -491,8 +491,8 @@ void ASTManager::Decompile() {
491 while (it != labels.end()) { 491 while (it != labels.end()) {
492 bool can_remove = true; 492 bool can_remove = true;
493 ASTNode label = *it; 493 ASTNode label = *it;
494 for (ASTNode goto_node : gotos) { 494 for (const ASTNode goto_node : gotos) {
495 u32 label_index = goto_node->GetGotoLabel(); 495 const u32 label_index = goto_node->GetGotoLabel();
496 ASTNode glabel = labels[label_index]; 496 ASTNode glabel = labels[label_index];
497 if (glabel == label) { 497 if (glabel == label) {
498 can_remove = false; 498 can_remove = false;
@@ -535,8 +535,8 @@ ASTNode CommonParent(ASTNode first, ASTNode second) {
535 if (first->GetParent() == second->GetParent()) { 535 if (first->GetParent() == second->GetParent()) {
536 return first->GetParent(); 536 return first->GetParent();
537 } 537 }
538 u32 first_level = first->GetLevel(); 538 const u32 first_level = first->GetLevel();
539 u32 second_level = second->GetLevel(); 539 const u32 second_level = second->GetLevel();
540 u32 min_level; 540 u32 min_level;
541 u32 max_level; 541 u32 max_level;
542 ASTNode max; 542 ASTNode max;
@@ -573,8 +573,8 @@ bool ASTManager::DirectlyRelated(ASTNode first, ASTNode second) {
573 if (first->GetParent() == second->GetParent()) { 573 if (first->GetParent() == second->GetParent()) {
574 return false; 574 return false;
575 } 575 }
576 u32 first_level = first->GetLevel(); 576 const u32 first_level = first->GetLevel();
577 u32 second_level = second->GetLevel(); 577 const u32 second_level = second->GetLevel();
578 u32 min_level; 578 u32 min_level;
579 u32 max_level; 579 u32 max_level;
580 ASTNode max; 580 ASTNode max;
@@ -613,42 +613,37 @@ void ASTManager::SanityCheck() {
613} 613}
614 614
615void ASTManager::EncloseDoWhile(ASTNode goto_node, ASTNode label) { 615void ASTManager::EncloseDoWhile(ASTNode goto_node, ASTNode label) {
616 // ShowCurrentState("Before DoWhile Enclose");
617 ASTZipper& zipper = goto_node->GetManager(); 616 ASTZipper& zipper = goto_node->GetManager();
618 ASTNode loop_start = label->GetNext(); 617 const ASTNode loop_start = label->GetNext();
619 if (loop_start == goto_node) { 618 if (loop_start == goto_node) {
620 zipper.Remove(goto_node); 619 zipper.Remove(goto_node);
621 // ShowCurrentState("Ignore DoWhile Enclose");
622 return; 620 return;
623 } 621 }
624 ASTNode parent = label->GetParent(); 622 const ASTNode parent = label->GetParent();
625 Expr condition = goto_node->GetGotoCondition(); 623 const Expr condition = goto_node->GetGotoCondition();
626 zipper.DetachSegment(loop_start, goto_node); 624 zipper.DetachSegment(loop_start, goto_node);
627 ASTNode do_while_node = ASTBase::Make<ASTDoWhile>(parent, condition); 625 const ASTNode do_while_node = ASTBase::Make<ASTDoWhile>(parent, condition);
628 ASTZipper* sub_zipper = do_while_node->GetSubNodes(); 626 ASTZipper* sub_zipper = do_while_node->GetSubNodes();
629 sub_zipper->Init(loop_start, do_while_node); 627 sub_zipper->Init(loop_start, do_while_node);
630 zipper.InsertAfter(do_while_node, label); 628 zipper.InsertAfter(do_while_node, label);
631 sub_zipper->Remove(goto_node); 629 sub_zipper->Remove(goto_node);
632 // ShowCurrentState("After DoWhile Enclose");
633} 630}
634 631
635void ASTManager::EncloseIfThen(ASTNode goto_node, ASTNode label) { 632void ASTManager::EncloseIfThen(ASTNode goto_node, ASTNode label) {
636 // ShowCurrentState("Before IfThen Enclose");
637 ASTZipper& zipper = goto_node->GetManager(); 633 ASTZipper& zipper = goto_node->GetManager();
638 ASTNode if_end = label->GetPrevious(); 634 const ASTNode if_end = label->GetPrevious();
639 if (if_end == goto_node) { 635 if (if_end == goto_node) {
640 zipper.Remove(goto_node); 636 zipper.Remove(goto_node);
641 // ShowCurrentState("Ignore IfThen Enclose");
642 return; 637 return;
643 } 638 }
644 ASTNode prev = goto_node->GetPrevious(); 639 const ASTNode prev = goto_node->GetPrevious();
645 Expr condition = goto_node->GetGotoCondition(); 640 const Expr condition = goto_node->GetGotoCondition();
646 bool do_else = false; 641 bool do_else = false;
647 if (!disable_else_derivation && prev->IsIfThen()) { 642 if (!disable_else_derivation && prev->IsIfThen()) {
648 Expr if_condition = prev->GetIfCondition(); 643 const Expr if_condition = prev->GetIfCondition();
649 do_else = ExprAreEqual(if_condition, condition); 644 do_else = ExprAreEqual(if_condition, condition);
650 } 645 }
651 ASTNode parent = label->GetParent(); 646 const ASTNode parent = label->GetParent();
652 zipper.DetachSegment(goto_node, if_end); 647 zipper.DetachSegment(goto_node, if_end);
653 ASTNode if_node; 648 ASTNode if_node;
654 if (do_else) { 649 if (do_else) {
@@ -667,34 +662,35 @@ void ASTManager::EncloseIfThen(ASTNode goto_node, ASTNode label) {
667void ASTManager::MoveOutward(ASTNode goto_node) { 662void ASTManager::MoveOutward(ASTNode goto_node) {
668 // ShowCurrentState("Before MoveOutward"); 663 // ShowCurrentState("Before MoveOutward");
669 ASTZipper& zipper = goto_node->GetManager(); 664 ASTZipper& zipper = goto_node->GetManager();
670 ASTNode parent = goto_node->GetParent(); 665 const ASTNode parent = goto_node->GetParent();
671 ASTZipper& zipper2 = parent->GetManager(); 666 ASTZipper& zipper2 = parent->GetManager();
672 ASTNode grandpa = parent->GetParent(); 667 const ASTNode grandpa = parent->GetParent();
673 bool is_loop = parent->IsLoop(); 668 const bool is_loop = parent->IsLoop();
674 bool is_else = parent->IsIfElse(); 669 const bool is_else = parent->IsIfElse();
675 bool is_if = parent->IsIfThen(); 670 const bool is_if = parent->IsIfThen();
676 671
677 ASTNode prev = goto_node->GetPrevious(); 672 const ASTNode prev = goto_node->GetPrevious();
678 ASTNode post = goto_node->GetNext(); 673 const ASTNode post = goto_node->GetNext();
679 674
680 Expr condition = goto_node->GetGotoCondition(); 675 const Expr condition = goto_node->GetGotoCondition();
681 zipper.DetachSingle(goto_node); 676 zipper.DetachSingle(goto_node);
682 if (is_loop) { 677 if (is_loop) {
683 u32 var_index = NewVariable(); 678 const u32 var_index = NewVariable();
684 Expr var_condition = MakeExpr<ExprVar>(var_index); 679 const Expr var_condition = MakeExpr<ExprVar>(var_index);
685 ASTNode var_node = ASTBase::Make<ASTVarSet>(parent, var_index, condition); 680 const ASTNode var_node = ASTBase::Make<ASTVarSet>(parent, var_index, condition);
686 ASTNode var_node_init = ASTBase::Make<ASTVarSet>(parent, var_index, false_condition); 681 const ASTNode var_node_init = ASTBase::Make<ASTVarSet>(parent, var_index, false_condition);
687 zipper2.InsertBefore(var_node_init, parent); 682 zipper2.InsertBefore(var_node_init, parent);
688 zipper.InsertAfter(var_node, prev); 683 zipper.InsertAfter(var_node, prev);
689 goto_node->SetGotoCondition(var_condition); 684 goto_node->SetGotoCondition(var_condition);
690 ASTNode break_node = ASTBase::Make<ASTBreak>(parent, var_condition); 685 const ASTNode break_node = ASTBase::Make<ASTBreak>(parent, var_condition);
691 zipper.InsertAfter(break_node, var_node); 686 zipper.InsertAfter(break_node, var_node);
692 } else if (is_if || is_else) { 687 } else if (is_if || is_else) {
693 if (post) { 688 if (post) {
694 u32 var_index = NewVariable(); 689 const u32 var_index = NewVariable();
695 Expr var_condition = MakeExpr<ExprVar>(var_index); 690 const Expr var_condition = MakeExpr<ExprVar>(var_index);
696 ASTNode var_node = ASTBase::Make<ASTVarSet>(parent, var_index, condition); 691 const ASTNode var_node = ASTBase::Make<ASTVarSet>(parent, var_index, condition);
697 ASTNode var_node_init = ASTBase::Make<ASTVarSet>(parent, var_index, false_condition); 692 const ASTNode var_node_init =
693 ASTBase::Make<ASTVarSet>(parent, var_index, false_condition);
698 if (is_if) { 694 if (is_if) {
699 zipper2.InsertBefore(var_node_init, parent); 695 zipper2.InsertBefore(var_node_init, parent);
700 } else { 696 } else {
@@ -703,7 +699,7 @@ void ASTManager::MoveOutward(ASTNode goto_node) {
703 zipper.InsertAfter(var_node, prev); 699 zipper.InsertAfter(var_node, prev);
704 goto_node->SetGotoCondition(var_condition); 700 goto_node->SetGotoCondition(var_condition);
705 zipper.DetachTail(post); 701 zipper.DetachTail(post);
706 ASTNode if_node = ASTBase::Make<ASTIfThen>(parent, MakeExprNot(var_condition)); 702 const ASTNode if_node = ASTBase::Make<ASTIfThen>(parent, MakeExprNot(var_condition));
707 ASTZipper* sub_zipper = if_node->GetSubNodes(); 703 ASTZipper* sub_zipper = if_node->GetSubNodes();
708 sub_zipper->Init(post, if_node); 704 sub_zipper->Init(post, if_node);
709 zipper.InsertAfter(if_node, var_node); 705 zipper.InsertAfter(if_node, var_node);
@@ -721,16 +717,14 @@ void ASTManager::MoveOutward(ASTNode goto_node) {
721 } else { 717 } else {
722 UNREACHABLE(); 718 UNREACHABLE();
723 } 719 }
724 ASTNode next = parent->GetNext(); 720 const ASTNode next = parent->GetNext();
725 if (is_if && next && next->IsIfElse()) { 721 if (is_if && next && next->IsIfElse()) {
726 zipper2.InsertAfter(goto_node, next); 722 zipper2.InsertAfter(goto_node, next);
727 goto_node->SetParent(grandpa); 723 goto_node->SetParent(grandpa);
728 // ShowCurrentState("After MoveOutward");
729 return; 724 return;
730 } 725 }
731 zipper2.InsertAfter(goto_node, parent); 726 zipper2.InsertAfter(goto_node, parent);
732 goto_node->SetParent(grandpa); 727 goto_node->SetParent(grandpa);
733 // ShowCurrentState("After MoveOutward");
734} 728}
735 729
736class ASTClearer { 730class ASTClearer {
diff --git a/src/video_core/shader/expr.h b/src/video_core/shader/expr.h
index b954cffb0..60598977a 100644
--- a/src/video_core/shader/expr.h
+++ b/src/video_core/shader/expr.h
@@ -4,8 +4,8 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <variant>
8#include <memory> 7#include <memory>
8#include <variant>
9 9
10#include "video_core/engines/shader_bytecode.h" 10#include "video_core/engines/shader_bytecode.h"
11 11