Skip to content

Commit a53b216

Browse files
[Backport to 14] add API call to display general information about the module (#2298) (#2698)
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 <[email protected]>
1 parent 1bbf856 commit a53b216

File tree

8 files changed

+306
-4
lines changed

8 files changed

+306
-4
lines changed

include/LLVMSPIRVLib.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,36 @@ 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+
llvm::Optional<SPIRVModuleReport> getSpirvReport(std::istream &IS);
123+
llvm::Optional<SPIRVModuleReport> getSpirvReport(std::istream &IS,
124+
int &ErrCode);
125+
126+
struct SPIRVModuleTextReport {
127+
std::string Version;
128+
std::string MemoryModel;
129+
std::string AddrModel;
130+
std::vector<std::string> Extensions;
131+
std::vector<std::string> ExtendedInstructionSets;
132+
std::vector<std::string> Capabilities;
133+
};
134+
/// \brief Create a human-readable form of the report returned by a call to
135+
/// getSpirvReport by decoding its binary fields.
136+
/// \returns String with the human-readable report.
137+
SPIRVModuleTextReport formatSpirvReport(const SPIRVModuleReport &Report);
138+
109139
} // End namespace SPIRV
110140

111141
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 std::string 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
@@ -4866,6 +4866,137 @@ Instruction *SPIRVToLLVM::transRelational(SPIRVInstruction *I, BasicBlock *BB) {
48664866
&BtnInfo, &Attrs, /*TakeFuncName=*/true)));
48674867
}
48684868

4869+
llvm::Optional<SPIRVModuleReport> getSpirvReport(std::istream &IS) {
4870+
int IgnoreErrCode;
4871+
return getSpirvReport(IS, IgnoreErrCode);
4872+
}
4873+
4874+
llvm::Optional<SPIRVModuleReport> getSpirvReport(std::istream &IS,
4875+
int &ErrCode) {
4876+
SPIRVWord Word;
4877+
std::string Name;
4878+
std::unique_ptr<SPIRVModule> BM(SPIRVModule::createSPIRVModule());
4879+
SPIRVDecoder D(IS, *BM);
4880+
D >> Word;
4881+
if (Word != MagicNumber) {
4882+
ErrCode = SPIRVEC_InvalidMagicNumber;
4883+
return {};
4884+
}
4885+
D >> Word;
4886+
if (!isSPIRVVersionKnown(Word)) {
4887+
ErrCode = SPIRVEC_InvalidVersionNumber;
4888+
return {};
4889+
}
4890+
SPIRVModuleReport Report;
4891+
Report.Version = static_cast<SPIRV::VersionNumber>(Word);
4892+
// Skip: Generator’s magic number, Bound and Reserved word
4893+
D.ignore(3);
4894+
4895+
bool IsReportGenCompleted = false, IsMemoryModelDefined = false;
4896+
while (!IS.bad() && !IsReportGenCompleted && D.getWordCountAndOpCode()) {
4897+
switch (D.OpCode) {
4898+
case OpCapability:
4899+
D >> Word;
4900+
Report.Capabilities.push_back(Word);
4901+
break;
4902+
case OpExtension:
4903+
Name.clear();
4904+
D >> Name;
4905+
Report.Extensions.push_back(Name);
4906+
break;
4907+
case OpExtInstImport:
4908+
Name.clear();
4909+
D >> Word >> Name;
4910+
Report.ExtendedInstructionSets.push_back(Name);
4911+
break;
4912+
case OpMemoryModel:
4913+
if (IsMemoryModelDefined) {
4914+
ErrCode = SPIRVEC_RepeatedMemoryModel;
4915+
return {};
4916+
}
4917+
SPIRVAddressingModelKind AddrModel;
4918+
SPIRVMemoryModelKind MemoryModel;
4919+
D >> AddrModel >> MemoryModel;
4920+
if (!isValid(AddrModel)) {
4921+
ErrCode = SPIRVEC_InvalidAddressingModel;
4922+
return {};
4923+
}
4924+
if (!isValid(MemoryModel)) {
4925+
ErrCode = SPIRVEC_InvalidMemoryModel;
4926+
return {};
4927+
}
4928+
Report.MemoryModel = MemoryModel;
4929+
Report.AddrModel = AddrModel;
4930+
IsMemoryModelDefined = true;
4931+
// In this report we don't analyze instructions after OpMemoryModel
4932+
IsReportGenCompleted = true;
4933+
break;
4934+
default:
4935+
// No more instructions to gather information about
4936+
IsReportGenCompleted = true;
4937+
}
4938+
}
4939+
if (IS.bad()) {
4940+
ErrCode = SPIRVEC_InvalidModule;
4941+
return {};
4942+
}
4943+
if (!IsMemoryModelDefined) {
4944+
ErrCode = SPIRVEC_UnspecifiedMemoryModel;
4945+
return {};
4946+
}
4947+
ErrCode = SPIRVEC_Success;
4948+
return llvm::Optional<SPIRV::SPIRVModuleReport>(std::move(Report));
4949+
}
4950+
4951+
std::string formatAddressingModel(uint32_t AddrModel) {
4952+
switch (AddrModel) {
4953+
case AddressingModelLogical:
4954+
return "Logical";
4955+
case AddressingModelPhysical32:
4956+
return "Physical32";
4957+
case AddressingModelPhysical64:
4958+
return "Physical64";
4959+
case AddressingModelPhysicalStorageBuffer64:
4960+
return "PhysicalStorageBuffer64";
4961+
default:
4962+
return "Unknown";
4963+
}
4964+
}
4965+
4966+
std::string formatMemoryModel(uint32_t MemoryModel) {
4967+
switch (MemoryModel) {
4968+
case MemoryModelSimple:
4969+
return "Simple";
4970+
case MemoryModelGLSL450:
4971+
return "GLSL450";
4972+
case MemoryModelOpenCL:
4973+
return "OpenCL";
4974+
case MemoryModelVulkan:
4975+
return "Vulkan";
4976+
default:
4977+
return "Unknown";
4978+
}
4979+
}
4980+
4981+
SPIRVModuleTextReport formatSpirvReport(const SPIRVModuleReport &Report) {
4982+
SPIRVModuleTextReport TextReport;
4983+
TextReport.Version =
4984+
formatVersionNumber(static_cast<uint32_t>(Report.Version));
4985+
TextReport.AddrModel = formatAddressingModel(Report.AddrModel);
4986+
TextReport.MemoryModel = formatMemoryModel(Report.MemoryModel);
4987+
// format capability codes as strings
4988+
std::string Name;
4989+
for (auto Capability : Report.Capabilities) {
4990+
const bool Found = SPIRVCapabilityNameMap::find(
4991+
static_cast<SPIRVCapabilityKind>(Capability), &Name);
4992+
TextReport.Capabilities.push_back(Found ? Name : "Unknown");
4993+
}
4994+
// other fields with string content can be copied as is
4995+
TextReport.Extensions = Report.Extensions;
4996+
TextReport.ExtendedInstructionSets = Report.ExtendedInstructionSets;
4997+
return TextReport;
4998+
}
4999+
48695000
std::unique_ptr<SPIRVModule> readSpirvModule(std::istream &IS,
48705001
const SPIRV::TranslatorOpts &Opts,
48715002
std::string &ErrMsg) {

lib/SPIRV/libSPIRV/SPIRVErrorEnum.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ _SPIRV_OP(Requires1_1, "Feature requires SPIR-V 1.1 or greater:")
2020
_SPIRV_OP(RequiresVersion, "Cannot fulfill SPIR-V version restriction:\n")
2121
_SPIRV_OP(RequiresExtension,
2222
"Feature requires the following SPIR-V extension:\n")
23+
_SPIRV_OP(InvalidMagicNumber,
24+
"Invalid Magic Number.")
25+
_SPIRV_OP(InvalidVersionNumber,
26+
"Invalid Version Number.")
27+
_SPIRV_OP(UnspecifiedMemoryModel, "Unspecified Memory Model.")
28+
_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
@@ -2138,9 +2138,7 @@ std::istream &operator>>(std::istream &I, SPIRVModule &M) {
21382138
}
21392139

21402140
Decoder >> MI.SPIRVVersion;
2141-
bool SPIRVVersionIsKnown =
2142-
static_cast<uint32_t>(VersionNumber::MinimumVersion) <= MI.SPIRVVersion &&
2143-
MI.SPIRVVersion <= static_cast<uint32_t>(VersionNumber::MaximumVersion);
2141+
const bool SPIRVVersionIsKnown = isSPIRVVersionKnown(MI.SPIRVVersion);
21442142
if (!M.getErrorLog().checkError(
21452143
SPIRVVersionIsKnown, SPIRVEC_InvalidModule,
21462144
"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
@@ -194,6 +194,12 @@ static cl::opt<bool> SpecConstInfo(
194194
cl::desc("Display id of constants available for specializaion and their "
195195
"size in bytes"));
196196

197+
static cl::opt<bool>
198+
SPIRVPrintReport("spirv-print-report", cl::init(false),
199+
cl::desc("Display general information about the module "
200+
"(capabilities, extensions, version, memory model"
201+
" and addressing model)"));
202+
197203
static cl::opt<SPIRV::FPContractMode> FPCMode(
198204
"spirv-fp-contract", cl::desc("Set FP Contraction mode:"),
199205
cl::init(SPIRV::FPContractMode::On),
@@ -767,7 +773,7 @@ int main(int Ac, char **Av) {
767773
return convertSPIRV();
768774
#endif
769775

770-
if (!IsReverse && !IsRegularization && !SpecConstInfo)
776+
if (!IsReverse && !IsRegularization && !SpecConstInfo && !SPIRVPrintReport)
771777
return convertLLVMToSPIRV(Opts);
772778

773779
if (IsReverse && IsRegularization) {
@@ -793,5 +799,39 @@ int main(int Ac, char **Av) {
793799
std::cout << "Spec const id = " << SpecConst.first
794800
<< ", size in bytes = " << SpecConst.second << "\n";
795801
}
802+
803+
if (SPIRVPrintReport) {
804+
std::ifstream IFS(InputFile, std::ios::binary);
805+
int ErrCode = 0;
806+
llvm::Optional<SPIRV::SPIRVModuleReport> BinReport =
807+
SPIRV::getSpirvReport(IFS, ErrCode);
808+
if (!BinReport) {
809+
std::cerr << "Invalid SPIR-V binary, error code is " << ErrCode << "\n";
810+
return -1;
811+
}
812+
813+
SPIRV::SPIRVModuleTextReport TextReport =
814+
SPIRV::formatSpirvReport(*BinReport);
815+
816+
std::cout << "SPIR-V module report:"
817+
<< "\n Version: " << TextReport.Version
818+
<< "\n Memory model: " << TextReport.MemoryModel
819+
<< "\n Addressing model: " << TextReport.AddrModel << "\n";
820+
821+
std::cout << " Number of capabilities: " << TextReport.Capabilities.size()
822+
<< "\n";
823+
for (auto &Capability : TextReport.Capabilities)
824+
std::cout << " Capability: " << Capability << "\n";
825+
826+
std::cout << " Number of extensions: " << TextReport.Extensions.size()
827+
<< "\n";
828+
for (auto &Extension : TextReport.Extensions)
829+
std::cout << " Extension: " << Extension << "\n";
830+
831+
std::cout << " Number of extended instruction sets: "
832+
<< TextReport.ExtendedInstructionSets.size() << "\n";
833+
for (auto &ExtendedInstructionSet : TextReport.ExtendedInstructionSets)
834+
std::cout << " Extended Instruction Set: " << ExtendedInstructionSet << "\n";
835+
}
796836
return 0;
797837
}

0 commit comments

Comments
 (0)