| 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 SPIRV_LIBSPIRV_SPIRVERROR_H |
| 40 |
#define SPIRV_LIBSPIRV_SPIRVERROR_H |
| 41 |
|
| 42 |
#include "SPIRVDebug.h" |
| 43 |
#include "SPIRVUtil.h" |
| 44 |
#include "llvm/IR/Instruction.h" |
| 45 |
#include <iostream> |
| 46 |
#include <sstream> |
| 47 |
#include <string> |
| 48 |
|
| 49 |
namespace SPIRV { |
| 50 |
|
| 51 |
// Check condition and set error code and error msg. |
| 52 |
// To use this macro, function checkError must be defined in the scope. |
| 53 |
// Emit absolute path only in debug mode. |
| 54 |
#ifdef NDEBUG |
| 55 |
#define SPIRVCK(Condition, ErrCode, ErrMsg) \ |
| 56 |
getErrorLog().checkError(Condition, SPIRVEC_##ErrCode, \ |
| 57 |
std::string() + (ErrMsg), #Condition) |
| 58 |
#else |
| 59 |
#define SPIRVCK(Condition, ErrCode, ErrMsg) \ |
| 60 |
getErrorLog().checkError(Condition, SPIRVEC_##ErrCode, \ |
| 61 |
std::string() + (ErrMsg), #Condition, __FILE__, \ |
| 62 |
__LINE__) |
| 63 |
#endif // NDEBUG |
| 64 |
|
| 65 |
// Check condition and set error code and error msg. If fail returns false. |
| 66 |
// Emit absolute path only in debug mode. |
| 67 |
#ifdef NDEBUG |
| 68 |
#define SPIRVCKRT(Condition, ErrCode, ErrMsg) \ |
| 69 |
if (!getErrorLog().checkError(Condition, SPIRVEC_##ErrCode, \ |
| 70 |
std::string() + (ErrMsg), #Condition)) \ |
| 71 |
return false; |
| 72 |
#else |
| 73 |
#define SPIRVCKRT(Condition, ErrCode, ErrMsg) \ |
| 74 |
if (!getErrorLog().checkError(Condition, SPIRVEC_##ErrCode, \ |
| 75 |
std::string() + (ErrMsg), #Condition, \ |
| 76 |
__FILE__, __LINE__)) \ |
| 77 |
return false; |
| 78 |
#endif // NDEBUG |
| 79 |
|
| 80 |
// Defines error code enum type SPIRVErrorCode. |
| 81 |
enum SPIRVErrorCode { |
| 82 |
#define _SPIRV_OP(x, y) SPIRVEC_##x, |
| 83 |
#include "SPIRVErrorEnum.h" |
| 84 |
#undef _SPIRV_OP |
| 85 |
}; |
| 86 |
|
| 87 |
// Defines SPIRVErrorMap which maps error code to a string describing the error. |
| 88 |
template <> inline void SPIRVMap<SPIRVErrorCode, std::string>::init() { |
| 89 |
#define _SPIRV_OP(x, y) add(SPIRVEC_##x, std::string(#x) + ": " + (y)); |
| 90 |
#include "SPIRVErrorEnum.h" |
| 91 |
#undef _SPIRV_OP |
| 92 |
} |
| 93 |
|
| 94 |
typedef SPIRVMap<SPIRVErrorCode, std::string> SPIRVErrorMap; |
| 95 |
|
| 96 |
class SPIRVErrorLog { |
| 97 |
public: |
| 98 |
SPIRVErrorLog() : ErrorCode(SPIRVEC_Success) {} |
| 99 |
SPIRVErrorCode getError(std::string &ErrMsg) { |
| 100 |
ErrMsg = ErrorMsg; |
| 101 |
return ErrorCode; |
| 102 |
} |
| 103 |
void setError(SPIRVErrorCode ErrCode, const std::string &ErrMsg) { |
| 104 |
ErrorCode = ErrCode; |
| 105 |
ErrorMsg = ErrMsg; |
| 106 |
} |
| 107 |
// Check if Condition is satisfied and set ErrCode and DetailedMsg |
| 108 |
// if not. Returns true if no error. |
| 109 |
bool checkError(bool Condition, SPIRVErrorCode ErrCode, |
| 110 |
const std::string &DetailedMsg = "", |
| 111 |
const char *CondString = nullptr, |
| 112 |
const char *FileName = nullptr, unsigned LineNumber = 0); |
| 113 |
// Check if Condition is satisfied and set ErrCode and DetailedMsg with Value |
| 114 |
// text representation if not. Returns true if no error. |
| 115 |
bool checkError(bool Condition, SPIRVErrorCode ErrCode, llvm::Value *Value, |
| 116 |
const std::string &DetailedMsg = "", |
| 117 |
const char *CondString = nullptr, |
| 118 |
const char *FileName = nullptr, unsigned LineNumber = 0); |
| 119 |
|
| 120 |
protected: |
| 121 |
SPIRVErrorCode ErrorCode; |
| 122 |
std::string ErrorMsg; |
| 123 |
}; |
| 124 |
|
| 125 |
inline bool SPIRVErrorLog::checkError(bool Cond, SPIRVErrorCode ErrCode, |
| 126 |
llvm::Value *Value, |
| 127 |
const std::string &Msg, |
| 128 |
const char *CondString, |
| 129 |
const char *FileName, unsigned LineNo) { |
| 130 |
// Do early exit to avoid expensive toString() function call unless it is |
| 131 |
// actually needed. That speeds up translator's execution. |
| 132 |
if (Cond) |
| 133 |
return Cond; |
| 134 |
// Do not overwrite previous failure. |
| 135 |
if (ErrorCode != SPIRVEC_Success) |
| 136 |
return Cond; |
| 137 |
std::string ValueIR = toString(Value); |
| 138 |
return checkError(Cond, ErrCode, Msg + "\n" + ValueIR, CondString, FileName, |
| 139 |
LineNo); |
| 140 |
} |
| 141 |
|
| 142 |
inline bool SPIRVErrorLog::checkError(bool Cond, SPIRVErrorCode ErrCode, |
| 143 |
const std::string &Msg, |
| 144 |
const char *CondString, |
| 145 |
const char *FileName, unsigned LineNo) { |
| 146 |
std::stringstream SS; |
| 147 |
if (Cond) |
| 148 |
return Cond; |
| 149 |
// Do not overwrite previous failure. |
| 150 |
if (ErrorCode != SPIRVEC_Success) |
| 151 |
return Cond; |
| 152 |
SS << SPIRVErrorMap::map(ErrCode) << " " << Msg; |
| 153 |
if (SPIRVDbgErrorMsgIncludesSourceInfo && FileName) |
| 154 |
SS << " [Src: " << FileName << ":" << LineNo << " " << CondString << " ]"; |
| 155 |
setError(ErrCode, SS.str()); |
| 156 |
switch (SPIRVDbgError) { |
| 157 |
case SPIRVDbgErrorHandlingKinds::Abort: |
| 158 |
std::cerr << SS.str() << std::endl; |
| 159 |
abort(); |
| 160 |
break; |
| 161 |
case SPIRVDbgErrorHandlingKinds::Exit: |
| 162 |
std::cerr << SS.str() << std::endl; |
| 163 |
std::exit(ErrCode); |
| 164 |
break; |
| 165 |
case SPIRVDbgErrorHandlingKinds::Ignore: |
| 166 |
// Still print info about the error into debug output stream |
| 167 |
// TODO: The value Ignore is not currently used but if it would be used |
| 168 |
// then places where this routine is called must be checked as just |
| 169 |
// ignoring the error may lead to NULL pointer dereferences |
| 170 |
spvdbgs() << SS.str() << '\n'; |
| 171 |
spvdbgs().flush(); |
| 172 |
break; |
| 173 |
} |
| 174 |
return Cond; |
| 175 |
} |
| 176 |
|
| 177 |
} // namespace SPIRV |
| 178 |
|
| 179 |
#endif // SPIRV_LIBSPIRV_SPIRVERROR_H |