diff options
| author | 2021-04-21 00:25:46 -0300 | |
|---|---|---|
| committer | 2021-07-22 21:51:28 -0400 | |
| commit | 6944cabb899c4367a63cde97ae2bc2eb1a0fb790 (patch) | |
| tree | b52f0687254b6c5c123dd1b5171bad124055c6dc /src/shader_recompiler | |
| parent | shader: Inline common IR::Block methods (diff) | |
| download | yuzu-6944cabb899c4367a63cde97ae2bc2eb1a0fb790.tar.gz yuzu-6944cabb899c4367a63cde97ae2bc2eb1a0fb790.tar.xz yuzu-6944cabb899c4367a63cde97ae2bc2eb1a0fb790.zip | |
shader: Inline common Opcode and Inst functions
Diffstat (limited to 'src/shader_recompiler')
| -rw-r--r-- | src/shader_recompiler/frontend/ir/microinstruction.cpp | 18 | ||||
| -rw-r--r-- | src/shader_recompiler/frontend/ir/microinstruction.h | 13 | ||||
| -rw-r--r-- | src/shader_recompiler/frontend/ir/opcodes.cpp | 90 | ||||
| -rw-r--r-- | src/shader_recompiler/frontend/ir/opcodes.h | 74 |
4 files changed, 83 insertions, 112 deletions
diff --git a/src/shader_recompiler/frontend/ir/microinstruction.cpp b/src/shader_recompiler/frontend/ir/microinstruction.cpp index 7555ac00a..41f9fa0cd 100644 --- a/src/shader_recompiler/frontend/ir/microinstruction.cpp +++ b/src/shader_recompiler/frontend/ir/microinstruction.cpp | |||
| @@ -221,28 +221,10 @@ Inst* Inst::GetAssociatedPseudoOperation(IR::Opcode opcode) { | |||
| 221 | } | 221 | } |
| 222 | } | 222 | } |
| 223 | 223 | ||
| 224 | size_t Inst::NumArgs() const { | ||
| 225 | return op == Opcode::Phi ? phi_args.size() : NumArgsOf(op); | ||
| 226 | } | ||
| 227 | |||
| 228 | IR::Type Inst::Type() const { | 224 | IR::Type Inst::Type() const { |
| 229 | return TypeOf(op); | 225 | return TypeOf(op); |
| 230 | } | 226 | } |
| 231 | 227 | ||
| 232 | Value Inst::Arg(size_t index) const { | ||
| 233 | if (op == Opcode::Phi) { | ||
| 234 | if (index >= phi_args.size()) { | ||
| 235 | throw InvalidArgument("Out of bounds argument index {} in phi instruction", index); | ||
| 236 | } | ||
| 237 | return phi_args[index].second; | ||
| 238 | } else { | ||
| 239 | if (index >= NumArgsOf(op)) { | ||
| 240 | throw InvalidArgument("Out of bounds argument index {} in opcode {}", index, op); | ||
| 241 | } | ||
| 242 | return args[index]; | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | void Inst::SetArg(size_t index, Value value) { | 228 | void Inst::SetArg(size_t index, Value value) { |
| 247 | if (index >= NumArgs()) { | 229 | if (index >= NumArgs()) { |
| 248 | throw InvalidArgument("Out of bounds argument index {} in opcode {}", index, op); | 230 | throw InvalidArgument("Out of bounds argument index {} in opcode {}", index, op); |
diff --git a/src/shader_recompiler/frontend/ir/microinstruction.h b/src/shader_recompiler/frontend/ir/microinstruction.h index dc9f683fe..ea55fc29c 100644 --- a/src/shader_recompiler/frontend/ir/microinstruction.h +++ b/src/shader_recompiler/frontend/ir/microinstruction.h | |||
| @@ -73,10 +73,19 @@ public: | |||
| 73 | [[nodiscard]] IR::Type Type() const; | 73 | [[nodiscard]] IR::Type Type() const; |
| 74 | 74 | ||
| 75 | /// Get the number of arguments this instruction has. | 75 | /// Get the number of arguments this instruction has. |
| 76 | [[nodiscard]] size_t NumArgs() const; | 76 | [[nodiscard]] size_t NumArgs() const { |
| 77 | return op == Opcode::Phi ? phi_args.size() : NumArgsOf(op); | ||
| 78 | } | ||
| 77 | 79 | ||
| 78 | /// Get the value of a given argument index. | 80 | /// Get the value of a given argument index. |
| 79 | [[nodiscard]] Value Arg(size_t index) const; | 81 | [[nodiscard]] Value Arg(size_t index) const noexcept { |
| 82 | if (op == Opcode::Phi) { | ||
| 83 | return phi_args[index].second; | ||
| 84 | } else { | ||
| 85 | return args[index]; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 80 | /// Set the value of a given argument index. | 89 | /// Set the value of a given argument index. |
| 81 | void SetArg(size_t index, Value value); | 90 | void SetArg(size_t index, Value value); |
| 82 | 91 | ||
diff --git a/src/shader_recompiler/frontend/ir/opcodes.cpp b/src/shader_recompiler/frontend/ir/opcodes.cpp index 4207d548c..24d024ad7 100644 --- a/src/shader_recompiler/frontend/ir/opcodes.cpp +++ b/src/shader_recompiler/frontend/ir/opcodes.cpp | |||
| @@ -2,102 +2,14 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <algorithm> | ||
| 6 | #include <array> | ||
| 7 | #include <string_view> | 5 | #include <string_view> |
| 8 | 6 | ||
| 9 | #include "shader_recompiler/exception.h" | ||
| 10 | #include "shader_recompiler/frontend/ir/opcodes.h" | 7 | #include "shader_recompiler/frontend/ir/opcodes.h" |
| 11 | 8 | ||
| 12 | namespace Shader::IR { | 9 | namespace Shader::IR { |
| 13 | namespace { | ||
| 14 | struct OpcodeMeta { | ||
| 15 | std::string_view name; | ||
| 16 | Type type; | ||
| 17 | std::array<Type, 5> arg_types; | ||
| 18 | }; | ||
| 19 | |||
| 20 | // using enum Type; | ||
| 21 | constexpr Type Void{Type::Void}; | ||
| 22 | constexpr Type Opaque{Type::Opaque}; | ||
| 23 | constexpr Type Label{Type::Label}; | ||
| 24 | constexpr Type Reg{Type::Reg}; | ||
| 25 | constexpr Type Pred{Type::Pred}; | ||
| 26 | constexpr Type Attribute{Type::Attribute}; | ||
| 27 | constexpr Type Patch{Type::Patch}; | ||
| 28 | constexpr Type U1{Type::U1}; | ||
| 29 | constexpr Type U8{Type::U8}; | ||
| 30 | constexpr Type U16{Type::U16}; | ||
| 31 | constexpr Type U32{Type::U32}; | ||
| 32 | constexpr Type U64{Type::U64}; | ||
| 33 | constexpr Type F16{Type::F16}; | ||
| 34 | constexpr Type F32{Type::F32}; | ||
| 35 | constexpr Type F64{Type::F64}; | ||
| 36 | constexpr Type U32x2{Type::U32x2}; | ||
| 37 | constexpr Type U32x3{Type::U32x3}; | ||
| 38 | constexpr Type U32x4{Type::U32x4}; | ||
| 39 | constexpr Type F16x2{Type::F16x2}; | ||
| 40 | constexpr Type F16x3{Type::F16x3}; | ||
| 41 | constexpr Type F16x4{Type::F16x4}; | ||
| 42 | constexpr Type F32x2{Type::F32x2}; | ||
| 43 | constexpr Type F32x3{Type::F32x3}; | ||
| 44 | constexpr Type F32x4{Type::F32x4}; | ||
| 45 | constexpr Type F64x2{Type::F64x2}; | ||
| 46 | constexpr Type F64x3{Type::F64x3}; | ||
| 47 | constexpr Type F64x4{Type::F64x4}; | ||
| 48 | |||
| 49 | constexpr std::array META_TABLE{ | ||
| 50 | #define OPCODE(name_token, type_token, ...) \ | ||
| 51 | OpcodeMeta{ \ | ||
| 52 | .name{#name_token}, \ | ||
| 53 | .type = type_token, \ | ||
| 54 | .arg_types{__VA_ARGS__}, \ | ||
| 55 | }, | ||
| 56 | #include "opcodes.inc" | ||
| 57 | #undef OPCODE | ||
| 58 | }; | ||
| 59 | |||
| 60 | constexpr size_t CalculateNumArgsOf(Opcode op) { | ||
| 61 | const auto& arg_types{META_TABLE[static_cast<size_t>(op)].arg_types}; | ||
| 62 | return std::distance(arg_types.begin(), std::ranges::find(arg_types, Type::Void)); | ||
| 63 | } | ||
| 64 | |||
| 65 | constexpr std::array NUM_ARGS{ | ||
| 66 | #define OPCODE(name_token, type_token, ...) CalculateNumArgsOf(Opcode::name_token), | ||
| 67 | #include "opcodes.inc" | ||
| 68 | #undef OPCODE | ||
| 69 | }; | ||
| 70 | |||
| 71 | void ValidateOpcode(Opcode op) { | ||
| 72 | const size_t raw{static_cast<size_t>(op)}; | ||
| 73 | if (raw >= META_TABLE.size()) { | ||
| 74 | throw InvalidArgument("Invalid opcode with raw value {}", raw); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | } // Anonymous namespace | ||
| 78 | |||
| 79 | Type TypeOf(Opcode op) { | ||
| 80 | ValidateOpcode(op); | ||
| 81 | return META_TABLE[static_cast<size_t>(op)].type; | ||
| 82 | } | ||
| 83 | |||
| 84 | size_t NumArgsOf(Opcode op) { | ||
| 85 | ValidateOpcode(op); | ||
| 86 | return NUM_ARGS[static_cast<size_t>(op)]; | ||
| 87 | } | ||
| 88 | |||
| 89 | Type ArgTypeOf(Opcode op, size_t arg_index) { | ||
| 90 | ValidateOpcode(op); | ||
| 91 | const auto& arg_types{META_TABLE[static_cast<size_t>(op)].arg_types}; | ||
| 92 | if (arg_index >= arg_types.size() || arg_types[arg_index] == Type::Void) { | ||
| 93 | throw InvalidArgument("Out of bounds argument"); | ||
| 94 | } | ||
| 95 | return arg_types[arg_index]; | ||
| 96 | } | ||
| 97 | 10 | ||
| 98 | std::string_view NameOf(Opcode op) { | 11 | std::string_view NameOf(Opcode op) { |
| 99 | ValidateOpcode(op); | 12 | return Detail::META_TABLE[static_cast<size_t>(op)].name; |
| 100 | return META_TABLE[static_cast<size_t>(op)].name; | ||
| 101 | } | 13 | } |
| 102 | 14 | ||
| 103 | } // namespace Shader::IR | 15 | } // namespace Shader::IR |
diff --git a/src/shader_recompiler/frontend/ir/opcodes.h b/src/shader_recompiler/frontend/ir/opcodes.h index 999fb2e77..b5697c7f9 100644 --- a/src/shader_recompiler/frontend/ir/opcodes.h +++ b/src/shader_recompiler/frontend/ir/opcodes.h | |||
| @@ -4,6 +4,8 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <array> | ||
| 8 | #include <algorithm> | ||
| 7 | #include <string_view> | 9 | #include <string_view> |
| 8 | 10 | ||
| 9 | #include <fmt/format.h> | 11 | #include <fmt/format.h> |
| @@ -18,14 +20,80 @@ enum class Opcode { | |||
| 18 | #undef OPCODE | 20 | #undef OPCODE |
| 19 | }; | 21 | }; |
| 20 | 22 | ||
| 23 | namespace Detail { | ||
| 24 | |||
| 25 | struct OpcodeMeta { | ||
| 26 | std::string_view name; | ||
| 27 | Type type; | ||
| 28 | std::array<Type, 5> arg_types; | ||
| 29 | }; | ||
| 30 | |||
| 31 | // using enum Type; | ||
| 32 | constexpr Type Void{Type::Void}; | ||
| 33 | constexpr Type Opaque{Type::Opaque}; | ||
| 34 | constexpr Type Label{Type::Label}; | ||
| 35 | constexpr Type Reg{Type::Reg}; | ||
| 36 | constexpr Type Pred{Type::Pred}; | ||
| 37 | constexpr Type Attribute{Type::Attribute}; | ||
| 38 | constexpr Type Patch{Type::Patch}; | ||
| 39 | constexpr Type U1{Type::U1}; | ||
| 40 | constexpr Type U8{Type::U8}; | ||
| 41 | constexpr Type U16{Type::U16}; | ||
| 42 | constexpr Type U32{Type::U32}; | ||
| 43 | constexpr Type U64{Type::U64}; | ||
| 44 | constexpr Type F16{Type::F16}; | ||
| 45 | constexpr Type F32{Type::F32}; | ||
| 46 | constexpr Type F64{Type::F64}; | ||
| 47 | constexpr Type U32x2{Type::U32x2}; | ||
| 48 | constexpr Type U32x3{Type::U32x3}; | ||
| 49 | constexpr Type U32x4{Type::U32x4}; | ||
| 50 | constexpr Type F16x2{Type::F16x2}; | ||
| 51 | constexpr Type F16x3{Type::F16x3}; | ||
| 52 | constexpr Type F16x4{Type::F16x4}; | ||
| 53 | constexpr Type F32x2{Type::F32x2}; | ||
| 54 | constexpr Type F32x3{Type::F32x3}; | ||
| 55 | constexpr Type F32x4{Type::F32x4}; | ||
| 56 | constexpr Type F64x2{Type::F64x2}; | ||
| 57 | constexpr Type F64x3{Type::F64x3}; | ||
| 58 | constexpr Type F64x4{Type::F64x4}; | ||
| 59 | |||
| 60 | constexpr std::array META_TABLE{ | ||
| 61 | #define OPCODE(name_token, type_token, ...) \ | ||
| 62 | OpcodeMeta{ \ | ||
| 63 | .name{#name_token}, \ | ||
| 64 | .type = type_token, \ | ||
| 65 | .arg_types{__VA_ARGS__}, \ | ||
| 66 | }, | ||
| 67 | #include "opcodes.inc" | ||
| 68 | #undef OPCODE | ||
| 69 | }; | ||
| 70 | |||
| 71 | constexpr size_t CalculateNumArgsOf(Opcode op) { | ||
| 72 | const auto& arg_types{META_TABLE[static_cast<size_t>(op)].arg_types}; | ||
| 73 | return std::distance(arg_types.begin(), std::ranges::find(arg_types, Type::Void)); | ||
| 74 | } | ||
| 75 | |||
| 76 | constexpr std::array NUM_ARGS{ | ||
| 77 | #define OPCODE(name_token, type_token, ...) CalculateNumArgsOf(Opcode::name_token), | ||
| 78 | #include "opcodes.inc" | ||
| 79 | #undef OPCODE | ||
| 80 | }; | ||
| 81 | } // namespace Detail | ||
| 82 | |||
| 21 | /// Get return type of an opcode | 83 | /// Get return type of an opcode |
| 22 | [[nodiscard]] Type TypeOf(Opcode op); | 84 | [[nodiscard]] inline Type TypeOf(Opcode op) noexcept { |
| 85 | return Detail::META_TABLE[static_cast<size_t>(op)].type; | ||
| 86 | } | ||
| 23 | 87 | ||
| 24 | /// Get the number of arguments an opcode accepts | 88 | /// Get the number of arguments an opcode accepts |
| 25 | [[nodiscard]] size_t NumArgsOf(Opcode op); | 89 | [[nodiscard]] inline size_t NumArgsOf(Opcode op) noexcept { |
| 90 | return Detail::NUM_ARGS[static_cast<size_t>(op)]; | ||
| 91 | } | ||
| 26 | 92 | ||
| 27 | /// Get the required type of an argument of an opcode | 93 | /// Get the required type of an argument of an opcode |
| 28 | [[nodiscard]] Type ArgTypeOf(Opcode op, size_t arg_index); | 94 | [[nodiscard]] inline Type ArgTypeOf(Opcode op, size_t arg_index) noexcept { |
| 95 | return Detail::META_TABLE[static_cast<size_t>(op)].arg_types[arg_index]; | ||
| 96 | } | ||
| 29 | 97 | ||
| 30 | /// Get the name of an opcode | 98 | /// Get the name of an opcode |
| 31 | [[nodiscard]] std::string_view NameOf(Opcode op); | 99 | [[nodiscard]] std::string_view NameOf(Opcode op); |