2018-08-09 21:07:19 +00:00
|
|
|
// Copyright (c) 2018 Google LLC
|
2017-10-03 21:36:37 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#ifndef SOURCE_COMP_MARKV_MODEL_H_
|
|
|
|
#define SOURCE_COMP_MARKV_MODEL_H_
|
2017-10-03 21:36:37 +00:00
|
|
|
|
|
|
|
#include <unordered_set>
|
|
|
|
|
2018-08-14 13:52:05 +00:00
|
|
|
#include "source/comp/huffman_codec.h"
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/latest_version_spirv_header.h"
|
2018-08-09 21:07:19 +00:00
|
|
|
#include "spirv-tools/libspirv.hpp"
|
2017-10-03 21:36:37 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
2018-07-06 11:32:39 +00:00
|
|
|
namespace comp {
|
2017-10-03 21:36:37 +00:00
|
|
|
|
|
|
|
// Base class for MARK-V models.
|
|
|
|
// The class contains encoding/decoding model with various constants and
|
|
|
|
// codecs used by the compression algorithm.
|
|
|
|
class MarkvModel {
|
|
|
|
public:
|
2017-11-08 17:40:02 +00:00
|
|
|
MarkvModel()
|
|
|
|
: operand_chunk_lengths_(
|
2017-11-14 22:02:42 +00:00
|
|
|
static_cast<size_t>(SPV_OPERAND_TYPE_NUM_OPERAND_TYPES), 0) {
|
|
|
|
// Set default values.
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_TYPE_ID] = 4;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_RESULT_ID] = 8;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_ID] = 8;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_SCOPE_ID] = 8;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID] = 8;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_LITERAL_INTEGER] = 6;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER] = 6;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_CAPABILITY] = 6;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_SOURCE_LANGUAGE] = 3;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_EXECUTION_MODEL] = 3;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_ADDRESSING_MODEL] = 2;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_MEMORY_MODEL] = 2;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_EXECUTION_MODE] = 6;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_STORAGE_CLASS] = 4;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_DIMENSIONALITY] = 3;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE] = 3;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE] = 2;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT] = 6;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_FP_ROUNDING_MODE] = 2;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_LINKAGE_TYPE] = 2;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_ACCESS_QUALIFIER] = 2;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER] = 2;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE] = 3;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_DECORATION] = 6;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_BUILT_IN] = 6;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_GROUP_OPERATION] = 2;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS] = 2;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO] = 2;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_FP_FAST_MATH_MODE] = 4;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_FUNCTION_CONTROL] = 4;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_LOOP_CONTROL] = 4;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_IMAGE] = 4;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_OPTIONAL_IMAGE] = 4;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS] = 4;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_SELECTION_CONTROL] = 4;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER] = 6;
|
|
|
|
operand_chunk_lengths_[SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER] = 6;
|
|
|
|
}
|
2017-10-03 21:36:37 +00:00
|
|
|
|
|
|
|
uint32_t model_type() const { return model_type_; }
|
|
|
|
uint32_t model_version() const { return model_version_; }
|
|
|
|
|
|
|
|
uint32_t opcode_chunk_length() const { return opcode_chunk_length_; }
|
2017-11-08 17:40:02 +00:00
|
|
|
uint32_t num_operands_chunk_length() const {
|
|
|
|
return num_operands_chunk_length_;
|
|
|
|
}
|
2017-10-03 21:36:37 +00:00
|
|
|
uint32_t mtf_rank_chunk_length() const { return mtf_rank_chunk_length_; }
|
|
|
|
|
|
|
|
uint32_t u64_chunk_length() const { return u64_chunk_length_; }
|
|
|
|
uint32_t s64_chunk_length() const { return s64_chunk_length_; }
|
|
|
|
uint32_t s64_block_exponent() const { return s64_block_exponent_; }
|
|
|
|
|
2017-11-14 22:02:42 +00:00
|
|
|
enum class IdFallbackStrategy {
|
|
|
|
kRuleBased = 0,
|
|
|
|
kShortDescriptor,
|
|
|
|
};
|
|
|
|
|
|
|
|
IdFallbackStrategy id_fallback_strategy() const {
|
|
|
|
return id_fallback_strategy_;
|
|
|
|
}
|
|
|
|
|
2017-10-03 21:36:37 +00:00
|
|
|
// Returns a codec for common opcode_and_num_operands words for the given
|
|
|
|
// previous opcode. May return nullptr if the codec doesn't exist.
|
2018-08-14 13:52:05 +00:00
|
|
|
const HuffmanCodec<uint64_t>* GetOpcodeAndNumOperandsMarkovHuffmanCodec(
|
|
|
|
uint32_t prev_opcode) const {
|
2017-10-03 21:36:37 +00:00
|
|
|
if (prev_opcode == SpvOpNop)
|
|
|
|
return opcode_and_num_operands_huffman_codec_.get();
|
|
|
|
|
|
|
|
const auto it =
|
|
|
|
opcode_and_num_operands_markov_huffman_codecs_.find(prev_opcode);
|
|
|
|
if (it == opcode_and_num_operands_markov_huffman_codecs_.end())
|
|
|
|
return nullptr;
|
|
|
|
return it->second.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a codec for common non-id words used for given operand slot.
|
|
|
|
// Operand slot is defined by the opcode and the operand index.
|
|
|
|
// May return nullptr if the codec doesn't exist.
|
2018-08-14 13:52:05 +00:00
|
|
|
const HuffmanCodec<uint64_t>* GetNonIdWordHuffmanCodec(
|
2017-10-03 21:36:37 +00:00
|
|
|
uint32_t opcode, uint32_t operand_index) const {
|
|
|
|
const auto it = non_id_word_huffman_codecs_.find(
|
|
|
|
std::pair<uint32_t, uint32_t>(opcode, operand_index));
|
2017-11-08 17:40:02 +00:00
|
|
|
if (it == non_id_word_huffman_codecs_.end()) return nullptr;
|
2017-10-03 21:36:37 +00:00
|
|
|
return it->second.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a codec for common id descriptos used for given operand slot.
|
|
|
|
// Operand slot is defined by the opcode and the operand index.
|
|
|
|
// May return nullptr if the codec doesn't exist.
|
2018-08-14 13:52:05 +00:00
|
|
|
const HuffmanCodec<uint64_t>* GetIdDescriptorHuffmanCodec(
|
2017-10-03 21:36:37 +00:00
|
|
|
uint32_t opcode, uint32_t operand_index) const {
|
|
|
|
const auto it = id_descriptor_huffman_codecs_.find(
|
|
|
|
std::pair<uint32_t, uint32_t>(opcode, operand_index));
|
2017-11-08 17:40:02 +00:00
|
|
|
if (it == id_descriptor_huffman_codecs_.end()) return nullptr;
|
2017-10-03 21:36:37 +00:00
|
|
|
return it->second.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a codec for common strings used by the given opcode.
|
|
|
|
// Operand slot is defined by the opcode and the operand index.
|
|
|
|
// May return nullptr if the codec doesn't exist.
|
2018-08-14 13:52:05 +00:00
|
|
|
const HuffmanCodec<std::string>* GetLiteralStringHuffmanCodec(
|
2017-10-03 21:36:37 +00:00
|
|
|
uint32_t opcode) const {
|
|
|
|
const auto it = literal_string_huffman_codecs_.find(opcode);
|
2017-11-08 17:40:02 +00:00
|
|
|
if (it == literal_string_huffman_codecs_.end()) return nullptr;
|
2017-10-03 21:36:37 +00:00
|
|
|
return it->second.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if |descriptor| has a coding scheme in any of
|
|
|
|
// id_descriptor_huffman_codecs_.
|
|
|
|
bool DescriptorHasCodingScheme(uint32_t descriptor) const {
|
|
|
|
return descriptors_with_coding_scheme_.count(descriptor);
|
|
|
|
}
|
|
|
|
|
2017-11-14 22:02:42 +00:00
|
|
|
// Checks if any descriptor has a coding scheme.
|
|
|
|
bool AnyDescriptorHasCodingScheme() const {
|
|
|
|
return !descriptors_with_coding_scheme_.empty();
|
|
|
|
}
|
|
|
|
|
2017-10-03 21:36:37 +00:00
|
|
|
// Returns chunk length used for variable length encoding of spirv operand
|
|
|
|
// words.
|
|
|
|
uint32_t GetOperandVariableWidthChunkLength(spv_operand_type_t type) const {
|
|
|
|
return operand_chunk_lengths_.at(static_cast<size_t>(type));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets model type.
|
2017-11-08 17:40:02 +00:00
|
|
|
void SetModelType(uint32_t in_model_type) { model_type_ = in_model_type; }
|
2017-10-03 21:36:37 +00:00
|
|
|
|
|
|
|
// Sets model version.
|
|
|
|
void SetModelVersion(uint32_t in_model_version) {
|
|
|
|
model_version_ = in_model_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns value used by Huffman codecs as a signal that a value is not in the
|
|
|
|
// coding table.
|
|
|
|
static uint64_t GetMarkvNoneOfTheAbove() {
|
|
|
|
// Magic number.
|
|
|
|
return 1111111111111111111;
|
|
|
|
}
|
|
|
|
|
|
|
|
MarkvModel(const MarkvModel&) = delete;
|
|
|
|
const MarkvModel& operator=(const MarkvModel&) = delete;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Huffman codec for base-rate of opcode_and_num_operands.
|
2018-08-14 13:52:05 +00:00
|
|
|
std::unique_ptr<HuffmanCodec<uint64_t>>
|
2017-10-03 21:36:37 +00:00
|
|
|
opcode_and_num_operands_huffman_codec_;
|
|
|
|
|
|
|
|
// Huffman codecs for opcode_and_num_operands. The map key is previous opcode.
|
2018-08-14 13:52:05 +00:00
|
|
|
std::map<uint32_t, std::unique_ptr<HuffmanCodec<uint64_t>>>
|
2017-10-03 21:36:37 +00:00
|
|
|
opcode_and_num_operands_markov_huffman_codecs_;
|
|
|
|
|
|
|
|
// Huffman codecs for non-id single-word operand values.
|
|
|
|
// The map key is pair <opcode, operand_index>.
|
|
|
|
std::map<std::pair<uint32_t, uint32_t>,
|
2018-08-14 13:52:05 +00:00
|
|
|
std::unique_ptr<HuffmanCodec<uint64_t>>>
|
2017-11-08 17:40:02 +00:00
|
|
|
non_id_word_huffman_codecs_;
|
2017-10-03 21:36:37 +00:00
|
|
|
|
|
|
|
// Huffman codecs for id descriptors. The map key is pair
|
|
|
|
// <opcode, operand_index>.
|
|
|
|
std::map<std::pair<uint32_t, uint32_t>,
|
2018-08-14 13:52:05 +00:00
|
|
|
std::unique_ptr<HuffmanCodec<uint64_t>>>
|
2017-11-08 17:40:02 +00:00
|
|
|
id_descriptor_huffman_codecs_;
|
2017-10-03 21:36:37 +00:00
|
|
|
|
|
|
|
// Set of all descriptors which have a coding scheme in any of
|
|
|
|
// id_descriptor_huffman_codecs_.
|
|
|
|
std::unordered_set<uint32_t> descriptors_with_coding_scheme_;
|
|
|
|
|
|
|
|
// Huffman codecs for literal strings. The map key is the opcode of the
|
|
|
|
// current instruction. This assumes, that there is no more than one literal
|
|
|
|
// string operand per instruction, but would still work even if this is not
|
|
|
|
// the case. Names and debug information strings are not collected.
|
2018-08-14 13:52:05 +00:00
|
|
|
std::map<uint32_t, std::unique_ptr<HuffmanCodec<std::string>>>
|
2017-10-03 21:36:37 +00:00
|
|
|
literal_string_huffman_codecs_;
|
|
|
|
|
|
|
|
// Chunk lengths used for variable width encoding of operands (index is
|
|
|
|
// spv_operand_type of the operand).
|
|
|
|
std::vector<uint32_t> operand_chunk_lengths_;
|
|
|
|
|
|
|
|
uint32_t opcode_chunk_length_ = 7;
|
2017-11-08 17:40:02 +00:00
|
|
|
uint32_t num_operands_chunk_length_ = 3;
|
2017-10-03 21:36:37 +00:00
|
|
|
uint32_t mtf_rank_chunk_length_ = 5;
|
|
|
|
|
|
|
|
uint32_t u64_chunk_length_ = 8;
|
|
|
|
uint32_t s64_chunk_length_ = 8;
|
|
|
|
uint32_t s64_block_exponent_ = 10;
|
|
|
|
|
2017-11-27 15:16:41 +00:00
|
|
|
IdFallbackStrategy id_fallback_strategy_ =
|
|
|
|
IdFallbackStrategy::kShortDescriptor;
|
2017-11-14 22:02:42 +00:00
|
|
|
|
2017-10-03 21:36:37 +00:00
|
|
|
uint32_t model_type_ = 0;
|
|
|
|
uint32_t model_version_ = 0;
|
|
|
|
};
|
|
|
|
|
2018-07-06 11:32:39 +00:00
|
|
|
} // namespace comp
|
2017-10-03 21:36:37 +00:00
|
|
|
} // namespace spvtools
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#endif // SOURCE_COMP_MARKV_MODEL_H_
|