blob: 4ada48507c8d1773f60e2c879cafdc1c9f0a632c [file] [log] [blame]
Antonio Maiorano268d7b82022-06-24 22:28:23 +00001// 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 Price7d2e2042023-05-11 13:10:59 +000015#ifndef SRC_TINT_AST_TRANSFORM_SPIRV_ATOMIC_H_
16#define SRC_TINT_AST_TRANSFORM_SPIRV_ATOMIC_H_
Antonio Maiorano268d7b82022-06-24 22:28:23 +000017
18#include <string>
19
20#include "src/tint/ast/internal_attribute.h"
James Price7d2e2042023-05-11 13:10:59 +000021#include "src/tint/ast/transform/transform.h"
dan sinclair9543f742023-03-09 01:20:16 +000022#include "src/tint/builtin/function.h"
Antonio Maiorano268d7b82022-06-24 22:28:23 +000023
24// Forward declarations
25namespace tint {
26class CloneContext;
27} // namespace tint
28
James Priceb4acbb82023-05-11 21:27:16 +000029namespace tint::ast::transform {
Antonio Maiorano268d7b82022-06-24 22:28:23 +000030
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 sinclair12fa3032023-04-19 23:52:33 +000035class SpirvAtomic final : public utils::Castable<SpirvAtomic, Transform> {
Antonio Maiorano268d7b82022-06-24 22:28:23 +000036 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 Price2b7406a2023-05-12 01:43:50 +000044 class Stub final : public utils::Castable<Stub, InternalAttribute> {
Antonio Maiorano268d7b82022-06-24 22:28:23 +000045 public:
Ben Clayton4a92a3c2022-07-18 20:50:02 +000046 /// @param pid the identifier of the program that owns this node
47 /// @param nid the unique node identifier
Antonio Maiorano268d7b82022-06-24 22:28:23 +000048 /// @param builtin the atomic builtin this stub represents
James Price2b7406a2023-05-12 01:43:50 +000049 Stub(ProgramID pid, NodeID nid, builtin::Function builtin);
Antonio Maiorano268d7b82022-06-24 22:28:23 +000050 /// 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 sinclair9543f742023-03-09 01:20:16 +000063 const builtin::Function builtin;
Antonio Maiorano268d7b82022-06-24 22:28:23 +000064 };
65
Ben Claytonc6b38142022-11-03 08:41:19 +000066 /// @copydoc Transform::Apply
67 ApplyResult Apply(const Program* program,
68 const DataMap& inputs,
69 DataMap& outputs) const override;
Antonio Maiorano268d7b82022-06-24 22:28:23 +000070
Ben Claytonc6b38142022-11-03 08:41:19 +000071 private:
Antonio Maiorano268d7b82022-06-24 22:28:23 +000072 struct State;
Antonio Maiorano268d7b82022-06-24 22:28:23 +000073};
74
James Priceb4acbb82023-05-11 21:27:16 +000075} // namespace tint::ast::transform
Antonio Maiorano268d7b82022-06-24 22:28:23 +000076
James Price7d2e2042023-05-11 13:10:59 +000077#endif // SRC_TINT_AST_TRANSFORM_SPIRV_ATOMIC_H_