[llvm] 8e8f9c0 - fix generation of unnecessary OpExecutionMode records (#81839)

via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 19 11:51:36 PST 2024


Author: Vyacheslav Levytskyy
Date: 2024-02-19T20:51:32+01:00
New Revision: 8e8f9c0bc0773147b8e9dbc0df9730efd630e18f

URL: https://github.com/llvm/llvm-project/commit/8e8f9c0bc0773147b8e9dbc0df9730efd630e18f
DIFF: https://github.com/llvm/llvm-project/commit/8e8f9c0bc0773147b8e9dbc0df9730efd630e18f.diff

LOG: fix generation of unnecessary OpExecutionMode records (#81839)

SPIRV-V Backend generates unnecessary OpExecutionMode records, putting
into the id's which are not the Entry Point operands of an OpEntryPoint
(ref: https://github.com/llvm/llvm-project/issues/81753). This PR is to
fix the issue.

Added: 
    llvm/test/CodeGen/SPIRV/execution-mode-per-entry-point.ll

Modified: 
    llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp
    llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp
    llvm/lib/Target/SPIRV/SPIRVUtils.cpp
    llvm/lib/Target/SPIRV/SPIRVUtils.h

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp b/llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp
index 27da0f21f1571d..1fbf3c3e11aedc 100644
--- a/llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp
@@ -450,7 +450,9 @@ void SPIRVAsmPrinter::outputExecutionMode(const Module &M) {
   }
   for (auto FI = M.begin(), E = M.end(); FI != E; ++FI) {
     const Function &F = *FI;
-    if (F.isDeclaration())
+    // Only operands of OpEntryPoint instructions are allowed to be
+    // <Entry Point> operands of OpExecutionMode
+    if (F.isDeclaration() || !isEntryPoint(F))
       continue;
     Register FReg = MAI->getFuncReg(&F);
     assert(FReg.isValid());

diff  --git a/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp b/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp
index baeed2ad895a4b..cc438b2bb8d4d7 100644
--- a/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp
@@ -181,20 +181,6 @@ static SPIRVType *getArgSPIRVType(const Function &F, unsigned ArgIdx,
                                                ArgAccessQual);
 }
 
-static bool isEntryPoint(const Function &F) {
-  // OpenCL handling: any function with the SPIR_KERNEL
-  // calling convention will be a potential entry point.
-  if (F.getCallingConv() == CallingConv::SPIR_KERNEL)
-    return true;
-
-  // HLSL handling: special attribute are emitted from the
-  // front-end.
-  if (F.getFnAttribute("hlsl.shader").isValid())
-    return true;
-
-  return false;
-}
-
 static SPIRV::ExecutionModel::ExecutionModel
 getExecutionModel(const SPIRVSubtarget &STI, const Function &F) {
   if (STI.isOpenCLEnv())

diff  --git a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
index 89daa19e666f63..05f766d3ec5483 100644
--- a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
@@ -351,4 +351,18 @@ bool isSpecialOpaqueType(const Type *Ty) {
 
   return false;
 }
+
+bool isEntryPoint(const Function &F) {
+  // OpenCL handling: any function with the SPIR_KERNEL
+  // calling convention will be a potential entry point.
+  if (F.getCallingConv() == CallingConv::SPIR_KERNEL)
+    return true;
+
+  // HLSL handling: special attribute are emitted from the
+  // front-end.
+  if (F.getFnAttribute("hlsl.shader").isValid())
+    return true;
+
+  return false;
+}
 } // namespace llvm

diff  --git a/llvm/lib/Target/SPIRV/SPIRVUtils.h b/llvm/lib/Target/SPIRV/SPIRVUtils.h
index 60742e2f272808..a33dc02f854f58 100644
--- a/llvm/lib/Target/SPIRV/SPIRVUtils.h
+++ b/llvm/lib/Target/SPIRV/SPIRVUtils.h
@@ -97,5 +97,8 @@ bool hasBuiltinTypePrefix(StringRef Name);
 
 // Check if given LLVM type is a special opaque builtin type.
 bool isSpecialOpaqueType(const Type *Ty);
+
+// Check if the function is an SPIR-V entry point
+bool isEntryPoint(const Function &F);
 } // namespace llvm
 #endif // LLVM_LIB_TARGET_SPIRV_SPIRVUTILS_H

diff  --git a/llvm/test/CodeGen/SPIRV/execution-mode-per-entry-point.ll b/llvm/test/CodeGen/SPIRV/execution-mode-per-entry-point.ll
new file mode 100644
index 00000000000000..6fd1abd2be59ab
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/execution-mode-per-entry-point.ll
@@ -0,0 +1,39 @@
+; RUN: llc -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}
+
+; CHECK: OpMemoryModel
+; CHECK-DAG: OpEntryPoint Kernel %[[#ENTRY1:]] "foo1"
+; CHECK-DAG: OpEntryPoint Kernel %[[#ENTRY4:]] "foo4"
+; CHECK-NOT: OpSource
+; CHECK-DAG: OpExecutionMode %[[#ENTRY1]] {{[a-zA-Z]+}}
+; CHECK-DAG: OpExecutionMode %[[#ENTRY4]] {{[a-zA-Z]+}}
+; CHECK: OpSource
+
+; RUN: llc -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s --check-prefix=CHECKN
+
+; CHECKN: OpMemoryModel
+; CHECKN-COUNT-2: OpEntryPoint Kernel
+; CHECKN-NOT: OpEntryPoint Kernel
+; CHECKN-COUNT-2: OpExecutionMode
+; CHECKN-NOT: OpExecutionMode
+; CHECKN: OpSource
+
+define spir_kernel void @foo1() {
+entry:
+  ret void
+}
+
+define void @foo2() {
+entry:
+  ret void
+}
+
+define dso_local spir_func void @foo3() {
+entry:
+  ret void
+}
+
+define spir_kernel void @foo4() {
+entry:
+  ret void
+}


        


More information about the llvm-commits mailing list