dawn-cmake/src/transform/loop_to_for_loop_test.cc
Ben Clayton 800b8e3175 optimizations: Implement transform::ShouldRun()
This change adds an override for Transform::ShouldRun() for many of the transforms that can trivially detect whether running would be a no-op or not. Most programs do not require all the transforms to be run, and by skipping those that are not needed, significant performance wins can be had.

This change also removes Transform::Requires() and Program::HasTransformApplied(). This makes little sense now that transforms can be skipped, and the usefulness of this information has been severely reduced since the introduction of transforms that need to be run more than once.
Instread, just document on the transform class what the expectations are.

Issue: tint:1383
Change-Id: I1a6f27cc4ba61ca1475a4ba912c465db619f76c7
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/77121
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
2022-01-25 21:36:04 +00:00

309 lines
4.4 KiB
C++

// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/transform/loop_to_for_loop.h"
#include "src/transform/test_helper.h"
namespace tint {
namespace transform {
namespace {
using LoopToForLoopTest = TransformTest;
TEST_F(LoopToForLoopTest, ShouldRunEmptyModule) {
auto* src = R"()";
EXPECT_FALSE(ShouldRun<LoopToForLoop>(src));
}
TEST_F(LoopToForLoopTest, ShouldRunHasForLoop) {
auto* src = R"(
fn f() {
loop {
break;
}
}
)";
EXPECT_TRUE(ShouldRun<LoopToForLoop>(src));
}
TEST_F(LoopToForLoopTest, EmptyModule) {
auto* src = "";
auto* expect = "";
auto got = Run<LoopToForLoop>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(LoopToForLoopTest, IfBreak) {
auto* src = R"(
fn f() {
var i : i32;
i = 0;
loop {
if (i > 15) {
break;
}
ignore(123);
continuing {
i = i + 1;
}
}
}
)";
auto* expect = R"(
fn f() {
var i : i32;
i = 0;
for(; !((i > 15)); i = (i + 1)) {
ignore(123);
}
}
)";
auto got = Run<LoopToForLoop>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(LoopToForLoopTest, IfElseBreak) {
auto* src = R"(
fn f() {
var i : i32;
i = 0;
loop {
if (i < 15) {
} else {
break;
}
ignore(123);
continuing {
i = i + 1;
}
}
}
)";
auto* expect = R"(
fn f() {
var i : i32;
i = 0;
for(; (i < 15); i = (i + 1)) {
ignore(123);
}
}
)";
auto got = Run<LoopToForLoop>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(LoopToForLoopTest, Nested) {
auto* src = R"(
let N = 16u;
fn f() {
var i : u32 = 0u;
loop {
if (i >= N) {
break;
}
{
var j : u32 = 0u;
loop {
if (j >= N) {
break;
}
ignore(i);
ignore(j);
continuing {
j = (j + 1u);
}
}
}
continuing {
i = (i + 1u);
}
}
}
)";
auto* expect = R"(
let N = 16u;
fn f() {
var i : u32 = 0u;
for(; !((i >= N)); i = (i + 1u)) {
{
var j : u32 = 0u;
for(; !((j >= N)); j = (j + 1u)) {
ignore(i);
ignore(j);
}
}
}
}
)";
auto got = Run<LoopToForLoop>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(LoopToForLoopTest, NoTransform_IfMultipleStmts) {
auto* src = R"(
fn f() {
var i : i32;
i = 0;
loop {
if ((i < 15)) {
ignore(i);
break;
}
ignore(123);
continuing {
i = (i + 1);
}
}
}
)";
auto* expect = src;
auto got = Run<LoopToForLoop>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(LoopToForLoopTest, NoTransform_IfElseMultipleStmts) {
auto* src = R"(
fn f() {
var i : i32;
i = 0;
loop {
if ((i < 15)) {
} else {
ignore(i);
break;
}
ignore(123);
continuing {
i = (i + 1);
}
}
}
)";
auto* expect = src;
auto got = Run<LoopToForLoop>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(LoopToForLoopTest, NoTransform_ContinuingIsCompound) {
auto* src = R"(
fn f() {
var i : i32;
i = 0;
loop {
if ((i < 15)) {
break;
}
ignore(123);
continuing {
if (false) {
}
}
}
}
)";
auto* expect = src;
auto got = Run<LoopToForLoop>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(LoopToForLoopTest, NoTransform_ContinuingMultipleStmts) {
auto* src = R"(
fn f() {
var i : i32;
i = 0;
loop {
if ((i < 15)) {
break;
}
ignore(123);
continuing {
i = (i + 1);
ignore(i);
}
}
}
)";
auto* expect = src;
auto got = Run<LoopToForLoop>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(LoopToForLoopTest, NoTransform_ContinuingUsesVarDeclInLoopBody) {
auto* src = R"(
fn f() {
var i : i32;
i = 0;
loop {
if ((i < 15)) {
break;
}
var j : i32;
continuing {
i = (i + j);
}
}
}
)";
auto* expect = src;
auto got = Run<LoopToForLoop>(src);
EXPECT_EQ(expect, str(got));
}
} // namespace
} // namespace transform
} // namespace tint