Skip to content

Commit 463bf60

Browse files
gcarranza-1copybara-github
authored andcommitted
Wrap LiteRtStatus into C++ enum litert::Status
LiteRT-PiperOrigin-RevId: 829018389
1 parent 43ac9de commit 463bf60

File tree

6 files changed

+82
-13
lines changed

6 files changed

+82
-13
lines changed

litert/c/litert_common.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,10 @@ typedef enum {
282282
kLiteRtStatusPatternNoMatch = 3000,
283283
kLiteRtStatusInvalidTransformation = 3001,
284284
} LiteRtStatus;
285-
// LINT.ThenChange(../kotlin/src/main/kotlin/com/google/ai/edge/litert/LiteRtException.kt:status_codes)
285+
// LINT.ThenChange(
286+
// ../kotlin/src/main/kotlin/com/google/ai/edge/litert/LiteRtException.kt:status_codes,
287+
// ../cc/litert_common.h:status_codes
288+
// )
286289

287290
// Returns a string describing the status value.
288291
const char* LiteRtGetStatusString(LiteRtStatus status);

litert/cc/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ cc_library(
136136
hdrs = ["litert_expected.h"],
137137
deps = [
138138
"//litert/c:litert_common",
139+
"//litert/cc:litert_common",
139140
"//litert/cc/internal:litert_detail",
140141
"@com_google_absl//absl/log:absl_check",
141142
"@com_google_absl//absl/strings",

litert/cc/litert_common.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,44 @@
2020

2121
namespace litert {
2222

23+
// LINT.IfChange(status_codes)
24+
enum class Status : int {
25+
kOk = kLiteRtStatusOk,
26+
// Generic errors.
27+
kErrorInvalidArgument = kLiteRtStatusErrorInvalidArgument,
28+
kErrorMemoryAllocationFailure = kLiteRtStatusErrorMemoryAllocationFailure,
29+
kErrorRuntimeFailure = kLiteRtStatusErrorRuntimeFailure,
30+
kErrorMissingInputTensor = kLiteRtStatusErrorMissingInputTensor,
31+
kErrorUnsupported = kLiteRtStatusErrorUnsupported,
32+
kErrorNotFound = kLiteRtStatusErrorNotFound,
33+
kErrorTimeoutExpired = kLiteRtStatusErrorTimeoutExpired,
34+
kErrorWrongVersion = kLiteRtStatusErrorWrongVersion,
35+
kErrorUnknown = kLiteRtStatusErrorUnknown,
36+
kErrorAlreadyExists = kLiteRtStatusErrorAlreadyExists,
37+
// Inference progression errors.
38+
kCancelled = kLiteRtStatusCancelled,
39+
// File and loading related errors.
40+
kErrorFileIO = kLiteRtStatusErrorFileIO,
41+
kErrorInvalidFlatbuffer = kLiteRtStatusErrorInvalidFlatbuffer,
42+
kErrorDynamicLoading = kLiteRtStatusErrorDynamicLoading,
43+
kErrorSerialization = kLiteRtStatusErrorSerialization,
44+
kErrorCompilation = kLiteRtStatusErrorCompilation,
45+
// IR related errors.
46+
kErrorIndexOOB = kLiteRtStatusErrorIndexOOB,
47+
kErrorInvalidIrType = kLiteRtStatusErrorInvalidIrType,
48+
kErrorInvalidGraphInvariant = kLiteRtStatusErrorInvalidGraphInvariant,
49+
kErrorGraphModification = kLiteRtStatusErrorGraphModification,
50+
// Tool related errors.
51+
kErrorInvalidToolConfig = kLiteRtStatusErrorInvalidToolConfig,
52+
// Legalization related errors.
53+
kLegalizeNoMatch = kLiteRtStatusLegalizeNoMatch,
54+
kErrorInvalidLegalization = kLiteRtStatusErrorInvalidLegalization,
55+
// Transformation related errors.
56+
kPatternNoMatch = kLiteRtStatusPatternNoMatch,
57+
kInvalidTransformation = kLiteRtStatusInvalidTransformation,
58+
};
59+
// LINT.ThenChange(../c/litert_common.h:status_codes)
60+
2361
enum class HwAccelerators : int {
2462
kNone = kLiteRtHwAcceleratorNone,
2563
kCpu = kLiteRtHwAcceleratorCpu,

litert/cc/litert_expected.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "absl/strings/str_cat.h" // from @com_google_absl
2929
#include "absl/strings/str_format.h" // from @com_google_absl
3030
#include "litert/c/litert_common.h"
31+
#include "litert/cc/litert_common.h"
3132
#include "litert/cc/internal/litert_detail.h"
3233

3334
namespace litert {
@@ -44,13 +45,27 @@ class Error {
4445
public:
4546
// Construct Unexpected from status and optional error message.
4647
//
47-
// NOTE: kLiteRtStatusOk should not be passed to Unexpected.
48+
// NOTE: ::litert::Status::kOk should not be passed to Unexpected.
49+
explicit Error(Status status, std::string message = "")
50+
: status_(static_cast<LiteRtStatus>(status)),
51+
message_(std::move(message)) {
52+
ABSL_DCHECK(status != Status::kOk);
53+
}
54+
55+
[[deprecated("Use the constructor that takes ::litert::Status instead.")]]
4856
explicit Error(LiteRtStatus status, std::string message = "")
4957
: status_(status), message_(std::move(message)) {
5058
ABSL_DCHECK(status != kLiteRtStatusOk);
5159
}
5260

5361
// Get the status.
62+
// TODO: b/454666070 - Rename to Status() after the deprecated function is
63+
// removed.
64+
constexpr Status StatusCC() const {
65+
return static_cast<enum Status>(status_);
66+
}
67+
68+
[[deprecated("Use StatusCC() instead.")]]
5469
constexpr LiteRtStatus Status() const { return status_; }
5570

5671
// Get the error message, empty string if none was attached.

litert/test/BUILD

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ cc_library(
196196
deps = [
197197
"//litert/c:litert_common",
198198
"//litert/c:litert_model_types",
199+
"//litert/cc:litert_common",
199200
"//litert/cc:litert_element_type",
200201
"//litert/cc:litert_expected",
201-
"//litert/cc:litert_macros",
202-
"//litert/cc:litert_model",
202+
"//litert/cc:litert_ranked_tensor_type",
203203
"//litert/cc/internal:litert_c_types_printing",
204204
"@com_google_absl//absl/status",
205205
"@com_google_absl//absl/status:statusor",
@@ -262,9 +262,11 @@ cc_library(
262262
deps = [
263263
"//litert/c:litert_common",
264264
"//litert/c:litert_model_types",
265+
"//litert/cc:litert_common",
265266
"//litert/cc:litert_element_type",
266267
"//litert/cc:litert_expected",
267268
"//litert/cc:litert_macros",
269+
"//litert/cc:litert_ranked_tensor_type",
268270
"//litert/cc/dynamic_runtime:litert_model",
269271
"//litert/cc/internal:litert_c_types_printing",
270272
"@com_google_absl//absl/status",

litert/test/matchers.h

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
#include "litert/c/litert_common.h"
3535
#include "litert/c/litert_model_types.h"
3636
#include "litert/cc/internal/litert_c_types_printing.h" // IWYU pragma: keep
37+
#include "litert/cc/litert_common.h"
3738
#include "litert/cc/litert_element_type.h"
3839
#include "litert/cc/litert_expected.h"
39-
#include "litert/cc/litert_macros.h"
40-
#include "litert/cc/litert_model.h"
40+
#include "litert/cc/litert_ranked_tensor_type.h"
4141

4242
// Is equivalent to `ASSERT_THAT(expr, testing::litert::IsOk())`
4343
#define LITERT_ASSERT_OK(EXPR) ASSERT_THAT((EXPR), ::testing::litert::IsOk())
@@ -348,32 +348,42 @@ inline IsErrorMatcher IsError() {
348348
}
349349

350350
// Matches `litert::Expected`, `litert::Unexpected`, `litert::Error` and
351-
// `LiteRtStatus` values that hold a specific error status.
351+
// `litert::Status` values that hold a specific error status.
352352
//
353353
// ```cpp
354354
// Expected<Something> BuildSomething();
355355
//
356356
// // Will fail the test if BuildSomething()'s returned object holds a value or
357-
// // if the error status is not `kLiteRtStatusErrorSystemError`.
358-
// EXPECT_THAT(BuildSomething(), IsError(kLiteRtStatusErrorSystemError));
357+
// // if the error status is not `kErrorSystemError`.
358+
// EXPECT_THAT(BuildSomething(), IsError(::litert::Status::kErrorSystemError));
359359
// ```
360+
inline IsErrorMatcher IsError(::litert::Status status) {
361+
return IsErrorMatcher(static_cast<LiteRtStatus>(status),
362+
/*msg=*/std::nullopt);
363+
}
364+
360365
inline IsErrorMatcher IsError(LiteRtStatus status) {
361366
return IsErrorMatcher(status, /*msg=*/std::nullopt);
362367
}
363368

364-
// Matches `litert::Expected` and `LiteRtStatus` values that have a specific
369+
// Matches `litert::Expected` and `litert::Status` values that have a specific
365370
// error status and error message.
366371
//
367-
// Warning: This will always return `false` for `LiteRtStatus` objects as those
368-
// do not convey a message.
372+
// Warning: This will always return `false` for `litert::Status` objects as
373+
// those do not convey a message.
369374
//
370375
// ```cpp
371376
// Expected<Something> BuildSomething();
372377
//
373378
// // Will fail the test if BuildSomething()'s returned object holds a value.
374-
// EXPECT_THAT(BuildSomething(), IsError(kLiteRtStatusErrorSystemError,
379+
// EXPECT_THAT(BuildSomething(), IsError(::litert::Status::kErrorSystemError,
375380
// "System is not initialised"));
376381
// ```
382+
inline IsErrorMatcher IsError(::litert::Status status, std::string msg) {
383+
return IsErrorMatcher(static_cast<LiteRtStatus>(status), std::move(msg));
384+
}
385+
386+
[[deprecated("Use the litert::Status version instead.")]]
377387
inline IsErrorMatcher IsError(LiteRtStatus status, std::string msg) {
378388
return IsErrorMatcher(status, std::move(msg));
379389
}

0 commit comments

Comments
 (0)