// Copyright 2022 The Dawn & Tint Authors // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // 3. Neither the name of the copyright holder nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "src/tint/lang/wgsl/ast/transform/direct_variable_access.h" #include <algorithm> #include <string> #include <utility> #include "src/tint/lang/core/fluent_types.h" #include "src/tint/lang/core/type/abstract_int.h" #include "src/tint/lang/wgsl/ast/transform/hoist_to_decl_before.h" #include "src/tint/lang/wgsl/ast/traverse_expressions.h" #include "src/tint/lang/wgsl/program/clone_context.h" #include "src/tint/lang/wgsl/program/program_builder.h" #include "src/tint/lang/wgsl/resolver/resolve.h" #include "src/tint/lang/wgsl/sem/call.h" #include "src/tint/lang/wgsl/sem/function.h" #include "src/tint/lang/wgsl/sem/index_accessor_expression.h" #include "src/tint/lang/wgsl/sem/member_accessor_expression.h" #include "src/tint/lang/wgsl/sem/module.h" #include "src/tint/lang/wgsl/sem/statement.h" #include "src/tint/lang/wgsl/sem/struct.h" #include "src/tint/lang/wgsl/sem/variable.h" #include "src/tint/utils/containers/reverse.h" #include "src/tint/utils/macros/scoped_assignment.h" #include "src/tint/utils/text/string_stream.h" TINT_INSTANTIATE_TYPEINFO(…); TINT_INSTANTIATE_TYPEINFO(…); usingnamespacetint::core::number_suffixes; // NOLINT usingnamespacetint::core::fluent_types; // NOLINT namespace tint::ast::transform { namespace { /// AccessRoot describes the root of an AccessShape. struct AccessRoot { … }; /// Inequality operator for AccessRoot bool operator!=(const AccessRoot& a, const AccessRoot& b) { … } /// DynamicIndex is used by DirectVariableAccess::State::AccessOp to indicate an array, matrix or /// vector index. struct DynamicIndex { … }; /// Inequality operator for DynamicIndex bool operator!=(const DynamicIndex&, const DynamicIndex&) { … } /// AccessOp describes a single access in an access chain. /// The access is one of: /// Symbol - a struct member access. /// DynamicIndex - a runtime index on an array, matrix column, or vector element. AccessOp; /// A vector of AccessOp. Describes the static "path" from a root variable to an element /// within the variable. Array accessors index expressions are held externally to the /// AccessShape, so AccessShape will be considered equal even if the array, matrix or vector /// index values differ. /// /// For example, consider the following: /// /// ``` /// struct A { /// x : array<i32, 8>, /// y : u32, /// }; /// struct B { /// x : i32, /// y : array<A, 4> /// }; /// var<workgroup> C : B; /// ``` /// /// The following AccessShape would describe the following: /// /// +==============================+===============+=================================+ /// | AccessShape | Type | Expression | /// +==============================+===============+=================================+ /// | [ Variable 'C', Symbol 'x' ] | i32 | C.x | /// +------------------------------+---------------+---------------------------------+ /// | [ Variable 'C', Symbol 'y' ] | array<A, 4> | C.y | /// +------------------------------+---------------+---------------------------------+ /// | [ Variable 'C', Symbol 'y', | A | C.y[dyn_idx[0]] | /// | DynamicIndex ] | | | /// +------------------------------+---------------+---------------------------------+ /// | [ Variable 'C', Symbol 'y', | array<i32, 8> | C.y[dyn_idx[0]].x | /// | DynamicIndex, Symbol 'x' ] | | | /// +------------------------------+---------------+---------------------------------+ /// | [ Variable 'C', Symbol 'y', | i32 | C.y[dyn_idx[0]].x[dyn_idx[1]] | /// | DynamicIndex, Symbol 'x', | | | /// | DynamicIndex ] | | | /// +------------------------------+---------------+---------------------------------+ /// | [ Variable 'C', Symbol 'y', | u32 | C.y[dyn_idx[0]].y | /// | DynamicIndex, Symbol 'y' ] | | | /// +------------------------------+---------------+---------------------------------+ /// /// Where: `dyn_idx` is the AccessChain::dynamic_indices. struct AccessShape { … }; /// Equality operator for AccessShape bool operator==(const AccessShape& a, const AccessShape& b) { … } /// Inequality operator for AccessShape bool operator!=(const AccessShape& a, const AccessShape& b) { … } /// AccessChain describes a chain of access expressions originating from a variable. struct AccessChain : AccessShape { … }; } // namespace /// The PIMPL state for the DirectVariableAccess transform struct DirectVariableAccess::State { … }; DirectVariableAccess::Config::Config() = default; DirectVariableAccess::Config::Config(const Options& opt) : … { … } DirectVariableAccess::Config::~Config() = default; DirectVariableAccess::DirectVariableAccess() = default; DirectVariableAccess::~DirectVariableAccess() = default; Transform::ApplyResult DirectVariableAccess::Apply(const Program& program, const DataMap& inputs, DataMap&) const { … } } // namespace tint::ast::transform