summaryrefslogtreecommitdiffstats
path: root/src/shader_recompiler/ir_opt/conditional_barrier_pass.cpp
blob: c3ed27f4f22469a2dba013d89a8665fb5ec852f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "shader_recompiler/frontend/ir/program.h"
#include "shader_recompiler/ir_opt/passes.h"

namespace Shader::Optimization {

void ConditionalBarrierPass(IR::Program& program) {
    s32 conditional_control_flow_count{0};
    s32 conditional_return_count{0};
    for (IR::AbstractSyntaxNode& node : program.syntax_list) {
        switch (node.type) {
        case IR::AbstractSyntaxNode::Type::If:
        case IR::AbstractSyntaxNode::Type::Loop:
            conditional_control_flow_count++;
            break;
        case IR::AbstractSyntaxNode::Type::EndIf:
        case IR::AbstractSyntaxNode::Type::Repeat:
            conditional_control_flow_count--;
            break;
        case IR::AbstractSyntaxNode::Type::Unreachable:
        case IR::AbstractSyntaxNode::Type::Return:
            if (conditional_control_flow_count > 0) {
                conditional_return_count++;
            }
            break;
        case IR::AbstractSyntaxNode::Type::Block:
            for (IR::Inst& inst : node.data.block->Instructions()) {
                if ((conditional_control_flow_count > 0 || conditional_return_count > 0) &&
                    inst.GetOpcode() == IR::Opcode::Barrier) {
                    LOG_WARNING(Shader, "Barrier within conditional control flow");
                    inst.ReplaceOpcode(IR::Opcode::Identity);
                }
            }
            break;
        default:
            break;
        }
    }
    ASSERT(conditional_control_flow_count == 0);
}

} // namespace Shader::Optimization