[ir][spirv-writer] Emit store instructions

Bug: tint:1906
Change-Id: I858e619fe2968ebe2708d0d47c12b0c17b20cc31
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/133224
Commit-Queue: James Price <jrprice@google.com>
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
James Price 2023-05-18 03:05:59 +00:00 committed by Dawn LUCI CQ
parent 75aaa49d77
commit 97ab6a3a70
3 changed files with 40 additions and 0 deletions

View File

@ -20,6 +20,7 @@
#include "src/tint/ir/function_terminator.h" #include "src/tint/ir/function_terminator.h"
#include "src/tint/ir/if.h" #include "src/tint/ir/if.h"
#include "src/tint/ir/module.h" #include "src/tint/ir/module.h"
#include "src/tint/ir/store.h"
#include "src/tint/ir/transform/add_empty_entry_point.h" #include "src/tint/ir/transform/add_empty_entry_point.h"
#include "src/tint/ir/var.h" #include "src/tint/ir/var.h"
#include "src/tint/switch.h" #include "src/tint/switch.h"
@ -295,6 +296,10 @@ void GeneratorImplIr::EmitBlock(const ir::Block* block) {
auto result = Switch( auto result = Switch(
inst, // inst, //
[&](const ir::Binary* b) { return EmitBinary(b); }, [&](const ir::Binary* b) { return EmitBinary(b); },
[&](const ir::Store* s) {
EmitStore(s);
return 0u;
},
[&](const ir::Var* v) { return EmitVar(v); }, [&](const ir::Var* v) { return EmitVar(v); },
[&](Default) { [&](Default) {
TINT_ICE(Writer, diagnostics_) TINT_ICE(Writer, diagnostics_)
@ -391,6 +396,10 @@ uint32_t GeneratorImplIr::EmitBinary(const ir::Binary* binary) {
return id; return id;
} }
void GeneratorImplIr::EmitStore(const ir::Store* store) {
current_function_.push_inst(spv::Op::OpStore, {Value(store->to), Value(store->from)});
}
uint32_t GeneratorImplIr::EmitVar(const ir::Var* var) { uint32_t GeneratorImplIr::EmitVar(const ir::Var* var) {
auto id = module_.NextId(); auto id = module_.NextId();
auto* ptr = var->Type()->As<type::Pointer>(); auto* ptr = var->Type()->As<type::Pointer>();

View File

@ -33,6 +33,7 @@ class Block;
class If; class If;
class Function; class Function;
class Module; class Module;
class Store;
class Value; class Value;
class Var; class Var;
} // namespace tint::ir } // namespace tint::ir
@ -105,6 +106,10 @@ class GeneratorImplIr {
/// @returns the result ID of the instruction /// @returns the result ID of the instruction
uint32_t EmitBinary(const ir::Binary* binary); uint32_t EmitBinary(const ir::Binary* binary);
/// Emit a store instruction.
/// @param store the store instruction to emit
void EmitStore(const ir::Store* store);
/// Emit a var instruction. /// Emit a var instruction.
/// @param var the var instruction to emit /// @param var the var instruction to emit
/// @returns the result ID of the instruction /// @returns the result ID of the instruction

View File

@ -138,5 +138,31 @@ OpFunctionEnd
)"); )");
} }
TEST_F(SpvGeneratorImplTest, FunctionVar_Store) {
auto* func = b.CreateFunction(mod.symbols.Register("foo"), mod.types.Get<type::Void>());
b.Branch(func->start_target, func->end_target);
auto* ty = mod.types.Get<type::Pointer>(
mod.types.Get<type::I32>(), builtin::AddressSpace::kFunction, builtin::Access::kReadWrite);
auto* v = b.Declare(ty);
func->start_target->instructions.Push(v);
func->start_target->instructions.Push(b.Store(v, b.Constant(42_i)));
generator_.EmitFunction(func);
EXPECT_EQ(DumpModule(generator_.Module()), R"(OpName %1 "foo"
%2 = OpTypeVoid
%3 = OpTypeFunction %2
%7 = OpTypeInt 32 1
%6 = OpTypePointer Function %7
%8 = OpConstant %7 42
%1 = OpFunction %2 None %3
%4 = OpLabel
%5 = OpVariable %6 Function
OpStore %5 %8
OpReturn
OpFunctionEnd
)");
}
} // namespace } // namespace
} // namespace tint::writer::spirv } // namespace tint::writer::spirv