Skip to content

Commit b90004b

Browse files
[Backport to 16] add API call to display general information about the module (#2298) (#2693)
Partially load SPIR-V from the stream and decode only selected for the report instructions, needed to retrieve general information about the module: capabilities, extensions, version, memory model and addressing model. In addition to immediately helpful for back-ends lists of capabilities and extensions declared in SPIR-V module, a general intent also is to extend report details in future by feedbacks about further potentially useful analysis, statistics, etc. Co-authored-by: Vyacheslav Levytskyy <89994100+VyacheslavLevytskyy@users.noreply.github.com>
1 parent 1f9e0e3 commit b90004b

File tree

8 files changed

+305
-4
lines changed

8 files changed

+305
-4
lines changed

include/LLVMSPIRVLib.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,35 @@ std::unique_ptr<SPIRVModule> readSpirvModule(std::istream &IS,
106106
const SPIRV::TranslatorOpts &Opts,
107107
std::string &ErrMsg);
108108

109+
struct SPIRVModuleReport {
110+
SPIRV::VersionNumber Version;
111+
uint32_t MemoryModel;
112+
uint32_t AddrModel;
113+
std::vector<std::string> Extensions;
114+
std::vector<std::string> ExtendedInstructionSets;
115+
std::vector<uint32_t> Capabilities;
116+
};
117+
/// \brief Partially load SPIR-V from the stream and decode only selected
118+
/// instructions that are needed to retrieve general information
119+
/// about the module. If this call fails, readSPIRVModule is
120+
/// expected to fail as well.
121+
/// \returns nullopt on failure.
122+
std::optional<SPIRVModuleReport> getSpirvReport(std::istream &IS);
123+
std::optional<SPIRVModuleReport> getSpirvReport(std::istream &IS, int &ErrCode);
124+
125+
struct SPIRVModuleTextReport {
126+
std::string Version;
127+
std::string MemoryModel;
128+
std::string AddrModel;
129+
std::vector<std::string> Extensions;
130+
std::vector<std::string> ExtendedInstructionSets;
131+
std::vector<std::string> Capabilities;
132+
};
133+
/// \brief Create a human-readable form of the report returned by a call to
134+
/// getSpirvReport by decoding its binary fields.
135+
/// \returns String with the human-readable report.
136+
SPIRVModuleTextReport formatSpirvReport(const SPIRVModuleReport &Report);
137+
109138
} // End namespace SPIRV
110139

111140
namespace llvm {

include/LLVMSPIRVOpts.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,27 @@ enum class VersionNumber : uint32_t {
6969
MaximumVersion = SPIRV_1_6
7070
};
7171

72+
inline constexpr std::string_view formatVersionNumber(uint32_t Version) {
73+
switch (Version) {
74+
case static_cast<uint32_t>(VersionNumber::SPIRV_1_0):
75+
return "1.0";
76+
case static_cast<uint32_t>(VersionNumber::SPIRV_1_1):
77+
return "1.1";
78+
case static_cast<uint32_t>(VersionNumber::SPIRV_1_2):
79+
return "1.2";
80+
case static_cast<uint32_t>(VersionNumber::SPIRV_1_3):
81+
return "1.3";
82+
case static_cast<uint32_t>(VersionNumber::SPIRV_1_4):
83+
return "1.4";
84+
}
85+
return "unknown";
86+
}
87+
88+
inline bool isSPIRVVersionKnown(uint32_t Ver) {
89+
return Ver >= static_cast<uint32_t>(VersionNumber::MinimumVersion) &&
90+
Ver <= static_cast<uint32_t>(VersionNumber::MaximumVersion);
91+
}
92+
7293
enum class ExtensionID : uint32_t {
7394
First,
7495
#define EXT(X) X,

lib/SPIRV/SPIRVReader.cpp

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4759,6 +4759,137 @@ Instruction *SPIRVToLLVM::transRelational(SPIRVInstruction *I, BasicBlock *BB) {
47594759
return cast<Instruction>(Mutator.getMutated());
47604760
}
47614761

4762+
std::optional<SPIRVModuleReport> getSpirvReport(std::istream &IS) {
4763+
int IgnoreErrCode;
4764+
return getSpirvReport(IS, IgnoreErrCode);
4765+
}
4766+
4767+
std::optional<SPIRVModuleReport> getSpirvReport(std::istream &IS,
4768+
int &ErrCode) {
4769+
SPIRVWord Word;
4770+
std::string Name;
4771+
std::unique_ptr<SPIRVModule> BM(SPIRVModule::createSPIRVModule());
4772+
SPIRVDecoder D(IS, *BM);
4773+
D >> Word;
4774+
if (Word != MagicNumber) {
4775+
ErrCode = SPIRVEC_InvalidMagicNumber;
4776+
return {};
4777+
}
4778+
D >> Word;
4779+
if (!isSPIRVVersionKnown(Word)) {
4780+
ErrCode = SPIRVEC_InvalidVersionNumber;
4781+
return {};
4782+
}
4783+
SPIRVModuleReport Report;
4784+
Report.Version = static_cast<SPIRV::VersionNumber>(Word);
4785+
// Skip: Generator’s magic number, Bound and Reserved word
4786+
D.ignore(3);
4787+
4788+
bool IsReportGenCompleted = false, IsMemoryModelDefined = false;
4789+
while (!IS.bad() && !IsReportGenCompleted && D.getWordCountAndOpCode()) {
4790+
switch (D.OpCode) {
4791+
case OpCapability:
4792+
D >> Word;
4793+
Report.Capabilities.push_back(Word);
4794+
break;
4795+
case OpExtension:
4796+
Name.clear();
4797+
D >> Name;
4798+
Report.Extensions.push_back(Name);
4799+
break;
4800+
case OpExtInstImport:
4801+
Name.clear();
4802+
D >> Word >> Name;
4803+
Report.ExtendedInstructionSets.push_back(Name);
4804+
break;
4805+
case OpMemoryModel:
4806+
if (IsMemoryModelDefined) {
4807+
ErrCode = SPIRVEC_RepeatedMemoryModel;
4808+
return {};
4809+
}
4810+
SPIRVAddressingModelKind AddrModel;
4811+
SPIRVMemoryModelKind MemoryModel;
4812+
D >> AddrModel >> MemoryModel;
4813+
if (!isValid(AddrModel)) {
4814+
ErrCode = SPIRVEC_InvalidAddressingModel;
4815+
return {};
4816+
}
4817+
if (!isValid(MemoryModel)) {
4818+
ErrCode = SPIRVEC_InvalidMemoryModel;
4819+
return {};
4820+
}
4821+
Report.MemoryModel = MemoryModel;
4822+
Report.AddrModel = AddrModel;
4823+
IsMemoryModelDefined = true;
4824+
// In this report we don't analyze instructions after OpMemoryModel
4825+
IsReportGenCompleted = true;
4826+
break;
4827+
default:
4828+
// No more instructions to gather information about
4829+
IsReportGenCompleted = true;
4830+
}
4831+
}
4832+
if (IS.bad()) {
4833+
ErrCode = SPIRVEC_InvalidModule;
4834+
return {};
4835+
}
4836+
if (!IsMemoryModelDefined) {
4837+
ErrCode = SPIRVEC_UnspecifiedMemoryModel;
4838+
return {};
4839+
}
4840+
ErrCode = SPIRVEC_Success;
4841+
return std::make_optional(std::move(Report));
4842+
}
4843+
4844+
constexpr std::string_view formatAddressingModel(uint32_t AddrModel) {
4845+
switch (AddrModel) {
4846+
case AddressingModelLogical:
4847+
return "Logical";
4848+
case AddressingModelPhysical32:
4849+
return "Physical32";
4850+
case AddressingModelPhysical64:
4851+
return "Physical64";
4852+
case AddressingModelPhysicalStorageBuffer64:
4853+
return "PhysicalStorageBuffer64";
4854+
default:
4855+
return "Unknown";
4856+
}
4857+
}
4858+
4859+
constexpr std::string_view formatMemoryModel(uint32_t MemoryModel) {
4860+
switch (MemoryModel) {
4861+
case MemoryModelSimple:
4862+
return "Simple";
4863+
case MemoryModelGLSL450:
4864+
return "GLSL450";
4865+
case MemoryModelOpenCL:
4866+
return "OpenCL";
4867+
case MemoryModelVulkan:
4868+
return "Vulkan";
4869+
default:
4870+
return "Unknown";
4871+
}
4872+
}
4873+
4874+
SPIRVModuleTextReport formatSpirvReport(const SPIRVModuleReport &Report) {
4875+
SPIRVModuleTextReport TextReport;
4876+
TextReport.Version =
4877+
formatVersionNumber(static_cast<uint32_t>(Report.Version));
4878+
TextReport.AddrModel = formatAddressingModel(Report.AddrModel);
4879+
TextReport.MemoryModel = formatMemoryModel(Report.MemoryModel);
4880+
// format capability codes as strings
4881+
std::string Name;
4882+
for (auto Capability : Report.Capabilities) {
4883+
bool Found = SPIRVCapabilityNameMap::find(
4884+
static_cast<SPIRVCapabilityKind>(Capability), &Name);
4885+
TextReport.Capabilities.push_back(Found ? Name : "Unknown");
4886+
}
4887+
// other fields with string content can be copied as is
4888+
TextReport.Extensions = Report.Extensions;
4889+
TextReport.ExtendedInstructionSets = Report.ExtendedInstructionSets;
4890+
return TextReport;
4891+
}
4892+
47624893
std::unique_ptr<SPIRVModule> readSpirvModule(std::istream &IS,
47634894
const SPIRV::TranslatorOpts &Opts,
47644895
std::string &ErrMsg) {

lib/SPIRV/libSPIRV/SPIRVErrorEnum.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ _SPIRV_OP(Requires1_1, "Feature requires SPIR-V 1.1 or greater:")
2323
_SPIRV_OP(RequiresVersion, "Cannot fulfill SPIR-V version restriction:\n")
2424
_SPIRV_OP(RequiresExtension,
2525
"Feature requires the following SPIR-V extension:\n")
26+
_SPIRV_OP(InvalidMagicNumber,
27+
"Invalid Magic Number.")
28+
_SPIRV_OP(InvalidVersionNumber,
29+
"Invalid Version Number.")
30+
_SPIRV_OP(UnspecifiedMemoryModel, "Unspecified Memory Model.")
31+
_SPIRV_OP(RepeatedMemoryModel, "Expects a single OpMemoryModel instruction.")

lib/SPIRV/libSPIRV/SPIRVModule.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,9 +2053,7 @@ std::istream &operator>>(std::istream &I, SPIRVModule &M) {
20532053
}
20542054

20552055
Decoder >> MI.SPIRVVersion;
2056-
bool SPIRVVersionIsKnown =
2057-
static_cast<uint32_t>(VersionNumber::MinimumVersion) <= MI.SPIRVVersion &&
2058-
MI.SPIRVVersion <= static_cast<uint32_t>(VersionNumber::MaximumVersion);
2056+
bool SPIRVVersionIsKnown = isSPIRVVersionKnown(MI.SPIRVVersion);
20592057
if (!M.getErrorLog().checkError(
20602058
SPIRVVersionIsKnown, SPIRVEC_InvalidModule,
20612059
"unsupported SPIR-V version number '" + to_string(MI.SPIRVVersion) +
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
; RUN: llvm-spirv %s -to-binary -o %t.spv
2+
; The next line is to corrupt the binary file by changing its Magic Number
3+
; RUN: echo "0" > %t_corrupted.spv && cat %t.spv >> %t_corrupted.spv
4+
; RUN: not llvm-spirv --spirv-print-report %t_corrupted.spv 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR
5+
;
6+
; CHECK-ERROR: Invalid SPIR-V binary
7+
8+
119734787 65536 393230 10 0
9+
2 Capability Addresses
10+
2 Capability Kernel
11+
2 Capability LoopFuseINTEL
12+
2 Capability BitInstructions
13+
6 Extension "SPV_INTEL_loop_fuse"
14+
8 Extension "SPV_KHR_bit_instructions"
15+
5 ExtInstImport 1 "OpenCL.std"
16+
3 MemoryModel 1 2
17+
7 EntryPoint 6 5 "TestSatPacked"
18+
3 Source 3 102000
19+
20+
5 Decorate 5 FuseLoopsInFunctionINTEL 3 1
21+
4 TypeInt 3 32 0
22+
2 TypeVoid 2
23+
5 TypeFunction 4 2 3 3
24+
25+
5 Function 2 5 0 4
26+
3 FunctionParameter 3 6
27+
3 FunctionParameter 3 7
28+
29+
2 Label 8
30+
4 BitReverse 3 9 6
31+
1 Return
32+
33+
1 FunctionEnd

test/spirv_report.spt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
; RUN: llvm-spirv %s -to-binary -o %t.spv
2+
; RUN: llvm-spirv --spirv-print-report %t.spv | FileCheck %s --check-prefix=CHECK-DAG
3+
4+
; CHECK-DAG: Version: 1.0
5+
; CHECK-DAG: Memory model: OpenCL
6+
; CHECK-DAG: Addressing model: Physical32
7+
; CHECK-DAG: Number of capabilities: 4
8+
; CHECK-DAG: Capability: Addresses
9+
; CHECK-DAG: Capability: Kernel
10+
; CHECK-DAG: Capability: LoopFuseINTEL
11+
; CHECK-DAG: Capability: BitInstructions
12+
; CHECK-DAG: Number of extensions: 2
13+
; CHECK-DAG: Extension: SPV_INTEL_loop_fuse
14+
; CHECK-DAG: Extension: SPV_KHR_bit_instructions
15+
; CHECK-DAG: Number of extended instruction sets: 1
16+
; CHECK-DAG: Extended Instruction Set: OpenCL.std
17+
18+
119734787 65536 393230 10 0
19+
2 Capability Addresses
20+
2 Capability Kernel
21+
2 Capability LoopFuseINTEL
22+
2 Capability BitInstructions
23+
6 Extension "SPV_INTEL_loop_fuse"
24+
8 Extension "SPV_KHR_bit_instructions"
25+
5 ExtInstImport 1 "OpenCL.std"
26+
3 MemoryModel 1 2
27+
7 EntryPoint 6 5 "TestSatPacked"
28+
3 Source 3 102000
29+
30+
5 Decorate 5 FuseLoopsInFunctionINTEL 3 1
31+
4 TypeInt 3 32 0
32+
2 TypeVoid 2
33+
5 TypeFunction 4 2 3 3
34+
35+
5 Function 2 5 0 4
36+
3 FunctionParameter 3 6
37+
3 FunctionParameter 3 7
38+
39+
2 Label 8
40+
4 BitReverse 3 9 6
41+
1 Return
42+
43+
1 FunctionEnd

tools/llvm-spirv/llvm-spirv.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ static cl::opt<bool> SpecConstInfo(
204204
cl::desc("Display id of constants available for specializaion and their "
205205
"size in bytes"));
206206

207+
static cl::opt<bool>
208+
SPIRVPrintReport("spirv-print-report", cl::init(false),
209+
cl::desc("Display general information about the module "
210+
"(capabilities, extensions, version, memory model"
211+
" and addressing model)"));
212+
207213
static cl::opt<SPIRV::FPContractMode> FPCMode(
208214
"spirv-fp-contract", cl::desc("Set FP Contraction mode:"),
209215
cl::init(SPIRV::FPContractMode::On),
@@ -787,7 +793,7 @@ int main(int Ac, char **Av) {
787793
return convertSPIRV();
788794
#endif
789795

790-
if (!IsReverse && !IsRegularization && !SpecConstInfo)
796+
if (!IsReverse && !IsRegularization && !SpecConstInfo && !SPIRVPrintReport)
791797
return convertLLVMToSPIRV(Opts);
792798

793799
if (IsReverse && IsRegularization) {
@@ -814,5 +820,39 @@ int main(int Ac, char **Av) {
814820
<< ", size in bytes = " << SpecConst.Size
815821
<< ", type = " << SpecConst.Type << "\n";
816822
}
823+
824+
if (SPIRVPrintReport) {
825+
std::ifstream IFS(InputFile, std::ios::binary);
826+
int ErrCode = 0;
827+
std::optional<SPIRV::SPIRVModuleReport> BinReport =
828+
SPIRV::getSpirvReport(IFS, ErrCode);
829+
if (!BinReport) {
830+
std::cerr << "Invalid SPIR-V binary, error code is " << ErrCode << "\n";
831+
return -1;
832+
}
833+
834+
SPIRV::SPIRVModuleTextReport TextReport =
835+
SPIRV::formatSpirvReport(BinReport.value());
836+
837+
std::cout << "SPIR-V module report:"
838+
<< "\n Version: " << TextReport.Version
839+
<< "\n Memory model: " << TextReport.MemoryModel
840+
<< "\n Addressing model: " << TextReport.AddrModel << "\n";
841+
842+
std::cout << " Number of capabilities: " << TextReport.Capabilities.size()
843+
<< "\n";
844+
for (auto &Capability : TextReport.Capabilities)
845+
std::cout << " Capability: " << Capability << "\n";
846+
847+
std::cout << " Number of extensions: " << TextReport.Extensions.size()
848+
<< "\n";
849+
for (auto &Extension : TextReport.Extensions)
850+
std::cout << " Extension: " << Extension << "\n";
851+
852+
std::cout << " Number of extended instruction sets: "
853+
<< TextReport.ExtendedInstructionSets.size() << "\n";
854+
for (auto &ExtendedInstructionSet : TextReport.ExtendedInstructionSets)
855+
std::cout << " Extended Instruction Set: " << ExtendedInstructionSet << "\n";
856+
}
817857
return 0;
818858
}

0 commit comments

Comments
 (0)