mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-17 17:05:31 +00:00
tint/transform/utils: Correctly scope for-loop init
When using HoistToDeclBefore on a for-loop initializer, the inserted statement would be scoped outside the for-loop. This was incorrect. Change-Id: I764d07068e907cc203145ac8d6f0110b1b73e667 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/122301 Commit-Queue: Ben Clayton <bclayton@chromium.org> Kokoro: Ben Clayton <bclayton@chromium.org> Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
This commit is contained in:
committed by
Dawn LUCI CQ
parent
b990d393f5
commit
3cde73cb1a
@@ -436,16 +436,32 @@ fn a_U_X_X(pre : i32, p : U_X_X, post : i32) -> vec4<i32> {
|
||||
}
|
||||
|
||||
fn b() {
|
||||
let ptr_index_save = first();
|
||||
for(let p1 = &(U[ptr_index_save]); true; ) {
|
||||
a_U_X_X(10, U_X_X(u32(ptr_index_save), u32(second())), 20);
|
||||
{
|
||||
let ptr_index_save = first();
|
||||
let p1 = &(U[ptr_index_save]);
|
||||
loop {
|
||||
if (!(true)) {
|
||||
break;
|
||||
}
|
||||
{
|
||||
a_U_X_X(10, U_X_X(u32(ptr_index_save), u32(second())), 20);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn c_U() {
|
||||
let ptr_index_save_1 = first();
|
||||
for(let p1 = &(U[ptr_index_save_1]); true; ) {
|
||||
a_U_X_X(10, U_X_X(u32(ptr_index_save_1), u32(second())), 20);
|
||||
{
|
||||
let ptr_index_save_1 = first();
|
||||
let p1 = &(U[ptr_index_save_1]);
|
||||
loop {
|
||||
if (!(true)) {
|
||||
break;
|
||||
}
|
||||
{
|
||||
a_U_X_X(10, U_X_X(u32(ptr_index_save_1), u32(second())), 20);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -391,10 +391,15 @@ fn idx2() -> i32 {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let tint_symbol = &(a[idx1()]);
|
||||
let tint_symbol_1 = idx2();
|
||||
for((*(tint_symbol))[tint_symbol_1] = ((*(tint_symbol))[tint_symbol_1] + 1); ; ) {
|
||||
break;
|
||||
{
|
||||
let tint_symbol = &(a[idx1()]);
|
||||
let tint_symbol_1 = idx2();
|
||||
(*(tint_symbol))[tint_symbol_1] = ((*(tint_symbol))[tint_symbol_1] + 1);
|
||||
loop {
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)";
|
||||
|
||||
@@ -272,9 +272,14 @@ fn f() {
|
||||
auto* expect = R"(
|
||||
fn f() {
|
||||
var insert_after = 1;
|
||||
let tint_symbol : array<f32, 4u> = array<f32, 4u>(0.0, 1.0, 2.0, 3.0);
|
||||
for(var i = tint_symbol[insert_after]; ; ) {
|
||||
break;
|
||||
{
|
||||
let tint_symbol : array<f32, 4u> = array<f32, 4u>(0.0, 1.0, 2.0, 3.0);
|
||||
var i = tint_symbol[insert_after];
|
||||
loop {
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)";
|
||||
@@ -301,9 +306,14 @@ fn f() {
|
||||
const arr = array<f32, 4u>(0.0, 1.0, 2.0, 3.0);
|
||||
let runtime_value = 1;
|
||||
var insert_after = 1;
|
||||
let tint_symbol : array<f32, 4u> = arr;
|
||||
for(var i = tint_symbol[runtime_value]; ; ) {
|
||||
break;
|
||||
{
|
||||
let tint_symbol : array<f32, 4u> = arr;
|
||||
var i = tint_symbol[runtime_value];
|
||||
loop {
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)";
|
||||
@@ -332,9 +342,14 @@ const arr = array<f32, 4u>(0.0, 1.0, 2.0, 3.0);
|
||||
fn f() {
|
||||
let runtime_value = 1;
|
||||
var insert_after = 1;
|
||||
let tint_symbol : array<f32, 4u> = arr;
|
||||
for(var i = tint_symbol[runtime_value]; ; ) {
|
||||
break;
|
||||
{
|
||||
let tint_symbol : array<f32, 4u> = arr;
|
||||
var i = tint_symbol[runtime_value];
|
||||
loop {
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)";
|
||||
@@ -377,9 +392,14 @@ fn get_b_runtime(s : S) -> f32 {
|
||||
|
||||
fn f() {
|
||||
var insert_after = 1;
|
||||
let tint_symbol : S = S(1, 2.0, vec3<f32>());
|
||||
for(var x = get_b_runtime(tint_symbol); ; ) {
|
||||
break;
|
||||
{
|
||||
let tint_symbol : S = S(1, 2.0, vec3<f32>());
|
||||
var x = get_b_runtime(tint_symbol);
|
||||
loop {
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)";
|
||||
@@ -412,9 +432,14 @@ struct S {
|
||||
auto* expect = R"(
|
||||
fn f() {
|
||||
var insert_after = 1;
|
||||
let tint_symbol : S = S(1, 2.0, vec3<f32>());
|
||||
for(var x = get_b_runtime(tint_symbol); ; ) {
|
||||
break;
|
||||
{
|
||||
let tint_symbol : S = S(1, 2.0, vec3<f32>());
|
||||
var x = get_b_runtime(tint_symbol);
|
||||
loop {
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -683,8 +708,8 @@ fn f() {
|
||||
auto* expect = R"(
|
||||
fn f() {
|
||||
let runtime_value = 0;
|
||||
let tint_symbol : array<f32, 1u> = array<f32, 1u>(0.0);
|
||||
{
|
||||
let tint_symbol : array<f32, 1u> = array<f32, 1u>(0.0);
|
||||
var f = tint_symbol[runtime_value];
|
||||
loop {
|
||||
let tint_symbol_1 : array<f32, 1u> = array<f32, 1u>(1.0);
|
||||
@@ -728,8 +753,8 @@ fn f() {
|
||||
const arr_a = array<f32, 1u>(0.0);
|
||||
const arr_b = array<f32, 1u>(1.0);
|
||||
const arr_c = array<f32, 1u>(2.0);
|
||||
let tint_symbol : array<f32, 1u> = arr_a;
|
||||
{
|
||||
let tint_symbol : array<f32, 1u> = arr_a;
|
||||
var f = tint_symbol[runtime_value];
|
||||
loop {
|
||||
let tint_symbol_1 : array<f32, 1u> = arr_b;
|
||||
@@ -1301,9 +1326,21 @@ fn Y() {
|
||||
|
||||
fn Z() {
|
||||
var i = 10;
|
||||
let tint_symbol_2 : array<i32, 1u> = array<i32, 1u>(i);
|
||||
for(var f = tint_symbol_2[0]; (f < 10); f = (f + 1)) {
|
||||
var i = 20;
|
||||
{
|
||||
let tint_symbol_2 : array<i32, 1u> = array<i32, 1u>(i);
|
||||
var f = tint_symbol_2[0];
|
||||
loop {
|
||||
if (!((f < 10))) {
|
||||
break;
|
||||
}
|
||||
{
|
||||
var i = 20;
|
||||
}
|
||||
|
||||
continuing {
|
||||
f = (f + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)";
|
||||
|
||||
@@ -848,10 +848,15 @@ fn a(i : i32) -> i32 {
|
||||
|
||||
fn f() {
|
||||
var b = 1;
|
||||
let tint_symbol = a(0);
|
||||
for(var r = (tint_symbol + b); ; ) {
|
||||
var marker = 0;
|
||||
break;
|
||||
{
|
||||
let tint_symbol = a(0);
|
||||
var r = (tint_symbol + b);
|
||||
loop {
|
||||
{
|
||||
var marker = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)";
|
||||
@@ -2169,13 +2174,18 @@ fn a(i : i32) -> bool {
|
||||
|
||||
fn f() {
|
||||
var b = true;
|
||||
var tint_symbol = a(0);
|
||||
if (tint_symbol) {
|
||||
tint_symbol = b;
|
||||
}
|
||||
for(var r = tint_symbol; ; ) {
|
||||
var marker = 0;
|
||||
break;
|
||||
{
|
||||
var tint_symbol = a(0);
|
||||
if (tint_symbol) {
|
||||
tint_symbol = b;
|
||||
}
|
||||
var r = tint_symbol;
|
||||
loop {
|
||||
{
|
||||
var marker = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)";
|
||||
|
||||
@@ -112,6 +112,7 @@ struct HoistToDeclBefore::State {
|
||||
/// loop, so that declaration statements can be inserted before the
|
||||
/// condition expression or continuing statement.
|
||||
struct LoopInfo {
|
||||
utils::Vector<StmtBuilder, 8> init_decls;
|
||||
utils::Vector<StmtBuilder, 8> cond_decls;
|
||||
utils::Vector<StmtBuilder, 8> cont_decls;
|
||||
};
|
||||
@@ -198,7 +199,7 @@ struct HoistToDeclBefore::State {
|
||||
// Next emit the for-loop body
|
||||
body_stmts.Push(ctx.Clone(for_loop->body));
|
||||
|
||||
// Finally create the continuing block if there was one.
|
||||
// Create the continuing block if there was one.
|
||||
const ast::BlockStatement* continuing = nullptr;
|
||||
if (auto* cont = for_loop->continuing) {
|
||||
// Continuing block starts with any let declarations used by
|
||||
@@ -210,8 +211,17 @@ struct HoistToDeclBefore::State {
|
||||
|
||||
auto* body = b.Block(body_stmts);
|
||||
auto* loop = b.Loop(body, continuing);
|
||||
if (auto* init = for_loop->initializer) {
|
||||
return b.Block(ctx.Clone(init), loop);
|
||||
|
||||
// If the loop has no initializer statements, then we're done.
|
||||
// Otherwise, wrap loop with another block, prefixed with the initializer
|
||||
// statements
|
||||
if (!info->init_decls.IsEmpty() || for_loop->initializer) {
|
||||
auto stmts = Build(info->init_decls);
|
||||
if (auto* init = for_loop->initializer) {
|
||||
stmts.Push(ctx.Clone(init));
|
||||
}
|
||||
stmts.Push(loop);
|
||||
return b.Block(std::move(stmts));
|
||||
}
|
||||
return loop;
|
||||
}
|
||||
@@ -299,7 +309,7 @@ struct HoistToDeclBefore::State {
|
||||
// Need to convert 'else if' to 'else { if }'.
|
||||
auto else_if_info = ElseIf(else_if->Declaration());
|
||||
|
||||
// Index the map to convert this else if, even if `stmt` is nullptr.
|
||||
// Index the map to decompose this else if, even if `stmt` is nullptr.
|
||||
auto& decls = else_if_info->cond_decls;
|
||||
if constexpr (!std::is_same_v<BUILDER, Decompose>) {
|
||||
decls.Push(std::forward<BUILDER>(builder));
|
||||
@@ -311,7 +321,7 @@ struct HoistToDeclBefore::State {
|
||||
// Insertion point is a for-loop condition.
|
||||
// For-loop needs to be decomposed to a loop.
|
||||
|
||||
// Index the map to convert this for-loop, even if `stmt` is nullptr.
|
||||
// Index the map to decompose this for-loop, even if `stmt` is nullptr.
|
||||
auto& decls = ForLoop(fl)->cond_decls;
|
||||
if constexpr (!std::is_same_v<BUILDER, Decompose>) {
|
||||
decls.Push(std::forward<BUILDER>(builder));
|
||||
@@ -323,7 +333,7 @@ struct HoistToDeclBefore::State {
|
||||
// Insertion point is a while condition.
|
||||
// While needs to be decomposed to a loop.
|
||||
|
||||
// Index the map to convert this while, even if `stmt` is nullptr.
|
||||
// Index the map to decompose this while, even if `stmt` is nullptr.
|
||||
auto& decls = WhileLoop(w)->cond_decls;
|
||||
if constexpr (!std::is_same_v<BUILDER, Decompose>) {
|
||||
decls.Push(std::forward<BUILDER>(builder));
|
||||
@@ -348,11 +358,14 @@ struct HoistToDeclBefore::State {
|
||||
// These require special care.
|
||||
if (fl->Declaration()->initializer == ip) {
|
||||
// Insertion point is a for-loop initializer.
|
||||
// Insert the new statement above the for-loop.
|
||||
// For-loop needs to be decomposed to a loop.
|
||||
|
||||
// Index the map to decompose this for-loop, even if `stmt` is nullptr.
|
||||
auto& decls = ForLoop(fl)->init_decls;
|
||||
if constexpr (!std::is_same_v<BUILDER, Decompose>) {
|
||||
ctx.InsertBefore(fl->Block()->Declaration()->statements, fl->Declaration(),
|
||||
std::forward<BUILDER>(builder));
|
||||
decls.Push(std::forward<BUILDER>(builder));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -360,11 +373,12 @@ struct HoistToDeclBefore::State {
|
||||
// Insertion point is a for-loop continuing statement.
|
||||
// For-loop needs to be decomposed to a loop.
|
||||
|
||||
// Index the map to convert this for-loop, even if `stmt` is nullptr.
|
||||
// Index the map to decompose this for-loop, even if `stmt` is nullptr.
|
||||
auto& decls = ForLoop(fl)->cont_decls;
|
||||
if constexpr (!std::is_same_v<BUILDER, Decompose>) {
|
||||
decls.Push(std::forward<BUILDER>(builder));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -82,8 +82,16 @@ TEST_F(HoistToDeclBeforeTest, ForLoopInit) {
|
||||
|
||||
auto* expect = R"(
|
||||
fn f() {
|
||||
var tint_symbol : i32 = 1i;
|
||||
for(var a = tint_symbol; true; ) {
|
||||
{
|
||||
var tint_symbol : i32 = 1i;
|
||||
var a = tint_symbol;
|
||||
loop {
|
||||
if (!(true)) {
|
||||
break;
|
||||
}
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)";
|
||||
@@ -545,8 +553,16 @@ fn foo() {
|
||||
}
|
||||
|
||||
fn f() {
|
||||
foo();
|
||||
for(var a = 1i; true; ) {
|
||||
{
|
||||
foo();
|
||||
var a = 1i;
|
||||
loop {
|
||||
if (!(true)) {
|
||||
break;
|
||||
}
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)";
|
||||
@@ -584,8 +600,16 @@ fn foo() {
|
||||
}
|
||||
|
||||
fn f() {
|
||||
foo();
|
||||
for(var a = 1i; true; ) {
|
||||
{
|
||||
foo();
|
||||
var a = 1i;
|
||||
loop {
|
||||
if (!(true)) {
|
||||
break;
|
||||
}
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)";
|
||||
|
||||
@@ -126,9 +126,14 @@ fn f() {
|
||||
fn f() {
|
||||
var i : i32;
|
||||
let p = array<array<i32, 2>, 2>(array<i32, 2>(1, 2), array<i32, 2>(3, 4));
|
||||
var var_for_index : array<array<i32, 2u>, 2u> = p;
|
||||
for(let x = var_for_index[i]; ; ) {
|
||||
break;
|
||||
{
|
||||
var var_for_index : array<array<i32, 2u>, 2u> = p;
|
||||
let x = var_for_index[i];
|
||||
loop {
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)";
|
||||
@@ -154,9 +159,14 @@ fn f() {
|
||||
fn f() {
|
||||
var i : i32;
|
||||
let p = mat2x2(1.0, 2.0, 3.0, 4.0);
|
||||
var var_for_index : mat2x2<f32> = p;
|
||||
for(let x = var_for_index[i]; ; ) {
|
||||
break;
|
||||
{
|
||||
var var_for_index : mat2x2<f32> = p;
|
||||
let x = var_for_index[i];
|
||||
loop {
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)";
|
||||
|
||||
Reference in New Issue
Block a user