Skip to content

Commit 6ebc287

Browse files
mkuperstcopybara-github
authored andcommitted
Make SourceLocation more closely mirror std::source_location, add an extension point so it can be customized for various platform types.
PiperOrigin-RevId: 501903611
1 parent 8a8d546 commit 6ebc287

File tree

6 files changed

+39
-122
lines changed

6 files changed

+39
-122
lines changed

tsl/platform/BUILD

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,21 +264,20 @@ cc_library(
264264
":logging",
265265
":macros",
266266
":mutex",
267-
":platform",
268267
":stack_frame",
269268
":stacktrace",
270269
":str_util",
271270
":strcat",
272271
":stringprintf",
273272
":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-
"//tsl/protobuf:error_codes_proto_impl_cc",
281-
] + tf_platform_deps("status"),
280+
],
282281
)
283282

284283
cc_library(

tsl/platform/default/BUILD

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -545,24 +545,6 @@ 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-
566548
bzl_library(
567549
name = "cuda_build_defs_bzl",
568550
srcs = ["cuda_build_defs.bzl"],
@@ -593,7 +575,6 @@ filegroup(
593575
"posix_file_system.cc",
594576
"posix_file_system.h",
595577
"stacktrace.h",
596-
"status.h",
597578
"tracing_impl.h",
598579
"//tsl/platform/profile_utils:cpu_utils.h",
599580
"//tsl/platform/profile_utils:i_cpu_utils_helper.h",

tsl/platform/default/build_config.bzl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,6 @@ 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"),
658657
clean_dep("//tsl/platform/default:tracing_impl.h"),
659658
clean_dep("//tsl/platform/default:unbounded_work_queue.h"),
660659
] + select({

tsl/platform/default/status.h

Lines changed: 0 additions & 70 deletions
This file was deleted.

tsl/platform/status.cc

Lines changed: 9 additions & 14 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,31 +320,26 @@ std::ostream& operator<<(std::ostream& os, const Status& x) {
320320

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

323-
Status FromAbslStatus(const absl::Status& s, SourceLocation loc) {
323+
Status FromAbslStatus(const absl::Status& s) {
324324
if (s.ok()) {
325325
return Status();
326326
}
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-
}
327+
Status converted(static_cast<tsl::error::Code>(s.code()), s.message());
334328
s.ForEachPayload(
335329
[&converted](absl::string_view key, const absl::Cord& value) {
336330
converted.SetPayload(key, std::string(value));
337331
});
332+
338333
return converted;
339334
}
340335

341-
absl::Status ToAbslStatus(const ::tsl::Status& s, SourceLocation loc) {
336+
absl::Status ToAbslStatus(const ::tsl::Status& s) {
342337
if (s.ok()) {
343338
return absl::OkStatus();
344339
}
345340

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

tsl/platform/status.h

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,42 @@ 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"
3534
#include "tsl/platform/stack_frame.h"
3635
#include "tsl/platform/types.h"
3736
#include "tsl/protobuf/error_codes.pb.h"
3837

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-
4638
namespace tsl {
4739

4840
#if TF_HAS_CPP_ATTRIBUTE(nodiscard)
4941
class [[nodiscard]] Status;
5042
#endif
5143

52-
typedef SourceLocationImpl SourceLocation;
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+
};
5370

5471
namespace errors {
5572
typedef ::tensorflow::error::Code Code;
@@ -191,8 +208,6 @@ class Status {
191208
absl::Span<const SourceLocation> GetSourceLocations() const;
192209

193210
private:
194-
friend Status FromAbslStatus(const absl::Status& s, SourceLocation loc);
195-
196211
void MaybeAddSourceLocation(SourceLocation loc);
197212

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

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());
241+
Status FromAbslStatus(const absl::Status& s);
242+
absl::Status ToAbslStatus(const ::tsl::Status& s);
230243

231244
// TODO(b/197552541) Move this namespace to errors.h.
232245
namespace errors {

0 commit comments

Comments
 (0)