Skip to content

Commit 8e8f9c0

Browse files
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: #81753). This PR is to fix the issue.
1 parent cd20a7f commit 8e8f9c0

File tree

5 files changed

+59
-15
lines changed

5 files changed

+59
-15
lines changed

llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,9 @@ void SPIRVAsmPrinter::outputExecutionMode(const Module &M) {
450450
}
451451
for (auto FI = M.begin(), E = M.end(); FI != E; ++FI) {
452452
const Function &F = *FI;
453-
if (F.isDeclaration())
453+
// Only operands of OpEntryPoint instructions are allowed to be
454+
// <Entry Point> operands of OpExecutionMode
455+
if (F.isDeclaration() || !isEntryPoint(F))
454456
continue;
455457
Register FReg = MAI->getFuncReg(&F);
456458
assert(FReg.isValid());

llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -181,20 +181,6 @@ static SPIRVType *getArgSPIRVType(const Function &F, unsigned ArgIdx,
181181
ArgAccessQual);
182182
}
183183

184-
static bool isEntryPoint(const Function &F) {
185-
// OpenCL handling: any function with the SPIR_KERNEL
186-
// calling convention will be a potential entry point.
187-
if (F.getCallingConv() == CallingConv::SPIR_KERNEL)
188-
return true;
189-
190-
// HLSL handling: special attribute are emitted from the
191-
// front-end.
192-
if (F.getFnAttribute("hlsl.shader").isValid())
193-
return true;
194-
195-
return false;
196-
}
197-
198184
static SPIRV::ExecutionModel::ExecutionModel
199185
getExecutionModel(const SPIRVSubtarget &STI, const Function &F) {
200186
if (STI.isOpenCLEnv())

llvm/lib/Target/SPIRV/SPIRVUtils.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,4 +351,18 @@ bool isSpecialOpaqueType(const Type *Ty) {
351351

352352
return false;
353353
}
354+
355+
bool isEntryPoint(const Function &F) {
356+
// OpenCL handling: any function with the SPIR_KERNEL
357+
// calling convention will be a potential entry point.
358+
if (F.getCallingConv() == CallingConv::SPIR_KERNEL)
359+
return true;
360+
361+
// HLSL handling: special attribute are emitted from the
362+
// front-end.
363+
if (F.getFnAttribute("hlsl.shader").isValid())
364+
return true;
365+
366+
return false;
367+
}
354368
} // namespace llvm

llvm/lib/Target/SPIRV/SPIRVUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,8 @@ bool hasBuiltinTypePrefix(StringRef Name);
9797

9898
// Check if given LLVM type is a special opaque builtin type.
9999
bool isSpecialOpaqueType(const Type *Ty);
100+
101+
// Check if the function is an SPIR-V entry point
102+
bool isEntryPoint(const Function &F);
100103
} // namespace llvm
101104
#endif // LLVM_LIB_TARGET_SPIRV_SPIRVUTILS_H
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
; RUN: llc -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
2+
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}
3+
4+
; CHECK: OpMemoryModel
5+
; CHECK-DAG: OpEntryPoint Kernel %[[#ENTRY1:]] "foo1"
6+
; CHECK-DAG: OpEntryPoint Kernel %[[#ENTRY4:]] "foo4"
7+
; CHECK-NOT: OpSource
8+
; CHECK-DAG: OpExecutionMode %[[#ENTRY1]] {{[a-zA-Z]+}}
9+
; CHECK-DAG: OpExecutionMode %[[#ENTRY4]] {{[a-zA-Z]+}}
10+
; CHECK: OpSource
11+
12+
; RUN: llc -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s --check-prefix=CHECKN
13+
14+
; CHECKN: OpMemoryModel
15+
; CHECKN-COUNT-2: OpEntryPoint Kernel
16+
; CHECKN-NOT: OpEntryPoint Kernel
17+
; CHECKN-COUNT-2: OpExecutionMode
18+
; CHECKN-NOT: OpExecutionMode
19+
; CHECKN: OpSource
20+
21+
define spir_kernel void @foo1() {
22+
entry:
23+
ret void
24+
}
25+
26+
define void @foo2() {
27+
entry:
28+
ret void
29+
}
30+
31+
define dso_local spir_func void @foo3() {
32+
entry:
33+
ret void
34+
}
35+
36+
define spir_kernel void @foo4() {
37+
entry:
38+
ret void
39+
}

0 commit comments

Comments
 (0)