cfg.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // Prints a program usage message to stdout.
  23. static void print_usage(const char* argv0) {
  24. printf(
  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. argv0, argv0);
  37. }
  38. static const auto kDefaultEnvironment = SPV_ENV_UNIVERSAL_1_2;
  39. int main(int argc, char** argv) {
  40. const char* inFile = nullptr;
  41. const char* outFile = nullptr; // Stays nullptr if printing to stdout.
  42. for (int argi = 1; argi < argc; ++argi) {
  43. if ('-' == argv[argi][0]) {
  44. switch (argv[argi][1]) {
  45. case 'h':
  46. print_usage(argv[0]);
  47. return 0;
  48. case 'o': {
  49. if (!outFile && argi + 1 < argc) {
  50. outFile = argv[++argi];
  51. } else {
  52. print_usage(argv[0]);
  53. return 1;
  54. }
  55. } break;
  56. case '-': {
  57. // Long options
  58. if (0 == strcmp(argv[argi], "--help")) {
  59. print_usage(argv[0]);
  60. return 0;
  61. } else if (0 == strcmp(argv[argi], "--version")) {
  62. printf("%s EXPERIMENTAL\n", spvSoftwareVersionDetailsString());
  63. printf("Target: %s\n",
  64. spvTargetEnvDescription(kDefaultEnvironment));
  65. return 0;
  66. } else {
  67. print_usage(argv[0]);
  68. return 1;
  69. }
  70. } break;
  71. case 0: {
  72. // Setting a filename of "-" to indicate stdin.
  73. if (!inFile) {
  74. inFile = argv[argi];
  75. } else {
  76. fprintf(stderr, "error: More than one input file specified\n");
  77. return 1;
  78. }
  79. } break;
  80. default:
  81. print_usage(argv[0]);
  82. return 1;
  83. }
  84. } else {
  85. if (!inFile) {
  86. inFile = argv[argi];
  87. } else {
  88. fprintf(stderr, "error: More than one input file specified\n");
  89. return 1;
  90. }
  91. }
  92. }
  93. // Read the input binary.
  94. std::vector<uint32_t> contents;
  95. if (!ReadFile<uint32_t>(inFile, "rb", &contents)) return 1;
  96. spv_context context = spvContextCreate(kDefaultEnvironment);
  97. spv_diagnostic diagnostic = nullptr;
  98. std::stringstream ss;
  99. auto error =
  100. BinaryToDot(context, contents.data(), contents.size(), &ss, &diagnostic);
  101. if (error) {
  102. spvDiagnosticPrint(diagnostic);
  103. spvDiagnosticDestroy(diagnostic);
  104. spvContextDestroy(context);
  105. return error;
  106. }
  107. std::string str = ss.str();
  108. WriteFile(outFile, "w", str.data(), str.size());
  109. spvDiagnosticDestroy(diagnostic);
  110. spvContextDestroy(context);
  111. return 0;
  112. }