From 0f4adaa09d9078391025a372892f7749bb84fcd0 Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Mon, 15 Jan 2018 09:35:09 +0100 Subject: [PATCH 1/2] Handle OpUnreachable in code-gen. Forgot to add it to emit_block_chain. It should just be a noop. --- .../opt/shaders/asm/frag/unreachable.asm.frag | 28 +++++++++ .../shaders/asm/frag/unreachable.asm.frag | 30 +++++++++ shaders/asm/frag/unreachable.asm.frag | 61 +++++++++++++++++++ spirv_glsl.cpp | 4 ++ 4 files changed, 123 insertions(+) create mode 100644 reference/opt/shaders/asm/frag/unreachable.asm.frag create mode 100644 reference/shaders/asm/frag/unreachable.asm.frag create mode 100644 shaders/asm/frag/unreachable.asm.frag diff --git a/reference/opt/shaders/asm/frag/unreachable.asm.frag b/reference/opt/shaders/asm/frag/unreachable.asm.frag new file mode 100644 index 00000000..3358f841 --- /dev/null +++ b/reference/opt/shaders/asm/frag/unreachable.asm.frag @@ -0,0 +1,28 @@ +#version 450 + +layout(location = 0) flat in int counter; +layout(location = 0) out vec4 FragColor; + +vec4 _21; + +void main() +{ + vec4 _15; + vec4 _17; + vec4 _33; + do + { + if (counter == 10) + { + _33 = vec4(10.0); + break; + } + else + { + _33 = vec4(30.0); + break; + } + } while (false); + FragColor = _33; +} + diff --git a/reference/shaders/asm/frag/unreachable.asm.frag b/reference/shaders/asm/frag/unreachable.asm.frag new file mode 100644 index 00000000..0ada3eaa --- /dev/null +++ b/reference/shaders/asm/frag/unreachable.asm.frag @@ -0,0 +1,30 @@ +#version 450 + +layout(location = 0) flat in int counter; +layout(location = 0) out vec4 FragColor; + +vec4 _21; + +void main() +{ + vec4 _15; + vec4 _17; + vec4 _24; + vec4 _33; + _24 = _21; + for (;;) + { + if (counter == 10) + { + _33 = vec4(10.0); + break; + } + else + { + _33 = vec4(30.0); + break; + } + } + FragColor = _33; +} + diff --git a/shaders/asm/frag/unreachable.asm.frag b/shaders/asm/frag/unreachable.asm.frag new file mode 100644 index 00000000..e2ce2eb5 --- /dev/null +++ b/shaders/asm/frag/unreachable.asm.frag @@ -0,0 +1,61 @@ +; SPIR-V +; Version: 1.0 +; Generator: Khronos Glslang Reference Front End; 3 +; Bound: 47 +; Schema: 0 + OpCapability Shader + %1 = OpExtInstImport "GLSL.std.450" + OpMemoryModel Logical GLSL450 + OpEntryPoint Fragment %main "main" %counter %FragColor + OpExecutionMode %main OriginUpperLeft + OpSource GLSL 450 + OpName %main "main" + OpName %counter "counter" + OpName %FragColor "FragColor" + OpDecorate %counter Flat + OpDecorate %counter Location 0 + OpDecorate %FragColor Location 0 + %void = OpTypeVoid + %3 = OpTypeFunction %void + %float = OpTypeFloat 32 + %v4float = OpTypeVector %float 4 + %8 = OpTypeFunction %v4float + %int = OpTypeInt 32 1 +%_ptr_Input_int = OpTypePointer Input %int + %counter = OpVariable %_ptr_Input_int Input + %int_10 = OpConstant %int 10 + %bool = OpTypeBool + %float_10 = OpConstant %float 10 + %21 = OpConstantComposite %v4float %float_10 %float_10 %float_10 %float_10 + %float_30 = OpConstant %float 30 + %25 = OpConstantComposite %v4float %float_30 %float_30 %float_30 %float_30 +%_ptr_Output_v4float = OpTypePointer Output %v4float + %FragColor = OpVariable %_ptr_Output_v4float Output +%_ptr_Function_v4float = OpTypePointer Function %v4float + %false = OpConstantFalse %bool + %44 = OpUndef %v4float + %main = OpFunction %void None %3 + %5 = OpLabel + OpBranch %33 + %33 = OpLabel + %45 = OpPhi %v4float %44 %5 %44 %35 + OpLoopMerge %34 %35 None + OpBranch %36 + %36 = OpLabel + %37 = OpLoad %int %counter + %38 = OpIEqual %bool %37 %int_10 + OpSelectionMerge %39 None + OpBranchConditional %38 %40 %41 + %40 = OpLabel + OpBranch %34 + %41 = OpLabel + OpBranch %34 + %39 = OpLabel + OpUnreachable + %35 = OpLabel + OpBranchConditional %false %33 %34 + %34 = OpLabel + %46 = OpPhi %v4float %21 %40 %25 %41 %44 %35 + OpStore %FragColor %46 + OpReturn + OpFunctionEnd diff --git a/spirv_glsl.cpp b/spirv_glsl.cpp index a3e5b5cf..53ad7c5d 100644 --- a/spirv_glsl.cpp +++ b/spirv_glsl.cpp @@ -8357,6 +8357,10 @@ void CompilerGLSL::emit_block_chain(SPIRBlock &block) statement(backend.discard_literal, ";"); break; + case SPIRBlock::Unreachable: + emit_next_block = false; + break; + default: SPIRV_CROSS_THROW("Unimplemented block terminator."); } From 168bcc7b3bf7fd8cb353568acd68125560beb28d Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Mon, 15 Jan 2018 09:39:15 +0100 Subject: [PATCH 2/2] Add unreachable tests for MSL/HLSL. --- .../asm/frag/unreachable.asm.frag | 44 +++++++++++++ .../shaders-msl/asm/frag/unreachable.asm.frag | 40 ++++++++++++ .../asm/frag/unreachable.asm.frag | 46 ++++++++++++++ .../shaders-msl/asm/frag/unreachable.asm.frag | 42 +++++++++++++ shaders-hlsl/asm/frag/unreachable.asm.frag | 61 +++++++++++++++++++ shaders-msl/asm/frag/unreachable.asm.frag | 61 +++++++++++++++++++ 6 files changed, 294 insertions(+) create mode 100644 reference/opt/shaders-hlsl/asm/frag/unreachable.asm.frag create mode 100644 reference/opt/shaders-msl/asm/frag/unreachable.asm.frag create mode 100644 reference/shaders-hlsl/asm/frag/unreachable.asm.frag create mode 100644 reference/shaders-msl/asm/frag/unreachable.asm.frag create mode 100644 shaders-hlsl/asm/frag/unreachable.asm.frag create mode 100644 shaders-msl/asm/frag/unreachable.asm.frag diff --git a/reference/opt/shaders-hlsl/asm/frag/unreachable.asm.frag b/reference/opt/shaders-hlsl/asm/frag/unreachable.asm.frag new file mode 100644 index 00000000..4ad8db30 --- /dev/null +++ b/reference/opt/shaders-hlsl/asm/frag/unreachable.asm.frag @@ -0,0 +1,44 @@ +static int counter; +static float4 FragColor; + +struct SPIRV_Cross_Input +{ + nointerpolation int counter : TEXCOORD0; +}; + +struct SPIRV_Cross_Output +{ + float4 FragColor : SV_Target0; +}; + +float4 _21; + +void frag_main() +{ + float4 _15; + float4 _17; + float4 _33; + do + { + if (counter == 10) + { + _33 = 10.0f.xxxx; + break; + } + else + { + _33 = 30.0f.xxxx; + break; + } + } while (false); + FragColor = _33; +} + +SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input) +{ + counter = stage_input.counter; + frag_main(); + SPIRV_Cross_Output stage_output; + stage_output.FragColor = FragColor; + return stage_output; +} diff --git a/reference/opt/shaders-msl/asm/frag/unreachable.asm.frag b/reference/opt/shaders-msl/asm/frag/unreachable.asm.frag new file mode 100644 index 00000000..86847ddb --- /dev/null +++ b/reference/opt/shaders-msl/asm/frag/unreachable.asm.frag @@ -0,0 +1,40 @@ +#include +#include + +using namespace metal; + +constant float4 _21 = {}; + +struct main0_in +{ + int counter [[user(locn0)]]; +}; + +struct main0_out +{ + float4 FragColor [[color(0)]]; +}; + +fragment main0_out main0(main0_in in [[stage_in]]) +{ + main0_out out = {}; + float4 _15; + float4 _17; + float4 _33; + do + { + if (in.counter == 10) + { + _33 = float4(10.0); + break; + } + else + { + _33 = float4(30.0); + break; + } + } while (false); + out.FragColor = _33; + return out; +} + diff --git a/reference/shaders-hlsl/asm/frag/unreachable.asm.frag b/reference/shaders-hlsl/asm/frag/unreachable.asm.frag new file mode 100644 index 00000000..aacadb91 --- /dev/null +++ b/reference/shaders-hlsl/asm/frag/unreachable.asm.frag @@ -0,0 +1,46 @@ +static int counter; +static float4 FragColor; + +struct SPIRV_Cross_Input +{ + nointerpolation int counter : TEXCOORD0; +}; + +struct SPIRV_Cross_Output +{ + float4 FragColor : SV_Target0; +}; + +float4 _21; + +void frag_main() +{ + float4 _15; + float4 _17; + float4 _24; + float4 _33; + _24 = _21; + for (;;) + { + if (counter == 10) + { + _33 = 10.0f.xxxx; + break; + } + else + { + _33 = 30.0f.xxxx; + break; + } + } + FragColor = _33; +} + +SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input) +{ + counter = stage_input.counter; + frag_main(); + SPIRV_Cross_Output stage_output; + stage_output.FragColor = FragColor; + return stage_output; +} diff --git a/reference/shaders-msl/asm/frag/unreachable.asm.frag b/reference/shaders-msl/asm/frag/unreachable.asm.frag new file mode 100644 index 00000000..c7c9a2d0 --- /dev/null +++ b/reference/shaders-msl/asm/frag/unreachable.asm.frag @@ -0,0 +1,42 @@ +#include +#include + +using namespace metal; + +constant float4 _21 = {}; + +struct main0_in +{ + int counter [[user(locn0)]]; +}; + +struct main0_out +{ + float4 FragColor [[color(0)]]; +}; + +fragment main0_out main0(main0_in in [[stage_in]]) +{ + main0_out out = {}; + float4 _15; + float4 _17; + float4 _24; + float4 _33; + _24 = _21; + for (;;) + { + if (in.counter == 10) + { + _33 = float4(10.0); + break; + } + else + { + _33 = float4(30.0); + break; + } + } + out.FragColor = _33; + return out; +} + diff --git a/shaders-hlsl/asm/frag/unreachable.asm.frag b/shaders-hlsl/asm/frag/unreachable.asm.frag new file mode 100644 index 00000000..e2ce2eb5 --- /dev/null +++ b/shaders-hlsl/asm/frag/unreachable.asm.frag @@ -0,0 +1,61 @@ +; SPIR-V +; Version: 1.0 +; Generator: Khronos Glslang Reference Front End; 3 +; Bound: 47 +; Schema: 0 + OpCapability Shader + %1 = OpExtInstImport "GLSL.std.450" + OpMemoryModel Logical GLSL450 + OpEntryPoint Fragment %main "main" %counter %FragColor + OpExecutionMode %main OriginUpperLeft + OpSource GLSL 450 + OpName %main "main" + OpName %counter "counter" + OpName %FragColor "FragColor" + OpDecorate %counter Flat + OpDecorate %counter Location 0 + OpDecorate %FragColor Location 0 + %void = OpTypeVoid + %3 = OpTypeFunction %void + %float = OpTypeFloat 32 + %v4float = OpTypeVector %float 4 + %8 = OpTypeFunction %v4float + %int = OpTypeInt 32 1 +%_ptr_Input_int = OpTypePointer Input %int + %counter = OpVariable %_ptr_Input_int Input + %int_10 = OpConstant %int 10 + %bool = OpTypeBool + %float_10 = OpConstant %float 10 + %21 = OpConstantComposite %v4float %float_10 %float_10 %float_10 %float_10 + %float_30 = OpConstant %float 30 + %25 = OpConstantComposite %v4float %float_30 %float_30 %float_30 %float_30 +%_ptr_Output_v4float = OpTypePointer Output %v4float + %FragColor = OpVariable %_ptr_Output_v4float Output +%_ptr_Function_v4float = OpTypePointer Function %v4float + %false = OpConstantFalse %bool + %44 = OpUndef %v4float + %main = OpFunction %void None %3 + %5 = OpLabel + OpBranch %33 + %33 = OpLabel + %45 = OpPhi %v4float %44 %5 %44 %35 + OpLoopMerge %34 %35 None + OpBranch %36 + %36 = OpLabel + %37 = OpLoad %int %counter + %38 = OpIEqual %bool %37 %int_10 + OpSelectionMerge %39 None + OpBranchConditional %38 %40 %41 + %40 = OpLabel + OpBranch %34 + %41 = OpLabel + OpBranch %34 + %39 = OpLabel + OpUnreachable + %35 = OpLabel + OpBranchConditional %false %33 %34 + %34 = OpLabel + %46 = OpPhi %v4float %21 %40 %25 %41 %44 %35 + OpStore %FragColor %46 + OpReturn + OpFunctionEnd diff --git a/shaders-msl/asm/frag/unreachable.asm.frag b/shaders-msl/asm/frag/unreachable.asm.frag new file mode 100644 index 00000000..e2ce2eb5 --- /dev/null +++ b/shaders-msl/asm/frag/unreachable.asm.frag @@ -0,0 +1,61 @@ +; SPIR-V +; Version: 1.0 +; Generator: Khronos Glslang Reference Front End; 3 +; Bound: 47 +; Schema: 0 + OpCapability Shader + %1 = OpExtInstImport "GLSL.std.450" + OpMemoryModel Logical GLSL450 + OpEntryPoint Fragment %main "main" %counter %FragColor + OpExecutionMode %main OriginUpperLeft + OpSource GLSL 450 + OpName %main "main" + OpName %counter "counter" + OpName %FragColor "FragColor" + OpDecorate %counter Flat + OpDecorate %counter Location 0 + OpDecorate %FragColor Location 0 + %void = OpTypeVoid + %3 = OpTypeFunction %void + %float = OpTypeFloat 32 + %v4float = OpTypeVector %float 4 + %8 = OpTypeFunction %v4float + %int = OpTypeInt 32 1 +%_ptr_Input_int = OpTypePointer Input %int + %counter = OpVariable %_ptr_Input_int Input + %int_10 = OpConstant %int 10 + %bool = OpTypeBool + %float_10 = OpConstant %float 10 + %21 = OpConstantComposite %v4float %float_10 %float_10 %float_10 %float_10 + %float_30 = OpConstant %float 30 + %25 = OpConstantComposite %v4float %float_30 %float_30 %float_30 %float_30 +%_ptr_Output_v4float = OpTypePointer Output %v4float + %FragColor = OpVariable %_ptr_Output_v4float Output +%_ptr_Function_v4float = OpTypePointer Function %v4float + %false = OpConstantFalse %bool + %44 = OpUndef %v4float + %main = OpFunction %void None %3 + %5 = OpLabel + OpBranch %33 + %33 = OpLabel + %45 = OpPhi %v4float %44 %5 %44 %35 + OpLoopMerge %34 %35 None + OpBranch %36 + %36 = OpLabel + %37 = OpLoad %int %counter + %38 = OpIEqual %bool %37 %int_10 + OpSelectionMerge %39 None + OpBranchConditional %38 %40 %41 + %40 = OpLabel + OpBranch %34 + %41 = OpLabel + OpBranch %34 + %39 = OpLabel + OpUnreachable + %35 = OpLabel + OpBranchConditional %false %33 %34 + %34 = OpLabel + %46 = OpPhi %v4float %21 %40 %25 %41 %44 %35 + OpStore %FragColor %46 + OpReturn + OpFunctionEnd