/src/spirv-tools/source/opt/struct_packing_pass.h
Line | Count | Source |
1 | | // Copyright (c) 2024 Epic Games, 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 SOURCE_OPT_STRUCT_PACKING_PASS_ |
16 | | #define SOURCE_OPT_STRUCT_PACKING_PASS_ |
17 | | |
18 | | #include <unordered_map> |
19 | | |
20 | | #include "source/opt/ir_context.h" |
21 | | #include "source/opt/module.h" |
22 | | #include "source/opt/pass.h" |
23 | | |
24 | | namespace spvtools { |
25 | | namespace opt { |
26 | | |
27 | | // This pass re-assigns all field offsets under the specified packing rules. |
28 | | class StructPackingPass final : public Pass { |
29 | | public: |
30 | | enum class PackingRules { |
31 | | Undefined, |
32 | | Std140, |
33 | | Std140EnhancedLayout, |
34 | | Std430, |
35 | | Std430EnhancedLayout, |
36 | | HlslCbuffer, |
37 | | HlslCbufferPackOffset, |
38 | | Scalar, |
39 | | ScalarEnhancedLayout, |
40 | | }; |
41 | | |
42 | | static PackingRules ParsePackingRuleFromString(const std::string& s); |
43 | | |
44 | | StructPackingPass(const char* structToPack, PackingRules rules); |
45 | 0 | const char* name() const override { return "struct-packing"; } |
46 | | Status Process() override; |
47 | | |
48 | 0 | IRContext::Analysis GetPreservedAnalyses() override { |
49 | 0 | return IRContext::kAnalysisCombinators | IRContext::kAnalysisCFG | |
50 | 0 | IRContext::kAnalysisDominatorAnalysis | |
51 | 0 | IRContext::kAnalysisLoopAnalysis | IRContext::kAnalysisNameMap | |
52 | 0 | IRContext::kAnalysisScalarEvolution | |
53 | 0 | IRContext::kAnalysisStructuredCFG | IRContext::kAnalysisConstants | |
54 | 0 | IRContext::kAnalysisDebugInfo | IRContext::kAnalysisLiveness; |
55 | 0 | } |
56 | | |
57 | | private: |
58 | | void buildConstantsMap(); |
59 | | uint32_t findStructIdByName(const char* structName) const; |
60 | | std::vector<const analysis::Type*> findStructMemberTypes( |
61 | | const Instruction& structDef) const; |
62 | | Status assignStructMemberOffsets( |
63 | | uint32_t structIdToPack, |
64 | | const std::vector<const analysis::Type*>& structMemberTypes); |
65 | | |
66 | | uint32_t getPackedAlignment(const analysis::Type& type) const; |
67 | | uint32_t getPackedSize(const analysis::Type& type) const; |
68 | | uint32_t getPackedArrayStride(const analysis::Array& arrayType) const; |
69 | | uint32_t getArrayLength(const analysis::Array& arrayType) const; |
70 | | uint32_t getConstantInt(spv::Id id) const; |
71 | | |
72 | | private: |
73 | | std::string structToPack_; |
74 | | PackingRules packingRules_ = PackingRules::Undefined; |
75 | | std::unordered_map<spv::Id, Instruction*> constantsMap_; |
76 | | }; |
77 | | |
78 | | } // namespace opt |
79 | | } // namespace spvtools |
80 | | |
81 | | #endif // SOURCE_OPT_STRUCT_PACKING_PASS_ |