Skip to content

Commit 80dfd86

Browse files
authored
Turn on SPIR-V builtin generation for OpenCL CPP sources (#2466)
It seems we currently allow OCL to SPIR-V translation only for OpenCL C sources. This PR tries to extend this to OpenCL C++ and C++ for OpenCL sources as well. Signed-off-by: Arvind Sudarsanam <[email protected]>
1 parent 1a1b648 commit 80dfd86

File tree

5 files changed

+67
-5
lines changed

5 files changed

+67
-5
lines changed

lib/SPIRV/OCLToSPIRV.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,11 @@ bool OCLToSPIRVBase::runOCLToSPIRV(Module &Module) {
168168
auto Src = getSPIRVSource(&Module);
169169
// This is a pre-processing pass, which transform LLVM IR module to a more
170170
// suitable form for the SPIR-V translation: it is specifically designed to
171-
// handle OpenCL C built-in functions and shouldn't be launched for other
172-
// source languages
173-
if (std::get<0>(Src) != spv::SourceLanguageOpenCL_C)
171+
// handle OpenCL C/C++ and C++ for OpenCL modules and shouldn't be launched
172+
// for other source languages.
173+
if (std::get<0>(Src) != spv::SourceLanguageOpenCL_C &&
174+
std::get<0>(Src) != spv::SourceLanguageOpenCL_CPP &&
175+
std::get<0>(Src) != spv::SourceLanguageCPP_for_OpenCL)
174176
return false;
175177

176178
CLVer = std::get<1>(Src);

lib/SPIRV/OCLTypeToSPIRV.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,13 @@ bool OCLTypeToSPIRVBase::runOCLTypeToSPIRV(Module &Module) {
8787
AdaptedTy.clear();
8888
WorkSet.clear();
8989
auto Src = getSPIRVSource(&Module);
90-
if (std::get<0>(Src) != spv::SourceLanguageOpenCL_C)
90+
// This is a pre-processing pass, which transform LLVM IR module to a more
91+
// suitable form for the SPIR-V translation: it is specifically designed to
92+
// handle OpenCL C/C++ and C++ for OpenCL types and shouldn't be launched for
93+
// other source languages.
94+
if (std::get<0>(Src) != spv::SourceLanguageOpenCL_C &&
95+
std::get<0>(Src) != spv::SourceLanguageOpenCL_CPP &&
96+
std::get<0>(Src) != spv::SourceLanguageCPP_for_OpenCL)
9197
return false;
9298

9399
for (auto &F : Module.functions())

test/extensions/INTEL/SPV_INTEL_arbitrary_precision_fixed_point/capability-arbitrary-precision-fixed-point-numbers.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ attributes #5 = { nounwind }
541541

542542
!0 = !{i32 1, !"wchar_size", i32 4}
543543
!1 = !{i32 1, i32 2}
544-
!2 = !{i32 4, i32 100000}
544+
!2 = !{i32 7, i32 100000}
545545
!3 = !{!"clang version 11.0.0"}
546546
!4 = !{}
547547
!5 = !{!6, !6, i64 0}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
;; This test checks if builtins in LLVM IR generated from CPP_for_OpenCL sources
2+
;; are correctly translated to SPIR-V builtin variables.
3+
4+
; RUN: llvm-as %s -o %t.bc
5+
; RUN: llvm-spirv %t.bc -o %t.spv
6+
; RUN: llvm-spirv %t.bc -spirv-text -o %t.txt
7+
; RUN: FileCheck < %t.txt %s --check-prefix=CHECK-SPIRV
8+
; RUN: spirv-val %t.spv
9+
10+
target datalayout = "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
11+
target triple = "spir-unknown-unknown"
12+
13+
%opencl.event_t = type opaque
14+
15+
; CHECK-SPIRV: {{[0-9]+}} Name {{[0-9]+}} "__spirv_BuiltInWorkgroupId"
16+
; Function Attrs: nounwind
17+
define spir_kernel void @test_fn(i32 %a) {
18+
entry:
19+
%call15 = call spir_func i32 @_Z12get_group_idj(i32 0)
20+
ret void
21+
}
22+
23+
declare spir_func i32 @_Z12get_group_idj(i32)
24+
25+
!spirv.Source = !{!0}
26+
27+
!0 = !{i32 6, i32 100000}

test/ocl_to_spirv_for_opencl_cpp.ll

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
;; This test checks if builtins in LLVM IR generated from OpenCL_CPP sources
2+
;; are correctly translated to SPIR-V builtin variables.
3+
4+
; RUN: llvm-as %s -o %t.bc
5+
; RUN: llvm-spirv %t.bc -o %t.spv
6+
; RUN: llvm-spirv %t.bc -spirv-text -o %t.txt
7+
; RUN: FileCheck < %t.txt %s --check-prefix=CHECK-SPIRV
8+
; RUN: spirv-val %t.spv
9+
10+
target datalayout = "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
11+
target triple = "spir-unknown-unknown"
12+
13+
%opencl.event_t = type opaque
14+
15+
; CHECK-SPIRV: {{[0-9]+}} Name {{[0-9]+}} "__spirv_BuiltInWorkgroupId"
16+
; Function Attrs: nounwind
17+
define spir_kernel void @test_fn(i32 %a) {
18+
entry:
19+
%call15 = call spir_func i32 @_Z12get_group_idj(i32 0)
20+
ret void
21+
}
22+
23+
declare spir_func i32 @_Z12get_group_idj(i32)
24+
25+
!spirv.Source = !{!0}
26+
27+
!0 = !{i32 4, i32 100000}

0 commit comments

Comments
 (0)