• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
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 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_COMPILER_MLIR_TENSORFLOW_UTILS_DUMP_MLIR_UTIL_H_
17 #define TENSORFLOW_COMPILER_MLIR_TENSORFLOW_UTILS_DUMP_MLIR_UTIL_H_
18 
19 #include <string>
20 
21 #include "llvm/ADT/StringRef.h"
22 #include "mlir/IR/Operation.h"  // from @llvm-project
23 #include "mlir/Pass/PassManager.h"  // from @llvm-project
24 #include "tensorflow/core/platform/status.h"
25 
26 namespace tensorflow {
27 
28 extern const char kCrashReproducerStdErr[];
29 extern const char kCrashReproducerCrashAnalysis[];
30 
31 // Creates a file to use for dumping and returns success if a file could be
32 // created. The opened file is placed in 'os' and the path of the file used is
33 // placed in 'filepath'.
34 //
35 // If the TF_DUMP_GRAPH_PREFIX environment variable is kCrashReproducerStdErr,
36 // then the LOG(INFO) macro is used instead.
37 //
38 // This will create a file name via prefixing `name` with the value of the
39 // TF_DUMP_GRAPH_PREFIX environment variable if `dirname` is empty and
40 // suffixing `name` with ".mlir".
41 Status CreateFileForDumping(llvm::StringRef name,
42                             std::unique_ptr<llvm::raw_ostream>* os,
43                             std::string* filepath,
44                             llvm::StringRef dirname = "");
45 
46 // Dumps MLIR operation to a file and returns the file name used.
47 //
48 // If the TF_DUMP_GRAPH_PREFIX environment variable is kCrashReproducerStdErr,
49 // then the MLIR operation will be logged (using the LOG(INFO) macro) instead.
50 //
51 // This will create a file name via prefixing `name` with the value of the
52 // TF_DUMP_GRAPH_PREFIX environment variable if `dirname` is empty and
53 // suffixing `name` with ".mlir".
54 std::string DumpMlirOpToFile(llvm::StringRef name, mlir::Operation* op,
55                              llvm::StringRef dirname = "");
56 
57 // Reads the directory to dump the MLIR module from environment variables.
58 // Default is reading from TF_DUMP_GRAPH_PREFIX, and if the string is 'sponge'
59 // read from TEST_UNDECLARED_OUTPUTS_DIR. Returns nullptr if the directory
60 // cannot be determined and generates a warning message.
61 std::string GetDumpDirFromEnvVar();
62 
63 // Dumps a raw string to a file and returns the file name used.
64 //
65 // This will create a file name via prefixing `name` with the value of the
66 // TF_DUMP_GRAPH_PREFIX environment variable if `dirname` is empty and
67 // suffixing `name` with ".mlir".
68 std::string DumpRawStringToFile(llvm::StringRef name, llvm::StringRef content,
69                                 llvm::StringRef dirname = "");
70 
71 // Enable the crash reproducer on the provided PassManager to the provided
72 // directory path.
73 // If the provided path is empty, it is retrieved from the
74 // environment variable `MLIR_CRASH_REPRODUCER_DIRECTORY`.
75 // If the provided path is the string "sponge", the file will be included
76 // in the sponge "Output Files" by looking up the environment to infer
77 // the directory path.
78 // If the provided path is the string kCrashReproducerStdErr, the data is
79 // dumped into the stderr.
80 // If the provided path is the string kCrashReproducerCrashAnalysis, the data
81 // is dumped to the crash analysis system. Note, environment var
82 // `MLIR_CRASH_REPRODUCER_DIRECTORY` can be used to override
83 // kCrashReproducerCrashAnalysis settings.
84 void SetCrashReproducer(
85     mlir::PassManager& pm,
86     llvm::StringRef dir_path = kCrashReproducerCrashAnalysis);
87 
88 // This applies both the PassManagerCLOptions provided by MLIR along with any
89 // tensorflow specific options.
90 //
91 // Note that this function should be in a more appropriate file, but it is
92 // unclear what a proper file would be as no other functions would currently be
93 // in the file also.
94 void applyTensorflowAndCLOptions(
95     mlir::PassManager& pm,
96     llvm::StringRef dir_path = kCrashReproducerCrashAnalysis);
97 
98 }  // namespace tensorflow
99 
100 #endif  // TENSORFLOW_COMPILER_MLIR_TENSORFLOW_UTILS_DUMP_MLIR_UTIL_H_
101