Skip to content

Commit 9705797

Browse files
Make SourceLocation more closely mirror std::source_location, add an extension point so it can be customized for various platform types.
PiperOrigin-RevId: 501753043
1 parent f32b738 commit 9705797

File tree

6 files changed

+122
-39
lines changed

6 files changed

+122
-39
lines changed

tsl/platform/BUILD

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,20 +264,21 @@ cc_library(
264264
":logging",
265265
":macros",
266266
":mutex",
267+
":platform",
267268
":stack_frame",
268269
":stacktrace",
269270
":str_util",
270271
":strcat",
271272
":stringprintf",
272273
":types",
273-
"//tsl/protobuf:error_codes_proto_impl_cc",
274274
"@com_google_absl//absl/base",
275275
"@com_google_absl//absl/base:core_headers",
276276
"@com_google_absl//absl/status",
277277
"@com_google_absl//absl/strings",
278278
"@com_google_absl//absl/strings:cord",
279279
"@com_google_absl//absl/types:optional",
280-
],
280+
"//tsl/protobuf:error_codes_proto_impl_cc",
281+
] + tf_platform_deps("status"),
281282
)
282283

283284
cc_library(

tsl/platform/default/BUILD

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,24 @@ cc_library(
545545
],
546546
)
547547

548+
cc_library(
549+
name = "status",
550+
tags = [
551+
"manual",
552+
"no_oss",
553+
"nobuilder",
554+
],
555+
textual_hdrs = ["status.h"],
556+
visibility = ["//third_party/tensorflow:__subpackages__"],
557+
deps = [
558+
"//tsl/platform:types",
559+
"//tsl/protobuf:error_codes_proto_impl_cc",
560+
"@com_google_absl//absl/base:core_headers",
561+
"@com_google_absl//absl/status",
562+
"@com_google_absl//absl/strings",
563+
],
564+
)
565+
548566
bzl_library(
549567
name = "cuda_build_defs_bzl",
550568
srcs = ["cuda_build_defs.bzl"],
@@ -575,6 +593,7 @@ filegroup(
575593
"posix_file_system.cc",
576594
"posix_file_system.h",
577595
"stacktrace.h",
596+
"status.h",
578597
"tracing_impl.h",
579598
"//tsl/platform/profile_utils:cpu_utils.h",
580599
"//tsl/platform/profile_utils:i_cpu_utils_helper.h",

tsl/platform/default/build_config.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@ def tf_additional_lib_hdrs():
654654
clean_dep("//tsl/platform/default:mutex_data.h"),
655655
clean_dep("//tsl/platform/default:notification.h"),
656656
clean_dep("//tsl/platform/default:stacktrace.h"),
657+
clean_dep("//tsl/platform/default:status.h"),
657658
clean_dep("//tsl/platform/default:tracing_impl.h"),
658659
clean_dep("//tsl/platform/default:unbounded_work_queue.h"),
659660
] + select({

tsl/platform/default/status.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* Copyright 2023 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+
#ifndef TENSORFLOW_TSL_PLATFORM_DEFAULT_STATUS_H_
16+
#define TENSORFLOW_TSL_PLATFORM_DEFAULT_STATUS_H_
17+
18+
#include "absl/status/status.h"
19+
#include "absl/strings/string_view.h"
20+
#include "absl/types/span.h"
21+
#include "tsl/platform/types.h"
22+
#include "tsl/protobuf/error_codes.pb.h"
23+
24+
namespace tsl {
25+
#if ABSL_HAVE_BUILTIN(__builtin_LINE) && ABSL_HAVE_BUILTIN(__builtin_FILE)
26+
#define TF_INTERNAL_HAVE_BUILTIN_LINE_FILE 1
27+
#endif
28+
29+
class SourceLocationImpl {
30+
public:
31+
uint32_t line() const { return line_; }
32+
const char* file_name() const { return file_name_; }
33+
34+
#ifdef TF_INTERNAL_HAVE_BUILTIN_LINE_FILE
35+
static SourceLocationImpl current(uint32_t line = __builtin_LINE(),
36+
const char* file_name = __builtin_FILE()) {
37+
return SourceLocationImpl(line, file_name);
38+
}
39+
#else
40+
static SourceLocationImpl current(uint32_t line = 0,
41+
const char* file_name = nullptr) {
42+
return SourceLocationImpl(line, file_name);
43+
}
44+
#endif
45+
private:
46+
SourceLocationImpl(uint32_t line, const char* file_name)
47+
: line_(line), file_name_(file_name) {}
48+
uint32_t line_;
49+
const char* file_name_;
50+
};
51+
52+
namespace internal {
53+
54+
inline absl::Status MakeAbslStatus(
55+
::tensorflow::error::Code code, absl::string_view message,
56+
absl::Span<const SourceLocationImpl>,
57+
SourceLocationImpl loc = SourceLocationImpl::current()) {
58+
return absl::Status(static_cast<absl::StatusCode>(code), message);
59+
}
60+
61+
inline absl::Span<const SourceLocationImpl> GetSourceLocations(
62+
const absl::Status& status) {
63+
return {};
64+
}
65+
66+
} // namespace internal
67+
68+
} // namespace tsl
69+
70+
#endif // TENSORFLOW_TSL_PLATFORM_DEFAULT_STATUS_H_

tsl/platform/status.cc

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,13 @@ void Status::MaybeAddSourceLocation(SourceLocation loc) {
154154
if (state_ == nullptr) {
155155
return;
156156
}
157-
if (loc.line <= 0) {
157+
if (loc.line() <= 0) {
158158
return;
159159
}
160-
if (loc.file_name == nullptr) {
160+
if (loc.file_name() == nullptr) {
161161
return;
162162
}
163-
if (loc.file_name[0] == '\0') {
163+
if (loc.file_name()[0] == '\0') {
164164
return;
165165
}
166166
state_->source_locations.push_back(loc);
@@ -320,26 +320,31 @@ std::ostream& operator<<(std::ostream& os, const Status& x) {
320320

321321
Status OkStatus() { return Status(); }
322322

323-
Status FromAbslStatus(const absl::Status& s) {
323+
Status FromAbslStatus(const absl::Status& s, SourceLocation loc) {
324324
if (s.ok()) {
325325
return Status();
326326
}
327-
Status converted(static_cast<tsl::error::Code>(s.code()), s.message());
327+
absl::Span<const SourceLocation> locs = internal::GetSourceLocations(s);
328+
const SourceLocation first_loc = locs.empty() ? loc : locs[0];
329+
Status converted(static_cast<tsl::error::Code>(s.code()), s.message(),
330+
first_loc);
331+
for (int i = 1; i < locs.size(); ++i) {
332+
converted.MaybeAddSourceLocation(locs[i]);
333+
}
328334
s.ForEachPayload(
329335
[&converted](absl::string_view key, const absl::Cord& value) {
330336
converted.SetPayload(key, std::string(value));
331337
});
332-
333338
return converted;
334339
}
335340

336-
absl::Status ToAbslStatus(const ::tsl::Status& s) {
341+
absl::Status ToAbslStatus(const ::tsl::Status& s, SourceLocation loc) {
337342
if (s.ok()) {
338343
return absl::OkStatus();
339344
}
340345

341-
absl::Status converted(static_cast<absl::StatusCode>(s.code()),
342-
s.error_message());
346+
absl::Status converted = internal::MakeAbslStatus(
347+
s.code(), s.error_message(), s.GetSourceLocations(), loc);
343348
s.ForEachPayload([&converted](tsl::StringPiece key, tsl::StringPiece value) {
344349
converted.SetPayload(key, absl::Cord(value));
345350
});

tsl/platform/status.h

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,42 +31,25 @@ limitations under the License.
3131
#include "absl/types/optional.h"
3232
#include "tsl/platform/logging.h"
3333
#include "tsl/platform/macros.h"
34+
#include "tsl/platform/platform.h"
3435
#include "tsl/platform/stack_frame.h"
3536
#include "tsl/platform/types.h"
3637
#include "tsl/protobuf/error_codes.pb.h"
3738

39+
// Include appropriate platform-dependent parts of status.
40+
#if defined(PLATFORM_GOOGLE)
41+
#include "tsl/platform/google/status.h" // IWYU pragma: export
42+
#else
43+
#include "tsl/platform/default/status.h" // IWYU pragma: export
44+
#endif
45+
3846
namespace tsl {
3947

4048
#if TF_HAS_CPP_ATTRIBUTE(nodiscard)
4149
class [[nodiscard]] Status;
4250
#endif
4351

44-
#if ABSL_HAVE_BUILTIN(__builtin_LINE) && ABSL_HAVE_BUILTIN(__builtin_FILE)
45-
#define TF_INTERNAL_HAVE_BUILTIN_LINE_FILE 1
46-
#endif
47-
48-
struct SourceLocation {
49-
uint32_t line;
50-
const char* file_name;
51-
52-
#ifdef TF_INTERNAL_HAVE_BUILTIN_LINE_FILE
53-
static SourceLocation current(uint32_t line = __builtin_LINE(),
54-
const char* file_name = __builtin_FILE()) {
55-
SourceLocation loc;
56-
loc.line = line;
57-
loc.file_name = file_name;
58-
return loc;
59-
}
60-
#else
61-
static SourceLocation current(uint32_t line = 0,
62-
const char* file_name = nullptr) {
63-
SourceLocation loc;
64-
loc.line = line;
65-
loc.file_name = file_name;
66-
return loc;
67-
}
68-
#endif
69-
};
52+
typedef SourceLocationImpl SourceLocation;
7053

7154
namespace errors {
7255
typedef ::tensorflow::error::Code Code;
@@ -208,6 +191,8 @@ class Status {
208191
absl::Span<const SourceLocation> GetSourceLocations() const;
209192

210193
private:
194+
friend Status FromAbslStatus(const absl::Status& s, SourceLocation loc);
195+
211196
void MaybeAddSourceLocation(SourceLocation loc);
212197

213198
static const std::string& empty_string();
@@ -238,8 +223,10 @@ class Status {
238223
// usage of `OkStatus()` when constructing such an OK status.
239224
Status OkStatus();
240225

241-
Status FromAbslStatus(const absl::Status& s);
242-
absl::Status ToAbslStatus(const ::tsl::Status& s);
226+
Status FromAbslStatus(const absl::Status& s,
227+
SourceLocation loc = SourceLocation::current());
228+
absl::Status ToAbslStatus(const ::tsl::Status& s,
229+
SourceLocation loc = SourceLocation::current());
243230

244231
// TODO(b/197552541) Move this namespace to errors.h.
245232
namespace errors {

0 commit comments

Comments
 (0)