1 // Copyright 2021 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 15 #ifndef SRC_TRANSFORM_FOLD_TRIVIAL_SINGLE_USE_LETS_H_ 16 #define SRC_TRANSFORM_FOLD_TRIVIAL_SINGLE_USE_LETS_H_ 17 18 #include <string> 19 #include <unordered_map> 20 21 #include "src/transform/transform.h" 22 23 namespace tint { 24 namespace transform { 25 26 /// FoldTrivialSingleUseLets is an optimizer for folding away trivial `let` 27 /// statements into their single place of use. This transform is intended to 28 /// clean up the SSA `let`s produced by the SPIR-V reader. 29 /// `let`s can only be folded if: 30 /// * There is a single usage of the `let` value. 31 /// * The `let` is constructed with a ScalarConstructorExpression, or with an 32 /// IdentifierExpression. 33 /// * There are only other foldable `let`s between the `let` declaration and its 34 /// single usage. 35 /// These rules prevent any hoisting of the let that may affect execution 36 /// behaviour. 37 class FoldTrivialSingleUseLets 38 : public Castable<FoldTrivialSingleUseLets, Transform> { 39 public: 40 /// Constructor 41 FoldTrivialSingleUseLets(); 42 43 /// Destructor 44 ~FoldTrivialSingleUseLets() override; 45 46 protected: 47 /// Runs the transform using the CloneContext built for transforming a 48 /// program. Run() is responsible for calling Clone() on the CloneContext. 49 /// @param ctx the CloneContext primed with the input program and 50 /// ProgramBuilder 51 /// @param inputs optional extra transform-specific input data 52 /// @param outputs optional extra transform-specific output data 53 void Run(CloneContext& ctx, const DataMap& inputs, DataMap& outputs) override; 54 }; 55 56 } // namespace transform 57 } // namespace tint 58 59 #endif // SRC_TRANSFORM_FOLD_TRIVIAL_SINGLE_USE_LETS_H_ 60