Remove forward references and add phi node patching

The previous API for forward declarations broke when more than one
definition was done. Forward references on instructions that are not
labels were only needed for phi nodes, so it has been replaced with a
deferred phi node instruction and a method to patch these after
everything has been defined.
This commit is contained in:
ReinUsesLisp 2021-04-11 02:02:40 -03:00
parent f1cccfd0f3
commit 51d541f1a1
4 changed files with 48 additions and 23 deletions

View file

@ -12,6 +12,7 @@
#include <optional>
#include <span>
#include <string>
#include <functional>
#include <string_view>
#include <type_traits>
#include <unordered_set>
@ -52,6 +53,9 @@ public:
*/
std::vector<std::uint32_t> Assemble() const;
/// Patches deferred phi nodes calling the passed function on each phi argument
void PatchDeferredPhi(const std::function<Id(std::size_t index)>& func);
/// Adds a SPIR-V extension.
void AddExtension(std::string extension_name);
@ -87,15 +91,6 @@ public:
AddExecutionMode(entry_point, mode, std::span<const Literal>({literals...}));
}
/// Generate a new id for forward declarations
[[nodiscard]] Id ForwardDeclarationId();
/// Returns the current generator id, useful for self-referencing phi nodes
[[nodiscard]] Id CurrentId() const noexcept;
/// Assign a new id and return the old one, useful for defining forward declarations
Id ExchangeCurrentId(Id new_current_id);
/**
* Adds an existing label to the code
* @param label Label to insert into code.
@ -253,6 +248,12 @@ public:
*/
Id OpPhi(Id result_type, std::span<const Id> operands);
/**
* The SSA phi function. This instruction will be revisited when patching phi nodes.
* @param operands An immutable span of block pairs
*/
Id DeferredOpPhi(Id result_type, std::span<const Id> blocks);
/// Declare a structured loop.
Id OpLoopMerge(Id merge_block, Id continue_target, spv::LoopControlMask loop_control,
std::span<const Id> literals = {});
@ -1236,6 +1237,7 @@ private:
std::unique_ptr<Declarations> declarations;
std::unique_ptr<Stream> global_variables;
std::unique_ptr<Stream> code;
std::vector<std::uint32_t> deferred_phi_nodes;
};
} // namespace Sirit