tint/transform: Remove FoldConstants
No longer used. Bug: tint:1504 Change-Id: I48548afc421a2658b5367125bd63c80a7f4bc87f Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/92084 Reviewed-by: Antonio Maiorano <amaiorano@google.com> Kokoro: Kokoro <noreply+kokoro@google.com> Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
parent
a20ef0c0f1
commit
e593585e21
|
@ -471,8 +471,6 @@ libtint_source_set("libtint_core_all_src") {
|
||||||
"transform/expand_compound_assignment.h",
|
"transform/expand_compound_assignment.h",
|
||||||
"transform/first_index_offset.cc",
|
"transform/first_index_offset.cc",
|
||||||
"transform/first_index_offset.h",
|
"transform/first_index_offset.h",
|
||||||
"transform/fold_constants.cc",
|
|
||||||
"transform/fold_constants.h",
|
|
||||||
"transform/fold_trivial_single_use_lets.cc",
|
"transform/fold_trivial_single_use_lets.cc",
|
||||||
"transform/fold_trivial_single_use_lets.h",
|
"transform/fold_trivial_single_use_lets.h",
|
||||||
"transform/for_loop_to_loop.cc",
|
"transform/for_loop_to_loop.cc",
|
||||||
|
|
|
@ -398,8 +398,6 @@ set(TINT_LIB_SRCS
|
||||||
transform/expand_compound_assignment.h
|
transform/expand_compound_assignment.h
|
||||||
transform/first_index_offset.cc
|
transform/first_index_offset.cc
|
||||||
transform/first_index_offset.h
|
transform/first_index_offset.h
|
||||||
transform/fold_constants.cc
|
|
||||||
transform/fold_constants.h
|
|
||||||
transform/fold_trivial_single_use_lets.cc
|
transform/fold_trivial_single_use_lets.cc
|
||||||
transform/fold_trivial_single_use_lets.h
|
transform/fold_trivial_single_use_lets.h
|
||||||
transform/for_loop_to_loop.cc
|
transform/for_loop_to_loop.cc
|
||||||
|
@ -1079,7 +1077,6 @@ if(TINT_BUILD_TESTS)
|
||||||
transform/disable_uniformity_analysis_test.cc
|
transform/disable_uniformity_analysis_test.cc
|
||||||
transform/expand_compound_assignment_test.cc
|
transform/expand_compound_assignment_test.cc
|
||||||
transform/first_index_offset_test.cc
|
transform/first_index_offset_test.cc
|
||||||
transform/fold_constants_test.cc
|
|
||||||
transform/fold_trivial_single_use_lets_test.cc
|
transform/fold_trivial_single_use_lets_test.cc
|
||||||
transform/for_loop_to_loop_test.cc
|
transform/for_loop_to_loop_test.cc
|
||||||
transform/expand_compound_assignment.cc
|
transform/expand_compound_assignment.cc
|
||||||
|
|
|
@ -1,125 +0,0 @@
|
||||||
// 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/tint/transform/fold_constants.h"
|
|
||||||
|
|
||||||
#include <unordered_map>
|
|
||||||
#include <utility>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "src/tint/program_builder.h"
|
|
||||||
#include "src/tint/sem/call.h"
|
|
||||||
#include "src/tint/sem/expression.h"
|
|
||||||
#include "src/tint/sem/type_constructor.h"
|
|
||||||
#include "src/tint/sem/type_conversion.h"
|
|
||||||
#include "src/tint/utils/transform.h"
|
|
||||||
|
|
||||||
TINT_INSTANTIATE_TYPEINFO(tint::transform::FoldConstants);
|
|
||||||
|
|
||||||
namespace tint::transform {
|
|
||||||
|
|
||||||
FoldConstants::FoldConstants() = default;
|
|
||||||
|
|
||||||
FoldConstants::~FoldConstants() = default;
|
|
||||||
|
|
||||||
void FoldConstants::Run(CloneContext& ctx, const DataMap&, DataMap&) const {
|
|
||||||
ctx.ReplaceAll([&](const ast::Expression* expr) -> const ast::Expression* {
|
|
||||||
auto* call = ctx.src->Sem().Get<sem::Call>(expr);
|
|
||||||
if (!call) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto value = call->ConstantValue();
|
|
||||||
if (!value.IsValid()) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto* ty = call->Type();
|
|
||||||
|
|
||||||
if (!call->Target()->IsAnyOf<sem::TypeConversion, sem::TypeConstructor>()) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If original ctor expression had no init values, don't replace the expression
|
|
||||||
if (call->Arguments().empty()) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto build_elements = [&](size_t limit) {
|
|
||||||
return Switch(
|
|
||||||
value.ElementType(), //
|
|
||||||
[&](const sem::Bool*) {
|
|
||||||
return utils::TransformN(value.IElements(), limit, [&](AInt i) {
|
|
||||||
return static_cast<const ast::Expression*>(
|
|
||||||
ctx.dst->Expr(static_cast<bool>(i.value)));
|
|
||||||
});
|
|
||||||
},
|
|
||||||
[&](const sem::I32*) {
|
|
||||||
return utils::TransformN(value.IElements(), limit, [&](AInt i) {
|
|
||||||
return static_cast<const ast::Expression*>(ctx.dst->Expr(i32(i.value)));
|
|
||||||
});
|
|
||||||
},
|
|
||||||
[&](const sem::U32*) {
|
|
||||||
return utils::TransformN(value.IElements(), limit, [&](AInt i) {
|
|
||||||
return static_cast<const ast::Expression*>(ctx.dst->Expr(u32(i.value)));
|
|
||||||
});
|
|
||||||
},
|
|
||||||
[&](const sem::F32*) {
|
|
||||||
return utils::TransformN(value.FElements(), limit, [&](AFloat f) {
|
|
||||||
return static_cast<const ast::Expression*>(ctx.dst->Expr(f32(f.value)));
|
|
||||||
});
|
|
||||||
},
|
|
||||||
[&](Default) {
|
|
||||||
TINT_ICE(Transform, ctx.dst->Diagnostics())
|
|
||||||
<< "unhandled Constant::Scalar type: "
|
|
||||||
<< value.ElementType()->FriendlyName(ctx.src->Symbols());
|
|
||||||
return ast::ExpressionList{};
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
if (auto* vec = ty->As<sem::Vector>()) {
|
|
||||||
uint32_t vec_size = static_cast<uint32_t>(vec->Width());
|
|
||||||
|
|
||||||
// We'd like to construct the new vector with the same number of
|
|
||||||
// constructor args that the original node had, but after folding
|
|
||||||
// constants, cases like the following are problematic:
|
|
||||||
//
|
|
||||||
// vec3<f32> = vec3<f32>(vec2<f32>(), 1.0) // vec_size=3, ctor_size=2
|
|
||||||
//
|
|
||||||
// In this case, creating a vec3 with 2 args is invalid, so we should
|
|
||||||
// create it with 3. So what we do is construct with vec_size args,
|
|
||||||
// except if the original vector was single-value initialized, in
|
|
||||||
// which case, we only construct with one arg again.
|
|
||||||
ast::ExpressionList ctors;
|
|
||||||
if (call->Arguments().size() == 1) {
|
|
||||||
ctors = build_elements(1);
|
|
||||||
} else {
|
|
||||||
ctors = build_elements(value.ElementCount());
|
|
||||||
}
|
|
||||||
|
|
||||||
auto* el_ty = CreateASTTypeFor(ctx, vec->type());
|
|
||||||
return ctx.dst->vec(el_ty, vec_size, ctors);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ty->is_scalar()) {
|
|
||||||
return build_elements(1)[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
return nullptr;
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.Clone();
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace tint::transform
|
|
|
@ -1,43 +0,0 @@
|
||||||
// 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.
|
|
||||||
|
|
||||||
#ifndef SRC_TINT_TRANSFORM_FOLD_CONSTANTS_H_
|
|
||||||
#define SRC_TINT_TRANSFORM_FOLD_CONSTANTS_H_
|
|
||||||
|
|
||||||
#include "src/tint/transform/transform.h"
|
|
||||||
|
|
||||||
namespace tint::transform {
|
|
||||||
|
|
||||||
/// FoldConstants transforms the AST by folding constant expressions
|
|
||||||
class FoldConstants final : public Castable<FoldConstants, Transform> {
|
|
||||||
public:
|
|
||||||
/// Constructor
|
|
||||||
FoldConstants();
|
|
||||||
|
|
||||||
/// Destructor
|
|
||||||
~FoldConstants() override;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
/// Runs the transform using the CloneContext built for transforming a
|
|
||||||
/// program. Run() is responsible for calling Clone() on the CloneContext.
|
|
||||||
/// @param ctx the CloneContext primed with the input program and
|
|
||||||
/// ProgramBuilder
|
|
||||||
/// @param inputs optional extra transform-specific input data
|
|
||||||
/// @param outputs optional extra transform-specific output data
|
|
||||||
void Run(CloneContext& ctx, const DataMap& inputs, DataMap& outputs) const override;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace tint::transform
|
|
||||||
|
|
||||||
#endif // SRC_TINT_TRANSFORM_FOLD_CONSTANTS_H_
|
|
|
@ -1,425 +0,0 @@
|
||||||
// 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/tint/transform/fold_constants.h"
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include <utility>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "src/tint/transform/test_helper.h"
|
|
||||||
|
|
||||||
namespace tint::transform {
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
using FoldConstantsTest = TransformTest;
|
|
||||||
|
|
||||||
TEST_F(FoldConstantsTest, Module_Scalar_NoConversion) {
|
|
||||||
auto* src = R"(
|
|
||||||
var<private> a : i32 = i32(123);
|
|
||||||
var<private> b : u32 = u32(123u);
|
|
||||||
var<private> c : f32 = f32(123.0);
|
|
||||||
var<private> d : bool = bool(true);
|
|
||||||
|
|
||||||
fn f() {
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto* expect = R"(
|
|
||||||
var<private> a : i32 = 123i;
|
|
||||||
|
|
||||||
var<private> b : u32 = 123u;
|
|
||||||
|
|
||||||
var<private> c : f32 = 123.0f;
|
|
||||||
|
|
||||||
var<private> d : bool = true;
|
|
||||||
|
|
||||||
fn f() {
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto got = Run<FoldConstants>(src);
|
|
||||||
|
|
||||||
EXPECT_EQ(expect, str(got));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(FoldConstantsTest, Module_Scalar_Conversion) {
|
|
||||||
auto* src = R"(
|
|
||||||
var<private> a : i32 = i32(123.0);
|
|
||||||
var<private> b : u32 = u32(123);
|
|
||||||
var<private> c : f32 = f32(123u);
|
|
||||||
var<private> d : bool = bool(123);
|
|
||||||
|
|
||||||
fn f() {
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto* expect = R"(
|
|
||||||
var<private> a : i32 = 123i;
|
|
||||||
|
|
||||||
var<private> b : u32 = 123u;
|
|
||||||
|
|
||||||
var<private> c : f32 = 123.0f;
|
|
||||||
|
|
||||||
var<private> d : bool = true;
|
|
||||||
|
|
||||||
fn f() {
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto got = Run<FoldConstants>(src);
|
|
||||||
|
|
||||||
EXPECT_EQ(expect, str(got));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(FoldConstantsTest, Module_Scalar_MultipleConversions) {
|
|
||||||
auto* src = R"(
|
|
||||||
var<private> a : i32 = i32(u32(f32(u32(i32(123.0)))));
|
|
||||||
var<private> b : u32 = u32(i32(f32(i32(u32(123)))));
|
|
||||||
var<private> c : f32 = f32(u32(i32(u32(f32(123u)))));
|
|
||||||
var<private> d : bool = bool(i32(f32(i32(u32(123)))));
|
|
||||||
|
|
||||||
fn f() {
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto* expect = R"(
|
|
||||||
var<private> a : i32 = 123i;
|
|
||||||
|
|
||||||
var<private> b : u32 = 123u;
|
|
||||||
|
|
||||||
var<private> c : f32 = 123.0f;
|
|
||||||
|
|
||||||
var<private> d : bool = true;
|
|
||||||
|
|
||||||
fn f() {
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto got = Run<FoldConstants>(src);
|
|
||||||
|
|
||||||
EXPECT_EQ(expect, str(got));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(FoldConstantsTest, Module_Vector_NoConversion) {
|
|
||||||
auto* src = R"(
|
|
||||||
var<private> a : vec3<i32> = vec3<i32>(123);
|
|
||||||
var<private> b : vec3<u32> = vec3<u32>(123u);
|
|
||||||
var<private> c : vec3<f32> = vec3<f32>(123.0);
|
|
||||||
var<private> d : vec3<bool> = vec3<bool>(true);
|
|
||||||
|
|
||||||
fn f() {
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto* expect = R"(
|
|
||||||
var<private> a : vec3<i32> = vec3<i32>(123i);
|
|
||||||
|
|
||||||
var<private> b : vec3<u32> = vec3<u32>(123u);
|
|
||||||
|
|
||||||
var<private> c : vec3<f32> = vec3<f32>(123.0f);
|
|
||||||
|
|
||||||
var<private> d : vec3<bool> = vec3<bool>(true);
|
|
||||||
|
|
||||||
fn f() {
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto got = Run<FoldConstants>(src);
|
|
||||||
|
|
||||||
EXPECT_EQ(expect, str(got));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(FoldConstantsTest, Module_Vector_Conversion) {
|
|
||||||
auto* src = R"(
|
|
||||||
var<private> a : vec3<i32> = vec3<i32>(vec3<f32>(123.0));
|
|
||||||
var<private> b : vec3<u32> = vec3<u32>(vec3<i32>(123));
|
|
||||||
var<private> c : vec3<f32> = vec3<f32>(vec3<u32>(123u));
|
|
||||||
var<private> d : vec3<bool> = vec3<bool>(vec3<i32>(123));
|
|
||||||
|
|
||||||
fn f() {
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto* expect = R"(
|
|
||||||
var<private> a : vec3<i32> = vec3<i32>(123i);
|
|
||||||
|
|
||||||
var<private> b : vec3<u32> = vec3<u32>(123u);
|
|
||||||
|
|
||||||
var<private> c : vec3<f32> = vec3<f32>(123.0f);
|
|
||||||
|
|
||||||
var<private> d : vec3<bool> = vec3<bool>(true);
|
|
||||||
|
|
||||||
fn f() {
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto got = Run<FoldConstants>(src);
|
|
||||||
|
|
||||||
EXPECT_EQ(expect, str(got));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(FoldConstantsTest, Module_Vector_MultipleConversions) {
|
|
||||||
auto* src = R"(
|
|
||||||
var<private> a : vec3<i32> = vec3<i32>(vec3<u32>(vec3<f32>(vec3<u32>(u32(123.0)))));
|
|
||||||
var<private> b : vec3<u32> = vec3<u32>(vec3<i32>(vec3<f32>(vec3<i32>(i32(123)))));
|
|
||||||
var<private> c : vec3<f32> = vec3<f32>(vec3<u32>(vec3<i32>(vec3<u32>(u32(123u)))));
|
|
||||||
var<private> d : vec3<bool> = vec3<bool>(vec3<i32>(vec3<f32>(vec3<i32>(i32(123)))));
|
|
||||||
|
|
||||||
fn f() {
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto* expect = R"(
|
|
||||||
var<private> a : vec3<i32> = vec3<i32>(123i);
|
|
||||||
|
|
||||||
var<private> b : vec3<u32> = vec3<u32>(123u);
|
|
||||||
|
|
||||||
var<private> c : vec3<f32> = vec3<f32>(123.0f);
|
|
||||||
|
|
||||||
var<private> d : vec3<bool> = vec3<bool>(true);
|
|
||||||
|
|
||||||
fn f() {
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto got = Run<FoldConstants>(src);
|
|
||||||
|
|
||||||
EXPECT_EQ(expect, str(got));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(FoldConstantsTest, Module_Vector_MixedSizeConversions) {
|
|
||||||
auto* src = R"(
|
|
||||||
var<private> a : vec4<i32> = vec4<i32>(vec3<i32>(vec3<u32>(1u, 2u, 3u)), 4);
|
|
||||||
var<private> b : vec4<i32> = vec4<i32>(vec2<i32>(vec2<u32>(1u, 2u)), vec2<i32>(4, 5));
|
|
||||||
var<private> c : vec4<i32> = vec4<i32>(1, vec2<i32>(vec2<f32>(2.0, 3.0)), 4);
|
|
||||||
var<private> d : vec4<i32> = vec4<i32>(1, 2, vec2<i32>(vec2<f32>(3.0, 4.0)));
|
|
||||||
var<private> e : vec4<bool> = vec4<bool>(false, bool(f32(1.0)), vec2<bool>(vec2<i32>(0, i32(4u))));
|
|
||||||
|
|
||||||
fn f() {
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto* expect = R"(
|
|
||||||
var<private> a : vec4<i32> = vec4<i32>(1i, 2i, 3i, 4i);
|
|
||||||
|
|
||||||
var<private> b : vec4<i32> = vec4<i32>(1i, 2i, 4i, 5i);
|
|
||||||
|
|
||||||
var<private> c : vec4<i32> = vec4<i32>(1i, 2i, 3i, 4i);
|
|
||||||
|
|
||||||
var<private> d : vec4<i32> = vec4<i32>(1i, 2i, 3i, 4i);
|
|
||||||
|
|
||||||
var<private> e : vec4<bool> = vec4<bool>(false, true, false, true);
|
|
||||||
|
|
||||||
fn f() {
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto got = Run<FoldConstants>(src);
|
|
||||||
|
|
||||||
EXPECT_EQ(expect, str(got));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(FoldConstantsTest, Function_Scalar_NoConversion) {
|
|
||||||
auto* src = R"(
|
|
||||||
fn f() {
|
|
||||||
var a : i32 = i32(123);
|
|
||||||
var b : u32 = u32(123u);
|
|
||||||
var c : f32 = f32(123.0);
|
|
||||||
var d : bool = bool(true);
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto* expect = R"(
|
|
||||||
fn f() {
|
|
||||||
var a : i32 = 123i;
|
|
||||||
var b : u32 = 123u;
|
|
||||||
var c : f32 = 123.0f;
|
|
||||||
var d : bool = true;
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto got = Run<FoldConstants>(src);
|
|
||||||
|
|
||||||
EXPECT_EQ(expect, str(got));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(FoldConstantsTest, Function_Scalar_Conversion) {
|
|
||||||
auto* src = R"(
|
|
||||||
fn f() {
|
|
||||||
var a : i32 = i32(123.0);
|
|
||||||
var b : u32 = u32(123);
|
|
||||||
var c : f32 = f32(123u);
|
|
||||||
var d : bool = bool(123);
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto* expect = R"(
|
|
||||||
fn f() {
|
|
||||||
var a : i32 = 123i;
|
|
||||||
var b : u32 = 123u;
|
|
||||||
var c : f32 = 123.0f;
|
|
||||||
var d : bool = true;
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto got = Run<FoldConstants>(src);
|
|
||||||
|
|
||||||
EXPECT_EQ(expect, str(got));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(FoldConstantsTest, Function_Scalar_MultipleConversions) {
|
|
||||||
auto* src = R"(
|
|
||||||
fn f() {
|
|
||||||
var a : i32 = i32(u32(f32(u32(i32(123.0)))));
|
|
||||||
var b : u32 = u32(i32(f32(i32(u32(123)))));
|
|
||||||
var c : f32 = f32(u32(i32(u32(f32(123u)))));
|
|
||||||
var d : bool = bool(i32(f32(i32(u32(123)))));
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto* expect = R"(
|
|
||||||
fn f() {
|
|
||||||
var a : i32 = 123i;
|
|
||||||
var b : u32 = 123u;
|
|
||||||
var c : f32 = 123.0f;
|
|
||||||
var d : bool = true;
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto got = Run<FoldConstants>(src);
|
|
||||||
|
|
||||||
EXPECT_EQ(expect, str(got));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(FoldConstantsTest, Function_Vector_NoConversion) {
|
|
||||||
auto* src = R"(
|
|
||||||
fn f() {
|
|
||||||
var a : vec3<i32> = vec3<i32>(123i);
|
|
||||||
var b : vec3<u32> = vec3<u32>(123u);
|
|
||||||
var c : vec3<f32> = vec3<f32>(123.0f);
|
|
||||||
var d : vec3<bool> = vec3<bool>(true);
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto* expect = R"(
|
|
||||||
fn f() {
|
|
||||||
var a : vec3<i32> = vec3<i32>(123i);
|
|
||||||
var b : vec3<u32> = vec3<u32>(123u);
|
|
||||||
var c : vec3<f32> = vec3<f32>(123.0f);
|
|
||||||
var d : vec3<bool> = vec3<bool>(true);
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto got = Run<FoldConstants>(src);
|
|
||||||
|
|
||||||
EXPECT_EQ(expect, str(got));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(FoldConstantsTest, Function_Vector_Conversion) {
|
|
||||||
auto* src = R"(
|
|
||||||
fn f() {
|
|
||||||
var a : vec3<i32> = vec3<i32>(vec3<f32>(123.0));
|
|
||||||
var b : vec3<u32> = vec3<u32>(vec3<i32>(123));
|
|
||||||
var c : vec3<f32> = vec3<f32>(vec3<u32>(123u));
|
|
||||||
var d : vec3<bool> = vec3<bool>(vec3<i32>(123));
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto* expect = R"(
|
|
||||||
fn f() {
|
|
||||||
var a : vec3<i32> = vec3<i32>(123i);
|
|
||||||
var b : vec3<u32> = vec3<u32>(123u);
|
|
||||||
var c : vec3<f32> = vec3<f32>(123.0f);
|
|
||||||
var d : vec3<bool> = vec3<bool>(true);
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto got = Run<FoldConstants>(src);
|
|
||||||
|
|
||||||
EXPECT_EQ(expect, str(got));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(FoldConstantsTest, Function_Vector_MultipleConversions) {
|
|
||||||
auto* src = R"(
|
|
||||||
fn f() {
|
|
||||||
var a : vec3<i32> = vec3<i32>(vec3<u32>(vec3<f32>(vec3<u32>(u32(123.0)))));
|
|
||||||
var b : vec3<u32> = vec3<u32>(vec3<i32>(vec3<f32>(vec3<i32>(i32(123)))));
|
|
||||||
var c : vec3<f32> = vec3<f32>(vec3<u32>(vec3<i32>(vec3<u32>(u32(123u)))));
|
|
||||||
var d : vec3<bool> = vec3<bool>(vec3<i32>(vec3<f32>(vec3<i32>(i32(123)))));
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto* expect = R"(
|
|
||||||
fn f() {
|
|
||||||
var a : vec3<i32> = vec3<i32>(123i);
|
|
||||||
var b : vec3<u32> = vec3<u32>(123u);
|
|
||||||
var c : vec3<f32> = vec3<f32>(123.0f);
|
|
||||||
var d : vec3<bool> = vec3<bool>(true);
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto got = Run<FoldConstants>(src);
|
|
||||||
|
|
||||||
EXPECT_EQ(expect, str(got));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(FoldConstantsTest, Function_Vector_MixedSizeConversions) {
|
|
||||||
auto* src = R"(
|
|
||||||
fn f() {
|
|
||||||
var a : vec4<i32> = vec4<i32>(vec3<i32>(vec3<u32>(1u, 2u, 3u)), 4);
|
|
||||||
var b : vec4<i32> = vec4<i32>(vec2<i32>(vec2<u32>(1u, 2u)), vec2<i32>(4, 5));
|
|
||||||
var c : vec4<i32> = vec4<i32>(1, vec2<i32>(vec2<f32>(2.0, 3.0)), 4);
|
|
||||||
var d : vec4<i32> = vec4<i32>(1, 2, vec2<i32>(vec2<f32>(3.0, 4.0)));
|
|
||||||
var e : vec4<bool> = vec4<bool>(false, bool(f32(1.0)), vec2<bool>(vec2<i32>(0, i32(4u))));
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto* expect = R"(
|
|
||||||
fn f() {
|
|
||||||
var a : vec4<i32> = vec4<i32>(1i, 2i, 3i, 4i);
|
|
||||||
var b : vec4<i32> = vec4<i32>(1i, 2i, 4i, 5i);
|
|
||||||
var c : vec4<i32> = vec4<i32>(1i, 2i, 3i, 4i);
|
|
||||||
var d : vec4<i32> = vec4<i32>(1i, 2i, 3i, 4i);
|
|
||||||
var e : vec4<bool> = vec4<bool>(false, true, false, true);
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto got = Run<FoldConstants>(src);
|
|
||||||
|
|
||||||
EXPECT_EQ(expect, str(got));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(FoldConstantsTest, Function_Vector_ConstantWithNonConstant) {
|
|
||||||
auto* src = R"(
|
|
||||||
fn f() {
|
|
||||||
var a : f32 = f32();
|
|
||||||
var b : vec2<f32> = vec2<f32>(f32(i32(1)), a);
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto* expect = R"(
|
|
||||||
fn f() {
|
|
||||||
var a : f32 = f32();
|
|
||||||
var b : vec2<f32> = vec2<f32>(1.0f, a);
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
auto got = Run<FoldConstants>(src);
|
|
||||||
|
|
||||||
EXPECT_EQ(expect, str(got));
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
} // namespace tint::transform
|
|
|
@ -334,7 +334,6 @@ tint_unittests_source_set("tint_unittests_transform_src") {
|
||||||
"../../src/tint/transform/disable_uniformity_analysis_test.cc",
|
"../../src/tint/transform/disable_uniformity_analysis_test.cc",
|
||||||
"../../src/tint/transform/expand_compound_assignment_test.cc",
|
"../../src/tint/transform/expand_compound_assignment_test.cc",
|
||||||
"../../src/tint/transform/first_index_offset_test.cc",
|
"../../src/tint/transform/first_index_offset_test.cc",
|
||||||
"../../src/tint/transform/fold_constants_test.cc",
|
|
||||||
"../../src/tint/transform/fold_trivial_single_use_lets_test.cc",
|
"../../src/tint/transform/fold_trivial_single_use_lets_test.cc",
|
||||||
"../../src/tint/transform/for_loop_to_loop_test.cc",
|
"../../src/tint/transform/for_loop_to_loop_test.cc",
|
||||||
"../../src/tint/transform/localize_struct_array_assignment_test.cc",
|
"../../src/tint/transform/localize_struct_array_assignment_test.cc",
|
||||||
|
|
Loading…
Reference in New Issue