xref: /aosp_15_r20/external/executorch/backends/vulkan/runtime/vk_api/Exception.cpp (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 #include <executorch/backends/vulkan/runtime/vk_api/Exception.h>
10 
11 #include <sstream>
12 
13 namespace vkcompute {
14 namespace vkapi {
15 
16 #define VK_RESULT_CASE(code) \
17   case code:                 \
18     out << #code;            \
19     break;
20 
operator <<(std::ostream & out,const VkResult result)21 std::ostream& operator<<(std::ostream& out, const VkResult result) {
22   switch (result) {
23     VK_RESULT_CASE(VK_SUCCESS)
24     VK_RESULT_CASE(VK_NOT_READY)
25     VK_RESULT_CASE(VK_TIMEOUT)
26     VK_RESULT_CASE(VK_EVENT_SET)
27     VK_RESULT_CASE(VK_EVENT_RESET)
28     VK_RESULT_CASE(VK_INCOMPLETE)
29     VK_RESULT_CASE(VK_ERROR_OUT_OF_HOST_MEMORY)
30     VK_RESULT_CASE(VK_ERROR_OUT_OF_DEVICE_MEMORY)
31     VK_RESULT_CASE(VK_ERROR_INITIALIZATION_FAILED)
32     VK_RESULT_CASE(VK_ERROR_DEVICE_LOST)
33     VK_RESULT_CASE(VK_ERROR_MEMORY_MAP_FAILED)
34     VK_RESULT_CASE(VK_ERROR_LAYER_NOT_PRESENT)
35     VK_RESULT_CASE(VK_ERROR_EXTENSION_NOT_PRESENT)
36     VK_RESULT_CASE(VK_ERROR_FEATURE_NOT_PRESENT)
37     VK_RESULT_CASE(VK_ERROR_INCOMPATIBLE_DRIVER)
38     VK_RESULT_CASE(VK_ERROR_TOO_MANY_OBJECTS)
39     VK_RESULT_CASE(VK_ERROR_FORMAT_NOT_SUPPORTED)
40     VK_RESULT_CASE(VK_ERROR_FRAGMENTED_POOL)
41     default:
42       out << "VK_ERROR_UNKNOWN (VkResult " << result << ")";
43       break;
44   }
45   return out;
46 }
47 
48 #undef VK_RESULT_CASE
49 
50 //
51 // SourceLocation
52 //
53 
operator <<(std::ostream & out,const SourceLocation & loc)54 std::ostream& operator<<(std::ostream& out, const SourceLocation& loc) {
55   out << loc.function << " at " << loc.file << ":" << loc.line;
56   return out;
57 }
58 
59 //
60 // Exception
61 //
62 
Error(SourceLocation source_location,std::string msg)63 Error::Error(SourceLocation source_location, std::string msg)
64     : msg_(std::move(msg)), source_location_{source_location} {
65   std::ostringstream oss;
66   oss << "Exception raised from " << source_location_ << ": ";
67   oss << msg_;
68   what_ = oss.str();
69 }
70 
Error(SourceLocation source_location,const char * cond,std::string msg)71 Error::Error(SourceLocation source_location, const char* cond, std::string msg)
72     : msg_(std::move(msg)), source_location_{source_location} {
73   std::ostringstream oss;
74   oss << "Exception raised from " << source_location_ << ": ";
75   oss << "(" << cond << ") is false! ";
76   oss << msg_;
77   what_ = oss.str();
78 }
79 
80 } // namespace vkapi
81 } // namespace vkcompute
82