From fb51f3b882c71539a90011df263f13915f0bfedd Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 9 Oct 2026 09:36:56 +0200 Subject: [PATCH 1/3] Translate sampler-less image reads and writes 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. --- lib/instns.cpp | 129 ++++++++++++++++++++++ lib/translator.h | 5 + tests/cases/image_read_write_builtins.spt | 52 +++++++++ 3 files changed, 186 insertions(+) create mode 100644 tests/cases/image_read_write_builtins.spt diff --git a/lib/instns.cpp b/lib/instns.cpp index 2d66352..af4b019 100644 --- a/lib/instns.cpp +++ b/lib/instns.cpp @@ -264,6 +264,124 @@ c::expr_ref translator_impl::dereference(uint32_t ptr, value(ptr))); } +bool translator_impl::emit_image_access(const Instruction &inst, + function_builder &fb, + c::expr_ref &result) { + bool is_read = inst.opcode() == spv::Op::OpImageRead; + unsigned first = is_read ? 2 : 0; + auto image = inst.GetSingleWordOperand(first); + auto coord = inst.GetSingleWordOperand(first + 1); + auto texel_tyid = + is_read ? inst.type_id() : type_id_for(inst.GetSingleWordOperand(2)); + unsigned operands_index = is_read ? 4 : 3; + uint32_t operands = inst.NumOperands() > operands_index + ? inst.GetSingleWordOperand(operands_index) + : 0; + + // OpenCL C takes 4-component texels: float4, half4, or (u)int4, which we + // represent as uint4. + auto *texel = type_for(texel_tyid)->AsVector(); + const Type *elem = texel ? texel->element_type() : nullptr; + std::string suffix; + if (texel && texel->element_count() == 4) { + if (elem->kind() == Type::Kind::kFloat) { + auto width = elem->AsFloat()->width(); + suffix = width == 32 ? "f" : width == 16 ? "h" : ""; + } else if (elem->kind() == Type::Kind::kInteger && + elem->AsInteger()->width() == 32) { + suffix = "i"; + } + } + if (suffix.empty()) { + std::cerr << "UNIMPLEMENTED image texel type " << texel_tyid << std::endl; + return false; + } + + // Nontemporal is only a hint. Other operands (Lod, offsets, ...) would + // change the meaning of the access. + operands &= ~SpvImageOperandsNontemporalMask; + const uint32_t extend = + SpvImageOperandsSignExtendMask | SpvImageOperandsZeroExtendMask; + if ((operands & ~extend) || (operands == extend) || + (operands && suffix != "i")) { + std::cerr << "UNIMPLEMENTED image operands " << operands << std::endl; + return false; + } + + // Sampler-less accesses take int, int2 or int4 coordinates; a 3D coordinate + // may come with only three components. + auto dim = type_for_val(image)->AsImage()->dim(); + unsigned dims = dim == spv::Dim::Dim1D ? 1 + : dim == spv::Dim::Dim2D ? 2 + : dim == spv::Dim::Dim3D ? 3 + : 0; + auto *coord_ty = type_for_val(coord); + auto *coord_vec = coord_ty->AsVector(); + auto *coord_elem = coord_vec ? coord_vec->element_type() : coord_ty; + unsigned ncoords = coord_vec ? coord_vec->element_count() : 1; + if (coord_elem->kind() != Type::Kind::kInteger || + !(ncoords == dims || (dims == 3 && ncoords == 4))) { + std::cerr << "UNIMPLEMENTED image coordinate type " << type_id_for(coord_ty) + << std::endl; + return false; + } + auto as_int = [](unsigned n, c::expr_ref e) { + return c::call(n == 1 ? "as_int" : "as_int" + std::to_string(n), + {std::move(e)}); + }; + auto coord_val = as_int(ncoords, value(coord)); + if (ncoords == 3) { + coord_val = c::vector_literal("int4", {coord_val, c::literal("0")}); + } + + auto access = [&](const std::string &variant) { + if (is_read) { + auto val = c::call("read_image" + variant, {value(image), coord_val}); + return variant == "i" ? c::call("as_uint4", {val}) : val; + } + auto val = value(inst.GetSingleWordOperand(2)); + if (variant == "i") { + val = c::call("as_int4", {val}); + } + return c::call("write_image" + variant, {value(image), coord_val, val}); + }; + auto emit = [&](c::expr_ref e) { + if (is_read) { + result = std::move(e); + } else { + fb.expression(e); + } + }; + + if (suffix != "i") { + emit(access(suffix)); + } else if (operands & SpvImageOperandsSignExtendMask) { + emit(access("i")); + } else if (operands & SpvImageOperandsZeroExtendMask) { + emit(access("ui")); + } else { + // SPIR-V integers are signless: unless the producer says otherwise, the + // image's channel type decides between sign- and zero-extension, and + // OpenCL C has separate builtins for either. + auto channel = m_name_allocator.allocate("_channel"); + fb.declare("int " + channel, + c::call("get_image_channel_data_type", {value(image)})); + auto is = [&](const char *type) { + return c::binary("==", c::name(channel), c::name(type)); + }; + auto is_signed = c::binary( + "||", c::binary("||", is("CLK_SIGNED_INT8"), is("CLK_SIGNED_INT16")), + is("CLK_SIGNED_INT32")); + if (is_read) { + result = c::ternary(is_signed, access("i"), access("ui")); + } else { + fb.statement("if (" + c::print(is_signed) + ") " + c::print(access("i")) + + "; else " + c::print(access("ui"))); + } + } + return true; +} + bool translator_impl::translate_instruction(const Instruction &inst, function_builder &fb) { auto opcode = inst.opcode(); @@ -492,6 +610,17 @@ bool translator_impl::translate_instruction(const Instruction &inst, // TODO check Lod break; } + case spv::Op::OpImageRead: + if (!emit_image_access(inst, fb, val)) { + return false; + } + break; + case spv::Op::OpImageWrite: + assign_result = false; + if (!emit_image_access(inst, fb, val)) { + return false; + } + break; case spv::Op::OpImageQuerySizeLod: { auto image = inst.GetSingleWordOperand(2); // auto lod = inst.GetSingleWordOperand(3); // FIXME validate diff --git a/lib/translator.h b/lib/translator.h index 1588901..7ecd381 100644 --- a/lib/translator.h +++ b/lib/translator.h @@ -171,6 +171,11 @@ struct translator_impl { bool emit_access_chain(const spvtools::opt::Instruction &inst, bool ptr_variant, c::expr_ref &result) const; + // A sampler-less image access, OpImageRead (whose value goes in `result`) or + // OpImageWrite (emitted into `fb`). + bool emit_image_access(const spvtools::opt::Instruction &inst, + function_builder &fb, c::expr_ref &result); + // Component `comp` of vector value `val`. c::expr_ref vector_component(uint32_t val, uint32_t comp) const; diff --git a/tests/cases/image_read_write_builtins.spt b/tests/cases/image_read_write_builtins.spt new file mode 100644 index 0000000..175a666 --- /dev/null +++ b/tests/cases/image_read_write_builtins.spt @@ -0,0 +1,52 @@ +; Sampler-less image reads and writes map to read_image*/write_image*, with +; integer coordinates (a 3D coordinate padded to int4). SPIR-V integers are +; signless, so an integer texel takes read_imagei/write_imagei with SignExtend, +; read_imageui/write_imageui with ZeroExtend, and without either the image's +; channel type decides at run time. The Nontemporal hint is dropped. +; RUN: %spirv2clc --asm --spirv-version=1.6 %s | %FileCheck %s +; RUN: %spirv2clc --asm --spirv-version=1.6 %s | %clang -c -cl-std=CL1.2 -x cl - -o /dev/null + OpCapability Kernel + OpCapability Addresses + OpCapability ImageBasic + OpMemoryModel Physical64 OpenCL + OpEntryPoint Kernel %k "k" + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %float = OpTypeFloat 32 + %v2uint = OpTypeVector %uint 2 + %v3uint = OpTypeVector %uint 3 + %v4uint = OpTypeVector %uint 4 + %v4float = OpTypeVector %float 4 + %ro = OpTypeImage %void 2D 0 0 0 0 Unknown ReadOnly + %ro3d = OpTypeImage %void 3D 0 0 0 0 Unknown ReadOnly + %wo = OpTypeImage %void 2D 0 0 0 0 Unknown WriteOnly + %uint_0 = OpConstant %uint 0 + %c2 = OpConstantComposite %v2uint %uint_0 %uint_0 + %c3 = OpConstantComposite %v3uint %uint_0 %uint_0 %uint_0 + %fnty = OpTypeFunction %void %ro %ro3d %wo + %k = OpFunction %void None %fnty + %r = OpFunctionParameter %ro + %r3d = OpFunctionParameter %ro3d + %w = OpFunctionParameter %wo + %bb = OpLabel + %f = OpImageRead %v4float %r3d %c3 + OpImageWrite %w %c2 %f + %si = OpImageRead %v4uint %r %c2 SignExtend|Nontemporal + %ui = OpImageRead %v4uint %r %c2 ZeroExtend + %i = OpImageRead %v4uint %r %c2 + OpImageWrite %w %c2 %si SignExtend + OpImageWrite %w %c2 %ui ZeroExtend + OpImageWrite %w %c2 %i + OpReturn + OpFunctionEnd +; CHECK: void kernel k(read_only image2d_t [[R:[^,]+]], read_only image3d_t [[R3D:[^,]+]], write_only image2d_t [[W:[^)]+]]) +; CHECK: float4 [[F:[^ ]+]] = read_imagef([[R3D]], (int4)(as_int3([[C3:.+]]), 0)); +; CHECK-NEXT: write_imagef([[W]], as_int2([[C2:.+]]), [[F]]); +; CHECK-NEXT: uint4 [[SI:[^ ]+]] = as_uint4(read_imagei([[R]], as_int2([[C2]]))); +; CHECK-NEXT: uint4 [[UI:[^ ]+]] = read_imageui([[R]], as_int2([[C2]])); +; CHECK-NEXT: int [[CH:[^ ]+]] = get_image_channel_data_type([[R]]); +; CHECK-NEXT: uint4 [[I:[^ ]+]] = [[CH]] == CLK_SIGNED_INT8 || [[CH]] == CLK_SIGNED_INT16 || [[CH]] == CLK_SIGNED_INT32 ? as_uint4(read_imagei([[R]], as_int2([[C2]]))) : read_imageui([[R]], as_int2([[C2]])); +; CHECK-NEXT: write_imagei([[W]], as_int2([[C2]]), as_int4([[SI]])); +; CHECK-NEXT: write_imageui([[W]], as_int2([[C2]]), [[UI]]); +; CHECK-NEXT: int [[CH2:[^ ]+]] = get_image_channel_data_type([[W]]); +; CHECK-NEXT: if ([[CH2]] == CLK_SIGNED_INT8 || [[CH2]] == CLK_SIGNED_INT16 || [[CH2]] == CLK_SIGNED_INT32) write_imagei([[W]], as_int2([[C2]]), as_int4([[I]])); else write_imageui([[W]], as_int2([[C2]]), [[I]]); From 3a0631f2be6665019ca9098ad3d5df70aa63df97 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 9 Oct 2026 09:41:51 +0200 Subject: [PATCH 2/3] Enable cl_khr_3d_image_writes for write-only 3D images below OpenCL C 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. --- lib/types.cpp | 7 +++++++ tests/cases/image_write_3d.spt | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 tests/cases/image_write_3d.spt diff --git a/lib/types.cpp b/lib/types.cpp index 80aa87f..4690ed5 100644 --- a/lib/types.cpp +++ b/lib/types.cpp @@ -447,6 +447,13 @@ bool translator_impl::translate_type(const Instruction &inst) { break; case SpvDim3D: typestr += "image3d_t"; + // Core in OpenCL C 2.0. Non-aggregate types are unique, so this is + // emitted at most once. + if (qual == SpvAccessQualifierWriteOnly && m_opencl_c_version < 200) { + m_out.extensions + << "#pragma OPENCL EXTENSION cl_khr_3d_image_writes : enable" + << std::endl; + } break; default: std::cerr << "UNIMPLEMENTED image dimensionality " << dim << std::endl; diff --git a/tests/cases/image_write_3d.spt b/tests/cases/image_write_3d.spt new file mode 100644 index 0000000..3a7352a --- /dev/null +++ b/tests/cases/image_write_3d.spt @@ -0,0 +1,29 @@ +; Writing 3D images takes cl_khr_3d_image_writes below OpenCL C 2.0, where it +; became core. +; RUN: %spirv2clc --asm %s | %FileCheck %s +; RUN: %spirv2clc --asm %s | %clang -c -cl-std=CL1.2 -x cl - -o /dev/null +; RUN: %spirv2clc --asm --cl-std=CL2.0 %s | %FileCheck %s --check-prefix=CL20 + OpCapability Kernel + OpCapability Addresses + OpCapability ImageBasic + OpMemoryModel Physical64 OpenCL + OpEntryPoint Kernel %k "k" + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %float = OpTypeFloat 32 + %v4uint = OpTypeVector %uint 4 + %v4float = OpTypeVector %float 4 + %wo = OpTypeImage %void 3D 0 0 0 0 Unknown WriteOnly + %uint_0 = OpConstant %uint 0 + %c4 = OpConstantComposite %v4uint %uint_0 %uint_0 %uint_0 %uint_0 + %t = OpConstantNull %v4float + %fnty = OpTypeFunction %void %wo + %k = OpFunction %void None %fnty + %w = OpFunctionParameter %wo + %bb = OpLabel + OpImageWrite %w %c4 %t + OpReturn + OpFunctionEnd +; CHECK: #pragma OPENCL EXTENSION cl_khr_3d_image_writes : enable +; CHECK: write_imagef({{.*}}, as_int4( +; CL20-NOT: cl_khr_3d_image_writes From fadb27834c03ffb28ddb44283c574d517410c64e Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 9 Oct 2026 09:36:56 +0200 Subject: [PATCH 3/3] Accept the ImageReadWrite capability from OpenCL C 2.0 on 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. --- lib/translator.cpp | 11 ++++++ lib/types.cpp | 8 +++++ tests/cases/image_read_write.spt | 35 +++++++++++++++++++ .../cases/image_read_write_no_capability.spt | 19 ++++++++++ 4 files changed, 73 insertions(+) create mode 100644 tests/cases/image_read_write.spt create mode 100644 tests/cases/image_read_write_no_capability.spt diff --git a/lib/translator.cpp b/lib/translator.cpp index 5367c97..0af9abd 100644 --- a/lib/translator.cpp +++ b/lib/translator.cpp @@ -195,6 +195,17 @@ bool translator_impl::translate_capabilities() { return false; } break; + case SpvCapabilityImageReadWrite: + // read_write images are core in OpenCL C 2.0 (an optional feature in 3.0, + // __opencl_c_read_write_images); see also the ReadWrite qualifier in + // translate_type (types.cpp). + if (m_opencl_c_version < 200) { + std::cerr << "UNIMPLEMENTED: read_write images require OpenCL C 2.0 " + "(targeting " + << opencl_c_version_str(m_opencl_c_version) << ").\n"; + return false; + } + break; case SpvCapabilityGroups: case SpvCapabilityGroupNonUniform: case SpvCapabilityGroupNonUniformShuffle: diff --git a/lib/types.cpp b/lib/types.cpp index 4690ed5..55a415f 100644 --- a/lib/types.cpp +++ b/lib/types.cpp @@ -428,6 +428,14 @@ bool translator_impl::translate_type(const Instruction &inst) { typestr = "write_only"; break; case SpvAccessQualifierReadWrite: + // Checked here as well as at the ImageReadWrite capability, since the + // ReadWrite qualifier only requires Kernel. + if (m_opencl_c_version < 200) { + std::cerr << "UNIMPLEMENTED: read_write images require OpenCL C 2.0 " + "(targeting " + << opencl_c_version_str(m_opencl_c_version) << ").\n"; + return false; + } typestr = "read_write"; m_read_write_images = true; break; diff --git a/tests/cases/image_read_write.spt b/tests/cases/image_read_write.spt new file mode 100644 index 0000000..96aad38 --- /dev/null +++ b/tests/cases/image_read_write.spt @@ -0,0 +1,35 @@ +; A kernel that reads and writes the same read_write image (OpenCL C 2.0, an +; optional feature in 3.0). Producers declare ImageReadWrite for it; below 2.0 +; there is no read_write qualifier, so translation must fail there. +; RUN: not %spirv2clc --asm --spirv-version=1.4 --cl-std=CL1.2 %s +; RUN: %spirv2clc --asm --spirv-version=1.4 --cl-std=CL2.0 %s | %FileCheck %s +; RUN: %spirv2clc --asm --spirv-version=1.4 --cl-std=CL2.0 %s | %clang -c -cl-std=CL2.0 -x cl - -o /dev/null +; RUN: %spirv2clc --asm --spirv-version=1.4 --cl-std=CL3.0 %s | %clang -c -cl-std=CL3.0 -cl-ext=+__opencl_c_images,+__opencl_c_read_write_images -x cl - -o /dev/null + OpCapability Kernel + OpCapability Addresses + OpCapability ImageBasic + OpCapability ImageReadWrite + OpMemoryModel Physical64 OpenCL + OpEntryPoint Kernel %k "k" + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %float = OpTypeFloat 32 + %v2uint = OpTypeVector %uint 2 + %v4float = OpTypeVector %float 4 + %img = OpTypeImage %void 2D 0 0 0 0 Unknown ReadWrite + %uint_0 = OpConstant %uint 0 + %float_1 = OpConstant %float 1 + %coord = OpConstantComposite %v2uint %uint_0 %uint_0 + %one = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1 + %fnty = OpTypeFunction %void %img + %k = OpFunction %void None %fnty + %i = OpFunctionParameter %img + %bb = OpLabel + %v = OpImageRead %v4float %i %coord + %w = OpFAdd %v4float %v %one + OpImageWrite %i %coord %w + OpReturn + OpFunctionEnd +; CHECK: void kernel k(read_write image2d_t [[IMG:[^)]+]]) +; CHECK: read_imagef([[IMG]], +; CHECK: write_imagef([[IMG]], diff --git a/tests/cases/image_read_write_no_capability.spt b/tests/cases/image_read_write_no_capability.spt new file mode 100644 index 0000000..c2ffee4 --- /dev/null +++ b/tests/cases/image_read_write_no_capability.spt @@ -0,0 +1,19 @@ +; The ReadWrite access qualifier only requires Kernel, so a module may use a +; read_write image without declaring ImageReadWrite. Below OpenCL C 2.0 the +; image type itself must then be refused. +; RUN: not %spirv2clc --asm --spirv-version=1.4 --cl-std=CL1.2 %s 2>&1 | %FileCheck %s +; RUN: %spirv2clc --asm --spirv-version=1.4 --cl-std=CL2.0 %s | %clang -c -cl-std=CL2.0 -x cl - -o /dev/null + OpCapability Kernel + OpCapability Addresses + OpCapability ImageBasic + OpMemoryModel Physical64 OpenCL + OpEntryPoint Kernel %k "k" + %void = OpTypeVoid + %img = OpTypeImage %void 2D 0 0 0 0 Unknown ReadWrite + %fnty = OpTypeFunction %void %img + %k = OpFunction %void None %fnty + %i = OpFunctionParameter %img + %bb = OpLabel + OpReturn + OpFunctionEnd +; CHECK: read_write images require OpenCL C 2.0