Skip to content

Commit a14c091

Browse files
authored
[SYCL][Bindless] Update spirv read/fetch from sampled image and sampled image array (intel#14493)
For spirv path, call __invoke__ImageReadLod to generate image builtin represented as ImageSampleExplicitLod instruction in SPIRV that can be consumed by Intel Graphics Compiler.
1 parent 3800814 commit a14c091

File tree

2 files changed

+62
-13
lines changed

2 files changed

+62
-13
lines changed

sycl/include/sycl/ext/oneapi/bindless_images.hpp

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,11 @@ template <int NDims>
812812
using OCLImageArrayTyWrite = typename sycl::detail::opencl_image_type<
813813
NDims, sycl::access::mode::write, sycl::access::target::image_array>::type;
814814

815+
template <int NDims>
816+
using OCLSampledImageArrayTyRead =
817+
typename sycl::detail::sampled_opencl_image_type<
818+
detail::OCLImageArrayTyRead<NDims>>::type;
819+
815820
// Macros are required because it is not legal for a function to return
816821
// a variable of type 'opencl_image_type'.
817822
#if defined(__SPIR__)
@@ -823,34 +828,64 @@ using OCLImageArrayTyWrite = typename sycl::detail::opencl_image_type<
823828
typename sycl::detail::sampled_opencl_image_type< \
824829
detail::OCLImageTyRead<NDims>>::type>(raw_handle)
825830

831+
#define CONVERT_HANDLE_TO_SAMPLED_IMAGE_ARRAY(raw_handle, NDims) \
832+
__spirv_ConvertHandleToSampledImageINTEL< \
833+
typename sycl::detail::sampled_opencl_image_type< \
834+
detail::OCLImageArrayTyRead<NDims>>::type>(raw_handle)
835+
826836
#define FETCH_UNSAMPLED_IMAGE(DataT, raw_handle, coords) \
827837
__invoke__ImageRead<DataT>(raw_handle, coords)
828838

829839
#define FETCH_SAMPLED_IMAGE(DataT, raw_handle, coords) \
830-
__invoke__ImageRead<DataT>(raw_handle, coords)
840+
__invoke__ImageReadLod<DataT>(raw_handle, coords, 0.f)
841+
842+
#define SAMPLE_IMAGE_READ(DataT, raw_handle, coords) \
843+
__invoke__ImageReadLod<DataT>(raw_handle, coords, 0.f)
831844

832845
#define FETCH_IMAGE_ARRAY(DataT, raw_handle, coords, arrayLayer, coordsLayer) \
833846
__invoke__ImageRead<DataT>(raw_handle, coordsLayer)
834847

835848
#define WRITE_IMAGE_ARRAY(raw_handle, coords, arrayLayer, coordsLayer, color) \
836849
__invoke__ImageWrite(raw_handle, coordsLayer, color)
837850

851+
#define FETCH_SAMPLED_IMAGE_ARRAY(DataT, raw_handle, coords, arrayLayer, \
852+
coordsLayer) \
853+
__invoke__ImageReadLod<DataT>(raw_handle, coordsLayer, 0.f)
854+
855+
#define READ_SAMPLED_IMAGE_ARRAY(DataT, raw_handle, coords, arrayLayer, \
856+
coordsLayer) \
857+
__invoke__ImageReadLod<DataT>(raw_handle, coordsLayer, 0.f)
858+
838859
#else
839860
#define CONVERT_HANDLE_TO_IMAGE(raw_handle, ImageType) raw_handle
840861

841862
#define CONVERT_HANDLE_TO_SAMPLED_IMAGE(raw_handle, NDims) raw_handle
842863

864+
#define CONVERT_HANDLE_TO_SAMPLED_IMAGE_ARRAY(raw_handle, NDims) raw_handle
865+
843866
#define FETCH_UNSAMPLED_IMAGE(DataT, raw_handle, coords) \
844867
__invoke__ImageFetch<DataT>(raw_handle, coords)
845868

846869
#define FETCH_SAMPLED_IMAGE(DataT, raw_handle, coords) \
847870
__invoke__SampledImageFetch<DataT>(raw_handle, coords)
848871

872+
#define SAMPLE_IMAGE_READ(DataT, raw_handle, coords) \
873+
__invoke__ImageRead<DataT>(raw_handle, coords)
874+
849875
#define FETCH_IMAGE_ARRAY(DataT, raw_handle, coords, arrayLayer, coordsLayer) \
850876
__invoke__ImageArrayFetch<DataT>(raw_handle, coords, arrayLayer)
851877

852878
#define WRITE_IMAGE_ARRAY(raw_handle, coords, arrayLayer, coordsLayer, color) \
853879
__invoke__ImageArrayWrite(raw_handle, coords, arrayLayer, color)
880+
881+
#define FETCH_SAMPLED_IMAGE_ARRAY(DataT, raw_handle, coords, arrayLayer, \
882+
coordsLayer) \
883+
__invoke__SampledImageArrayFetch<DataT>(raw_handle, coords, arrayLayer)
884+
885+
#define READ_SAMPLED_IMAGE_ARRAY(DataT, raw_handle, coords, arrayLayer, \
886+
coordsLayer) \
887+
__invoke__ImageArrayRead<DataT>(raw_handle, coords, arrayLayer)
888+
854889
#endif
855890

856891
#endif // __SYCL_DEVICE_ONLY__
@@ -1030,11 +1065,13 @@ DataT sample_image(const sampled_image_handle &imageHandle [[maybe_unused]],
10301065

10311066
#ifdef __SYCL_DEVICE_ONLY__
10321067
if constexpr (detail::is_recognized_standard_type<DataT>()) {
1033-
return __invoke__ImageRead<DataT>(
1068+
return SAMPLE_IMAGE_READ(
1069+
DataT,
10341070
CONVERT_HANDLE_TO_SAMPLED_IMAGE(imageHandle.raw_handle, coordSize),
10351071
coords);
10361072
} else {
1037-
return sycl::bit_cast<DataT>(__invoke__ImageRead<HintT>(
1073+
return sycl::bit_cast<DataT>(SAMPLE_IMAGE_READ(
1074+
HintT,
10381075
CONVERT_HANDLE_TO_SAMPLED_IMAGE(imageHandle.raw_handle, coordSize),
10391076
coords));
10401077
}
@@ -1410,17 +1447,23 @@ DataT fetch_image_array(const sampled_image_handle &imageHandle
14101447
"and 2D images respectively.");
14111448

14121449
#ifdef __SYCL_DEVICE_ONLY__
1450+
sycl::vec<int, coordSize + 1> coordsLayer{coords, arrayLayer};
14131451
if constexpr (detail::is_recognized_standard_type<DataT>()) {
1414-
return __invoke__SampledImageArrayFetch<DataT>(imageHandle.raw_handle,
1415-
coords, arrayLayer);
1452+
return FETCH_SAMPLED_IMAGE_ARRAY(DataT,
1453+
CONVERT_HANDLE_TO_SAMPLED_IMAGE_ARRAY(
1454+
imageHandle.raw_handle, coordSize),
1455+
coords, arrayLayer, coordsLayer);
14161456
} else {
14171457
static_assert(sizeof(HintT) == sizeof(DataT),
14181458
"When trying to fetch a user-defined type, HintT must be of "
14191459
"the same size as the user-defined DataT.");
14201460
static_assert(detail::is_recognized_standard_type<HintT>(),
14211461
"HintT must always be a recognized standard type");
1422-
return sycl::bit_cast<DataT>(__invoke__SampledImageArrayFetch<HintT>(
1423-
imageHandle.raw_handle, coords, arrayLayer));
1462+
return sycl::bit_cast<DataT>(
1463+
FETCH_SAMPLED_IMAGE_ARRAY(HintT,
1464+
CONVERT_HANDLE_TO_SAMPLED_IMAGE_ARRAY(
1465+
imageHandle.raw_handle, coordSize),
1466+
coords, arrayLayer, coordsLayer));
14241467
}
14251468
#else
14261469
assert(false); // Bindless images not yet implemented on host.
@@ -1454,17 +1497,23 @@ DataT sample_image_array(const sampled_image_handle &imageHandle
14541497
"and 2D images respectively.");
14551498

14561499
#ifdef __SYCL_DEVICE_ONLY__
1500+
sycl::vec<float, coordSize + 1> coordsLayer{coords, arrayLayer};
14571501
if constexpr (detail::is_recognized_standard_type<DataT>()) {
1458-
return __invoke__ImageArrayRead<DataT>(imageHandle.raw_handle, coords,
1459-
arrayLayer);
1502+
return READ_SAMPLED_IMAGE_ARRAY(DataT,
1503+
CONVERT_HANDLE_TO_SAMPLED_IMAGE_ARRAY(
1504+
imageHandle.raw_handle, coordSize),
1505+
coords, arrayLayer, coordsLayer);
14601506
} else {
14611507
static_assert(sizeof(HintT) == sizeof(DataT),
14621508
"When trying to fetch a user-defined type, HintT must be of "
14631509
"the same size as the user-defined DataT.");
14641510
static_assert(detail::is_recognized_standard_type<HintT>(),
14651511
"HintT must always be a recognized standard type");
1466-
return sycl::bit_cast<DataT>(__invoke__ImageArrayRead<HintT>(
1467-
imageHandle.raw_handle, coords, arrayLayer));
1512+
return sycl::bit_cast<DataT>(
1513+
READ_SAMPLED_IMAGE_ARRAY(HintT,
1514+
CONVERT_HANDLE_TO_SAMPLED_IMAGE_ARRAY(
1515+
imageHandle.raw_handle, coordSize),
1516+
coords, arrayLayer, coordsLayer));
14681517
}
14691518
#else
14701519
assert(false); // Bindless images not yet implemented on host.

sycl/test/check_device_code/extensions/bindless_images.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// RUN: %clangxx -S -emit-llvm -fsycl -fsycl-device-only -fsycl-targets=spir64-unknown-unknown %s -o - | FileCheck %s --check-prefix=CHECK-LLVM
55

66
// CHECK-LLVM: tail call spir_func noundef <4 x float> @_Z17__spirv_ImageReadIDv4
7-
// CHECK-LLVM: tail call spir_func noundef <4 x float> @_Z17__spirv_ImageReadIDv4
7+
// CHECK-LLVM: tail call spir_func noundef <4 x float> @_Z30__spirv_ImageSampleExplicitLod
88
// CHECK-LLVM: tail call spir_func void @_Z18__spirv_ImageWriteI14
99

1010
// RUN: %clangxx -fsycl -fsycl-device-only -fsycl-targets=spir64-unknown-unknown %s -o %t.out
@@ -50,7 +50,7 @@
5050

5151
// Read sampled image
5252
// Arguments: Result Type, Result, Image, Coords
53-
// CHECK-SPIRV-NEXT: ImageRead [[PIXELTYPE]] {{[0-9]+}} [[SAMPIMAGEVAR]] {{[0-9]+}}
53+
// CHECK-SPIRV-NEXT: ImageSampleExplicitLod [[PIXELTYPE]] {{[0-9]+}} [[SAMPIMAGEVAR]] {{[0-9]+}}
5454

5555
// Convert handle to SPIR-V image
5656
// Arguments: Result Type, Result, Handle

0 commit comments

Comments
 (0)