Skip to content

Commit f0ac661

Browse files
VyacheslavLevytskyysys-ce-bb
authored andcommitted
add API to query error message by an error code (#2304)
The goal of the PR is to add API to SPIR-V LLVM Translator to query error message by an error code as discussed in #2298 A need and possible application is a way to generate human-readable error info by error codes returned by other SPIRV Translator API calls, including getSpirvReport(). Original commit: KhronosGroup/SPIRV-LLVM-Translator@afe1971
1 parent 6f35f7c commit f0ac661

File tree

6 files changed

+67
-3
lines changed

6 files changed

+67
-3
lines changed

llvm-spirv/include/LLVMSPIRVLib.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ struct SPIRVModuleTextReport {
135135
/// \returns String with the human-readable report.
136136
SPIRVModuleTextReport formatSpirvReport(const SPIRVModuleReport &Report);
137137

138+
/// \brief Returns the message associated with the error code.
139+
/// \returns empty string if no known error code is found.
140+
std::string getErrorMessage(int ErrCode);
141+
138142
} // End namespace SPIRV
139143

140144
namespace llvm {

llvm-spirv/lib/SPIRV/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ set(SRC_LIST
3838
libSPIRV/SPIRVStream.cpp
3939
libSPIRV/SPIRVType.cpp
4040
libSPIRV/SPIRVValue.cpp
41+
libSPIRV/SPIRVError.cpp
4142
)
4243
add_llvm_library(LLVMSPIRVLib
4344
${SRC_LIST}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===- SPIRVError.cpp - SPIR-V error code and checking ----------*- C++ -*-===//
2+
//
3+
// The LLVM/SPIR-V 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) 2024 The Khronos Group Inc.
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 The Khronos Group, 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 implements SPIRV error code and checking utility.
36+
//
37+
//===----------------------------------------------------------------------===//
38+
39+
#include "SPIRVError.h"
40+
#include <string>
41+
42+
namespace SPIRV {
43+
44+
// Return the message associated with the error code. If error code is invalid,
45+
// return the message associated with InternalMaxErrorCode.
46+
std::string getErrorMessage(int ErrCode) {
47+
std::string ErrorMessage;
48+
bool Found =
49+
(ErrCode >= SPIRVEC_Success && ErrCode < SPIRVEC_InternalMaxErrorCode)
50+
? SPIRVErrorMap::find(static_cast<SPIRVErrorCode>(ErrCode),
51+
&ErrorMessage)
52+
: false;
53+
return Found ? ErrorMessage : std::string("Unknown error code");
54+
}
55+
56+
} // namespace SPIRV

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVErrorEnum.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* The error code name should be meaningful since it is part of error message */
2-
_SPIRV_OP(Success, "")
2+
_SPIRV_OP(Success, "Success")
33
_SPIRV_OP(InvalidTargetTriple,
44
"Expects spir-unknown-unknown or spir64-unknown-unknown.")
55
_SPIRV_OP(InvalidSubArch, "Expecting v1.0-v1.4.")
@@ -28,3 +28,6 @@ _SPIRV_OP(InvalidVersionNumber,
2828
"Invalid Version Number.")
2929
_SPIRV_OP(UnspecifiedMemoryModel, "Unspecified Memory Model.")
3030
_SPIRV_OP(RepeatedMemoryModel, "Expects a single OpMemoryModel instruction.")
31+
32+
/* This is the last error code to have a maximum valid value to compare to */
33+
_SPIRV_OP(InternalMaxErrorCode, "Unknown error code")

llvm-spirv/test/negative/spirv_report_bad_input.spt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
; RUN: echo "0" > %t_corrupted.spv && cat %t.spv >> %t_corrupted.spv
44
; RUN: not llvm-spirv --spirv-print-report %t_corrupted.spv 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR
55
;
6-
; CHECK-ERROR: Invalid SPIR-V binary
6+
; CHECK-ERROR: Invalid SPIR-V binary: "InvalidMagicNumber: Invalid Magic Number."
77

88
119734787 65536 393230 10 0
99
2 Capability Addresses

llvm-spirv/tools/llvm-spirv/llvm-spirv.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ int main(int Ac, char **Av) {
837837
std::optional<SPIRV::SPIRVModuleReport> BinReport =
838838
SPIRV::getSpirvReport(IFS, ErrCode);
839839
if (!BinReport) {
840-
std::cerr << "Invalid SPIR-V binary, error code is " << ErrCode << "\n";
840+
std::cerr << "Invalid SPIR-V binary: \"" << SPIRV::getErrorMessage(ErrCode) << "\"\n";
841841
return -1;
842842
}
843843

0 commit comments

Comments
 (0)