cfg.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (c) 2015-2016 The Khronos Group 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. #include <cstdio>
  15. #include <cstring>
  16. #include <sstream>
  17. #include <string>
  18. #include <vector>
  19. #include "spirv-tools/libspirv.h"
  20. #include "tools/cfg/bin_to_dot.h"
  21. #include "tools/io.h"
  22. #include "tools/util/flags.h"
  23. static const auto kDefaultEnvironment = SPV_ENV_UNIVERSAL_1_6;
  24. static const std::string kHelpText =
  25. R"(%s - Show the control flow graph in GraphiViz "dot" form. EXPERIMENTAL
  26. Usage: %s [options] [<filename>]
  27. The SPIR-V binary is read from <filename>. If no file is specified,
  28. or if the filename is "-", then the binary is read from standard input.
  29. Options:
  30. -h, --help Print this help.
  31. --version Display version information.
  32. -o <filename> Set the output filename.
  33. Output goes to standard output if this option is
  34. not specified, or if the filename is "-".
  35. )";
  36. // clang-format off
  37. FLAG_SHORT_bool( h, /* default_value= */ false, /* required= */ false);
  38. FLAG_LONG_bool( help, /* default_value= */ false, /* required= */false);
  39. FLAG_LONG_bool( version, /* default_value= */ false, /* required= */ false);
  40. FLAG_SHORT_string(o, /* default_value= */ "", /* required= */ false);
  41. // clang-format on
  42. int main(int, const char** argv) {
  43. if (!flags::Parse(argv)) {
  44. return 1;
  45. }
  46. if (flags::h.value() || flags::help.value()) {
  47. printf(kHelpText.c_str(), argv[0], argv[0]);
  48. return 0;
  49. }
  50. if (flags::version.value()) {
  51. printf("%s EXPERIMENTAL\n", spvSoftwareVersionDetailsString());
  52. printf("Target: %s\n", spvTargetEnvDescription(kDefaultEnvironment));
  53. return 0;
  54. }
  55. if (flags::positional_arguments.size() != 1) {
  56. fprintf(stderr, "error: exactly one input file must be specified.\n");
  57. return 1;
  58. }
  59. std::string inFile = flags::positional_arguments[0];
  60. std::string outFile = flags::o.value();
  61. // Read the input binary.
  62. std::vector<uint32_t> contents;
  63. if (!ReadBinaryFile(inFile.c_str(), &contents)) return 1;
  64. spv_context context = spvContextCreate(kDefaultEnvironment);
  65. spv_diagnostic diagnostic = nullptr;
  66. std::stringstream ss;
  67. auto error =
  68. BinaryToDot(context, contents.data(), contents.size(), &ss, &diagnostic);
  69. if (error) {
  70. spvDiagnosticPrint(diagnostic);
  71. spvDiagnosticDestroy(diagnostic);
  72. spvContextDestroy(context);
  73. return error;
  74. }
  75. std::string str = ss.str();
  76. WriteFile(outFile.empty() ? nullptr : outFile.c_str(), "w", str.data(),
  77. str.size());
  78. spvDiagnosticDestroy(diagnostic);
  79. spvContextDestroy(context);
  80. return 0;
  81. }