Skip to content

Commit

Permalink
[tsl] Implement tsl::NullTerminatedMessage() in terms of `absl::Sta…
Browse files Browse the repository at this point in the history
…tusMessageAsCStr()`

* This allows TF to outsource correct acquisition of a null-terminated C-string of the status message to the Abseil library itself
* The previous implementation happened to work currently, but depended on internal implementation details of `absl::Status::message()` (namely, that when the message isn't empty, the returned `absl::string_view` is backed by a null-terminated `std::string`)
* Unfortunately we currently have to duplicate the definition depending on the platform due to linker-issues in the Windows build

PiperOrigin-RevId: 625785085
  • Loading branch information
lwolfsonkin authored and tensorflower-gardener committed Apr 17, 2024
1 parent 10ab091 commit 6c9e1d2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
15 changes: 6 additions & 9 deletions third_party/xla/third_party/tsl/tsl/platform/status.cc
Expand Up @@ -155,16 +155,13 @@ std::vector<StackFrame> GetStackTrace(const ::tsl::Status& status) {

} // namespace errors

const absl::string_view kEmptyString = "";

const char* NullTerminatedMessage(const Status& status) {
auto message = status.message();
if (message.empty()) {
return kEmptyString.data();
}
return message.data();
// NB: This Windows-only implementation is exists only to avoid a linker error.
// Remove if this is resolved.
#ifdef _WIN32
const char* NullTerminatedMessage(const absl::Status& status) {
return absl::StatusMessageAsCStr(status);
}

#endif

std::string* TfCheckOpHelperOutOfLine(const ::tsl::Status& v, const char* msg) {
std::string r("Non-OK-status: ");
Expand Down
10 changes: 9 additions & 1 deletion third_party/xla/third_party/tsl/tsl/platform/status.h
Expand Up @@ -119,7 +119,15 @@ inline absl::Status ToAbslStatus(const ::absl::Status& s) { return s; }
// the Tensorflow C-API.
// A more robust API would be to get both a `char*` of the beginning of the
// string, plus the size (see e.g. `XlaCustomCallStatusSetFailure`).
const char* NullTerminatedMessage(const Status& status);
// NB: This Windows-only implementation is exists only to avoid a linker error.
// Remove if this is resolved.
#ifdef _WIN32
const char* NullTerminatedMessage(const absl::Status& status);
#else
inline const char* NullTerminatedMessage(const absl::Status& status) {
return absl::StatusMessageAsCStr(status);
}
#endif

// TODO(b/197552541) Move this namespace to errors.h.
namespace errors {
Expand Down

0 comments on commit 6c9e1d2

Please sign in to comment.