Commit d854d98ba for llama.cpp
commit d854d98bae598388ecea1ed84396587e5edc326d
Author: Adrien Gallouët <angt@huggingface.co>
Date: Mon Oct 26 00:08:22 2026 +0100
cuda : use std::string instead of VLA for archName (#30463)
* cuda : use std::string instead of VLA for archName
Modernize the code (old C style) and fix the following warnings:
```
ggml/src/ggml-cuda/ggml-cuda.cu:180:19: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
180 | char archName[archLen + 1];
| ^~~~~~~~~~~
ggml/src/ggml-cuda/ggml-cuda.cu:180:19: note: read of non-const variable 'archLen' is not allowed in a constant expression
ggml/src/ggml-cuda/ggml-cuda.cu:179:9: note: declared here
179 | int archLen = strlen(devName);
| ^
```
Signed-off-by: Adrien Gallouët <angt@huggingface.co>
* Add a comment about archName
Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
---------
Signed-off-by: Adrien Gallouët <angt@huggingface.co>
Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
diff --git a/ggml/src/ggml-cuda/ggml-cuda.cu b/ggml/src/ggml-cuda/ggml-cuda.cu
index aff687a9a..b795951b2 100644
--- a/ggml/src/ggml-cuda/ggml-cuda.cu
+++ b/ggml/src/ggml-cuda/ggml-cuda.cu
@@ -214,52 +214,36 @@ static cudaError_t ggml_cuda_device_malloc(void ** ptr, size_t size, int device)
}
#if defined(GGML_USE_HIP)
-static int ggml_cuda_parse_id(char devName[]) {
+static int ggml_cuda_parse_id(const char * devName) {
// A list of possible Target IDs can be found under the rocclr/clr repo in device.cpp
// these values are not stable so this is susceptible to breakage
// https://github.com/ROCm/clr/blob/amd-staging/rocclr/device/device.cpp
- int archMajor = 0x0;
- int archMinor = 0x0;
- int archNum = GGML_CUDA_CC_OFFSET_AMD;
- int archLen = strlen(devName);
- char archName[archLen + 1];
+ // Extract the base architecture model ("gfx908:sramecc+:xnack-" -> "908")
+ std::string archName = devName;
+ archName.erase(0, 3);
+ archName = archName.substr(0, archName.find(':'));
- // strip leading 'gfx' while copying into our buffer
- if (archLen > 3) {
- strcpy(archName, &devName[3]);
- archLen -= 3;
- }
+ const auto hex = [](const std::string & s) { return (int) strtoul(s.c_str(), nullptr, 16); };
- // trim trailing :xnack- or :sramecc- statuses
- archLen = strcspn(archName, ":");
- archName[archLen] = '\0';
+ int cc = 0;
// tease out the version information
- if (archLen > 8) {
+ if (archName.size() > 8) {
// versions labeled generic use '-' as delimiter
- // strip the trailing "-generic" then iterate through what remains
- if ((strstr(archName, "-generic"))) {
- archName[archLen - 8] = '\0';
- char * pch;
- if ((pch = strtok(archName, "-"))) {
- archMajor = (int)strtoul(pch, 0, 16);
- if ((pch = strtok(NULL, "-"))) {
- archMinor = 0x10 * (int)strtoul(pch, 0, 16);
- }
+ if (archName.compare(archName.size() - 8, 8, "-generic") == 0) {
+ archName.resize(archName.size() - 8);
+ const size_t sep = archName.find('-');
+ cc = 0x100 * hex(archName.substr(0, sep));
+ if (sep != std::string::npos) {
+ cc += 0x10 * hex(archName.substr(sep + 1));
}
}
- } else if (archLen >= 3) {
- // last two digits should be the minor * 0x10 + stepping
- archMinor = (int)strtoul(&archName[archLen - 2], 0, 16);
- archName[archLen - 2] = '\0';
-
- // only the major version remains
- archMajor = (int)strtoul(archName, 0, 16);
+ } else if (archName.size() >= 3) {
+ // ordinary IDs are the version in hex
+ cc = hex(archName);
}
- archNum += archMajor * 0x100;
- archNum += archMinor;
- return archNum;
+ return GGML_CUDA_CC_OFFSET_AMD + cc;
}
#endif // defined(GGML_USE_HIP)