Skip to content

Commit

Permalink
Move target triple check before translation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
svenvh committed Aug 1, 2019
1 parent 21e0587 commit 1d48cd8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/SPIRV/SPIRVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1248,9 +1248,6 @@ SPIRVValue *LLVMToSPIRV::transCallInst(CallInst *CI, SPIRVBasicBlock *BB) {
bool LLVMToSPIRV::transAddressingMode() {
Triple TargetTriple(M->getTargetTriple());

SPIRVCKRT(isSupportedTriple(TargetTriple), InvalidTargetTriple,
"Actual target triple is " + M->getTargetTriple());

if (TargetTriple.isArch32Bit())
BM->setAddressingModel(AddressingModelPhysical32);
else
Expand Down Expand Up @@ -1759,8 +1756,23 @@ void addPassesForSPIRV(legacy::PassManager &PassMgr) {
PassMgr.add(createSPIRVLowerMemmove());
}

bool isValidLLVMModule(Module *M, SPIRVErrorLog &ErrorLog) {
if (!M)
return false;

Triple TT(M->getTargetTriple());
if (!ErrorLog.checkError(isSupportedTriple(TT), SPIRVEC_InvalidTargetTriple,
"Actual target triple is " + M->getTargetTriple()))
return false;

return true;
}

bool llvm::writeSpirv(Module *M, std::ostream &OS, std::string &ErrMsg) {
std::unique_ptr<SPIRVModule> BM(SPIRVModule::createSPIRVModule());
if (!isValidLLVMModule(M, BM->getErrorLog()))
return false;

legacy::PassManager PassMgr;
addPassesForSPIRV(PassMgr);
if (hasLoopUnrollMetadata(M))
Expand All @@ -1776,6 +1788,9 @@ bool llvm::writeSpirv(Module *M, std::ostream &OS, std::string &ErrMsg) {

bool llvm::regularizeLlvmForSpirv(Module *M, std::string &ErrMsg) {
std::unique_ptr<SPIRVModule> BM(SPIRVModule::createSPIRVModule());
if (!isValidLLVMModule(M, BM->getErrorLog()))
return false;

legacy::PassManager PassMgr;
addPassesForSPIRV(PassMgr);
PassMgr.run(*M);
Expand Down
20 changes: 20 additions & 0 deletions test/negative/unsupported-triple.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
; RUN: llvm-as %s -o %t.bc
; RUN: not --crash llvm-spirv %t.bc -o %t.spv 2>&1 | FileCheck %s

; CHECK: InvalidTargetTriple: Expects spir-unknown-unknown or spir64-unknown-unknown. Actual target triple is aarch64

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"
target triple = "aarch64"

; Function Attrs: convergent noinline nounwind optnone
define spir_func void @_Z3foov() {
entry:
ret void
}

!llvm.module.flags = !{!0}
!opencl.spir.version = !{!1}
!opencl.ocl.version = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 2, i32 0}

0 comments on commit 1d48cd8

Please sign in to comment.