mirror of
https://github.com/azahar-emu/sirit.git
synced 2026-04-09 03:17:12 +02:00
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:
@@ -68,6 +68,20 @@ std::vector<u32> Module::Assemble() const {
|
||||
return words;
|
||||
}
|
||||
|
||||
void Module::PatchDeferredPhi(const std::function<Id(std::size_t index)>& func) {
|
||||
for (const u32 phi_index : deferred_phi_nodes) {
|
||||
const u32 first_word = code->Value(phi_index);
|
||||
[[maybe_unused]] const spv::Op op = static_cast<spv::Op>(first_word & 0xffff);
|
||||
assert(op == spv::Op::OpPhi);
|
||||
const u32 num_words = first_word >> 16;
|
||||
const u32 num_args = (num_words - 3) / 2;
|
||||
u32 cursor = phi_index + 3;
|
||||
for (u32 arg = 0; arg < num_args; ++arg, cursor += 2) {
|
||||
code->SetValue(cursor, func(arg).value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Module::AddExtension(std::string extension_name) {
|
||||
extensions.insert(std::move(extension_name));
|
||||
}
|
||||
@@ -95,19 +109,6 @@ void Module::AddExecutionMode(Id entry_point, spv::ExecutionMode mode,
|
||||
*execution_modes << spv::Op::OpExecutionMode << entry_point << mode << literals << EndOp{};
|
||||
}
|
||||
|
||||
Id Module::ForwardDeclarationId() {
|
||||
return Id{++bound};
|
||||
}
|
||||
|
||||
Id Module::CurrentId() const noexcept {
|
||||
return Id{bound + 1};
|
||||
}
|
||||
|
||||
Id Module::ExchangeCurrentId(Id new_current_id) {
|
||||
const std::uint32_t old_id = std::exchange(bound, new_current_id.value - 1);
|
||||
return Id{old_id + 1};
|
||||
}
|
||||
|
||||
Id Module::AddLabel(Id label) {
|
||||
assert(label.value != 0);
|
||||
code->Reserve(2);
|
||||
|
||||
Reference in New Issue
Block a user