@@ -43,12 +43,15 @@ using alloc = cl::sycl::usm::alloc;
43
43
namespace detail {
44
44
namespace usm {
45
45
46
- void *alignedAllocHost (size_t Alignment, size_t Size, const context &Ctxt,
47
- alloc Kind, const detail::code_location &CL) {
46
+ static pi_result alignedAllocHostHelper (size_t Alignment, size_t Size,
47
+ const context &Ctxt, alloc Kind,
48
+ const detail::code_location &CL,
49
+ void **OutPtr) {
48
50
XPTI_CREATE_TRACEPOINT (CL);
49
- void *RetVal = nullptr ;
50
- if (Size == 0 )
51
- return nullptr ;
51
+ if (Size == 0 ) {
52
+ *OutPtr = nullptr ;
53
+ return PI_SUCCESS;
54
+ }
52
55
if (Ctxt.is_host ()) {
53
56
if (!Alignment) {
54
57
// worst case default
@@ -57,10 +60,11 @@ void *alignedAllocHost(size_t Alignment, size_t Size, const context &Ctxt,
57
60
58
61
aligned_allocator<char > Alloc (Alignment);
59
62
try {
60
- RetVal = Alloc.allocate (Size);
63
+ *OutPtr = Alloc.allocate (Size);
61
64
} catch (const std::bad_alloc &) {
62
65
// Conform with Specification behavior
63
- RetVal = nullptr ;
66
+ *OutPtr = nullptr ;
67
+ return PI_MEM_OBJECT_ALLOCATION_FAILURE;
64
68
}
65
69
} else {
66
70
std::shared_ptr<context_impl> CtxImpl = detail::getSyclObjImpl (Ctxt);
@@ -71,13 +75,13 @@ void *alignedAllocHost(size_t Alignment, size_t Size, const context &Ctxt,
71
75
switch (Kind) {
72
76
case alloc::host: {
73
77
Error = Plugin.call_nocheck <PiApiKind::piextUSMHostAlloc>(
74
- &RetVal , C, nullptr , Size, Alignment);
78
+ OutPtr , C, nullptr , Size, Alignment);
75
79
break ;
76
80
}
77
81
case alloc::device:
78
82
case alloc::shared:
79
83
case alloc::unknown: {
80
- RetVal = nullptr ;
84
+ *OutPtr = nullptr ;
81
85
Error = PI_INVALID_VALUE;
82
86
break ;
83
87
}
@@ -86,21 +90,41 @@ void *alignedAllocHost(size_t Alignment, size_t Size, const context &Ctxt,
86
90
// Error is for debugging purposes.
87
91
// The spec wants a nullptr returned, not an exception.
88
92
if (Error != PI_SUCCESS)
89
- return nullptr ;
93
+ *OutPtr = nullptr ;
94
+
95
+ return Error;
96
+ }
97
+ return PI_SUCCESS;
98
+ }
99
+
100
+ void *alignedAllocHost (size_t Alignment, size_t Size, const context &Ctxt,
101
+ alloc Kind, const detail::code_location &CL) {
102
+ void *RetVal;
103
+ pi_result Err =
104
+ alignedAllocHostHelper (Alignment, Size, Ctxt, Kind, CL, &RetVal);
105
+
106
+ std::shared_ptr<context_impl> CtxImpl = detail::getSyclObjImpl (Ctxt);
107
+ ResourcePool &Resources = CtxImpl->getResourcePool ();
108
+ if (Err == PI_OUT_OF_RESOURCES && Resources.isEnabled ()) {
109
+ // Clear resource pool and retry allocation.
110
+ Resources.clear ();
111
+ alignedAllocHostHelper (Alignment, Size, Ctxt, Kind, CL, &RetVal);
90
112
}
91
113
return RetVal;
92
114
}
93
115
94
- void *alignedAlloc (size_t Alignment, size_t Size, const context &Ctxt,
95
- const device &Dev, alloc Kind,
96
- const detail::code_location &CL) {
116
+ static pi_result alignedAllocHelper (size_t Alignment, size_t Size,
117
+ const context &Ctxt, const device &Dev,
118
+ alloc Kind, const detail::code_location &CL,
119
+ void **OutPtr) {
97
120
XPTI_CREATE_TRACEPOINT (CL);
98
- void *RetVal = nullptr ;
99
- if (Size == 0 )
100
- return nullptr ;
121
+ if (Size == 0 ) {
122
+ *OutPtr = nullptr ;
123
+ return PI_SUCCESS;
124
+ }
101
125
if (Ctxt.is_host ()) {
102
126
if (Kind == alloc::unknown) {
103
- RetVal = nullptr ;
127
+ *OutPtr = nullptr ;
104
128
} else {
105
129
if (!Alignment) {
106
130
// worst case default
@@ -109,10 +133,11 @@ void *alignedAlloc(size_t Alignment, size_t Size, const context &Ctxt,
109
133
110
134
aligned_allocator<char > Alloc (Alignment);
111
135
try {
112
- RetVal = Alloc.allocate (Size);
136
+ *OutPtr = Alloc.allocate (Size);
113
137
} catch (const std::bad_alloc &) {
114
138
// Conform with Specification behavior
115
- RetVal = nullptr ;
139
+ *OutPtr = nullptr ;
140
+ return PI_MEM_OBJECT_ALLOCATION_FAILURE;
116
141
}
117
142
}
118
143
} else {
@@ -126,18 +151,18 @@ void *alignedAlloc(size_t Alignment, size_t Size, const context &Ctxt,
126
151
case alloc::device: {
127
152
Id = detail::getSyclObjImpl (Dev)->getHandleRef ();
128
153
Error = Plugin.call_nocheck <PiApiKind::piextUSMDeviceAlloc>(
129
- &RetVal , C, Id, nullptr , Size, Alignment);
154
+ OutPtr , C, Id, nullptr , Size, Alignment);
130
155
break ;
131
156
}
132
157
case alloc::shared: {
133
158
Id = detail::getSyclObjImpl (Dev)->getHandleRef ();
134
159
Error = Plugin.call_nocheck <PiApiKind::piextUSMSharedAlloc>(
135
- &RetVal , C, Id, nullptr , Size, Alignment);
160
+ OutPtr , C, Id, nullptr , Size, Alignment);
136
161
break ;
137
162
}
138
163
case alloc::host:
139
164
case alloc::unknown: {
140
- RetVal = nullptr ;
165
+ *OutPtr = nullptr ;
141
166
Error = PI_INVALID_VALUE;
142
167
break ;
143
168
}
@@ -146,7 +171,27 @@ void *alignedAlloc(size_t Alignment, size_t Size, const context &Ctxt,
146
171
// Error is for debugging purposes.
147
172
// The spec wants a nullptr returned, not an exception.
148
173
if (Error != PI_SUCCESS)
149
- return nullptr ;
174
+ *OutPtr = nullptr ;
175
+
176
+ return Error;
177
+ }
178
+
179
+ return PI_SUCCESS;
180
+ }
181
+
182
+ void *alignedAlloc (size_t Alignment, size_t Size, const context &Ctxt,
183
+ const device &Dev, alloc Kind,
184
+ const detail::code_location &CL) {
185
+ void *RetVal;
186
+ pi_result Err =
187
+ alignedAllocHelper (Alignment, Size, Ctxt, Dev, Kind, CL, &RetVal);
188
+
189
+ std::shared_ptr<context_impl> CtxImpl = detail::getSyclObjImpl (Ctxt);
190
+ ResourcePool &Resources = CtxImpl->getResourcePool ();
191
+ if (Err == PI_OUT_OF_RESOURCES && Resources.isEnabled ()) {
192
+ // Clear resource pool and retry allocation.
193
+ Resources.clear ();
194
+ alignedAllocHelper (Alignment, Size, Ctxt, Dev, Kind, CL, &RetVal);
150
195
}
151
196
return RetVal;
152
197
}
0 commit comments