Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent undefined behaviour of SPIR-V Backend non-asserts builds when dealing with token type #78437

Merged

Conversation

VyacheslavLevytskyy
Copy link
Contributor

The goal of this PR is to fix the issue when use of token type in LLVM intrinsic causes undefined behavior of SPIR-V Backend code generator when assertions are disabled: #78434

Among possible fix options, discussed in the #78434 issue description, the option to generate a meaningful error before execution arrives at the llvm_unreachable call looks like a better solution for now, because SPIR-V doesn't support token type anyway without additional extensions.

The PR is to generate a user-friendly error message and exit without generating a stack dump when such a usage of token type was detected that would lead to undefined behavior of SPIR-V Backend code generator.

…s detected that would lead to undefined behavior of SPIR-V Backend code generator
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 17, 2024

@llvm/pr-subscribers-backend-spir-v

Author: Vyacheslav Levytskyy (VyacheslavLevytskyy)

Changes

The goal of this PR is to fix the issue when use of token type in LLVM intrinsic causes undefined behavior of SPIR-V Backend code generator when assertions are disabled: #78434

Among possible fix options, discussed in the #78434 issue description, the option to generate a meaningful error before execution arrives at the llvm_unreachable call looks like a better solution for now, because SPIR-V doesn't support token type anyway without additional extensions.

The PR is to generate a user-friendly error message and exit without generating a stack dump when such a usage of token type was detected that would lead to undefined behavior of SPIR-V Backend code generator.


Full diff: https://github.com/llvm/llvm-project/pull/78437.diff

3 Files Affected:

  • (modified) llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp (+10)
  • (added) llvm/test/CodeGen/SPIRV/token/token_type_preallocated_setup_arg.ll (+17)
  • (added) llvm/test/CodeGen/SPIRV/token/token_type_requires_extension.ll (+11)
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
+}

@VyacheslavLevytskyy
Copy link
Contributor Author

@michalpaszkowski Can you please have a look at this one as well when you have time. Thank you!

Copy link
Contributor

@Keenuts Keenuts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM on my end. Thanks!

@@ -0,0 +1,17 @@
; Example of token usage is from https://llvm.org/docs/LangRef.html (Preallocated Operand Bundles)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change the name to use dashes instead of underscores

Copy link
Member

@michalpaszkowski michalpaszkowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants