// Copyright 2023 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/core/ir/transform/direct_variable_access.h" #include <string> #include <utility> #include "src/tint/lang/core/ir/builder.h" #include "src/tint/lang/core/ir/clone_context.h" #include "src/tint/lang/core/ir/module.h" #include "src/tint/lang/core/ir/traverse.h" #include "src/tint/lang/core/ir/user_call.h" #include "src/tint/lang/core/ir/validator.h" #include "src/tint/lang/core/ir/var.h" #include "src/tint/utils/containers/reverse.h" usingnamespacetint::core::fluent_types; // NOLINT usingnamespacetint::core::number_suffixes; // NOLINT namespace tint::core::ir::transform { namespace { /// An access root, originating from a module-scope var. /// These roots are not passed by parameter, but instead the callee references the module-scope var /// directly. struct RootModuleScopeVar { … }; /// An access root, originating from a pointer parameter or function-scope var. /// These roots are passed by pointer parameter. struct RootPtrParameter { … }; /// An access root. Either a RootModuleScopeVar or RootPtrParameter. AccessRoot; /// MemberAccess is an access operator to a struct member. struct MemberAccess { … }; /// IndexAccess is an access operator to an array element or matrix column. /// The ordered list of indices is passed by parameter. struct IndexAccess { … }; /// An access operation. Either a MemberAccess or IndexAccess. AccessOp; /// A AccessShape describes the static "path" from a root variable to an element within the /// variable. /// /// Functions that have pointer parameters which need transforming will be forked into one or more /// 'variants'. Each variant has different AccessShapes for the pointer parameters - the transform /// will only emit one variant when the shapes of the pointer parameter accesses match. /// /// Array accessors index expressions are held externally to the AccessShape, so /// AccessShape will be considered equal even if the array or matrix 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 | /// +====================================+===============+=================================+ /// | [ Var 'C', MemberAccess 'x' ] | i32 | C.x | /// +------------------------------------+---------------+---------------------------------+ /// | [ Var 'C', MemberAccess 'y' ] | array<A, 4> | C.y | /// +------------------------------------+---------------+---------------------------------+ /// | [ Var 'C', MemberAccess 'y', | A | C.y[indices[0]] | /// | IndexAccess ] | | | /// +------------------------------------+---------------+---------------------------------+ /// | [ Var 'C', MemberAccess 'y', | array<i32, 8> | C.y[indices[0]].x | /// | IndexAccess, MemberAccess 'x' ] | | | /// +------------------------------------+---------------+---------------------------------+ /// | [ Var 'C', MemberAccess 'y', | i32 | C.y[indices[0]].x[indices[1]] | /// | IndexAccess, MemberAccess 'x', | | | /// | IndexAccess ] | | | /// +------------------------------------+---------------+---------------------------------+ /// | [ Var 'C', MemberAccess 'y', | u32 | C.y[indices[0]].y | /// | IndexAccess, MemberAccess 'y' ] | | | /// +------------------------------------+---------------+---------------------------------+ /// /// Where: `indices` is the AccessChain::indices. struct AccessShape { … }; /// AccessChain describes a chain of access expressions originating from a variable. struct AccessChain { … }; /// A variant signature describes the access shape of all the function's pointer parameters. /// This is a map of pointer parameter index to access shape. VariantSignature; /// FnInfo describes a function that has pointer parameters which need replacing. /// This function will be replaced by zero, one or many variants. Each variant will have a unique /// access shape for the function's the pointer arguments. struct FnInfo { … }; /// FnVariant describes a unique variant of a function that has pointer parameters that need /// replacing. struct FnVariant { … }; /// PIMPL state for the transform. struct State { … }; } // namespace Result<SuccessType> DirectVariableAccess(Module& ir, const DirectVariableAccessOptions& options) { … } } // namespace tint::core::ir::transform