From 1b0cf2309c760c1cb97a230a1572f8e87f84444a Mon Sep 17 00:00:00 2001
From: ReinUsesLisp <reinuseslisp@airmail.cc>
Date: Sun, 14 Feb 2021 22:46:40 -0300
Subject: shader: Add support for forward declarations

---
 src/shader_recompiler/frontend/ir/basic_block.cpp    |  2 +-
 src/shader_recompiler/frontend/ir/basic_block.h      |  2 +-
 src/shader_recompiler/frontend/ir/ir_emitter.h       |  4 ++--
 .../frontend/ir/microinstruction.cpp                 |  2 +-
 src/shader_recompiler/frontend/ir/microinstruction.h | 20 +++++++++++++++++---
 src/shader_recompiler/frontend/ir/modifiers.h        | 10 +++++-----
 6 files changed, 27 insertions(+), 13 deletions(-)

(limited to 'src/shader_recompiler/frontend')

diff --git a/src/shader_recompiler/frontend/ir/basic_block.cpp b/src/shader_recompiler/frontend/ir/basic_block.cpp
index c976267128..5ae91dd7dc 100644
--- a/src/shader_recompiler/frontend/ir/basic_block.cpp
+++ b/src/shader_recompiler/frontend/ir/basic_block.cpp
@@ -26,7 +26,7 @@ void Block::AppendNewInst(Opcode op, std::initializer_list<Value> args) {
 }
 
 Block::iterator Block::PrependNewInst(iterator insertion_point, Opcode op,
-                                      std::initializer_list<Value> args, u64 flags) {
+                                      std::initializer_list<Value> args, u32 flags) {
     Inst* const inst{inst_pool->Create(op, flags)};
     const auto result_it{instructions.insert(insertion_point, *inst)};
 
diff --git a/src/shader_recompiler/frontend/ir/basic_block.h b/src/shader_recompiler/frontend/ir/basic_block.h
index 3205705e79..778b32e432 100644
--- a/src/shader_recompiler/frontend/ir/basic_block.h
+++ b/src/shader_recompiler/frontend/ir/basic_block.h
@@ -42,7 +42,7 @@ public:
 
     /// Prepends a new instruction to this basic block before the insertion point.
     iterator PrependNewInst(iterator insertion_point, Opcode op,
-                            std::initializer_list<Value> args = {}, u64 flags = 0);
+                            std::initializer_list<Value> args = {}, u32 flags = 0);
 
     /// Set the branches to jump to when all instructions have executed.
     void SetBranches(Condition cond, Block* branch_true, Block* branch_false);
diff --git a/src/shader_recompiler/frontend/ir/ir_emitter.h b/src/shader_recompiler/frontend/ir/ir_emitter.h
index 4decb46bc1..24b012a393 100644
--- a/src/shader_recompiler/frontend/ir/ir_emitter.h
+++ b/src/shader_recompiler/frontend/ir/ir_emitter.h
@@ -178,7 +178,7 @@ private:
     }
 
     template <typename T>
-    requires(sizeof(T) <= sizeof(u64) && std::is_trivially_copyable_v<T>) struct Flags {
+    requires(sizeof(T) <= sizeof(u32) && std::is_trivially_copyable_v<T>) struct Flags {
         Flags() = default;
         Flags(T proxy_) : proxy{proxy_} {}
 
@@ -187,7 +187,7 @@ private:
 
     template <typename T = Value, typename FlagType, typename... Args>
     T Inst(Opcode op, Flags<FlagType> flags, Args... args) {
-        u64 raw_flags{};
+        u32 raw_flags{};
         std::memcpy(&raw_flags, &flags.proxy, sizeof(flags.proxy));
         auto it{block->PrependNewInst(insertion_point, op, {Value{args}...}, raw_flags)};
         return T{Value{&*it}};
diff --git a/src/shader_recompiler/frontend/ir/microinstruction.cpp b/src/shader_recompiler/frontend/ir/microinstruction.cpp
index 9279b96928..ee76db9adf 100644
--- a/src/shader_recompiler/frontend/ir/microinstruction.cpp
+++ b/src/shader_recompiler/frontend/ir/microinstruction.cpp
@@ -31,7 +31,7 @@ static void RemovePseudoInstruction(IR::Inst*& inst, IR::Opcode expected_opcode)
     inst = nullptr;
 }
 
-Inst::Inst(IR::Opcode op_, u64 flags_) noexcept : op{op_}, flags{flags_} {
+Inst::Inst(IR::Opcode op_, u32 flags_) noexcept : op{op_}, flags{flags_} {
     if (op == Opcode::Phi) {
         std::construct_at(&phi_args);
     } else {
diff --git a/src/shader_recompiler/frontend/ir/microinstruction.h b/src/shader_recompiler/frontend/ir/microinstruction.h
index ddf0f90a9e..5b244fa0bf 100644
--- a/src/shader_recompiler/frontend/ir/microinstruction.h
+++ b/src/shader_recompiler/frontend/ir/microinstruction.h
@@ -12,6 +12,7 @@
 
 #include <boost/intrusive/list.hpp>
 
+#include "common/bit_cast.h"
 #include "common/common_types.h"
 #include "shader_recompiler/frontend/ir/opcodes.h"
 #include "shader_recompiler/frontend/ir/type.h"
@@ -25,7 +26,7 @@ constexpr size_t MAX_ARG_COUNT = 4;
 
 class Inst : public boost::intrusive::list_base_hook<> {
 public:
-    explicit Inst(Opcode op_, u64 flags_) noexcept;
+    explicit Inst(Opcode op_, u32 flags_) noexcept;
     ~Inst();
 
     Inst& operator=(const Inst&) = delete;
@@ -86,13 +87,25 @@ public:
     void ReplaceUsesWith(Value replacement);
 
     template <typename FlagsType>
-    requires(sizeof(FlagsType) <= sizeof(u64) && std::is_trivially_copyable_v<FlagsType>)
+    requires(sizeof(FlagsType) <= sizeof(u32) && std::is_trivially_copyable_v<FlagsType>)
         [[nodiscard]] FlagsType Flags() const noexcept {
         FlagsType ret;
         std::memcpy(&ret, &flags, sizeof(ret));
         return ret;
     }
 
+    /// Intrusively store the host definition of this instruction.
+    template <typename DefinitionType>
+    void SetDefinition(DefinitionType def) {
+        definition = Common::BitCast<u32>(def);
+    }
+
+    /// Return the intrusively stored host definition of this instruction.
+    template <typename DefinitionType>
+    [[nodiscard]] DefinitionType Definition() const noexcept {
+        return Common::BitCast<DefinitionType>(definition);
+    }
+
 private:
     struct NonTriviallyDummy {
         NonTriviallyDummy() noexcept {}
@@ -103,7 +116,8 @@ private:
 
     IR::Opcode op{};
     int use_count{};
-    u64 flags{};
+    u32 flags{};
+    u32 definition{};
     union {
         NonTriviallyDummy dummy{};
         std::array<Value, MAX_ARG_COUNT> args;
diff --git a/src/shader_recompiler/frontend/ir/modifiers.h b/src/shader_recompiler/frontend/ir/modifiers.h
index 28bb9e798c..c288eede03 100644
--- a/src/shader_recompiler/frontend/ir/modifiers.h
+++ b/src/shader_recompiler/frontend/ir/modifiers.h
@@ -6,13 +6,13 @@
 
 namespace Shader::IR {
 
-enum class FmzMode {
+enum class FmzMode : u8 {
     None, // Denorms are not flushed, NAN is propagated (nouveau)
     FTZ,  // Flush denorms to zero, NAN is propagated (D3D11, NVN, GL, VK)
     FMZ,  // Flush denorms to zero, x * 0 == 0 (D3D9)
 };
 
-enum class FpRounding {
+enum class FpRounding : u8 {
     RN, // Round to nearest even,
     RM, // Round towards negative infinity
     RP, // Round towards positive infinity
@@ -21,8 +21,8 @@ enum class FpRounding {
 
 struct FpControl {
     bool no_contraction{false};
-    FpRounding rounding : 8 = FpRounding::RN;
-    FmzMode fmz_mode : 8 = FmzMode::FTZ;
+    FpRounding rounding{FpRounding::RN};
+    FmzMode fmz_mode{FmzMode::FTZ};
 };
-static_assert(sizeof(FpControl) <= sizeof(u64));
+static_assert(sizeof(FpControl) <= sizeof(u32));
 } // namespace Shader::IR
-- 
cgit v1.2.3-70-g09d2