Skip to content

Commit

Permalink
[SPIR-V] SPIR-V Backend must generate a valid OCL version if working …
Browse files Browse the repository at this point in the history
…in OpenCL environment (#89199)

If there is no information about OpenCL version we are forced to
generate OpenCL 1.0 by default for the OpenCL environment to avoid
puzzling run-times with Unknown/0.0 version output. For a reference,
LLVM-SPIRV Translator avoids potential issues with run-times in a
similar manner.
  • Loading branch information
VyacheslavLevytskyy committed Apr 19, 2024
1 parent e8fce95 commit 14193f4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
17 changes: 14 additions & 3 deletions llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,21 @@ void SPIRVModuleAnalysis::setBaseInfo(const Module &M) {
unsigned MajorNum = getMetadataUInt(VersionMD, 0, 2);
unsigned MinorNum = getMetadataUInt(VersionMD, 1);
unsigned RevNum = getMetadataUInt(VersionMD, 2);
MAI.SrcLangVersion = (MajorNum * 100 + MinorNum) * 1000 + RevNum;
// Prevent Major part of OpenCL version to be 0
MAI.SrcLangVersion =
(std::max(1U, MajorNum) * 100 + MinorNum) * 1000 + RevNum;
} else {
MAI.SrcLang = SPIRV::SourceLanguage::Unknown;
MAI.SrcLangVersion = 0;
// If there is no information about OpenCL version we are forced to generate
// OpenCL 1.0 by default for the OpenCL environment to avoid puzzling
// run-times with Unknown/0.0 version output. For a reference, LLVM-SPIRV
// Translator avoids potential issues with run-times in a similar manner.
if (ST->isOpenCLEnv()) {
MAI.SrcLang = SPIRV::SourceLanguage::OpenCL_CPP;
MAI.SrcLangVersion = 100000;
} else {
MAI.SrcLang = SPIRV::SourceLanguage::Unknown;
MAI.SrcLangVersion = 0;
}
}

if (auto ExtNode = M.getNamedMetadata("opencl.used.extensions")) {
Expand Down
20 changes: 13 additions & 7 deletions llvm/test/CodeGen/SPIRV/empty-module.ll
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
; RUN: llc -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
; RUN: llc -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-OCL
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}

; CHECK-DAG: OpCapability Addresses
; CHECK-DAG: OpCapability Linkage
; CHECK-DAG: OpCapability Kernel
; CHECK: %1 = OpExtInstImport "OpenCL.std"
; CHECK: OpMemoryModel Physical64 OpenCL
; CHECK: OpSource Unknown 0
; RUN: llc -O0 -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-NOOCL
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val %}

; CHECK-DAG: OpCapability Linkage
; CHECK-NOOCL-DAG: OpCapability Shader
; CHECK-OCL-DAG: OpCapability Addresses
; CHECK-OCL-DAG: OpCapability Kernel
; CHECK-OCL: %1 = OpExtInstImport "OpenCL.std"
; CHECK-NOOCL: OpMemoryModel Logical GLSL450
; CHECK-OCL: OpMemoryModel Physical64 OpenCL
; CHECK-NOOCL: OpSource Unknown 0
; CHECK-OCL: OpSource OpenCL_CPP 100000

0 comments on commit 14193f4

Please sign in to comment.