Skip to content

Commit 1d48cd8

Browse files
committed
Move target triple check before translation
Check that the target triple is supported before LLVM to SPIR-V translation, rather than during translation. This allows us to gracefully report InvalidTargetTriple instead of failing the target triple assertion in PreprocessMetadata.cpp.
1 parent 21e0587 commit 1d48cd8

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

lib/SPIRV/SPIRVWriter.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,9 +1248,6 @@ SPIRVValue *LLVMToSPIRV::transCallInst(CallInst *CI, SPIRVBasicBlock *BB) {
12481248
bool LLVMToSPIRV::transAddressingMode() {
12491249
Triple TargetTriple(M->getTargetTriple());
12501250

1251-
SPIRVCKRT(isSupportedTriple(TargetTriple), InvalidTargetTriple,
1252-
"Actual target triple is " + M->getTargetTriple());
1253-
12541251
if (TargetTriple.isArch32Bit())
12551252
BM->setAddressingModel(AddressingModelPhysical32);
12561253
else
@@ -1759,8 +1756,23 @@ void addPassesForSPIRV(legacy::PassManager &PassMgr) {
17591756
PassMgr.add(createSPIRVLowerMemmove());
17601757
}
17611758

1759+
bool isValidLLVMModule(Module *M, SPIRVErrorLog &ErrorLog) {
1760+
if (!M)
1761+
return false;
1762+
1763+
Triple TT(M->getTargetTriple());
1764+
if (!ErrorLog.checkError(isSupportedTriple(TT), SPIRVEC_InvalidTargetTriple,
1765+
"Actual target triple is " + M->getTargetTriple()))
1766+
return false;
1767+
1768+
return true;
1769+
}
1770+
17621771
bool llvm::writeSpirv(Module *M, std::ostream &OS, std::string &ErrMsg) {
17631772
std::unique_ptr<SPIRVModule> BM(SPIRVModule::createSPIRVModule());
1773+
if (!isValidLLVMModule(M, BM->getErrorLog()))
1774+
return false;
1775+
17641776
legacy::PassManager PassMgr;
17651777
addPassesForSPIRV(PassMgr);
17661778
if (hasLoopUnrollMetadata(M))
@@ -1776,6 +1788,9 @@ bool llvm::writeSpirv(Module *M, std::ostream &OS, std::string &ErrMsg) {
17761788

17771789
bool llvm::regularizeLlvmForSpirv(Module *M, std::string &ErrMsg) {
17781790
std::unique_ptr<SPIRVModule> BM(SPIRVModule::createSPIRVModule());
1791+
if (!isValidLLVMModule(M, BM->getErrorLog()))
1792+
return false;
1793+
17791794
legacy::PassManager PassMgr;
17801795
addPassesForSPIRV(PassMgr);
17811796
PassMgr.run(*M);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; RUN: llvm-as %s -o %t.bc
2+
; RUN: not --crash llvm-spirv %t.bc -o %t.spv 2>&1 | FileCheck %s
3+
4+
; CHECK: InvalidTargetTriple: Expects spir-unknown-unknown or spir64-unknown-unknown. Actual target triple is aarch64
5+
6+
target datalayout = "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
7+
target triple = "aarch64"
8+
9+
; Function Attrs: convergent noinline nounwind optnone
10+
define spir_func void @_Z3foov() {
11+
entry:
12+
ret void
13+
}
14+
15+
!llvm.module.flags = !{!0}
16+
!opencl.spir.version = !{!1}
17+
!opencl.ocl.version = !{!1}
18+
19+
!0 = !{i32 1, !"wchar_size", i32 4}
20+
!1 = !{i32 2, i32 0}

0 commit comments

Comments
 (0)