/src/spirv-tools/source/opt/nonwritable_propagation_pass.h
Line | Count | Source |
1 | | // Copyright (c) 2026 The Khronos Group Inc. |
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 | | |
15 | | #ifndef LIBSPIRV_OPT_NONWRITABLE_PROPAGATION_PASS_H_ |
16 | | #define LIBSPIRV_OPT_NONWRITABLE_PROPAGATION_PASS_H_ |
17 | | |
18 | | #include "source/opt/pass.h" |
19 | | |
20 | | namespace spvtools { |
21 | | namespace opt { |
22 | | |
23 | | // Propagates NonWritable from a buffer's members to the buffer variable. |
24 | | // |
25 | | // Some producers decorate every member of a buffer's struct NonWritable |
26 | | // instead of decorating the variable itself. That is legal, but consumers |
27 | | // that read the decoration off the variable -- for example the mapping APIs |
28 | | // in VK_EXT_descriptor_heap -- do not see it, so the buffer looks writable. |
29 | | // |
30 | | // For each buffer variable (OpVariable or OpUntypedVariableKHR) whose |
31 | | // struct has every member decorated NonWritable, this pass decorates the |
32 | | // variable NonWritable and then removes the now-redundant member |
33 | | // decorations. Every direct member must carry the decoration, whatever its |
34 | | // type: a struct-typed member without it is writable as far as SPIR-V is |
35 | | // concerned, so the variable is left alone. |
36 | | class NonWritablePropagationPass : public Pass { |
37 | | public: |
38 | 0 | const char* name() const override { return "nonwritable-propagation"; } |
39 | | |
40 | | Status Process() override; |
41 | | |
42 | 0 | IRContext::Analysis GetPreservedAnalyses() override { |
43 | | // Decorations are the only thing added or removed, and the removal goes |
44 | | // through the context, so every analysis except the decoration cache |
45 | | // survives untouched. |
46 | 0 | return IRContext::kAnalysisDefUse | |
47 | 0 | IRContext::kAnalysisInstrToBlockMapping | |
48 | 0 | IRContext::kAnalysisCombinators | IRContext::kAnalysisCFG | |
49 | 0 | IRContext::kAnalysisDominatorAnalysis | IRContext::kAnalysisNameMap | |
50 | 0 | IRContext::kAnalysisConstants | IRContext::kAnalysisTypes; |
51 | 0 | } |
52 | | |
53 | | private: |
54 | | // Returns the OpTypeStruct backing |var|'s buffer, or nullptr if |var| is |
55 | | // not a variable that may carry NonWritable itself. |
56 | | Instruction* GetBufferStructType(const Instruction& var) const; |
57 | | |
58 | | // Returns true if every member of |struct_type| is decorated NonWritable. |
59 | | bool AllMembersNonWritable(const Instruction& struct_type) const; |
60 | | }; |
61 | | |
62 | | } // namespace opt |
63 | | } // namespace spvtools |
64 | | |
65 | | #endif // LIBSPIRV_OPT_NONWRITABLE_PROPAGATION_PASS_H_ |