// Copyright (c) 2016 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "source/opt/set_spec_constant_default_value_pass.h" #include <algorithm> #include <cctype> #include <cstring> #include <tuple> #include <vector> #include "source/opt/def_use_manager.h" #include "source/opt/types.h" #include "source/util/make_unique.h" #include "source/util/parse_number.h" #include "spirv-tools/libspirv.h" namespace spvtools { namespace opt { namespace { EncodeNumberStatus; NumberType; ParseAndEncodeNumber; ParseNumber; // Given a numeric value in a null-terminated c string and the expected type of // the value, parses the string and encodes it in a vector of words. If the // value is a scalar integer or floating point value, encodes the value in // SPIR-V encoding format. If the value is 'false' or 'true', returns a vector // with single word with value 0 or 1 respectively. Returns the vector // containing the encoded value on success. Otherwise returns an empty vector. std::vector<uint32_t> ParseDefaultValueStr(const char* text, const analysis::Type* type) { … } // Given a bit pattern and a type, checks if the bit pattern is compatible // with the type. If so, returns the bit pattern, otherwise returns an empty // bit pattern. If the given bit pattern is empty, returns an empty bit // pattern. If the given type represents a SPIR-V Boolean type, the bit pattern // to be returned is determined with the following standard: // If any words in the input bit pattern are non zero, returns a bit pattern // with 0x1, which represents a 'true'. // If all words in the bit pattern are zero, returns a bit pattern with 0x0, // which represents a 'false'. // For integer and floating point types narrower than 32 bits, the upper bits // in the input bit pattern are ignored. Instead the upper bits are set // according to SPIR-V literal requirements: sign extend a signed integer, and // otherwise set the upper bits to zero. std::vector<uint32_t> ParseDefaultValueBitPattern( const std::vector<uint32_t>& input_bit_pattern, const analysis::Type* type) { … } // Returns true if the given instruction's result id could have a SpecId // decoration. bool CanHaveSpecIdDecoration(const Instruction& inst) { … } // Given a decoration group defining instruction that is decorated with SpecId // decoration, finds the spec constant defining instruction which is the real // target of the SpecId decoration. Returns the spec constant defining // instruction if such an instruction is found, otherwise returns a nullptr. Instruction* GetSpecIdTargetFromDecorationGroup( const Instruction& decoration_group_defining_inst, analysis::DefUseManager* def_use_mgr) { … } } // namespace Pass::Status SetSpecConstantDefaultValuePass::Process() { … } // Returns true if the given char is ':', '\0' or considered as blank space // (i.e.: '\n', '\r', '\v', '\t', '\f' and ' '). bool IsSeparator(char ch) { … } std::unique_ptr<SetSpecConstantDefaultValuePass::SpecIdToValueStrMap> SetSpecConstantDefaultValuePass::ParseDefaultValuesString(const char* str) { … } } // namespace opt } // namespace spvtools