[llvm] [SPIR-V] SPIR-V Backend must generata a valid OCL version if working in OpenCL environment (PR #89199)

Vyacheslav Levytskyy via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 18 03:14:44 PDT 2024


https://github.com/VyacheslavLevytskyy created https://github.com/llvm/llvm-project/pull/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.

>From fd50b685f8b42c0dbe8ace82f54b6ac9a9338587 Mon Sep 17 00:00:00 2001
From: "Levytskyy, Vyacheslav" <vyacheslav.levytskyy at intel.com>
Date: Thu, 18 Apr 2024 03:13:09 -0700
Subject: [PATCH] generat a valid OCL version if working in OpenCL environment

---
 llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp | 17 +++++++++++++---
 llvm/test/CodeGen/SPIRV/empty-module.ll       | 20 ++++++++++++-------
 2 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp b/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
index 40c3e5f9c6bdab..8395d4b2bf66b5 100644
--- a/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
@@ -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")) {
diff --git a/llvm/test/CodeGen/SPIRV/empty-module.ll b/llvm/test/CodeGen/SPIRV/empty-module.ll
index b56e58ccaf8f1b..b7cea042ee5ddc 100644
--- a/llvm/test/CodeGen/SPIRV/empty-module.ll
+++ b/llvm/test/CodeGen/SPIRV/empty-module.ll
@@ -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



More information about the llvm-commits mailing list