[llvm] prevent undefined behaviour of SPIR-V Backend non-asserts builds when dealing with token type (PR #78437)

Vyacheslav Levytskyy via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 25 14:07:44 PST 2024


https://github.com/VyacheslavLevytskyy updated https://github.com/llvm/llvm-project/pull/78437

>From e5ae4114f51c1a8f9374a9228df9f48025f7a9fa Mon Sep 17 00:00:00 2001
From: "Levytskyy, Vyacheslav" <vyacheslav.levytskyy at intel.com>
Date: Wed, 17 Jan 2024 04:30:17 -0800
Subject: [PATCH 1/2] generate an error message and exit when such a usage of
 token type was detected that would lead to undefined behavior of SPIR-V
 Backend code generator

---
 llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp   | 10 ++++++++++
 .../token/token_type_preallocated_setup_arg.ll  | 17 +++++++++++++++++
 .../token/token_type_requires_extension.ll      | 11 +++++++++++
 3 files changed, 38 insertions(+)
 create mode 100644 llvm/test/CodeGen/SPIRV/token/token_type_preallocated_setup_arg.ll
 create mode 100644 llvm/test/CodeGen/SPIRV/token/token_type_requires_extension.ll

diff --git a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
index ec62a819b00eedf..907bf87dc406d31 100644
--- a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
@@ -147,6 +147,13 @@ static bool requireAssignType(Instruction *I) {
   return true;
 }
 
+static inline void reportFatalOnTokenType(const Instruction *I) {
+  if (I->getType()->isTokenTy())
+    report_fatal_error("A token is encountered but SPIR-V without extensions "
+                       "does not support token type",
+                       false);
+}
+
 void SPIRVEmitIntrinsics::replaceMemInstrUses(Instruction *Old,
                                               Instruction *New) {
   while (!Old->user_empty()) {
@@ -402,6 +409,7 @@ void SPIRVEmitIntrinsics::processGlobalValue(GlobalVariable &GV) {
 }
 
 void SPIRVEmitIntrinsics::insertAssignPtrTypeIntrs(Instruction *I) {
+  reportFatalOnTokenType(I);
   if (I->getType()->isVoidTy() || !requireAssignPtrType(I))
     return;
 
@@ -424,6 +432,7 @@ void SPIRVEmitIntrinsics::insertAssignPtrTypeIntrs(Instruction *I) {
 }
 
 void SPIRVEmitIntrinsics::insertAssignTypeIntrs(Instruction *I) {
+  reportFatalOnTokenType(I);
   Type *Ty = I->getType();
   if (!Ty->isVoidTy() && requireAssignType(I) && !requireAssignPtrType(I)) {
     setInsertPointSkippingPhis(*IRB, I->getNextNode());
@@ -483,6 +492,7 @@ void SPIRVEmitIntrinsics::processInstrAfterVisit(Instruction *I) {
     }
   }
   if (I->hasName()) {
+    reportFatalOnTokenType(I);
     setInsertPointSkippingPhis(*IRB, I->getNextNode());
     std::vector<Value *> Args = {I};
     addStringImm(I->getName(), *IRB, Args);
diff --git a/llvm/test/CodeGen/SPIRV/token/token_type_preallocated_setup_arg.ll b/llvm/test/CodeGen/SPIRV/token/token_type_preallocated_setup_arg.ll
new file mode 100644
index 000000000000000..42e59c726b6fdc7
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/token/token_type_preallocated_setup_arg.ll
@@ -0,0 +1,17 @@
+; Example of token usage is from https://llvm.org/docs/LangRef.html (Preallocated Operand Bundles)
+
+; RUN: not llc -O0 -mtriple=spirv64-unknown-unknown %s -o - 2>&1 | FileCheck %s
+
+; CHECK: A token is encountered but SPIR-V without extensions does not support token type
+
+%foo = type { i64, i32 }
+
+define dso_local spir_func void @test() {
+entry:
+  %tok = call token @llvm.call.preallocated.setup(i32 1)
+  %a = call ptr @llvm.call.preallocated.arg(token %tok, i32 0) preallocated(%foo)
+  ret void
+}
+
+declare token @llvm.call.preallocated.setup(i32 %num_args)
+declare ptr @llvm.call.preallocated.arg(token %setup_token, i32 %arg_index)
diff --git a/llvm/test/CodeGen/SPIRV/token/token_type_requires_extension.ll b/llvm/test/CodeGen/SPIRV/token/token_type_requires_extension.ll
new file mode 100644
index 000000000000000..ee545a77e5d68f8
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/token/token_type_requires_extension.ll
@@ -0,0 +1,11 @@
+; RUN: not llc -O0 -mtriple=spirv64-unknown-unknown %s -o - 2>&1 | FileCheck %s
+
+; CHECK: A token is encountered but SPIR-V without extensions does not support token type
+
+declare token @llvm.myfun()
+
+define dso_local spir_func void @func() {
+entry:
+  %tok = call token @llvm.myfun()
+  ret void
+}

>From 9c888a4683d7fc1ad011aee4be762965b40b12bb Mon Sep 17 00:00:00 2001
From: "Levytskyy, Vyacheslav" <vyacheslav.levytskyy at intel.com>
Date: Thu, 25 Jan 2024 14:04:16 -0800
Subject: [PATCH 2/2] change the name of the test to use dashes instead of
 underscores

---
 ...llocated_setup_arg.ll => token-type-preallocated-setup-arg.ll} | 0
 ...ype_requires_extension.ll => token-type-requires-extension.ll} | 0
 2 files changed, 0 insertions(+), 0 deletions(-)
 rename llvm/test/CodeGen/SPIRV/token/{token_type_preallocated_setup_arg.ll => token-type-preallocated-setup-arg.ll} (100%)
 rename llvm/test/CodeGen/SPIRV/token/{token_type_requires_extension.ll => token-type-requires-extension.ll} (100%)

diff --git a/llvm/test/CodeGen/SPIRV/token/token_type_preallocated_setup_arg.ll b/llvm/test/CodeGen/SPIRV/token/token-type-preallocated-setup-arg.ll
similarity index 100%
rename from llvm/test/CodeGen/SPIRV/token/token_type_preallocated_setup_arg.ll
rename to llvm/test/CodeGen/SPIRV/token/token-type-preallocated-setup-arg.ll
diff --git a/llvm/test/CodeGen/SPIRV/token/token_type_requires_extension.ll b/llvm/test/CodeGen/SPIRV/token/token-type-requires-extension.ll
similarity index 100%
rename from llvm/test/CodeGen/SPIRV/token/token_type_requires_extension.ll
rename to llvm/test/CodeGen/SPIRV/token/token-type-requires-extension.ll



More information about the llvm-commits mailing list