Skip to content

Support read-write images - #21

Merged
maleadt merged 3 commits into
mainfrom
tb/read-write-images
Oct 9, 2026
Merged

maleadt merged 3 commits into
mainfrom
tb/read-write-images

Conversation

@maleadt

@maleadt maleadt commented Oct 9, 2026

Copy link
Copy Markdown
Member

Kernels taking a read_write image couldn't be translated (#13). Accepting the ImageReadWrite capability wasn't enough on its own: OpImageRead and OpImageWrite, the only way to use such an image, weren't implemented either, so the issue's kernel then failed on those.

  • Image reads and writes: OpImageRead/OpImageWrite map to read_image*/write_image*, with the builtin picked from the texel's component type and integer coordinates. SPIR-V integers are signless, and clang's SPIR-V backend emits neither SignExtend nor ZeroExtend; without them the SPIR-V environment lets the image's channel type decide, whereas OpenCL C has separate _i/_ui builtins and leaves mismatches undefined. So integer texels dispatch on get_image_channel_data_type, unless an operand states the signedness. Other image operands than the Nontemporal hint are refused rather than dropped.
  • 3D image writes take cl_khr_3d_image_writes below OpenCL C 2.0, whose pragma is now emitted with the write-only 3D image type.
  • Read-write images: ImageReadWrite is accepted from OpenCL C 2.0 on and refused below, as is the ReadWrite image type itself, which only requires Kernel and would otherwise produce a read_write qualifier that 1.2 compilers reject.

The issue's kernel, and an integer read-modify-write kernel on signed and unsigned 8-bit images, produce correct results on PoCL and Intel's CPU runtime (NVIDIA doesn't support read-write images). On those and on NVIDIA, the _i and _ui builtins in fact behave identically, following the channel type, so the dispatch is about conforming to OpenCL C rather than fixing an observed miscompile.

Fixes #13. Related problems found along the way: #19, #20.

OpImageRead and OpImageWrite map to read_image*/write_image*, picking the
builtin from the texel's component type, with integer coordinates (a 3D
coordinate padded to int4). Other image operands than the Nontemporal hint
and the signedness of integer texels are refused rather than dropped.

SPIR-V integers are signless, and clang's SPIR-V backend emits neither
SignExtend nor ZeroExtend. Without them, the SPIR-V environment has the
image's channel type decide the extension, while OpenCL C has a builtin for
either and leaves mismatches undefined. Dispatch on
get_image_channel_data_type then, and honour the operands when present.
… 2.0

Writing 3D images is an extension before OpenCL C 2.0, where it became
core. Now that image writes are translated, a module writing one at 1.2
otherwise produces source compilers without the extension pragma reject.
Producers declare it for read_write images, which are core in OpenCL C 2.0
and optional in 3.0. Below 2.0 OpenCL C has no read_write qualifier, so
refuse the capability there, and the ReadWrite image type too, since it
only requires the Kernel capability.

Fixes #13.
@maleadt
maleadt merged commit e7382d6 into main Oct 9, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Modules with read-write images fail on the ImageReadWrite capability

1 participant