diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index b9e0f43dc..fbf2a0dcd 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -275,7 +275,6 @@ set(SPIRV_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/print.cpp ${CMAKE_CURRENT_SOURCE_DIR}/software_version.cpp ${CMAKE_CURRENT_SOURCE_DIR}/spirv_endian.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/spirv_stats.cpp ${CMAKE_CURRENT_SOURCE_DIR}/spirv_target_env.cpp ${CMAKE_CURRENT_SOURCE_DIR}/spirv_validator_options.cpp ${CMAKE_CURRENT_SOURCE_DIR}/table.cpp diff --git a/test/stats/CMakeLists.txt b/test/stats/CMakeLists.txt index 20f05fd0f..3e4a0742f 100644 --- a/test/stats/CMakeLists.txt +++ b/test/stats/CMakeLists.txt @@ -19,6 +19,7 @@ set(VAL_TEST_COMMON_SRCS add_spvtools_unittest(TARGET stats_aggregate SRCS stats_aggregate_test.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/../../tools/stats/spirv_stats.cpp ${VAL_TEST_COMMON_SRCS} LIBS ${SPIRV_TOOLS} ) diff --git a/test/stats/stats_aggregate_test.cpp b/test/stats/stats_aggregate_test.cpp index c94de2809..64b6dccba 100644 --- a/test/stats/stats_aggregate_test.cpp +++ b/test/stats/stats_aggregate_test.cpp @@ -17,11 +17,12 @@ #include #include -#include "source/spirv_stats.h" #include "test/test_fixture.h" #include "test/unit_spirv.h" +#include "tools/stats/spirv_stats.h" namespace spvtools { +namespace stats { namespace { using spvtest::ScopedContext; @@ -482,4 +483,5 @@ OpMemoryModel Logical GLSL450 } } // namespace +} // namespace stats } // namespace spvtools diff --git a/test/stats/stats_analyzer_test.cpp b/test/stats/stats_analyzer_test.cpp index 53a414c52..3764c5bdd 100644 --- a/test/stats/stats_analyzer_test.cpp +++ b/test/stats/stats_analyzer_test.cpp @@ -22,6 +22,7 @@ #include "tools/stats/stats_analyzer.h" namespace spvtools { +namespace stats { namespace { // Fills |stats| with some synthetic header stats, as if aggregated from 100 @@ -169,4 +170,5 @@ TEST(StatsAnalyzer, OpcodeMarkov) { } } // namespace +} // namespace stats } // namespace spvtools diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 9904572b3..67143d879 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -45,7 +45,10 @@ if (NOT ${SPIRV_SKIP_EXECUTABLES}) add_spvtools_tool(TARGET spirv-link SRCS link/linker.cpp LIBS SPIRV-Tools-link ${SPIRV_TOOLS}) add_spvtools_tool(TARGET spirv-stats SRCS stats/stats.cpp - stats/stats_analyzer.cpp + stats/stats_analyzer.cpp + stats/stats_analyzer.h + stats/spirv_stats.cpp + stats/spirv_stats.h LIBS ${SPIRV_TOOLS}) add_spvtools_tool(TARGET spirv-cfg SRCS cfg/cfg.cpp diff --git a/source/spirv_stats.cpp b/tools/stats/spirv_stats.cpp similarity index 99% rename from source/spirv_stats.cpp rename to tools/stats/spirv_stats.cpp index bf4107a14..081b54817 100644 --- a/source/spirv_stats.cpp +++ b/tools/stats/spirv_stats.cpp @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "source/spirv_stats.h" +#include "tools/stats/spirv_stats.h" #include @@ -33,6 +33,7 @@ #include "spirv-tools/libspirv.h" namespace spvtools { +namespace stats { namespace { // Helper class for stats aggregation. Receives as in/out parameter. @@ -259,4 +260,5 @@ spv_result_t AggregateStats(const spv_context_t& context, const uint32_t* words, return SPV_SUCCESS; } +} // namespace stats } // namespace spvtools diff --git a/source/spirv_stats.h b/tools/stats/spirv_stats.h similarity index 97% rename from source/spirv_stats.h rename to tools/stats/spirv_stats.h index 348bba161..de564bf34 100644 --- a/source/spirv_stats.h +++ b/tools/stats/spirv_stats.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef SOURCE_SPIRV_STATS_H_ -#define SOURCE_SPIRV_STATS_H_ +#ifndef TOOLS_STATS_SPIRV_STATS_H_ +#define TOOLS_STATS_SPIRV_STATS_H_ #include #include @@ -24,6 +24,7 @@ #include "spirv-tools/libspirv.hpp" namespace spvtools { +namespace stats { struct SpirvStats { // Version histogram, version_word -> count. @@ -126,6 +127,7 @@ spv_result_t AggregateStats(const spv_context_t& context, const uint32_t* words, const size_t num_words, spv_diagnostic* pDiagnostic, SpirvStats* stats); +} // namespace stats } // namespace spvtools -#endif // SOURCE_SPIRV_STATS_H_ +#endif // TOOLS_STATS_SPIRV_STATS_H_ diff --git a/tools/stats/stats.cpp b/tools/stats/stats.cpp index c00a42d72..256ec1e1b 100644 --- a/tools/stats/stats.cpp +++ b/tools/stats/stats.cpp @@ -19,14 +19,12 @@ #include #include -#include "source/spirv_stats.h" #include "source/table.h" #include "spirv-tools/libspirv.h" #include "tools/io.h" +#include "tools/stats/spirv_stats.h" #include "tools/stats/stats_analyzer.h" -using spvtools::SpirvStats; - namespace { struct ScopedContext { @@ -83,7 +81,6 @@ int main(int argc, char** argv) { bool expect_output_path = false; bool verbose = false; - bool export_text = true; std::vector paths; const char* output_path = nullptr; @@ -126,7 +123,7 @@ int main(int argc, char** argv) { ScopedContext ctx(SPV_ENV_UNIVERSAL_1_1); spvtools::SetContextMessageConsumer(ctx.context, DiagnosticsMessageHandler); - spvtools::SpirvStats stats; + spvtools::stats::SpirvStats stats; stats.opcode_markov_hist.resize(1); for (size_t index = 0; index < paths.size(); ++index) { @@ -140,15 +137,15 @@ int main(int argc, char** argv) { std::vector contents; if (!ReadFile(path, "rb", &contents)) return 1; - if (SPV_SUCCESS != spvtools::AggregateStats(*ctx.context, contents.data(), - contents.size(), nullptr, - &stats)) { + if (SPV_SUCCESS != + spvtools::stats::AggregateStats(*ctx.context, contents.data(), + contents.size(), nullptr, &stats)) { std::cerr << "error: Failed to aggregate stats for " << path << std::endl; return 1; } } - StatsAnalyzer analyzer(stats); + spvtools::stats::StatsAnalyzer analyzer(stats); std::ofstream fout; if (output_path) { @@ -160,27 +157,24 @@ int main(int argc, char** argv) { } std::ostream& out = fout.is_open() ? fout : std::cout; + out << std::endl; + analyzer.WriteVersion(out); + analyzer.WriteGenerator(out); - if (export_text) { - out << std::endl; - analyzer.WriteVersion(out); - analyzer.WriteGenerator(out); + out << std::endl; + analyzer.WriteCapability(out); - out << std::endl; - analyzer.WriteCapability(out); + out << std::endl; + analyzer.WriteExtension(out); - out << std::endl; - analyzer.WriteExtension(out); + out << std::endl; + analyzer.WriteOpcode(out); - out << std::endl; - analyzer.WriteOpcode(out); + out << std::endl; + analyzer.WriteOpcodeMarkov(out); - out << std::endl; - analyzer.WriteOpcodeMarkov(out); - - out << std::endl; - analyzer.WriteConstantLiterals(out); - } + out << std::endl; + analyzer.WriteConstantLiterals(out); return 0; } diff --git a/tools/stats/stats_analyzer.cpp b/tools/stats/stats_analyzer.cpp index 2f573574b..6d4cabbf6 100644 --- a/tools/stats/stats_analyzer.cpp +++ b/tools/stats/stats_analyzer.cpp @@ -33,14 +33,14 @@ #include "source/operand.h" #include "source/spirv_constant.h" +namespace spvtools { +namespace stats { namespace { -using spvtools::SpirvStats; - // Signals that the value is not in the coding scheme and a fallback method // needs to be used. const uint64_t kMarkvNoneOfTheAbove = - spvtools::comp::MarkvModel::GetMarkvNoneOfTheAbove(); + comp::MarkvModel::GetMarkvNoneOfTheAbove(); std::string GetVersionString(uint32_t word) { std::stringstream ss; @@ -58,7 +58,7 @@ std::string GetOpcodeString(uint32_t word) { } std::string GetCapabilityString(uint32_t word) { - return spvtools::CapabilityToString(static_cast(word)); + return CapabilityToString(static_cast(word)); } template @@ -230,3 +230,6 @@ void StatsAnalyzer::WriteOpcodeMarkov(std::ostream& out) { } } } + +} // namespace stats +} // namespace spvtools diff --git a/tools/stats/stats_analyzer.h b/tools/stats/stats_analyzer.h index 00426d850..f1c37bfaa 100644 --- a/tools/stats/stats_analyzer.h +++ b/tools/stats/stats_analyzer.h @@ -18,11 +18,14 @@ #include #include -#include "source/spirv_stats.h" +#include "tools/stats/spirv_stats.h" + +namespace spvtools { +namespace stats { class StatsAnalyzer { public: - explicit StatsAnalyzer(const spvtools::SpirvStats& stats); + explicit StatsAnalyzer(const SpirvStats& stats); // Writes respective histograms to |out|. void WriteVersion(std::ostream& out); @@ -38,7 +41,7 @@ class StatsAnalyzer { void WriteOpcodeMarkov(std::ostream& out); private: - const spvtools::SpirvStats& stats_; + const SpirvStats& stats_; uint32_t num_modules_; @@ -49,4 +52,7 @@ class StatsAnalyzer { std::unordered_map opcode_freq_; }; +} // namespace stats +} // namespace spvtools + #endif // TOOLS_STATS_STATS_ANALYZER_H_