1 /* Copyright 2017 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 #include "tensorflow/compiler/xla/service/gpu/llvm_gpu_backend/utils.h" 17 18 #include "tensorflow/core/platform/logging.h" 19 20 #include "llvm/IR/LLVMContext.h" 21 #include "llvm/IR/Module.h" 22 #include "llvm/IRReader/IRReader.h" 23 #include "llvm/Support/SourceMgr.h" 24 #include "tensorflow/compiler/xla/types.h" 25 #include "tensorflow/core/lib/core/stringpiece.h" 26 #include "tensorflow/core/lib/strings/strcat.h" 27 28 namespace { 29 30 static void DieWithSMDiagnosticError(llvm::SMDiagnostic* diagnostic) { 31 LOG(FATAL) << diagnostic->getFilename().str() << ":" 32 << diagnostic->getLineNo() << ":" << diagnostic->getColumnNo() 33 << ": " << diagnostic->getMessage().str(); 34 } 35 36 } // namespace 37 38 namespace xla { 39 namespace gpu { 40 41 std::unique_ptr<llvm::Module> LoadIRModule(const string& filename, 42 llvm::LLVMContext* llvm_context) { 43 llvm::SMDiagnostic diagnostic_err; 44 std::unique_ptr<llvm::Module> module( 45 llvm::parseIRFile(llvm::StringRef(filename.data(), filename.size()), 46 diagnostic_err, *llvm_context)); 47 48 if (module == nullptr) { 49 DieWithSMDiagnosticError(&diagnostic_err); 50 } 51 52 return module; 53 } 54 55 string ReplaceFilenameExtension(tensorflow::StringPiece filename, 56 tensorflow::StringPiece new_extension) { 57 auto pos = filename.rfind('.'); 58 tensorflow::StringPiece stem = 59 pos == tensorflow::StringPiece::npos 60 ? filename 61 : tensorflow::StringPiece(filename.data(), pos); 62 return tensorflow::strings::StrCat(stem, ".", new_extension); 63 } 64 65 } // namespace gpu 66 } // namespace xla 67