Skip to content

Commit 6fb416d

Browse files
fs-eireguschmue
authored andcommitted
[nodejs binding] Fix building in latest clang (#23146)
### Description This change fixes the build break for Node.js binding on latest AppleClang: ``` ...tensor_helper.cc:65:5 error: integer value -1 is outside of the valid range of values [0,15] for the enumeration type 'napi_typedarray_type' [-Wenum-constexpr-conversion] ``` Use the underlying type of enum `napi_typedarray_type` for `DATA_TYPE_TYPEDARRAY_MAP` to solve this issue. Because the underlying type is implementation defined (it's `int` for MSVC and `unsigned int` for Clang), we use `std::underlying_type_t` to get the correct type.
1 parent 998fcb1 commit 6fb416d

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

js/node/src/tensor_helper.cc

+34-21
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,24 @@ constexpr size_t DATA_TYPE_ELEMENT_SIZE_MAP[] = {
5353
static_assert(sizeof(DATA_TYPE_ELEMENT_SIZE_MAP) == sizeof(size_t) * ONNX_TENSOR_ELEMENT_DATA_TYPE_COUNT,
5454
"definition not matching");
5555

56-
constexpr napi_typedarray_type DATA_TYPE_TYPEDARRAY_MAP[] = {
57-
(napi_typedarray_type)(-1), // ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED not supported
58-
napi_float32_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT
59-
napi_uint8_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8
60-
napi_int8_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8
61-
napi_uint16_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT16
62-
napi_int16_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16
63-
napi_int32_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32
64-
napi_bigint64_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64
65-
(napi_typedarray_type)(-1), // ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING not supported
66-
napi_uint8_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL
67-
napi_uint16_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT16 FLOAT16 uses Uint16Array
68-
napi_float64_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE
69-
napi_uint32_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32
70-
napi_biguint64_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64
71-
(napi_typedarray_type)(-1), // ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX64 not supported
72-
(napi_typedarray_type)(-1), // ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX128 not supported
73-
(napi_typedarray_type)(-1) // ONNX_TENSOR_ELEMENT_DATA_TYPE_BFLOAT16 not supported
56+
constexpr std::underlying_type_t<napi_typedarray_type> DATA_TYPE_TYPEDARRAY_MAP[] = {
57+
std::underlying_type_t<napi_typedarray_type>(-1), // ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED not supported
58+
napi_float32_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT
59+
napi_uint8_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8
60+
napi_int8_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8
61+
napi_uint16_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT16
62+
napi_int16_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16
63+
napi_int32_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32
64+
napi_bigint64_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64
65+
std::underlying_type_t<napi_typedarray_type>(-1), // ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING not supported
66+
napi_uint8_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL
67+
napi_uint16_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT16 FLOAT16 uses Uint16Array
68+
napi_float64_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE
69+
napi_uint32_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32
70+
napi_biguint64_array, // ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64
71+
std::underlying_type_t<napi_typedarray_type>(-1), // ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX64 not supported
72+
std::underlying_type_t<napi_typedarray_type>(-1), // ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX128 not supported
73+
std::underlying_type_t<napi_typedarray_type>(-1) // ONNX_TENSOR_ELEMENT_DATA_TYPE_BFLOAT16 not supported
7474
};
7575
static_assert(sizeof(DATA_TYPE_TYPEDARRAY_MAP) == sizeof(napi_typedarray_type) * ONNX_TENSOR_ELEMENT_DATA_TYPE_COUNT,
7676
"definition not matching");
@@ -98,7 +98,20 @@ static_assert(sizeof(DATA_TYPE_ID_TO_NAME_MAP) == sizeof(const char*) * ONNX_TEN
9898
"definition not matching");
9999

100100
const std::unordered_map<std::string, ONNXTensorElementDataType> DATA_TYPE_NAME_TO_ID_MAP = {
101-
{"float32", ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT}, {"uint8", ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8}, {"int8", ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8}, {"uint16", ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT16}, {"int16", ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16}, {"int32", ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32}, {"int64", ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64}, {"string", ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING}, {"bool", ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL}, {"float16", ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT16}, {"float64", ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE}, {"uint32", ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32}, {"uint64", ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64}};
101+
{"float32", ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT},
102+
{"uint8", ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8},
103+
{"int8", ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8},
104+
{"uint16", ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT16},
105+
{"int16", ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16},
106+
{"int32", ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32},
107+
{"int64", ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64},
108+
{"string", ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING},
109+
{"bool", ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL},
110+
{"float16", ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT16},
111+
{"float64", ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE},
112+
{"uint32", ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32},
113+
{"uint64", ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64},
114+
};
102115

103116
// currently only support tensor
104117
Ort::Value NapiValueToOrtValue(Napi::Env env, Napi::Value value, OrtMemoryInfo* cpu_memory_info, OrtMemoryInfo* webgpu_memory_info) {
@@ -181,7 +194,7 @@ Ort::Value NapiValueToOrtValue(Napi::Env env, Napi::Value value, OrtMemoryInfo*
181194
"Tensor.data must be a typed array for numeric tensor.");
182195

183196
auto tensorDataTypedArray = tensorDataValue.As<Napi::TypedArray>();
184-
auto typedArrayType = tensorDataValue.As<Napi::TypedArray>().TypedArrayType();
197+
std::underlying_type_t<napi_typedarray_type> typedArrayType = tensorDataValue.As<Napi::TypedArray>().TypedArrayType();
185198
ORT_NAPI_THROW_TYPEERROR_IF(DATA_TYPE_TYPEDARRAY_MAP[elemType] != typedArrayType, env,
186199
"Tensor.data must be a typed array (", DATA_TYPE_TYPEDARRAY_MAP[elemType], ") for ",
187200
tensorTypeString, " tensors, but got typed array (", typedArrayType, ").");
@@ -294,7 +307,7 @@ Napi::Value OrtValueToNapiValue(Napi::Env env, Ort::Value&& value) {
294307
}
295308
napi_value typedArrayData;
296309
napi_status status =
297-
napi_create_typedarray(env, DATA_TYPE_TYPEDARRAY_MAP[elemType], size, arrayBuffer, 0, &typedArrayData);
310+
napi_create_typedarray(env, (napi_typedarray_type)DATA_TYPE_TYPEDARRAY_MAP[elemType], size, arrayBuffer, 0, &typedArrayData);
298311
NAPI_THROW_IF_FAILED(env, status, Napi::Value);
299312

300313
// new Tensor(type, typedArrayData, dims)

0 commit comments

Comments
 (0)