KhronosGroup / KhronosGroup/OpenCL-Headers

Signed and Unsigned Mismatches

Open
#55 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
766
Forks
273
Avg merge
2d 2h
Merged PRs (30d)
3

Description

One of our engineers reported an issue in the OpenCL headers recently regarding signed and unsigned mismatch compiler warnings in some cases with modern compilers at higher warning levels. The crux of the issue is that many of the OpenCL types are unsigned, e.g.:

```cpp
typedef cl_uint cl_command_type;
```

But, the corresponding enums are signed, e.g.:

```cpp
/* cl_command_type */
#define CL_COMMAND_NDRANGE_KERNEL 0x11F0
#define CL_COMMAND_TASK 0x11F1
#define CL_COMMAND_NATIVE_KERNEL 0x11F2
...
```

This can lead to an error in code like the following (see godbolt example [here](https://godbolt.org/z/-cDoY6)):

```cpp
#define MY_VALUE 0x10

template
bool is_eq(const T1& lhs, const T2& rhs) {
if (lhs == rhs) {
return true;
}
return false;
}

void f() {
unsigned x = 2;
is_eq(x, MY_VALUE);
}
```

This seems like a rather narrow use-case, but it's actually fairly common in googletest unit tests:

```cpp
const cl_command_type command_type;
clGetEventInfo(event, CL_EVENT_COMMAND_TYPE, sizeof(command_type), &command_type, nullptr);
EXPECT_EQ(CL_COMMAND_TYPE_NDRANGE_KERNEL, command_type);
```

Should we define most OpenCL enums - all but error codes I think? - as explicitly unsigned? I don't think this will break existing code, and it will fix this compile warning:

```cpp
/* cl_command_type */
#define CL_COMMAND_NDRANGE_KERNEL 0x11F0u
#define CL_COMMAND_TASK 0x11F1u
#define CL_COMMAND_NATIVE_KERNEL 0x11F2u
...
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reviewing the OpenCL header definitions referenced in the issue and reproducing the linked Godbolt example with modern warning levels. Compare the signedness of the affected constants and types, then check compatibility implications across the proposed enum-like definitions. Done means the project has a decided scope and the relevant mismatch warnings are addressed without breaking existing header use.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cpp
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.