Skip to content

Commit 40c0554

Browse files
authored
[SYCL] Fix 2D slicepitch info (#18960)
Make `slicePitch` equal to image size and `numSlices` 1 for a 2D image. This was incorrect but wasn't failing as many backends ignore `Pitch[1]` for a 2D image.
1 parent 789555b commit 40c0554

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed

sycl/source/detail/image_impl.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,10 @@ class image_impl final : public SYCLMemObjT {
7070

7171
void setPitches(const range<2> &Pitch) {
7272
MRowPitch = Pitch[0];
73-
MSlicePitch =
74-
(MDimensions == 3) ? Pitch[1] : MRowPitch; // Dimensions will be 2/3.
75-
// NumSlices is depth when dim==3, and height when dim==2.
73+
MSlicePitch = (MDimensions == 3) ? Pitch[1] : MRowPitch * MRange[1];
74+
// NumSlices is depth when dim==3, and 1 when dim==2.
7675
size_t NumSlices =
77-
(MDimensions == 3) ? MRange[2] : MRange[1]; // Dimensions will be 2/3.
76+
(MDimensions == 3) ? MRange[2] : 1; // Dimensions will be 2/3.
7877

7978
BaseT::MSizeInBytes = MSlicePitch * NumSlices;
8079
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// REQUIRES: aspect-ext_intel_legacy_image, cpu
2+
//
3+
// This test ensures that the correct pitch is used for cases when
4+
// UR_MEM_FLAG_USE_HOST_POINTER is passed to the backend.
5+
// UR_MEM_FLAG_USE_HOST_POINTER is used for contexts where CPU virtual memory
6+
// is accessible on device, so restrict this test to CPU platforms, although
7+
// there may be other additional platforms that support this behavior.
8+
//
9+
// RUN: %{build} -o %t.out
10+
// RUN: env SYCL_UR_TRACE=-1 %{run} %t.out | FileCheck %s
11+
//
12+
//==------------------- image_trace.cpp - SYCL image trace test ------------==//
13+
//
14+
// Ensures the correct params are being passed to urMemImageCreate
15+
//
16+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
17+
// See https://llvm.org/LICENSE.txt for license information.
18+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
19+
//
20+
//===----------------------------------------------------------------------===//
21+
22+
#include <sycl/accessor_image.hpp>
23+
#include <sycl/builtins.hpp>
24+
#include <sycl/detail/core.hpp>
25+
#include <sycl/image.hpp>
26+
27+
#include <iostream>
28+
#include <vector>
29+
30+
#include "../../helpers.hpp"
31+
32+
int main() {
33+
const sycl::image_channel_order ChanOrder = sycl::image_channel_order::rgba;
34+
const sycl::image_channel_type ChanType = sycl::image_channel_type::fp32;
35+
36+
constexpr auto SYCLRead = sycl::access::mode::read;
37+
constexpr auto SYCLWrite = sycl::access::mode::write;
38+
39+
const sycl::range<2> Img1Size(4, 4);
40+
const sycl::range<2> Img2Size(4, 4);
41+
42+
std::vector<sycl::float4> Img1HostData(Img1Size.size(), {1, 2, 3, 4});
43+
std::vector<sycl::float4> Img2HostData(Img2Size.size(), {0, 0, 0, 0});
44+
45+
{
46+
sycl::image<2> Img1(Img1HostData.data(), ChanOrder, ChanType, Img1Size);
47+
sycl::image<2> Img2(Img2HostData.data(), ChanOrder, ChanType, Img2Size);
48+
49+
TestQueue Q{sycl::default_selector_v};
50+
Q.submit([&](sycl::handler &CGH) {
51+
auto Img1Acc = Img1.get_access<sycl::float4, SYCLRead>(CGH);
52+
auto Img2Acc = Img2.get_access<sycl::float4, SYCLWrite>(CGH);
53+
54+
CGH.parallel_for<class ImgCopy>(Img1Size, [=](sycl::item<2> Item) {
55+
sycl::float4 Data = Img1Acc.read(sycl::int2{Item[0], Item[1]});
56+
Img2Acc.write(sycl::int2{Item[0], Item[1]}, Data);
57+
});
58+
// CHECK: <--- urMemImageCreate
59+
// CHECK-SAME: UR_MEM_FLAG_USE_HOST_POINTER
60+
// CHECK-SAME: .width = 4, .height = 4, .depth = 1, .arraySize = 0, .rowPitch = 64, .slicePitch = 256
61+
// CHECK: <--- urMemImageCreate
62+
// CHECK-SAME: UR_MEM_FLAG_USE_HOST_POINTER
63+
// CHECK-SAME: .width = 4, .height = 4, .depth = 1, .arraySize = 0, .rowPitch = 64, .slicePitch = 256
64+
});
65+
}
66+
67+
for (int X = 0; X < Img2Size[0]; ++X)
68+
for (int Y = 0; Y < Img2Size[1]; ++Y) {
69+
sycl::float4 Vec1 = Img1HostData[X * Img1Size[1] + Y];
70+
sycl::float4 Vec2 = Img2HostData[X * Img2Size[1] + Y];
71+
72+
if (sycl::any(sycl::isnotequal(Vec1, Vec2))) {
73+
std::cerr << "Failed" << std::endl;
74+
std::cerr << "Element [ " << X << ", " << Y << " ]" << std::endl;
75+
std::cerr << "Expected: " << printableVec(Vec1) << std::endl;
76+
std::cerr << " Got : " << printableVec(Vec2) << std::endl;
77+
return 1;
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)