Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions lib/instns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions lib/translator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions lib/translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
15 changes: 15 additions & 0 deletions lib/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -447,6 +455,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;
Expand Down
35 changes: 35 additions & 0 deletions tests/cases/image_read_write.spt
Original file line number Diff line number Diff line change
@@ -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]],
52 changes: 52 additions & 0 deletions tests/cases/image_read_write_builtins.spt
Original file line number Diff line number Diff line change
@@ -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]]);
19 changes: 19 additions & 0 deletions tests/cases/image_read_write_no_capability.spt
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions tests/cases/image_write_3d.spt
Original file line number Diff line number Diff line change
@@ -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
Loading