-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamdmlss_api.cpp
More file actions
667 lines (551 loc) · 21 KB
/
Copy pathamdmlss_api.cpp
File metadata and controls
667 lines (551 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/* Copyright (c) 2026 Advanced Micro Devices, Inc. All rights reserved. */
#include "api_backend.hpp"
#include "amdmlss/amdmlss_api.h"
#include "core/core.hpp"
#include "shaders/shaders.hpp"
#include <string_view>
#include <memory>
#include <format>
#include <cstdlib>
#include <cstdarg>
#include <string>
#include <iostream>
#include <unordered_map>
#include <mutex>
#include <cstring>
#include <vector>
#ifdef _WIN32
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef ERROR // windows.h defines ERROR=0 which clashes with mlss::VerboseLevel::ERROR
// SEH shim helpers — __try/__except cannot be used inside lambdas or functions
// with C++ object unwinding on MSVC/Clang, so each dangerous call site gets
// its own plain-C-style shim function that wraps exactly one MLSS C-API call.
// If an access violation or similar structured exception occurs inside MLSS,
// the shim returns MLSS_ERROR_FAILURE instead of crashing the host process.
static MLSSstatus seh_createContext(MLSScontext* ctx, const MLSSstring asic, const MLSSstring op) noexcept
{
__try { return mlss::setLastError(mlss::createContext(*ctx, asic, op, nullptr)); }
__except(EXCEPTION_EXECUTE_HANDLER) { return MLSS_ERROR_FAILURE; }
}
static MLSSstatus seh_getCaps(const MLSScontext ctx, MLSSstatus** ps, MLSSsize* ns) noexcept
{
__try { return mlss::setLastError(mlss::getCaps(ctx, ps, ns)); }
__except(EXCEPTION_EXECUTE_HANDLER) { return MLSS_ERROR_FAILURE; }
}
struct SetParamArgs { MLSScontext* ctx; const MLSSstring op; MLSSenum flag; const MLSSvoid* val; };
static MLSSstatus seh_setParamByEnum(SetParamArgs a) noexcept
{
__try {
if (!mlss::setParams(a.ctx, a.op, a.flag, a.val))
return mlss::setLastError(MLSS_ERROR_PARAMETER_NOT_FOUND);
return mlss::setLastError(MLSS_SUCCESS);
}
__except(EXCEPTION_EXECUTE_HANDLER) { return MLSS_ERROR_FAILURE; }
}
static MLSSstatus seh_getBinariesEx(const MLSScontext ctx, MLSSbinary** bins,
MLSSsize* n, MLSSbinaryKind kind) noexcept
{
__try { return mlss::setLastError(mlss::createBinariesEx(*bins, ctx, n, kind)); }
__except(EXCEPTION_EXECUTE_HANDLER) { return MLSS_ERROR_FAILURE; }
}
#endif
#ifndef MLSS_VERSION_MAJOR
#define MLSS_VERSION_MAJOR 0
#endif
#ifndef MLSS_VERSION_MEDIAN
#define MLSS_VERSION_MEDIAN 0
#endif
#ifndef MLSS_VERSION_MINOR
#define MLSS_VERSION_MINOR 0
#endif
namespace
{
const char* const ERROR_STRINGS[] =
{
"Success",
"Failure",
"Configuration not supported",
"Graphix not supported",
"Operator not supported",
"Invalid parameter",
"Parameter not found",
"Operator not found",
"Bad memory allocation",
"Context update failed",
"Context not created",
"Unknown attribute",
"Context already existing"};
constexpr size_t ERROR_STRINGS_SIZE = sizeof(ERROR_STRINGS) / sizeof(ERROR_STRINGS[0]);
} // namespace
extern "C"
{
// Error handling functions
MLSSenum mlssPeakAtLastError()
{
return mlss::returnLastError();
}
MLSSenum mlssGetLastError()
{
return mlss::resetLastError();
}
MLSSstring mlssGetErrorString(const MLSSenum error)
{
size_t index = static_cast<size_t>(error);
if (index < ERROR_STRINGS_SIZE)
{
return const_cast<MLSSstring>(ERROR_STRINGS[index]);
}
return const_cast<MLSSstring>("Unknown error");
}
// Version functions
MLSSstatus mlssGetVersion(MLSSuint32* const major, MLSSuint32* const median, MLSSuint32* const minor, MLSSstringref flavor)
{
if (major != nullptr)
{
*major = MLSS_VERSION_MAJOR;
}
if (median != nullptr)
{
*median = MLSS_VERSION_MEDIAN;
}
if (minor != nullptr)
{
*minor = MLSS_VERSION_MINOR;
}
if (flavor != nullptr)
{
*flavor = const_cast<MLSSstring>(MLSS_FLAVOR);
}
return mlss::setLastError(MLSS_SUCCESS);
}
MLSSstring mlssGetVersionAsString()
{
// Static buffer for thread safety concerns
static thread_local char buffer[64];
MLSSuint32 minor = 0, median = 0, major = 0;
MLSSstring rev = nullptr;
mlssGetVersion(&major, &median, &minor, &rev);
std::string tmp = std::format("{}.{}.{}-{}", major, median, minor, rev ? rev : "unknown");
// Copy to static buffer to ensure persistence using std::string::copy
size_t copy_length = std::min(tmp.size(), static_cast<size_t>(63));
tmp.copy(buffer, copy_length);
buffer[copy_length] = '\0'; // Ensure null termination
return buffer;
}
// Context management
MLSSstatus mlssCreateContext(MLSScontext* context, const MLSSstring asic, const MLSSstring opName)
{
if (context == 0)
{
return mlss::setLastError(MLSS_ERROR_INVALID_PARAMETER);
}
#ifdef _WIN32
return seh_createContext(context, asic, opName);
#else
return mlss::setLastError(mlss::createContext(*context, asic, opName, nullptr));
#endif
}
MLSSstatus mlssCreateContextList(MLSScontext* context, const MLSSstring asic, const MLSSstring opName, ...)
{
if (context == 0)
{
return mlss::setLastError(MLSS_ERROR_INVALID_PARAMETER);
}
va_list args;
va_start(args, opName);
if (auto status = mlss::createContext(*context, asic, opName, &args); status != MLSS_SUCCESS)
{
return mlss::setLastError(status);
}
va_end(args);
return mlss::setLastError(MLSS_SUCCESS);
}
// get caps
MLSSstatus mlssGetCaps(const MLSScontext context, MLSSstatus** const pStatuses, MLSSsize* const nStatuses)
{
#ifdef _WIN32
return seh_getCaps(context, pStatuses, nStatuses);
#else
return mlss::setLastError(mlss::getCaps(context, pStatuses, nStatuses));
#endif
}
// Binary management
MLSSstatus mlssGetBinaries(const MLSScontext context, MLSSbinary** const binaries, MLSSsize* const numBinaries)
{
if (context == 0)
{
return mlss::setLastError(MLSS_ERROR_INVALID_PARAMETER);
}
return mlss::setLastError(mlss::createBinaries(*binaries, context, numBinaries));
}
MLSSstatus mlssGetBinariesEx(const MLSScontext context, MLSSbinary** const binaries,
MLSSsize* const numBinaries, MLSSbinaryKind kind)
{
if ((context == 0) || (binaries == nullptr) || (numBinaries == nullptr))
{
return mlss::setLastError(MLSS_ERROR_INVALID_PARAMETER);
}
#ifdef _WIN32
return seh_getBinariesEx(context, binaries, numBinaries, kind);
#else
return mlss::setLastError(mlss::createBinariesEx(*binaries, context, numBinaries, kind));
#endif
}
// Operator management
MLSSvoid mlssEnumerateOperators(MLSSstringarray operators, MLSSint32* count)
{
(MLSSvoid) operators;
(MLSSvoid) count;
mlss::not_implemented_as_a_warning();
mlss::setLastError(MLSS_WARNING_NOT_IMPLEMENTED);
}
// Parameter management for binary creation
MLSSstatus mlssGetParameterByName(const MLSScontext context, const MLSSstring opName, const MLSSstring parameterName, MLSSvoid* value)
{
(MLSSvoid) context;
(MLSSvoid) opName;
(MLSSvoid) parameterName;
(MLSSvoid) value;
mlss::not_implemented_as_a_warning();
return mlss::setLastError(MLSS_WARNING_NOT_IMPLEMENTED);
}
MLSSstatus mlssGetParameterByEnum(const MLSScontext context, const MLSSstring opName, const MLSSenum parameterFlag, MLSSvoid* value)
{
(MLSSvoid) context;
(MLSSvoid) opName;
(MLSSvoid) parameterFlag;
(MLSSvoid) value;
mlss::not_implemented_as_a_warning();
return mlss::setLastError(MLSS_WARNING_NOT_IMPLEMENTED);
}
MLSSstatus mlssSetParameterByName(MLSScontext* const context, const MLSSstring opName, const MLSSstring parameterName, const MLSSvoid* const value)
{
if ((context == 0) || (opName == nullptr))
{
return mlss::setLastError(MLSS_ERROR_INVALID_PARAMETER);
}
if (!mlss::setParams(context, opName, parameterName, value))
{
return mlss::setLastError(MLSS_ERROR_PARAMETER_NOT_FOUND);
}
return mlss::setLastError(MLSS_SUCCESS);
}
MLSSstatus mlssSetParameterByEnum(MLSScontext* const context, const MLSSstring opName, const MLSSenum parameterFlag, const MLSSvoid* const value)
{
if ((context == 0) || (opName == nullptr))
{
return mlss::setLastError(MLSS_ERROR_INVALID_PARAMETER);
}
#ifdef _WIN32
return seh_setParamByEnum({context, opName, parameterFlag, value});
#else
if (!mlss::setParams(context, opName, parameterFlag, value))
return mlss::setLastError(MLSS_ERROR_PARAMETER_NOT_FOUND);
return mlss::setLastError(MLSS_SUCCESS);
#endif
}
MLSSstatus mlssPrintParameters(const MLSScontext context, const MLSSstring opName)
{
return mlss::printParams(context, opName);
}
MLSSstatus mlssPrintBinaries(const MLSSbinary* const binaries, const MLSSsize n)
{
return mlss::printBinaries(binaries, n);
}
MLSSstatus mlssVectorRetrieveData(const MLSSvector vector,
MLSSvoid** const data,
MLSSsize* const n,
MLSSenum* const type)
{
return mlss::retrieveVectorData(vector, data, n, type);
}
MLSSbool mlssVectorIsValid(const MLSSvector vector)
{
return mlss::isVectorValid(vector);
}
// Verbose mode management
MLSSstatus mlssSetVerboseLevel(MLSSenum level)
{
// Convert MLSSenum to VerboseLevel
mlss::VerboseLevel verboseLevel = mlss::VerboseLevel::NONE;
switch (level)
{
case 0:
verboseLevel = mlss::VerboseLevel::NONE;
break;
case 1:
verboseLevel = mlss::VerboseLevel::ERROR;
break;
case 2:
verboseLevel = mlss::VerboseLevel::WARNING;
break;
case 3:
verboseLevel = mlss::VerboseLevel::INFO;
break;
case 4:
verboseLevel = mlss::VerboseLevel::DEBUG;
break;
case 5:
verboseLevel = mlss::VerboseLevel::TRACE;
break;
default:
return mlss::setLastError(MLSS_ERROR_INVALID_PARAMETER);
}
mlss::VerboseManager::getInstance().setLevel(verboseLevel);
return mlss::setLastError(MLSS_SUCCESS);
}
MLSSenum mlssGetVerboseLevel()
{
mlss::VerboseLevel level = mlss::VerboseManager::getInstance().getLevel();
return static_cast<MLSSenum>(level);
}
MLSSstatus mlssEnableVerboseMode()
{
mlss::VerboseManager::getInstance().setLevel(mlss::VerboseLevel::INFO);
return mlss::setLastError(MLSS_SUCCESS);
}
MLSSstatus mlssDisableVerboseMode()
{
mlss::VerboseManager::getInstance().setLevel(mlss::VerboseLevel::NONE);
return mlss::setLastError(MLSS_SUCCESS);
}
// Device query functions
MLSSstatus mlssGetDeviceFeatures(MLSSdevicefeatures** devices, MLSSsize* numDevices)
{
if (!devices || !numDevices)
{
return mlss::setLastError(MLSS_ERROR_INVALID_PARAMETER);
}
auto result = mlss::getDeviceFeatures();
if (!result.has_value())
{
*devices = nullptr;
*numDevices = 0;
return mlss::setLastError(MLSS_ERROR_FAILURE);
}
const auto& deviceVector = result.value();
*numDevices = deviceVector.size();
if (*numDevices == 0)
{
*devices = nullptr;
return mlss::setLastError(MLSS_SUCCESS);
}
// Create a collection to store device features with proper lifetime management
struct DeviceFeatureCollection_t
{
std::vector<MLSSdevicefeatures> devices;
std::vector<std::string> gfxStrings;
};
auto collection = std::make_unique<DeviceFeatureCollection_t>();
collection->devices.reserve(deviceVector.size());
collection->gfxStrings.reserve(deviceVector.size());
// Copy device features
for (const auto& device : deviceVector)
{
collection->devices.push_back(device);
collection->gfxStrings.push_back(device.m_gfx);
// Update the pointer to point to our stored string
collection->devices.back().m_gfx = collection->gfxStrings.back().c_str();
}
// Store the collection in MemoryManager using Any
mlss::Any collection_any = std::move(collection);
MLSShandle handle = mlss::MemoryManager::addObject(std::move(collection_any));
// Mark as initialized
mlss::Any* stored_any = mlss::MemoryManager::template getPointer<mlss::Any>(handle);
if (!stored_any)
{
*devices = nullptr;
*numDevices = 0;
return mlss::setLastError(MLSS_ERROR_BAD_MEMORY_ALLOCATION);
}
mlss::MemoryManager::markAsInitialized(&stored_any);
// Get the stored collection to return pointer to its data
if (mlss::anyIs<std::unique_ptr<DeviceFeatureCollection_t>>(*stored_any))
{
auto& stored_collection = mlss::anyCast<std::unique_ptr<DeviceFeatureCollection_t>&>(*stored_any);
*devices = stored_collection->devices.data();
}
else
{
*devices = nullptr;
*numDevices = 0;
return mlss::setLastError(MLSS_ERROR_BAD_MEMORY_ALLOCATION);
}
return mlss::setLastError(MLSS_SUCCESS);
}
MLSSstatus mlssGetOptimalDeviceFeatures(MLSSdevicefeatures* device)
{
if (!device)
{
return mlss::setLastError(MLSS_ERROR_INVALID_PARAMETER);
}
auto result = mlss::getOptimalDeviceFeatures();
if (!result.has_value())
{
return mlss::setLastError(MLSS_ERROR_FAILURE);
}
// Create a structure to hold the device and its string
struct OptimalDeviceHolder_t
{
MLSSdevicefeatures device;
std::string gfxString;
};
auto holder = std::make_unique<OptimalDeviceHolder_t>();
holder->device = result.value();
holder->gfxString = result.value().m_gfx;
holder->device.m_gfx = holder->gfxString.c_str();
// Copy to output
*device = holder->device;
// Store the holder in MemoryManager to keep the string alive
mlss::Any holder_any = std::move(holder);
MLSShandle handle = mlss::MemoryManager::addObject(std::move(holder_any));
// Mark as initialized
mlss::Any* stored_any = mlss::MemoryManager::template getPointer<mlss::Any>(handle);
if (stored_any)
{
mlss::MemoryManager::markAsInitialized(&stored_any);
}
return mlss::setLastError(MLSS_SUCCESS);
}
// Custom Type Registration Functions
// Global registry for custom types
static std::unordered_map<MLSSenum, MLSScustomtypeinfo> g_customTypeRegistry;
static MLSSenum g_nextCustomTypeId = MLSS_CUSTOM_TYPE_START;
static std::mutex g_customTypeRegistryMutex;
// String storage for custom type names and operators (owns the memory)
struct CustomTypeStringStorage
{
std::string typeName;
std::vector<std::string> supportedOperators;
};
static std::unordered_map<MLSSenum, CustomTypeStringStorage> g_customTypeStrings;
MLSSenum mlssRegisterCustomType(const MLSSstring typeName,
MLSScustomtypeset setFunc,
MLSScustomtypeget getFunc,
...)
{
if (!typeName || !setFunc || !getFunc)
{
mlss::setLastError(MLSS_ERROR_INVALID_PARAMETER);
return 0;
}
std::lock_guard<std::mutex> lock(g_customTypeRegistryMutex);
// Check if type name already exists
for (const auto& [id, info] : g_customTypeRegistry)
{
if (info.m_typeName && std::strcmp(info.m_typeName, typeName) == 0)
{
mlss::setLastError(MLSS_ERROR_ALREADY_EXISTING_CONTEXT);
return 0;
}
}
// Collect supported operators from variadic arguments
std::vector<std::string> supportedOps;
va_list args;
va_start(args, getFunc);
const char* op = va_arg(args, const char*);
while (op && std::strcmp(op, MLSS_END_LIST) != 0)
{
supportedOps.push_back(op);
op = va_arg(args, const char*);
}
va_end(args);
// Create custom type info
MLSScustomtypeinfo info;
info.m_typeId = g_nextCustomTypeId++;
info.m_setFunc = setFunc;
info.m_getFunc = getFunc;
info.m_printFunc = nullptr; // Optional, can be added later
// Store strings in our storage (owns the memory)
CustomTypeStringStorage& storage = g_customTypeStrings[info.m_typeId];
storage.typeName = typeName;
storage.supportedOperators = std::move(supportedOps);
// Point the C struct to our owned strings
info.m_typeName = storage.typeName.c_str();
// Create pointer array for supported operators
if (!storage.supportedOperators.empty())
{
info.m_supportedOperators = new char*[storage.supportedOperators.size() + 1];
for (size_t i = 0; i < storage.supportedOperators.size(); ++i)
{
// Cast away const since the C API expects char** but we won't modify
info.m_supportedOperators[i] = const_cast<char*>(storage.supportedOperators[i].c_str());
}
info.m_supportedOperators[storage.supportedOperators.size()] = nullptr; // NULL-terminate
}
else
{
info.m_supportedOperators = nullptr;
}
// Register the type
g_customTypeRegistry[info.m_typeId] = info;
mlss::setLastError(MLSS_SUCCESS);
return info.m_typeId;
}
MLSSstatus mlssGetCustomTypeInfo(MLSSenum customTypeId, MLSScustomtypeinfo* info)
{
if (!info)
{
return mlss::setLastError(MLSS_ERROR_INVALID_PARAMETER);
}
std::lock_guard<std::mutex> lock(g_customTypeRegistryMutex);
auto it = g_customTypeRegistry.find(customTypeId);
if (it == g_customTypeRegistry.end())
{
return mlss::setLastError(MLSS_ERROR_PARAMETER_NOT_FOUND);
}
*info = it->second;
return mlss::setLastError(MLSS_SUCCESS);
}
MLSSstatus mlssUnregisterCustomType(MLSSenum customTypeId)
{
std::lock_guard<std::mutex> lock(g_customTypeRegistryMutex);
auto it = g_customTypeRegistry.find(customTypeId);
if (it == g_customTypeRegistry.end())
{
return mlss::setLastError(MLSS_ERROR_PARAMETER_NOT_FOUND);
}
// Clean up allocated memory
MLSScustomtypeinfo& info = it->second;
// Delete the pointer array (strings are owned by g_customTypeStrings)
if (info.m_supportedOperators)
{
delete[] info.m_supportedOperators;
}
// Remove from registries
g_customTypeStrings.erase(info.m_typeId);
g_customTypeRegistry.erase(it);
return mlss::setLastError(MLSS_SUCCESS);
}
MLSSbool mlssIsCustomType(MLSSenum typeId)
{
std::lock_guard<std::mutex> lock(g_customTypeRegistryMutex);
return g_customTypeRegistry.find(typeId) != g_customTypeRegistry.end();
}
MLSSstatus mlssSetParameterByNameTyped(MLSScontext* const context,
const MLSSstring opName,
const MLSSstring parameterName,
MLSSenum valueType,
const MLSSvoid* const value)
{
if (!context || !opName || !parameterName || !value)
{
return mlss::setLastError(MLSS_ERROR_INVALID_PARAMETER);
}
// Check if it's a custom type
if (mlssIsCustomType(valueType))
{
std::lock_guard<std::mutex> lock(g_customTypeRegistryMutex);
auto it = g_customTypeRegistry.find(valueType);
if (it != g_customTypeRegistry.end() && it->second.m_setFunc)
{
// Use the custom type's set function
return it->second.m_setFunc(context, opName, value);
}
}
// Fall back to regular parameter setting for built-in types
return mlssSetParameterByName(context, opName, parameterName, value);
}
} // extern "C"