From 97ab6a3a706121a6a3c046c13361e9964d53feaa Mon Sep 17 00:00:00 2001 From: James Price Date: Thu, 18 May 2023 03:05:59 +0000 Subject: [PATCH] [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 Reviewed-by: Dan Sinclair Kokoro: Kokoro --- src/tint/writer/spirv/ir/generator_impl_ir.cc | 9 +++++++ src/tint/writer/spirv/ir/generator_impl_ir.h | 5 ++++ .../spirv/ir/generator_impl_ir_var_test.cc | 26 +++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/src/tint/writer/spirv/ir/generator_impl_ir.cc b/src/tint/writer/spirv/ir/generator_impl_ir.cc index e55443acbc..4ec4d9df6b 100644 --- a/src/tint/writer/spirv/ir/generator_impl_ir.cc +++ b/src/tint/writer/spirv/ir/generator_impl_ir.cc @@ -20,6 +20,7 @@ #include "src/tint/ir/function_terminator.h" #include "src/tint/ir/if.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/var.h" #include "src/tint/switch.h" @@ -295,6 +296,10 @@ void GeneratorImplIr::EmitBlock(const ir::Block* block) { auto result = Switch( inst, // [&](const ir::Binary* b) { return EmitBinary(b); }, + [&](const ir::Store* s) { + EmitStore(s); + return 0u; + }, [&](const ir::Var* v) { return EmitVar(v); }, [&](Default) { TINT_ICE(Writer, diagnostics_) @@ -391,6 +396,10 @@ uint32_t GeneratorImplIr::EmitBinary(const ir::Binary* binary) { 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) { auto id = module_.NextId(); auto* ptr = var->Type()->As(); diff --git a/src/tint/writer/spirv/ir/generator_impl_ir.h b/src/tint/writer/spirv/ir/generator_impl_ir.h index 437edb243c..66ffe484fb 100644 --- a/src/tint/writer/spirv/ir/generator_impl_ir.h +++ b/src/tint/writer/spirv/ir/generator_impl_ir.h @@ -33,6 +33,7 @@ class Block; class If; class Function; class Module; +class Store; class Value; class Var; } // namespace tint::ir @@ -105,6 +106,10 @@ class GeneratorImplIr { /// @returns the result ID of the instruction 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. /// @param var the var instruction to emit /// @returns the result ID of the instruction diff --git a/src/tint/writer/spirv/ir/generator_impl_ir_var_test.cc b/src/tint/writer/spirv/ir/generator_impl_ir_var_test.cc index ecf8bf7574..ff94862783 100644 --- a/src/tint/writer/spirv/ir/generator_impl_ir_var_test.cc +++ b/src/tint/writer/spirv/ir/generator_impl_ir_var_test.cc @@ -138,5 +138,31 @@ OpFunctionEnd )"); } +TEST_F(SpvGeneratorImplTest, FunctionVar_Store) { + auto* func = b.CreateFunction(mod.symbols.Register("foo"), mod.types.Get()); + b.Branch(func->start_target, func->end_target); + + auto* ty = mod.types.Get( + mod.types.Get(), 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 tint::writer::spirv