diff --git a/src/SpirvIR.cpp b/src/SpirvIR.cpp index 73fdd7f24871..21bf5a1e696f 100644 --- a/src/SpirvIR.cpp +++ b/src/SpirvIR.cpp @@ -150,9 +150,8 @@ SpvInstruction SpvInstruction::make(SpvOp op_code) { return instance; } -void SpvInstruction::set_block(SpvBlock block) { - check_defined(); - contents->block = std::move(block); +SpvInstruction::~SpvInstruction() { + clear(); } void SpvInstruction::set_result_id(SpvId result_id) { @@ -270,6 +269,10 @@ bool SpvInstruction::is_defined() const { return contents.defined(); } +void SpvInstruction::clear() { + contents = nullptr; +} + bool SpvInstruction::is_immediate(uint32_t index) const { check_defined(); return (contents->value_types[index] != SpvOperandId); @@ -280,11 +283,6 @@ uint32_t SpvInstruction::length() const { return (uint32_t)contents->operands.size(); } -SpvBlock SpvInstruction::block() const { - check_defined(); - return contents->block; -} - void SpvInstruction::add_data(uint32_t bytes, const void *data, SpvValueType value_type) { check_defined(); @@ -346,34 +344,25 @@ void SpvInstruction::encode(SpvBinary &binary) const { // -- -SpvBlock SpvBlock::make(SpvFunction func, SpvId block_id) { +SpvBlock SpvBlock::make(SpvId block_id) { SpvBlock instance; instance.contents = SpvBlockContentsPtr(new SpvBlockContents()); - instance.contents->parent = std::move(func); instance.contents->block_id = block_id; return instance; } -void SpvBlock::add_instruction(SpvInstruction inst) { - check_defined(); - inst.set_block(*this); - contents->instructions.push_back(inst); -} - -void SpvBlock::add_variable(SpvInstruction var) { - check_defined(); - var.set_block(*this); - contents->variables.push_back(var); +SpvBlock::~SpvBlock() { + clear(); } -void SpvBlock::set_function(SpvFunction func) { +void SpvBlock::add_instruction(SpvInstruction inst) { check_defined(); - contents->parent = std::move(func); + contents->instructions.emplace_back(std::move(inst)); } -SpvFunction SpvBlock::function() const { +void SpvBlock::add_variable(SpvInstruction var) { check_defined(); - return contents->parent; + contents->variables.emplace_back(std::move(var)); } const SpvBlock::Instructions &SpvBlock::instructions() const { @@ -423,6 +412,10 @@ void SpvBlock::check_defined() const { user_assert(is_defined()) << "An SpvBlock must be defined before accessing its properties\n"; } +void SpvBlock::clear() { + contents = nullptr; +} + void SpvBlock::encode(SpvBinary &binary) const { check_defined(); @@ -453,10 +446,18 @@ SpvFunction SpvFunction::make(SpvId func_type_id, SpvId func_id, SpvId return_ty return instance; } +SpvFunction::~SpvFunction() { + clear(); +} + bool SpvFunction::is_defined() const { return contents.defined(); } +void SpvFunction::clear() { + contents = nullptr; +} + SpvBlock SpvFunction::create_block(SpvId block_id) { check_defined(); if (!contents->blocks.empty()) { @@ -465,12 +466,12 @@ SpvBlock SpvFunction::create_block(SpvId block_id) { last_block.add_instruction(SpvFactory::branch(block_id)); } } - SpvBlock block = SpvBlock::make(*this, block_id); + SpvBlock block = SpvBlock::make(block_id); contents->blocks.push_back(block); return block; } -void SpvFunction::add_block(const SpvBlock &block) { +void SpvFunction::add_block(SpvBlock block) { check_defined(); if (!contents->blocks.empty()) { SpvBlock last_block = tail_block(); @@ -478,12 +479,12 @@ void SpvFunction::add_block(const SpvBlock &block) { last_block.add_instruction(SpvFactory::branch(block.id())); } } - contents->blocks.push_back(block); + contents->blocks.emplace_back(std::move(block)); } -void SpvFunction::add_parameter(const SpvInstruction ¶m) { +void SpvFunction::add_parameter(SpvInstruction param) { check_defined(); - contents->parameters.push_back(param); + contents->parameters.emplace_back(std::move(param)); } uint32_t SpvFunction::parameter_count() const { @@ -535,11 +536,6 @@ SpvPrecision SpvFunction::parameter_precision(uint32_t index) const { } } -void SpvFunction::set_module(SpvModule module) { - check_defined(); - contents->parent = std::move(module); -} - SpvInstruction SpvFunction::declaration() const { check_defined(); return contents->declaration; @@ -555,11 +551,6 @@ const SpvFunction::Parameters &SpvFunction::parameters() const { return contents->parameters; } -SpvModule SpvFunction::module() const { - check_defined(); - return contents->parent; -} - SpvId SpvFunction::return_type_id() const { check_defined(); return contents->return_type_id; @@ -608,54 +599,63 @@ SpvModule SpvModule::make(SpvId module_id, return instance; } +SpvModule::~SpvModule() { + clear(); +} + bool SpvModule::is_defined() const { return contents.defined(); } +void SpvModule::clear() { + contents = nullptr; +} + void SpvModule::add_debug_string(SpvId result_id, const std::string &string) { check_defined(); - contents->debug_source.push_back(SpvFactory::debug_string(result_id, string)); + SpvInstruction inst = SpvFactory::debug_string(result_id, string); + contents->debug_source.emplace_back(std::move(inst)); } void SpvModule::add_debug_symbol(SpvId id, const std::string &symbol) { check_defined(); - contents->debug_symbols.push_back(SpvFactory::debug_symbol(id, symbol)); + SpvInstruction inst = SpvFactory::debug_symbol(id, symbol); + contents->debug_symbols.emplace_back(std::move(inst)); } -void SpvModule::add_annotation(const SpvInstruction &val) { +void SpvModule::add_annotation(SpvInstruction val) { check_defined(); - contents->annotations.push_back(val); + contents->annotations.emplace_back(std::move(val)); } -void SpvModule::add_type(const SpvInstruction &val) { +void SpvModule::add_type(SpvInstruction val) { check_defined(); - contents->types.push_back(val); + contents->types.emplace_back(std::move(val)); } -void SpvModule::add_constant(const SpvInstruction &val) { +void SpvModule::add_constant(SpvInstruction val) { check_defined(); - contents->constants.push_back(val); + contents->constants.emplace_back(std::move(val)); } -void SpvModule::add_global(const SpvInstruction &val) { +void SpvModule::add_global(SpvInstruction val) { check_defined(); - contents->globals.push_back(val); + contents->globals.emplace_back(std::move(val)); } -void SpvModule::add_execution_mode(const SpvInstruction &val) { +void SpvModule::add_execution_mode(SpvInstruction val) { check_defined(); - contents->execution_modes.push_back(val); + contents->execution_modes.emplace_back(std::move(val)); } -void SpvModule::add_instruction(const SpvInstruction &val) { +void SpvModule::add_instruction(SpvInstruction val) { check_defined(); - contents->instructions.push_back(val); + contents->instructions.emplace_back(std::move(val)); } void SpvModule::add_function(SpvFunction val) { check_defined(); - val.set_module(*this); - contents->functions.emplace_back(val); + contents->functions.emplace_back(std::move(val)); } void SpvModule::add_entry_point(const std::string &name, SpvInstruction inst) { @@ -1297,7 +1297,7 @@ SpvId SpvBuilder::add_function(const std::string &name, SpvId return_type_id, co func.add_parameter(param_inst); } SpvId block_id = make_id(SpvBlockId); - SpvBlock entry_block = SpvBlock::make(func, block_id); + SpvBlock entry_block = SpvBlock::make(block_id); func.add_block(entry_block); module.add_function(func); function_map[func_id] = func; diff --git a/src/SpirvIR.h b/src/SpirvIR.h index d92eaa696bd7..ccbae3c28098 100644 --- a/src/SpirvIR.h +++ b/src/SpirvIR.h @@ -113,6 +113,7 @@ class SpvFunction; class SpvBlock; class SpvInstruction; class SpvBuilder; +class SpvContext; struct SpvFactory; /** Pre-declarations for SPIR-V IR data structures */ @@ -136,14 +137,13 @@ class SpvInstruction { using ValueTypes = std::vector; SpvInstruction() = default; - ~SpvInstruction() = default; + ~SpvInstruction(); SpvInstruction(const SpvInstruction &) = default; SpvInstruction &operator=(const SpvInstruction &) = default; SpvInstruction(SpvInstruction &&) = default; SpvInstruction &operator=(SpvInstruction &&) = default; - void set_block(SpvBlock block); void set_result_id(SpvId id); void set_type_id(SpvId id); void set_op_code(SpvOp opcode); @@ -170,8 +170,8 @@ class SpvInstruction { bool is_defined() const; bool is_immediate(uint32_t index) const; uint32_t length() const; - SpvBlock block() const; void check_defined() const; + void clear(); void encode(SpvBinary &binary) const; @@ -189,7 +189,7 @@ class SpvBlock { using Blocks = std::vector; SpvBlock() = default; - ~SpvBlock() = default; + ~SpvBlock(); SpvBlock(const SpvBlock &) = default; SpvBlock &operator=(const SpvBlock &) = default; @@ -198,19 +198,18 @@ class SpvBlock { void add_instruction(SpvInstruction inst); void add_variable(SpvInstruction var); - void set_function(SpvFunction func); const Instructions &instructions() const; const Variables &variables() const; - SpvFunction function() const; bool is_reachable() const; bool is_terminated() const; bool is_defined() const; SpvId id() const; void check_defined() const; + void clear(); void encode(SpvBinary &binary) const; - static SpvBlock make(SpvFunction func, SpvId id); + static SpvBlock make(SpvId block_id); protected: SpvBlockContentsPtr contents; @@ -223,7 +222,7 @@ class SpvFunction { using Parameters = std::vector; SpvFunction() = default; - ~SpvFunction() = default; + ~SpvFunction(); SpvFunction(const SpvFunction &) = default; SpvFunction &operator=(const SpvFunction &) = default; @@ -231,12 +230,12 @@ class SpvFunction { SpvFunction &operator=(SpvFunction &&) = default; SpvBlock create_block(SpvId block_id); - void add_block(const SpvBlock &block); - void add_parameter(const SpvInstruction ¶m); - void set_module(SpvModule module); + void add_block(SpvBlock block); + void add_parameter(SpvInstruction param); void set_return_precision(SpvPrecision precision); void set_parameter_precision(uint32_t index, SpvPrecision precision); bool is_defined() const; + void clear(); const Blocks &blocks() const; SpvBlock entry_block() const; @@ -247,7 +246,6 @@ class SpvFunction { uint32_t parameter_count() const; uint32_t control_mask() const; SpvInstruction declaration() const; - SpvModule module() const; SpvId return_type_id() const; SpvId type_id() const; SpvId id() const; @@ -274,7 +272,7 @@ class SpvModule { using Imports = std::vector; SpvModule() = default; - ~SpvModule() = default; + ~SpvModule(); SpvModule(const SpvModule &) = default; SpvModule &operator=(const SpvModule &) = default; @@ -283,13 +281,13 @@ class SpvModule { void add_debug_string(SpvId result_id, const std::string &string); void add_debug_symbol(SpvId id, const std::string &symbol); - void add_annotation(const SpvInstruction &val); - void add_type(const SpvInstruction &val); - void add_constant(const SpvInstruction &val); - void add_global(const SpvInstruction &val); - void add_execution_mode(const SpvInstruction &val); + void add_annotation(SpvInstruction val); + void add_type(SpvInstruction val); + void add_constant(SpvInstruction val); + void add_global(SpvInstruction val); + void add_execution_mode(SpvInstruction val); void add_function(SpvFunction val); - void add_instruction(const SpvInstruction &val); + void add_instruction(SpvInstruction val); void add_entry_point(const std::string &name, SpvInstruction entry_point); void import_instruction_set(SpvId id, const std::string &instruction_set); @@ -334,6 +332,7 @@ class SpvModule { bool is_defined() const; SpvId id() const; void check_defined() const; + void clear(); void encode(SpvBinary &binary) const; @@ -724,7 +723,6 @@ struct SpvInstructionContents { SpvId type_id = SpvNoType; Operands operands; ValueTypes value_types; - SpvBlock block; }; /** Contents of a SPIR-V code block */ @@ -734,7 +732,6 @@ struct SpvBlockContents { using Blocks = std::vector; mutable RefCount ref_count; SpvId block_id = SpvInvalidId; - SpvFunction parent; Instructions instructions; Variables variables; Blocks before; @@ -748,7 +745,6 @@ struct SpvFunctionContents { using Parameters = std::vector; using Blocks = std::vector; mutable RefCount ref_count; - SpvModule parent; SpvId function_id; SpvId function_type_id; SpvId return_type_id;