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 // Creates a file to use for dumping and returns success if a file could be 29 // created. The opened file is placed in 'os' and the path of the file used is 30 // placed in 'filepath'. 31 // 32 // If the TF_DUMP_GRAPH_PREFIX environment variable is "-", then the LOG(INFO) 33 // macro is used instead. 34 // 35 // This will create a file name via prefixing `name` with the value of the 36 // TF_DUMP_GRAPH_PREFIX environment variable if `dirname` is empty and 37 // suffixing `name` with ".mlir". 38 Status CreateFileForDumping(llvm::StringRef name, 39 std::unique_ptr<llvm::raw_ostream>* os, 40 std::string* filepath, 41 llvm::StringRef dirname = ""); 42 43 // Dumps MLIR operation to a file and returns the file name used. 44 // 45 // If the TF_DUMP_GRAPH_PREFIX environment variable is "-", then the MLIR 46 // operation will be logged (using the LOG(INFO) macro) instead. 47 // 48 // This will create a file name via prefixing `name` with the value of the 49 // TF_DUMP_GRAPH_PREFIX environment variable if `dirname` is empty and 50 // suffixing `name` with ".mlir". 51 std::string DumpMlirOpToFile(llvm::StringRef name, mlir::Operation* op, 52 llvm::StringRef dirname = ""); 53 54 // Reads the directory to dump the MLIR module from environment variables. 55 // Default is reading from TF_DUMP_GRAPH_PREFIX, and if the string is 'sponge' 56 // read from TEST_UNDECLARED_OUTPUTS_DIR. Returns nullptr if the directory 57 // cannot be determined and generates a warning message. 58 std::string GetDumpDirFromEnvVar(); 59 60 // Dumps a raw string to a file and returns the file name used. 61 // 62 // This will create a file name via prefixing `name` with the value of the 63 // TF_DUMP_GRAPH_PREFIX environment variable if `dirname` is empty and 64 // suffixing `name` with ".mlir". 65 std::string DumpRawStringToFile(llvm::StringRef name, llvm::StringRef content, 66 llvm::StringRef dirname = ""); 67 68 // Enable the crash reproducer on the provided PassManager to the provided 69 // directory path. If the provided path is empty, it is retrieved from the 70 // environment variable `MLIR_CRASH_REPRODUCER_DIRECTORY`. If the provided path 71 // is the string "sponge", the file will be included in the sponge "Output 72 // Files" by looking up the environment to infer the directory path. 73 void SetCrashReproducer(mlir::PassManager& pm, llvm::StringRef dir_path = ""); 74 75 // This applies both the PassManagerCLOptions provided by MLIR along with any 76 // tensorflow specific options. 77 // 78 // Note that this function should be in a more appropriate file, but it is 79 // unclear what a proper file would be as no other functions would currently be 80 // in the file also. 81 void applyTensorflowAndCLOptions(mlir::PassManager& pm, 82 llvm::StringRef dir_path = ""); 83 84 } // namespace tensorflow 85 86 #endif // TENSORFLOW_COMPILER_MLIR_TENSORFLOW_UTILS_DUMP_MLIR_UTIL_H_ 87