Skip to content

Commit 526c42e

Browse files
committed
[OpenCL] Hide sampler-less read_image builtins before CL1.2
Ensure sampler-less image read functions are not available with `-fdeclare-opencl-builtins` before OpenCL 1.2.
1 parent 6617529 commit 526c42e

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

clang/lib/Sema/OpenCLBuiltins.td

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,24 +1092,26 @@ foreach coordTy = [Int, Float] in {
10921092
}
10931093

10941094
// --- Table 23: Sampler-less Read Functions ---
1095-
foreach aQual = ["RO", "RW"] in {
1096-
foreach imgTy = [Image2d, Image1dArray] in {
1097-
def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>], Attr.Pure>;
1098-
def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>], Attr.Pure>;
1099-
def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>], Attr.Pure>;
1100-
}
1101-
foreach imgTy = [Image3d, Image2dArray] in {
1102-
def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>], Attr.Pure>;
1103-
def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>], Attr.Pure>;
1104-
def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>], Attr.Pure>;
1105-
}
1106-
foreach imgTy = [Image1d, Image1dBuffer] in {
1107-
def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, aQual>, Int], Attr.Pure>;
1108-
def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, aQual>, Int], Attr.Pure>;
1109-
def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Int], Attr.Pure>;
1095+
let MinVersion = CL12 in {
1096+
foreach aQual = ["RO", "RW"] in {
1097+
foreach imgTy = [Image2d, Image1dArray] in {
1098+
def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>], Attr.Pure>;
1099+
def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>], Attr.Pure>;
1100+
def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>], Attr.Pure>;
1101+
}
1102+
foreach imgTy = [Image3d, Image2dArray] in {
1103+
def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>], Attr.Pure>;
1104+
def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>], Attr.Pure>;
1105+
def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>], Attr.Pure>;
1106+
}
1107+
foreach imgTy = [Image1d, Image1dBuffer] in {
1108+
def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, aQual>, Int], Attr.Pure>;
1109+
def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, aQual>, Int], Attr.Pure>;
1110+
def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Int], Attr.Pure>;
1111+
}
1112+
def : Builtin<"read_imagef", [Float, ImageType<Image2dDepth, aQual>, VectorType<Int, 2>], Attr.Pure>;
1113+
def : Builtin<"read_imagef", [Float, ImageType<Image2dArrayDepth, aQual>, VectorType<Int, 4>], Attr.Pure>;
11101114
}
1111-
def : Builtin<"read_imagef", [Float, ImageType<Image2dDepth, aQual>, VectorType<Int, 2>], Attr.Pure>;
1112-
def : Builtin<"read_imagef", [Float, ImageType<Image2dArrayDepth, aQual>, VectorType<Int, 4>], Attr.Pure>;
11131115
}
11141116

11151117
// --- Table 24: Image Write Functions ---
@@ -1187,16 +1189,18 @@ foreach aQual = ["RO"] in {
11871189
}
11881190
// OpenCL extension v2.0 s5.1.10: Built-in Image Sampler-less Read Functions
11891191
// --- Table 9 ---
1190-
foreach aQual = ["RO", "RW"] in {
1191-
foreach name = ["read_imageh"] in {
1192-
foreach imgTy = [Image2d, Image1dArray] in {
1193-
def : Builtin<name, [VectorType<Half, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>], Attr.Pure>;
1194-
}
1195-
foreach imgTy = [Image3d, Image2dArray] in {
1196-
def : Builtin<name, [VectorType<Half, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>], Attr.Pure>;
1197-
}
1198-
foreach imgTy = [Image1d, Image1dBuffer] in {
1199-
def : Builtin<name, [VectorType<Half, 4>, ImageType<imgTy, aQual>, Int], Attr.Pure>;
1192+
let MinVersion = CL12 in {
1193+
foreach aQual = ["RO", "RW"] in {
1194+
foreach name = ["read_imageh"] in {
1195+
foreach imgTy = [Image2d, Image1dArray] in {
1196+
def : Builtin<name, [VectorType<Half, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>], Attr.Pure>;
1197+
}
1198+
foreach imgTy = [Image3d, Image2dArray] in {
1199+
def : Builtin<name, [VectorType<Half, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>], Attr.Pure>;
1200+
}
1201+
foreach imgTy = [Image1d, Image1dBuffer] in {
1202+
def : Builtin<name, [VectorType<Half, 4>, ImageType<imgTy, aQual>, Int], Attr.Pure>;
1203+
}
12001204
}
12011205
}
12021206
}

clang/test/CodeGenOpenCL/fdeclare-opencl-builtins.cl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -emit-llvm -o - -O0 -triple spir-unknown-unknown -fdeclare-opencl-builtins -finclude-default-header %s | FileCheck %s
1+
// RUN: %clang_cc1 -emit-llvm -o - -O0 -triple spir-unknown-unknown -cl-std=CL1.2 -fdeclare-opencl-builtins -finclude-default-header %s | FileCheck %s
22

33
// Test that Attr.Const from OpenCLBuiltins.td is lowered to a readnone attribute.
44
// CHECK-LABEL: @test_const_attr

clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ kernel void basic_image_readonly(read_only image2d_t image_read_only_image2d) {
113113

114114
resf = read_imagef(image_read_only_image2d, i2);
115115
res = read_imageh(image_read_only_image2d, i2);
116+
#if __OPENCL_C_VERSION__ < CL_VERSION_1_2 && !defined(__OPENCL_CPP_VERSION__)
117+
// expected-error@-3{{no matching function for call to 'read_imagef'}}
118+
// expected-note@-4 + {{candidate function not viable}}
119+
// expected-error@-4{{no matching function for call to 'read_imageh'}}
120+
// expected-note@-5 + {{candidate function not viable}}
121+
#endif
116122
res = read_imageh(image_read_only_image2d, sampler, i2);
117123

118124
int imgWidth = get_image_width(image_read_only_image2d);

0 commit comments

Comments
 (0)