• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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_XLA_SERVICE_SOURCE_MAP_UTIL_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_SOURCE_MAP_UTIL_H_
18 
19 #include "absl/strings/str_format.h"
20 #include "tensorflow/compiler/xla/service/executable.h"
21 #include "tensorflow/compiler/xla/status.h"
22 #include "tensorflow/core/platform/macros.h"
23 
24 namespace xla {
25 namespace source_map_util {
26 
27 // Creates an INVALID_ARGUMENT status with the given format string.
28 template <typename... Args>
InvalidParameterArgument(const OpMetadata & op_metadata,const absl::FormatSpec<Args...> & format,const Args &...args)29 Status InvalidParameterArgument(const OpMetadata& op_metadata,
30                                 const absl::FormatSpec<Args...>& format,
31                                 const Args&... args) {
32   string message = absl::StrFormat(format, args...);
33   if (!op_metadata.source_file().empty()) {
34     absl::StrAppendFormat(&message, " (%s:%d)", op_metadata.source_file(),
35                           op_metadata.source_line());
36   }
37   return InvalidArgument("%s", message);
38 }
39 
40 // Creates an INVALID_ARGUMENT status with the given format string.
41 //
42 // Also, attempts to extract the OpMetadata for parameter_number on executable
43 // and append it to the status message for source mapping to user code.
44 //
45 // executable may be nullptr, but parameter_number should not be out of bounds
46 // or a CHECK-failure may occur.
47 template <typename... Args>
InvalidParameterArgument(Executable * executable,int parameter_number,const absl::FormatSpec<Args...> & format,const Args &...args)48 Status InvalidParameterArgument(Executable* executable, int parameter_number,
49                                 const absl::FormatSpec<Args...>& format,
50                                 const Args&... args) {
51   if (executable != nullptr && executable->has_module()) {
52     const HloModule& module = executable->module();
53     const HloComputation& computation = *module.entry_computation();
54     HloInstruction* param = computation.parameter_instruction(parameter_number);
55     const OpMetadata& metadata = param->metadata();
56     return InvalidParameterArgument(metadata, format, args...);
57   }
58   return InvalidArgument(format, args...);
59 }
60 
61 }  // namespace source_map_util
62 }  // namespace xla
63 
64 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_SOURCE_MAP_UTIL_H_
65