Skip to content

Commit c4c20a3

Browse files
Fznamznonvmaksimo
authored andcommitted
Translate empty LLVM module to empty SPIR-V module
When translator tool is used in toolchain it is possible that it can receive an empty LLVM module as an input, just emit an empty SPIR-V module with Linkage capability in this case.
1 parent c623ec4 commit c4c20a3

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2861,9 +2861,17 @@ void LLVMToSPIRV::transFunction(Function *I) {
28612861
}
28622862
}
28632863

2864+
bool isEmptyLLVMModule(Module *M) {
2865+
return M->empty() && // No functions
2866+
M->global_empty(); // No global variables
2867+
}
2868+
28642869
bool LLVMToSPIRV::translate() {
28652870
BM->setGeneratorVer(KTranslatorVer);
28662871

2872+
if (isEmptyLLVMModule(M))
2873+
BM->addCapability(CapabilityLinkage);
2874+
28672875
if (!transSourceLanguage())
28682876
return false;
28692877
if (!transExtension())
@@ -3599,6 +3607,9 @@ bool isValidLLVMModule(Module *M, SPIRVErrorLog &ErrorLog) {
35993607
if (!M)
36003608
return false;
36013609

3610+
if (isEmptyLLVMModule(M))
3611+
return true;
3612+
36023613
Triple TT(M->getTargetTriple());
36033614
if (!ErrorLog.checkError(isSupportedTriple(TT), SPIRVEC_InvalidTargetTriple,
36043615
"Actual target triple is " + M->getTargetTriple()))

llvm-spirv/test/empty-module.ll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; RUN: llvm-as %s -o %t.bc
2+
; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck %s
3+
; RUN: llvm-spirv %t.bc -o %t.spv
4+
; RUN: spirv-val %t.spv
5+
6+
; CHECK: 119734787 {{[0-9]*}} {{[0-9]*}} {{[0-9]*}} 0
7+
; CHECK: Capability Addresses
8+
; CHECK: Capability Linkage
9+
; CHECK: Capability Kernel
10+
; CHECK: ExtInstImport 1 "OpenCL.std"
11+
; CHECK: MemoryModel 2 2
12+
; CHECK: Source 0 0

0 commit comments

Comments
 (0)