From 1cc770a7d8d0f460bf5a491e53961d8213e7c46b Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Sat, 24 Apr 2021 23:45:04 -0400 Subject: [PATCH] [spv-out] return Null by default --- src/back/spv/instructions.rs | 7 +++++++ src/back/spv/writer.rs | 15 +++++++++++++-- tests/out/texture-array.spvasm | 2 +- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/back/spv/instructions.rs b/src/back/spv/instructions.rs index 9bc4f56f9..828b944a8 100644 --- a/src/back/spv/instructions.rs +++ b/src/back/spv/instructions.rs @@ -350,6 +350,13 @@ impl super::Instruction { // Constant-Creation Instructions // + pub(super) fn constant_null(result_type_id: Word, id: Word) -> Self { + let mut instruction = Self::new(Op::ConstantNull); + instruction.set_type(result_type_id); + instruction.set_result(id); + instruction + } + pub(super) fn constant_true(result_type_id: Word, id: Word) -> Self { let mut instruction = Self::new(Op::ConstantTrue); instruction.set_type(result_type_id); diff --git a/src/back/spv/writer.rs b/src/back/spv/writer.rs index c966ddf1b..e1a7b7d3c 100644 --- a/src/back/spv/writer.rs +++ b/src/back/spv/writer.rs @@ -2741,8 +2741,19 @@ impl Writer { Some(id) => Instruction::branch(id), // This can happen if the last branch had all the paths // leading out of the graph (i.e. returning). - // So it doesn't matter what we do here, but it has to be valid. - None => Instruction::branch(label_id), + // Or it may be the end of the function. + None => match ir_function.result { + Some(ref result) if function.entry_point_context.is_none() => { + // create a Null and return it + let null_id = self.id_gen.next(); + let type_id = + self.get_type_id(&ir_module.types, LookupType::Handle(result.ty))?; + Instruction::constant_null(type_id, null_id) + .to_words(&mut self.logical_layout.declarations); + Instruction::return_value(null_id) + } + _ => Instruction::return_void(), + }, }); } diff --git a/tests/out/texture-array.spvasm b/tests/out/texture-array.spvasm index 1f4ba03f4..58a2d5b3e 100644 --- a/tests/out/texture-array.spvasm +++ b/tests/out/texture-array.spvasm @@ -76,5 +76,5 @@ OpReturn OpStore %22 %44 OpReturn %37 = OpLabel -OpBranch %29 +OpReturn OpFunctionEnd \ No newline at end of file