Skip to content

Commit f0f42ea

Browse files
committed
fix getImageRegionHelper() in l0 adapter
1 parent 17d7288 commit f0f42ea

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

unified-runtime/source/adapters/level_zero/image_common.cpp

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -731,33 +731,25 @@ ur_result_t getImageRegionHelper(ze_image_desc_t ZeImageDesc,
731731
UR_ASSERT(Origin, UR_RESULT_ERROR_INVALID_VALUE);
732732
UR_ASSERT(Region, UR_RESULT_ERROR_INVALID_VALUE);
733733

734+
// Runtime validation of Origin values based on image type
734735
if (ZeImageDesc.type == ZE_IMAGE_TYPE_1D) {
735-
Region->height = 1;
736-
Region->depth = 1;
736+
if (Origin->y != 0 || Origin->z != 0) {
737+
return UR_RESULT_ERROR_INVALID_VALUE;
738+
}
739+
} else if (ZeImageDesc.type == ZE_IMAGE_TYPE_1DARRAY) {
740+
if (Origin->y != 0) {
741+
return UR_RESULT_ERROR_INVALID_VALUE;
742+
}
737743
} else if (ZeImageDesc.type == ZE_IMAGE_TYPE_2D) {
738-
Region->depth = 1;
744+
if (Origin->z != 0) {
745+
return UR_RESULT_ERROR_INVALID_VALUE;
746+
}
739747
}
740748

741-
#ifndef NDEBUG
742-
UR_ASSERT((ZeImageDesc.type == ZE_IMAGE_TYPE_1D && Origin->y == 0 &&
743-
Origin->z == 0) ||
744-
(ZeImageDesc.type == ZE_IMAGE_TYPE_1DARRAY && Origin->y == 0) ||
745-
(ZeImageDesc.type == ZE_IMAGE_TYPE_2D && Origin->z == 0) ||
746-
(ZeImageDesc.type == ZE_IMAGE_TYPE_2DARRAY) ||
747-
(ZeImageDesc.type == ZE_IMAGE_TYPE_3D),
748-
UR_RESULT_ERROR_INVALID_VALUE);
749-
750-
UR_ASSERT(Region->width && Region->height && Region->depth,
751-
UR_RESULT_ERROR_INVALID_VALUE);
752-
UR_ASSERT(
753-
(ZeImageDesc.type == ZE_IMAGE_TYPE_1D && Region->height == 1 &&
754-
Region->depth == 1) ||
755-
(ZeImageDesc.type == ZE_IMAGE_TYPE_1DARRAY && Region->height == 1) ||
756-
(ZeImageDesc.type == ZE_IMAGE_TYPE_2D && Region->depth == 1) ||
757-
(ZeImageDesc.type == ZE_IMAGE_TYPE_2DARRAY) ||
758-
(ZeImageDesc.type == ZE_IMAGE_TYPE_3D),
759-
UR_RESULT_ERROR_INVALID_VALUE);
760-
#endif // !NDEBUG
749+
// Verify Region width is non-zero
750+
if (Region->width == 0) {
751+
return UR_RESULT_ERROR_INVALID_VALUE;
752+
}
761753

762754
uint32_t OriginX = ur_cast<uint32_t>(Origin->x);
763755
uint32_t OriginY = ur_cast<uint32_t>(Origin->y);
@@ -767,25 +759,39 @@ ur_result_t getImageRegionHelper(ze_image_desc_t ZeImageDesc,
767759
uint32_t Height = ur_cast<uint32_t>(Region->height);
768760
uint32_t Depth = ur_cast<uint32_t>(Region->depth);
769761

762+
// Normalize Region dimensions based on image type
770763
if (ZeImageDesc.type == ZE_IMAGE_TYPE_1D) {
771-
OriginY = 0;
772-
OriginZ = 0;
773764
Height = 1;
774765
Depth = 1;
775766
} else if (ZeImageDesc.type == ZE_IMAGE_TYPE_1DARRAY) {
776767
// UR uses z for the array layer when describing a 1D array subregion.
777768
// Level Zero expects the array layer in originY/height for 1D arrays.
778769
OriginY = ur_cast<uint32_t>(Origin->z);
779770
OriginZ = 0;
780-
Height = ur_cast<uint32_t>(Region->depth);
771+
Height = ur_cast<uint32_t>(Region->depth); // Array layer count
781772
Depth = 1;
773+
fprintf(stderr, "[DEBUG] 1DARRAY normalization: OriginY=%u (from z), OriginZ=0, Height=%u (array layers), Depth=1\n",
774+
OriginY, Height);
782775
} else if (ZeImageDesc.type == ZE_IMAGE_TYPE_2D) {
783-
OriginZ = 0;
784776
Depth = 1;
785777
}
786778

787779
ZeRegion = {OriginX, OriginY, OriginZ, Width, Height, Depth};
788780

781+
#ifndef NDEBUG
782+
// Post-normalization assertions
783+
UR_ASSERT((ZeImageDesc.type == ZE_IMAGE_TYPE_1D && Origin->y == 0 &&
784+
Origin->z == 0) ||
785+
(ZeImageDesc.type == ZE_IMAGE_TYPE_1DARRAY && Origin->y == 0) ||
786+
(ZeImageDesc.type == ZE_IMAGE_TYPE_2D && Origin->z == 0) ||
787+
(ZeImageDesc.type == ZE_IMAGE_TYPE_2DARRAY) ||
788+
(ZeImageDesc.type == ZE_IMAGE_TYPE_3D),
789+
UR_RESULT_ERROR_INVALID_VALUE);
790+
791+
UR_ASSERT(ZeRegion.width && ZeRegion.height && ZeRegion.depth,
792+
UR_RESULT_ERROR_INVALID_VALUE);
793+
#endif // !NDEBUG
794+
789795
return UR_RESULT_SUCCESS;
790796
}
791797

0 commit comments

Comments
 (0)