1//===- SPIRVError.h - SPIR-V error code and checking -------------*- C++ -*-===// 2// 3// The LLVM/SPIRV Translator 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8// Copyright (c) 2014 Advanced Micro Devices, Inc. All rights reserved. 9// 10// Permission is hereby granted, free of charge, to any person obtaining a 11// copy of this software and associated documentation files (the "Software"), 12// to deal with the Software without restriction, including without limitation 13// the rights to use, copy, modify, merge, publish, distribute, sublicense, 14// and/or sell copies of the Software, and to permit persons to whom the 15// Software is furnished to do so, subject to the following conditions: 16// 17// Redistributions of source code must retain the above copyright notice, 18// this list of conditions and the following disclaimers. 19// Redistributions in binary form must reproduce the above copyright notice, 20// this list of conditions and the following disclaimers in the documentation 21// and/or other materials provided with the distribution. 22// Neither the names of Advanced Micro Devices, Inc., nor the names of its 23// contributors may be used to endorse or promote products derived from this 24// Software without specific prior written permission. 25// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 26// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 27// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 28// CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 29// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 30// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH 31// THE SOFTWARE. 32// 33//===----------------------------------------------------------------------===// 34// 35// This file defines SPIRV error code and checking utility. 36// 37//===----------------------------------------------------------------------===// 38 39#ifndef SPIRVERROR_HPP_ 40#define SPIRVERROR_HPP_ 41 42#include "SPIRVUtil.h" 43#include "SPIRVDebug.h" 44#include <string> 45#include <sstream> 46 47namespace SPIRV{ 48 49// Check condition and set error code and error msg. 50// To use this macro, function checkError must be defined in the scope. 51#define SPIRVCK(Condition,ErrCode,ErrMsg) \ 52 getErrorLog().checkError(Condition, SPIRVEC_##ErrCode, std::string()+ErrMsg,\ 53 #Condition, __FILE__, __LINE__) 54 55// Check condition and set error code and error msg. If fail returns false. 56#define SPIRVCKRT(Condition,ErrCode,ErrMsg) \ 57 if (!getErrorLog().checkError(Condition, SPIRVEC_##ErrCode,\ 58 std::string()+ErrMsg, #Condition, __FILE__, __LINE__))\ 59 return false; 60 61// Defines error code enum type SPIRVErrorCode. 62enum SPIRVErrorCode { 63#define _SPIRV_OP(x,y) SPIRVEC_##x, 64#include "SPIRVErrorEnum.h" 65#undef _SPIRV_OP 66}; 67 68// Defines OpErorMap which maps error code to a string describing the error. 69template<> inline void 70SPIRVMap<SPIRVErrorCode, std::string>::init() { 71#define _SPIRV_OP(x,y) add(SPIRVEC_##x, std::string(#x)+": "+y); 72#include "SPIRVErrorEnum.h" 73#undef _SPIRV_OP 74} 75 76typedef SPIRVMap<SPIRVErrorCode, std::string> SPIRVErrorMap; 77 78class SPIRVErrorLog { 79public: 80 SPIRVErrorLog():ErrorCode(SPIRVEC_Success){} 81 SPIRVErrorCode getError(std::string& ErrMsg) { 82 ErrMsg = ErrorMsg; 83 return ErrorCode; 84 } 85 void setError(SPIRVErrorCode ErrCode, const std::string& ErrMsg) { 86 ErrorCode = ErrCode; 87 ErrorMsg = ErrMsg; 88 } 89 // Check if Condition is satisfied and set ErrCode and DetailedMsg 90 // if not. Returns true if no error. 91 bool checkError(bool Condition, SPIRVErrorCode ErrCode, 92 const std::string& DetailedMsg = "", 93 const char *CondString = nullptr, 94 const char *FileName = nullptr, 95 unsigned LineNumber = 0); 96protected: 97 SPIRVErrorCode ErrorCode; 98 std::string ErrorMsg; 99 100}; 101 102inline bool 103SPIRVErrorLog::checkError(bool Cond, SPIRVErrorCode ErrCode, 104 const std::string& Msg, const char *CondString, const char *FileName, 105 unsigned LineNo) { 106 std::stringstream SS; 107 if (Cond) 108 return Cond; 109 // Do not overwrite previous failure. 110 if (ErrorCode != SPIRVEC_Success) 111 return Cond; 112 SS << SPIRVErrorMap::map(ErrCode) << " " << Msg; 113 if (SPIRVDbgErrorMsgIncludesSourceInfo) 114 SS <<" [Src: " << FileName << ":" << LineNo << " " << CondString << " ]"; 115 setError(ErrCode, SS.str()); 116 if (SPIRVDbgAssertOnError) { 117 spvdbgs() << SS.str() << '\n'; 118 spvdbgs().flush(); 119 assert (0); 120 } 121 return Cond; 122} 123 124} 125 126 127#endif /* SPIRVERROR_HPP_ */ 128