Antonio Maiorano | 268d7b8 | 2022-06-24 22:28:23 +0000 | [diff] [blame] | 1 | // Copyright 2022 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
James Price | 7d2e204 | 2023-05-11 13:10:59 +0000 | [diff] [blame] | 15 | #ifndef SRC_TINT_AST_TRANSFORM_SPIRV_ATOMIC_H_ |
| 16 | #define SRC_TINT_AST_TRANSFORM_SPIRV_ATOMIC_H_ |
Antonio Maiorano | 268d7b8 | 2022-06-24 22:28:23 +0000 | [diff] [blame] | 17 | |
| 18 | #include <string> |
| 19 | |
| 20 | #include "src/tint/ast/internal_attribute.h" |
James Price | 7d2e204 | 2023-05-11 13:10:59 +0000 | [diff] [blame] | 21 | #include "src/tint/ast/transform/transform.h" |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 22 | #include "src/tint/builtin/function.h" |
Antonio Maiorano | 268d7b8 | 2022-06-24 22:28:23 +0000 | [diff] [blame] | 23 | |
| 24 | // Forward declarations |
| 25 | namespace tint { |
| 26 | class CloneContext; |
| 27 | } // namespace tint |
| 28 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 29 | namespace tint::ast::transform { |
Antonio Maiorano | 268d7b8 | 2022-06-24 22:28:23 +0000 | [diff] [blame] | 30 | |
| 31 | /// SpirvAtomic is a transform that replaces calls to stub functions created by the SPIR-V reader |
| 32 | /// with calls to the WGSL atomic builtin. It also makes sure to replace variable declarations that |
| 33 | /// are the target of the atomic operations with an atomic declaration of the same type. For |
| 34 | /// structs, it creates a copy of the original struct with atomic members. |
dan sinclair | 12fa303 | 2023-04-19 23:52:33 +0000 | [diff] [blame] | 35 | class SpirvAtomic final : public utils::Castable<SpirvAtomic, Transform> { |
Antonio Maiorano | 268d7b8 | 2022-06-24 22:28:23 +0000 | [diff] [blame] | 36 | public: |
| 37 | /// Constructor |
| 38 | SpirvAtomic(); |
| 39 | /// Destructor |
| 40 | ~SpirvAtomic() override; |
| 41 | |
| 42 | /// Stub is an attribute applied to stub SPIR-V reader generated functions that need to be |
| 43 | /// translated to an atomic builtin. |
James Price | 2b7406a | 2023-05-12 01:43:50 +0000 | [diff] [blame] | 44 | class Stub final : public utils::Castable<Stub, InternalAttribute> { |
Antonio Maiorano | 268d7b8 | 2022-06-24 22:28:23 +0000 | [diff] [blame] | 45 | public: |
Ben Clayton | 4a92a3c | 2022-07-18 20:50:02 +0000 | [diff] [blame] | 46 | /// @param pid the identifier of the program that owns this node |
| 47 | /// @param nid the unique node identifier |
Antonio Maiorano | 268d7b8 | 2022-06-24 22:28:23 +0000 | [diff] [blame] | 48 | /// @param builtin the atomic builtin this stub represents |
James Price | 2b7406a | 2023-05-12 01:43:50 +0000 | [diff] [blame] | 49 | Stub(ProgramID pid, NodeID nid, builtin::Function builtin); |
Antonio Maiorano | 268d7b8 | 2022-06-24 22:28:23 +0000 | [diff] [blame] | 50 | /// Destructor |
| 51 | ~Stub() override; |
| 52 | |
| 53 | /// @return a short description of the internal attribute which will be |
| 54 | /// displayed as `@internal(<name>)` |
| 55 | std::string InternalName() const override; |
| 56 | |
| 57 | /// Performs a deep clone of this object using the CloneContext `ctx`. |
| 58 | /// @param ctx the clone context |
| 59 | /// @return the newly cloned object |
| 60 | const Stub* Clone(CloneContext* ctx) const override; |
| 61 | |
| 62 | /// The type of the intrinsic |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 63 | const builtin::Function builtin; |
Antonio Maiorano | 268d7b8 | 2022-06-24 22:28:23 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
Ben Clayton | c6b3814 | 2022-11-03 08:41:19 +0000 | [diff] [blame] | 66 | /// @copydoc Transform::Apply |
| 67 | ApplyResult Apply(const Program* program, |
| 68 | const DataMap& inputs, |
| 69 | DataMap& outputs) const override; |
Antonio Maiorano | 268d7b8 | 2022-06-24 22:28:23 +0000 | [diff] [blame] | 70 | |
Ben Clayton | c6b3814 | 2022-11-03 08:41:19 +0000 | [diff] [blame] | 71 | private: |
Antonio Maiorano | 268d7b8 | 2022-06-24 22:28:23 +0000 | [diff] [blame] | 72 | struct State; |
Antonio Maiorano | 268d7b8 | 2022-06-24 22:28:23 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 75 | } // namespace tint::ast::transform |
Antonio Maiorano | 268d7b8 | 2022-06-24 22:28:23 +0000 | [diff] [blame] | 76 | |
James Price | 7d2e204 | 2023-05-11 13:10:59 +0000 | [diff] [blame] | 77 | #endif // SRC_TINT_AST_TRANSFORM_SPIRV_ATOMIC_H_ |