transform: Fix PromoteInitializersToConstVar handling of for-loops

PromoteInitializersToConstVar was erroring on for loops that contained array or structure constructor expressions.

Added lots more tests.

Fixed: tint:1364
Change-Id: I033eaad94756ea496fc8bc5f03f39c6dba4e3a88
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/75580
Reviewed-by: James Price <jrprice@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton
2022-01-06 21:32:41 +00:00
committed by Tint LUCI CQ
parent 4d245d9a36
commit e4e7485854
63 changed files with 895 additions and 96 deletions

View File

@@ -1,5 +0,0 @@
fn f() {
var i : i32;
for (;i < 4;) {
}
}

View File

@@ -0,0 +1,4 @@
fn f() {
var i : i32;
for (;i < array<i32, 1>(1)[0];) {}
}

View File

@@ -0,0 +1,14 @@
[numthreads(1, 1, 1)]
void unused_entry_point() {
return;
}
void f() {
int i = 0;
[loop] while (true) {
const int tint_symbol[1] = {1};
if (!((i < tint_symbol[0]))) {
break;
}
}
}

View File

@@ -0,0 +1,17 @@
#include <metal_stdlib>
using namespace metal;
struct tint_array_wrapper {
int arr[1];
};
void f() {
int i = 0;
while (true) {
tint_array_wrapper const tint_symbol = {.arr={1}};
if (!((i < tint_symbol.arr[0]))) {
break;
}
}
}

View File

@@ -0,0 +1,52 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 28
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
OpExecutionMode %unused_entry_point LocalSize 1 1 1
OpName %unused_entry_point "unused_entry_point"
OpName %f "f"
OpName %i "i"
OpDecorate %_arr_int_uint_1 ArrayStride 4
%void = OpTypeVoid
%1 = OpTypeFunction %void
%int = OpTypeInt 32 1
%_ptr_Function_int = OpTypePointer Function %int
%10 = OpConstantNull %int
%uint = OpTypeInt 32 0
%uint_1 = OpConstant %uint 1
%_arr_int_uint_1 = OpTypeArray %int %uint_1
%int_1 = OpConstant %int 1
%21 = OpConstantComposite %_arr_int_uint_1 %int_1
%int_0 = OpConstant %int 0
%bool = OpTypeBool
%unused_entry_point = OpFunction %void None %1
%4 = OpLabel
OpReturn
OpFunctionEnd
%f = OpFunction %void None %1
%6 = OpLabel
%i = OpVariable %_ptr_Function_int Function %10
OpBranch %11
%11 = OpLabel
OpLoopMerge %12 %13 None
OpBranch %14
%14 = OpLabel
%16 = OpLoad %int %i
%23 = OpCompositeExtract %int %21 0
%24 = OpSLessThan %bool %16 %23
%15 = OpLogicalNot %bool %24
OpSelectionMerge %26 None
OpBranchConditional %15 %27 %26
%27 = OpLabel
OpBranch %12
%26 = OpLabel
OpBranch %13
%13 = OpLabel
OpBranch %11
%12 = OpLabel
OpReturn
OpFunctionEnd

View File

@@ -0,0 +1,5 @@
fn f() {
var i : i32;
for(; (i < array<i32, 1>(1)[0]); ) {
}
}

View File

@@ -0,0 +1,5 @@
fn f() {
var i : i32;
for (;i < 4;) {
}
}

View File

@@ -0,0 +1,8 @@
struct S {
i : i32;
};
fn f() {
var i : i32;
for (; i < S(1).i;) {}
}

View File

@@ -0,0 +1,18 @@
[numthreads(1, 1, 1)]
void unused_entry_point() {
return;
}
struct S {
int i;
};
void f() {
int i = 0;
[loop] while (true) {
const S tint_symbol = {1};
if (!((i < tint_symbol.i))) {
break;
}
}
}

View File

@@ -0,0 +1,17 @@
#include <metal_stdlib>
using namespace metal;
struct S {
int i;
};
void f() {
int i = 0;
while (true) {
S const tint_symbol = {.i=1};
if (!((i < tint_symbol.i))) {
break;
}
}
}

View File

@@ -0,0 +1,51 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 25
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
OpExecutionMode %unused_entry_point LocalSize 1 1 1
OpName %unused_entry_point "unused_entry_point"
OpName %f "f"
OpName %i "i"
OpName %S "S"
OpMemberName %S 0 "i"
OpMemberDecorate %S 0 Offset 0
%void = OpTypeVoid
%1 = OpTypeFunction %void
%int = OpTypeInt 32 1
%_ptr_Function_int = OpTypePointer Function %int
%10 = OpConstantNull %int
%S = OpTypeStruct %int
%int_1 = OpConstant %int 1
%19 = OpConstantComposite %S %int_1
%bool = OpTypeBool
%unused_entry_point = OpFunction %void None %1
%4 = OpLabel
OpReturn
OpFunctionEnd
%f = OpFunction %void None %1
%6 = OpLabel
%i = OpVariable %_ptr_Function_int Function %10
OpBranch %11
%11 = OpLabel
OpLoopMerge %12 %13 None
OpBranch %14
%14 = OpLabel
%16 = OpLoad %int %i
%20 = OpCompositeExtract %int %19 0
%21 = OpSLessThan %bool %16 %20
%15 = OpLogicalNot %bool %21
OpSelectionMerge %23 None
OpBranchConditional %15 %24 %23
%24 = OpLabel
OpBranch %12
%23 = OpLabel
OpBranch %13
%13 = OpLabel
OpBranch %11
%12 = OpLabel
OpReturn
OpFunctionEnd

View File

@@ -0,0 +1,9 @@
struct S {
i : i32;
};
fn f() {
var i : i32;
for(; (i < S(1).i); ) {
}
}

View File

@@ -1,4 +0,0 @@
fn f() {
var i : i32;
for (;;i = i + 1) {}
}

View File

@@ -0,0 +1,4 @@
fn f() {
var i : i32;
for (;;i = i + array<i32, 1>(1)[0]) {}
}

View File

@@ -0,0 +1,14 @@
[numthreads(1, 1, 1)]
void unused_entry_point() {
return;
}
void f() {
int i = 0;
[loop] while (true) {
{
const int tint_symbol[1] = {1};
i = (i + tint_symbol[0]);
}
}
}

View File

@@ -0,0 +1,17 @@
#include <metal_stdlib>
using namespace metal;
struct tint_array_wrapper {
int arr[1];
};
void f() {
int i = 0;
while (true) {
{
tint_array_wrapper const tint_symbol = {.arr={1}};
i = as_type<int>((as_type<uint>(i) + as_type<uint>(tint_symbol.arr[0])));
}
}
}

View File

@@ -0,0 +1,46 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 24
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
OpExecutionMode %unused_entry_point LocalSize 1 1 1
OpName %unused_entry_point "unused_entry_point"
OpName %f "f"
OpName %i "i"
OpDecorate %_arr_int_uint_1 ArrayStride 4
%void = OpTypeVoid
%1 = OpTypeFunction %void
%int = OpTypeInt 32 1
%_ptr_Function_int = OpTypePointer Function %int
%10 = OpConstantNull %int
%uint = OpTypeInt 32 0
%uint_1 = OpConstant %uint 1
%_arr_int_uint_1 = OpTypeArray %int %uint_1
%int_1 = OpConstant %int 1
%20 = OpConstantComposite %_arr_int_uint_1 %int_1
%int_0 = OpConstant %int 0
%unused_entry_point = OpFunction %void None %1
%4 = OpLabel
OpReturn
OpFunctionEnd
%f = OpFunction %void None %1
%6 = OpLabel
%i = OpVariable %_ptr_Function_int Function %10
OpBranch %11
%11 = OpLabel
OpLoopMerge %12 %13 None
OpBranch %14
%14 = OpLabel
OpBranch %13
%13 = OpLabel
%15 = OpLoad %int %i
%22 = OpCompositeExtract %int %20 0
%23 = OpIAdd %int %15 %22
OpStore %i %23
OpBranch %11
%12 = OpLabel
OpReturn
OpFunctionEnd

View File

@@ -0,0 +1,5 @@
fn f() {
var i : i32;
for(; ; i = (i + array<i32, 1>(1)[0])) {
}
}

View File

@@ -0,0 +1,4 @@
fn f() {
var i : i32;
for (;;i = i + 1) {}
}

View File

@@ -0,0 +1,7 @@
struct S {
i : i32;
};
fn f() {
for (var i = 0;; i = i + S(1).i) {}
}

View File

@@ -0,0 +1,20 @@
[numthreads(1, 1, 1)]
void unused_entry_point() {
return;
}
struct S {
int i;
};
void f() {
{
int i = 0;
[loop] while (true) {
{
const S tint_symbol = {1};
i = (i + tint_symbol.i);
}
}
}
}

View File

@@ -0,0 +1,19 @@
#include <metal_stdlib>
using namespace metal;
struct S {
int i;
};
void f() {
{
int i = 0;
while (true) {
{
S const tint_symbol = {.i=1};
i = as_type<int>((as_type<uint>(i) + as_type<uint>(tint_symbol.i)));
}
}
}
}

View File

@@ -0,0 +1,47 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 22
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
OpExecutionMode %unused_entry_point LocalSize 1 1 1
OpName %unused_entry_point "unused_entry_point"
OpName %f "f"
OpName %i "i"
OpName %S "S"
OpMemberName %S 0 "i"
OpMemberDecorate %S 0 Offset 0
%void = OpTypeVoid
%1 = OpTypeFunction %void
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
%_ptr_Function_int = OpTypePointer Function %int
%11 = OpConstantNull %int
%S = OpTypeStruct %int
%int_1 = OpConstant %int 1
%19 = OpConstantComposite %S %int_1
%unused_entry_point = OpFunction %void None %1
%4 = OpLabel
OpReturn
OpFunctionEnd
%f = OpFunction %void None %1
%6 = OpLabel
%i = OpVariable %_ptr_Function_int Function %11
OpStore %i %int_0
OpBranch %12
%12 = OpLabel
OpLoopMerge %13 %14 None
OpBranch %15
%15 = OpLabel
OpBranch %14
%14 = OpLabel
%16 = OpLoad %int %i
%20 = OpCompositeExtract %int %19 0
%21 = OpIAdd %int %16 %20
OpStore %i %21
OpBranch %12
%13 = OpLabel
OpReturn
OpFunctionEnd

View File

@@ -0,0 +1,8 @@
struct S {
i : i32;
};
fn f() {
for(var i = 0; ; i = (i + S(1).i)) {
}
}

View File

@@ -1,3 +0,0 @@
fn f() {
for (var i : i32 = 0;;) {}
}

View File

@@ -0,0 +1,3 @@
fn f() {
for (var i : i32 = array<i32, 1>(1)[0];;) {}
}

View File

@@ -0,0 +1,12 @@
[numthreads(1, 1, 1)]
void unused_entry_point() {
return;
}
void f() {
const int tint_symbol[1] = {1};
{
[loop] for(int i = tint_symbol[0]; ; ) {
}
}
}

View File

@@ -0,0 +1,13 @@
#include <metal_stdlib>
using namespace metal;
struct tint_array_wrapper {
int arr[1];
};
void f() {
tint_array_wrapper const tint_symbol = {.arr={1}};
for(int i = tint_symbol.arr[0]; ; ) {
}
}

View File

@@ -0,0 +1,44 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 22
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
OpExecutionMode %unused_entry_point LocalSize 1 1 1
OpName %unused_entry_point "unused_entry_point"
OpName %f "f"
OpName %i "i"
OpDecorate %_arr_int_uint_1 ArrayStride 4
%void = OpTypeVoid
%1 = OpTypeFunction %void
%int = OpTypeInt 32 1
%uint = OpTypeInt 32 0
%uint_1 = OpConstant %uint 1
%_arr_int_uint_1 = OpTypeArray %int %uint_1
%int_1 = OpConstant %int 1
%12 = OpConstantComposite %_arr_int_uint_1 %int_1
%int_0 = OpConstant %int 0
%_ptr_Function_int = OpTypePointer Function %int
%17 = OpConstantNull %int
%unused_entry_point = OpFunction %void None %1
%4 = OpLabel
OpReturn
OpFunctionEnd
%f = OpFunction %void None %1
%6 = OpLabel
%i = OpVariable %_ptr_Function_int Function %17
%14 = OpCompositeExtract %int %12 0
OpStore %i %14
OpBranch %18
%18 = OpLabel
OpLoopMerge %19 %20 None
OpBranch %21
%21 = OpLabel
OpBranch %20
%20 = OpLabel
OpBranch %18
%19 = OpLabel
OpReturn
OpFunctionEnd

View File

@@ -0,0 +1,4 @@
fn f() {
for(var i : i32 = array<i32, 1>(1)[0]; ; ) {
}
}

View File

@@ -0,0 +1,3 @@
fn f() {
for (var i : i32 = 0;;) {}
}

View File

@@ -0,0 +1,7 @@
struct S {
i : i32;
};
fn f() {
for (var i : i32 = S(1).i;;) {}
}

View File

@@ -0,0 +1,16 @@
[numthreads(1, 1, 1)]
void unused_entry_point() {
return;
}
struct S {
int i;
};
void f() {
const S tint_symbol = {1};
{
[loop] for(int i = tint_symbol.i; ; ) {
}
}
}

View File

@@ -0,0 +1,13 @@
#include <metal_stdlib>
using namespace metal;
struct S {
int i;
};
void f() {
S const tint_symbol = {.i=1};
for(int i = tint_symbol.i; ; ) {
}
}

View File

@@ -0,0 +1,43 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 19
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
OpExecutionMode %unused_entry_point LocalSize 1 1 1
OpName %unused_entry_point "unused_entry_point"
OpName %f "f"
OpName %S "S"
OpMemberName %S 0 "i"
OpName %i "i"
OpMemberDecorate %S 0 Offset 0
%void = OpTypeVoid
%1 = OpTypeFunction %void
%int = OpTypeInt 32 1
%S = OpTypeStruct %int
%int_1 = OpConstant %int 1
%10 = OpConstantComposite %S %int_1
%_ptr_Function_int = OpTypePointer Function %int
%14 = OpConstantNull %int
%unused_entry_point = OpFunction %void None %1
%4 = OpLabel
OpReturn
OpFunctionEnd
%f = OpFunction %void None %1
%6 = OpLabel
%i = OpVariable %_ptr_Function_int Function %14
%11 = OpCompositeExtract %int %10 0
OpStore %i %11
OpBranch %15
%15 = OpLabel
OpLoopMerge %16 %17 None
OpBranch %18
%18 = OpLabel
OpBranch %17
%17 = OpLabel
OpBranch %15
%16 = OpLabel
OpReturn
OpFunctionEnd

View File

@@ -0,0 +1,8 @@
struct S {
i : i32;
};
fn f() {
for(var i : i32 = S(1).i; ; ) {
}
}