mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-13 07:06:11 +00:00
transform: remove VarForDynamicIndex transform.
Dynamic indexes are limited to references to matrices and arrays https://github.com/gpuweb/gpuweb/pull/1801 Bug: tint:867 Change-Id: I114daa053c8a4ffd23ce784ac4538567a551cc21 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/54701 Kokoro: Kokoro <noreply+kokoro@google.com> Reviewed-by: Ben Clayton <bclayton@google.com> Commit-Queue: Ben Clayton <bclayton@google.com> Auto-Submit: Sarah Mashayekhi <sarahmashay@google.com>
This commit is contained in:
@@ -30,7 +30,6 @@
|
||||
#include "src/transform/inline_pointer_lets.h"
|
||||
#include "src/transform/manager.h"
|
||||
#include "src/transform/simplify.h"
|
||||
#include "src/transform/var_for_dynamic_index.h"
|
||||
|
||||
TINT_INSTANTIATE_TYPEINFO(tint::transform::Spirv::Config);
|
||||
|
||||
@@ -45,7 +44,6 @@ Output Spirv::Run(const Program* in, const DataMap& data) {
|
||||
manager.Add<InlinePointerLets>(); // Required for arrayLength()
|
||||
manager.Add<Simplify>(); // Required for arrayLength()
|
||||
manager.Add<ExternalTextureTransform>();
|
||||
manager.Add<VarForDynamicIndex>();
|
||||
auto transformedInput = manager.Run(in, data);
|
||||
|
||||
auto* cfg = data.Get<Config>();
|
||||
|
||||
@@ -1,80 +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/transform/var_for_dynamic_index.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "src/program_builder.h"
|
||||
#include "src/sem/array.h"
|
||||
#include "src/sem/block_statement.h"
|
||||
#include "src/sem/expression.h"
|
||||
#include "src/sem/statement.h"
|
||||
|
||||
namespace tint {
|
||||
namespace transform {
|
||||
|
||||
VarForDynamicIndex::VarForDynamicIndex() = default;
|
||||
|
||||
VarForDynamicIndex::~VarForDynamicIndex() = default;
|
||||
|
||||
Output VarForDynamicIndex::Run(const Program* in, const DataMap&) {
|
||||
ProgramBuilder out;
|
||||
CloneContext ctx(&out, in);
|
||||
|
||||
for (auto* node : in->ASTNodes().Objects()) {
|
||||
if (auto* access_expr = node->As<ast::ArrayAccessorExpression>()) {
|
||||
// Found an array accessor expression
|
||||
auto* index_expr = access_expr->idx_expr();
|
||||
auto* indexed_expr = access_expr->array();
|
||||
|
||||
if (index_expr->Is<ast::ScalarConstructorExpression>()) {
|
||||
// Index expression is a literal value. As this isn't a dynamic index,
|
||||
// we can ignore this.
|
||||
continue;
|
||||
}
|
||||
|
||||
auto* indexed = ctx.src->Sem().Get(indexed_expr);
|
||||
if (!indexed->Type()->IsAnyOf<sem::Array, sem::Matrix>()) {
|
||||
// This transform currently only cares about array and matrices.
|
||||
continue;
|
||||
}
|
||||
|
||||
auto* stmt = indexed->Stmt(); // Statement that owns the expression
|
||||
auto* block = stmt->Block(); // Block that owns the statement
|
||||
|
||||
// Construct a `var` declaration to hold the value in memory.
|
||||
auto* ty = CreateASTTypeFor(&ctx, indexed->Type());
|
||||
auto var_name = ctx.dst->Symbols().New("var_for_index");
|
||||
auto* var_decl = ctx.dst->Decl(ctx.dst->Var(
|
||||
var_name, ty, ast::StorageClass::kNone, ctx.Clone(indexed_expr)));
|
||||
|
||||
// Insert the `var` declaration before the statement that performs the
|
||||
// indexing. Note that for indexing chains, AST node ordering guarantees
|
||||
// that the inner-most index variable will be placed first in the block.
|
||||
ctx.InsertBefore(block->Declaration()->statements(), stmt->Declaration(),
|
||||
var_decl);
|
||||
|
||||
// Replace the original index expression with the new `var`.
|
||||
ctx.Replace(indexed_expr, ctx.dst->Expr(var_name));
|
||||
}
|
||||
}
|
||||
|
||||
ctx.Clone();
|
||||
|
||||
return Output(Program(std::move(out)));
|
||||
}
|
||||
|
||||
} // namespace transform
|
||||
} // namespace tint
|
||||
@@ -1,48 +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_TRANSFORM_VAR_FOR_DYNAMIC_INDEX_H_
|
||||
#define SRC_TRANSFORM_VAR_FOR_DYNAMIC_INDEX_H_
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "src/transform/transform.h"
|
||||
|
||||
namespace tint {
|
||||
namespace transform {
|
||||
|
||||
/// A transform that extracts array and matrix values that are dynamically
|
||||
/// indexed to a temporary `var` local before performing the index. This
|
||||
/// transform is used by the SPIR-V writer as there is no SPIR-V instruction
|
||||
/// that can dynamically index a non-pointer composite.
|
||||
class VarForDynamicIndex : public Transform {
|
||||
public:
|
||||
/// Constructor
|
||||
VarForDynamicIndex();
|
||||
|
||||
/// Destructor
|
||||
~VarForDynamicIndex() override;
|
||||
|
||||
/// Runs the transform on `program`, returning the transformation result.
|
||||
/// @param program the source program to transform
|
||||
/// @param data optional extra transform-specific input data
|
||||
/// @returns the transformation result
|
||||
Output Run(const Program* program, const DataMap& data = {}) override;
|
||||
};
|
||||
|
||||
} // namespace transform
|
||||
} // namespace tint
|
||||
|
||||
#endif // SRC_TRANSFORM_VAR_FOR_DYNAMIC_INDEX_H_
|
||||
@@ -1,121 +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/transform/var_for_dynamic_index.h"
|
||||
|
||||
#include "src/transform/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace transform {
|
||||
namespace {
|
||||
|
||||
using VarForDynamicIndexTest = TransformTest;
|
||||
|
||||
TEST_F(VarForDynamicIndexTest, EmptyModule) {
|
||||
auto* src = "";
|
||||
auto* expect = "";
|
||||
|
||||
auto got = Run<VarForDynamicIndex>(src);
|
||||
|
||||
EXPECT_EQ(expect, str(got));
|
||||
}
|
||||
|
||||
TEST_F(VarForDynamicIndexTest, ArrayIndexDynamic) {
|
||||
auto* src = R"(
|
||||
fn f() {
|
||||
var i : i32;
|
||||
let p : array<i32, 4> = array<i32, 4>(1, 2, 3, 4);
|
||||
let x : i32 = p[i];
|
||||
}
|
||||
)";
|
||||
|
||||
auto* expect = R"(
|
||||
fn f() {
|
||||
var i : i32;
|
||||
let p : array<i32, 4> = array<i32, 4>(1, 2, 3, 4);
|
||||
var var_for_index : array<i32, 4> = p;
|
||||
let x : i32 = var_for_index[i];
|
||||
}
|
||||
)";
|
||||
|
||||
auto got = Run<VarForDynamicIndex>(src);
|
||||
|
||||
EXPECT_EQ(expect, str(got));
|
||||
}
|
||||
|
||||
TEST_F(VarForDynamicIndexTest, ArrayIndexDynamicChain) {
|
||||
auto* src = R"(
|
||||
fn f() {
|
||||
var i : i32;
|
||||
var j : i32;
|
||||
let p : array<array<i32, 2>, 2> = array<array<i32, 2>, 2>(array<i32, 2>(1, 2), array<i32, 2>(3, 4));
|
||||
let x : i32 = p[i][j];
|
||||
}
|
||||
)";
|
||||
|
||||
// TODO(bclayton): Optimize this case:
|
||||
// This output is not as efficient as it could be.
|
||||
// We only actually need to hoist the inner-most array to a `var`
|
||||
// (`var_for_index`), as later indexing operations will be working with
|
||||
// references, not values.
|
||||
|
||||
auto* expect = R"(
|
||||
fn f() {
|
||||
var i : i32;
|
||||
var j : i32;
|
||||
let p : array<array<i32, 2>, 2> = array<array<i32, 2>, 2>(array<i32, 2>(1, 2), array<i32, 2>(3, 4));
|
||||
var var_for_index : array<array<i32, 2>, 2> = p;
|
||||
var var_for_index_1 : array<i32, 2> = var_for_index[i];
|
||||
let x : i32 = var_for_index_1[j];
|
||||
}
|
||||
)";
|
||||
|
||||
auto got = Run<VarForDynamicIndex>(src);
|
||||
|
||||
EXPECT_EQ(expect, str(got));
|
||||
}
|
||||
|
||||
TEST_F(VarForDynamicIndexTest, ArrayIndexLiteral) {
|
||||
auto* src = R"(
|
||||
fn f() {
|
||||
let p : array<i32, 4> = array<i32, 4>(1, 2, 3, 4);
|
||||
let x : i32 = p[1];
|
||||
}
|
||||
)";
|
||||
|
||||
auto* expect = src;
|
||||
|
||||
auto got = Run<VarForDynamicIndex>(src);
|
||||
|
||||
EXPECT_EQ(expect, str(got));
|
||||
}
|
||||
|
||||
TEST_F(VarForDynamicIndexTest, ArrayIndexLiteralChain) {
|
||||
auto* src = R"(
|
||||
fn f() {
|
||||
let p : array<array<i32, 2>, 2> = array<array<i32, 2>, 2>(array<i32, 2>(1, 2), array<i32, 2>(3, 4));
|
||||
let x : i32 = p[0][1];
|
||||
}
|
||||
)";
|
||||
|
||||
auto* expect = src;
|
||||
|
||||
auto got = Run<VarForDynamicIndex>(src);
|
||||
|
||||
EXPECT_EQ(expect, str(got));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace transform
|
||||
} // namespace tint
|
||||
Reference in New Issue
Block a user