Perform the loop-exit test when continuing from inside a do-while.

SPIR-V would previously jump to the top of the do-while loop when
encountering a `continue`. This is not correct; it should jump to the
loop-exit test. We don't have any real shaders which test this case, and
it can't be checked from inside Runtime Effects since do-while is not
part of ES2.

Change-Id: Iac58e9e7ad0a94640e6418afea096b19ba50a2df
Bug: skia:11874
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/397576
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
Commit-Queue: John Stiles <johnstiles@google.com>
diff --git a/src/sksl/codegen/SkSLSPIRVCodeGenerator.cpp b/src/sksl/codegen/SkSLSPIRVCodeGenerator.cpp
index 116c927..907187e 100644
--- a/src/sksl/codegen/SkSLSPIRVCodeGenerator.cpp
+++ b/src/sksl/codegen/SkSLSPIRVCodeGenerator.cpp
@@ -3268,10 +3268,10 @@
         this->writeInstruction(SpvOpBranch, next, out);
     }
     this->writeLabel(next, out);
-    SpvId test = this->writeExpression(*d.test(), out);
-    this->writeInstruction(SpvOpBranchConditional, test, continueTarget, end, out);
+    this->writeInstruction(SpvOpBranch, continueTarget, out);
     this->writeLabel(continueTarget, out);
-    this->writeInstruction(SpvOpBranch, header, out);
+    SpvId test = this->writeExpression(*d.test(), out);
+    this->writeInstruction(SpvOpBranchConditional, test, header, end, out);
     this->writeLabel(end, out);
     fBreakTarget.pop();
     fContinueTarget.pop();
diff --git a/tests/sksl/shared/Control.asm.frag b/tests/sksl/shared/Control.asm.frag
index 0e9a24d9..239d042 100644
--- a/tests/sksl/shared/Control.asm.frag
+++ b/tests/sksl/shared/Control.asm.frag
@@ -87,12 +87,12 @@
 OpStore %sk_FragColor %48
 OpBranch %42
 %42 = OpLabel
+OpBranch %43
+%43 = OpLabel
 %49 = OpLoad %v4float %sk_FragColor
 %50 = OpCompositeExtract %float %49 0
 %51 = OpFOrdLessThan %bool %50 %float_0_75
-OpBranchConditional %51 %43 %44
-%43 = OpLabel
-OpBranch %40
+OpBranchConditional %51 %40 %44
 %44 = OpLabel
 OpStore %i_0 %int_0
 OpBranch %53
diff --git a/tests/sksl/shared/DeadDoWhileLoop.asm.frag b/tests/sksl/shared/DeadDoWhileLoop.asm.frag
index 9c77c48..3249911 100644
--- a/tests/sksl/shared/DeadDoWhileLoop.asm.frag
+++ b/tests/sksl/shared/DeadDoWhileLoop.asm.frag
@@ -33,9 +33,9 @@
 OpStore %sk_FragColor %19
 OpBranch %15
 %15 = OpLabel
-OpBranchConditional %false %16 %17
+OpBranch %16
 %16 = OpLabel
-OpBranch %13
+OpBranchConditional %false %13 %17
 %17 = OpLabel
 OpReturn
 OpFunctionEnd
diff --git a/tests/sksl/shared/DoWhileControlFlow.asm.frag b/tests/sksl/shared/DoWhileControlFlow.asm.frag
index 2bb7c83..5bf9724 100644
--- a/tests/sksl/shared/DoWhileControlFlow.asm.frag
+++ b/tests/sksl/shared/DoWhileControlFlow.asm.frag
@@ -76,12 +76,12 @@
 %38 = OpLabel
 OpBranch %23
 %23 = OpLabel
+OpBranch %24
+%24 = OpLabel
 %39 = OpLoad %v4float %x
 %40 = OpCompositeExtract %float %39 3
 %41 = OpFOrdEqual %bool %40 %float_1
-OpBranchConditional %41 %24 %25
-%24 = OpLabel
-OpBranch %21
+OpBranchConditional %41 %21 %25
 %25 = OpLabel
 OpBranch %42
 %42 = OpLabel
@@ -104,12 +104,12 @@
 OpStore %56 %float_0
 OpBranch %44
 %44 = OpLabel
+OpBranch %45
+%45 = OpLabel
 %58 = OpLoad %v4float %x
 %59 = OpCompositeExtract %float %58 2
 %60 = OpFOrdGreaterThan %bool %59 %float_0
-OpBranchConditional %60 %45 %46
-%45 = OpLabel
-OpBranch %42
+OpBranchConditional %60 %42 %46
 %46 = OpLabel
 %61 = OpLoad %v4float %x
 OpReturnValue %61
diff --git a/tests/sksl/shared/EmptyBlocksES3.asm.frag b/tests/sksl/shared/EmptyBlocksES3.asm.frag
index a7167d1..b9a7460 100644
--- a/tests/sksl/shared/EmptyBlocksES3.asm.frag
+++ b/tests/sksl/shared/EmptyBlocksES3.asm.frag
@@ -144,11 +144,11 @@
 %79 = OpLabel
 OpBranch %80
 %80 = OpLabel
+OpBranch %81
+%81 = OpLabel
 %83 = OpExtInst %float %1 Sqrt %float_1
 %84 = OpFOrdEqual %bool %83 %float_2
-OpBranchConditional %84 %81 %82
-%81 = OpLabel
-OpBranch %78
+OpBranchConditional %84 %78 %82
 %82 = OpLabel
 %85 = OpLoad %v4float %color
 OpReturnValue %85
diff --git a/tests/sksl/shared/ReturnsValueOnEveryPathES3.asm.frag b/tests/sksl/shared/ReturnsValueOnEveryPathES3.asm.frag
index 39e063c..01c5c9e 100644
--- a/tests/sksl/shared/ReturnsValueOnEveryPathES3.asm.frag
+++ b/tests/sksl/shared/ReturnsValueOnEveryPathES3.asm.frag
@@ -264,9 +264,9 @@
 %133 = OpLabel
 OpReturnValue %true
 %134 = OpLabel
-OpBranchConditional %true %135 %136
+OpBranch %135
 %135 = OpLabel
-OpBranch %132
+OpBranchConditional %true %132 %136
 %136 = OpLabel
 OpUnreachable
 OpFunctionEnd
@@ -294,9 +294,9 @@
 %145 = OpLabel
 OpBranch %148
 %146 = OpLabel
-OpBranchConditional %true %147 %148
+OpBranch %147
 %147 = OpLabel
-OpBranch %144
+OpBranchConditional %true %144 %148
 %148 = OpLabel
 OpReturnValue %true
 OpFunctionEnd