xref: /aosp_15_r20/external/executorch/backends/vulkan/runtime/vk_api/Exception.h (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #pragma once
10 // @lint-ignore-every CLANGTIDY facebook-hte-BadMemberName
11 
12 #include <executorch/backends/vulkan/runtime/vk_api/vk_api.h>
13 
14 #include <executorch/backends/vulkan/runtime/utils/StringUtils.h>
15 
16 #include <exception>
17 #include <ostream>
18 #include <string>
19 #include <vector>
20 
21 #define VK_CHECK(function)                                                  \
22   do {                                                                      \
23     const VkResult result = (function);                                     \
24     if (VK_SUCCESS != result) {                                             \
25       throw ::vkcompute::vkapi::Error(                                      \
26           {__func__, __FILE__, static_cast<uint32_t>(__LINE__)},            \
27           ::vkcompute::utils::concat_str(#function, " returned ", result)); \
28     }                                                                       \
29   } while (false)
30 
31 #define VK_CHECK_COND(cond, ...)                                 \
32   do {                                                           \
33     if (!(cond)) {                                               \
34       throw ::vkcompute::vkapi::Error(                           \
35           {__func__, __FILE__, static_cast<uint32_t>(__LINE__)}, \
36           #cond,                                                 \
37           ::vkcompute::utils::concat_str(__VA_ARGS__));          \
38     }                                                            \
39   } while (false)
40 
41 #define VK_THROW(...)                                          \
42   do {                                                         \
43     throw ::vkcompute::vkapi::Error(                           \
44         {__func__, __FILE__, static_cast<uint32_t>(__LINE__)}, \
45         ::vkcompute::utils::concat_str(__VA_ARGS__));          \
46   } while (false)
47 
48 namespace vkcompute {
49 namespace vkapi {
50 
51 std::ostream& operator<<(std::ostream& out, const VkResult loc);
52 
53 struct SourceLocation {
54   const char* function;
55   const char* file;
56   uint32_t line;
57 };
58 
59 std::ostream& operator<<(std::ostream& out, const SourceLocation& loc);
60 
61 class Error : public std::exception {
62  public:
63   Error(SourceLocation source_location, std::string msg);
64   Error(SourceLocation source_location, const char* cond, std::string msg);
65 
66  private:
67   std::string msg_;
68   SourceLocation source_location_;
69   std::string what_;
70 
71  public:
msg()72   const std::string& msg() const {
73     return msg_;
74   }
75 
what()76   const char* what() const noexcept override {
77     return what_.c_str();
78   }
79 };
80 
81 } // namespace vkapi
82 } // namespace vkcompute
83