Skip to content

Commit 00a4309

Browse files
[Backport to 15] add API call to display general information about the module (#2298) (#2699)
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 dbca87e commit 00a4309

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
@@ -109,6 +109,35 @@ std::unique_ptr<SPIRVModule> readSpirvModule(std::istream &IS,
109109
/// This contains a pair of the pointer element type and an indirection
110110
/// parameter (to capture cases where an array of OpenCL types is used).
111111
typedef llvm::PointerIntPair<llvm::Type *, 1, bool> PointerIndirectPair;
112+
struct SPIRVModuleReport {
113+
SPIRV::VersionNumber Version;
114+
uint32_t MemoryModel;
115+
uint32_t AddrModel;
116+
std::vector<std::string> Extensions;
117+
std::vector<std::string> ExtendedInstructionSets;
118+
std::vector<uint32_t> Capabilities;
119+
};
120+
/// \brief Partially load SPIR-V from the stream and decode only selected
121+
/// instructions that are needed to retrieve general information
122+
/// about the module. If this call fails, readSPIRVModule is
123+
/// expected to fail as well.
124+
/// \returns nullopt on failure.
125+
llvm::Optional<SPIRVModuleReport> getSpirvReport(std::istream &IS);
126+
llvm::Optional<SPIRVModuleReport> getSpirvReport(std::istream &IS,
127+
int &ErrCode);
128+
129+
struct SPIRVModuleTextReport {
130+
std::string Version;
131+
std::string MemoryModel;
132+
std::string AddrModel;
133+
std::vector<std::string> Extensions;
134+
std::vector<std::string> ExtendedInstructionSets;
135+
std::vector<std::string> Capabilities;
136+
};
137+
/// \brief Create a human-readable form of the report returned by a call to
138+
/// getSpirvReport by decoding its binary fields.
139+
/// \returns String with the human-readable report.
140+
SPIRVModuleTextReport formatSpirvReport(const SPIRVModuleReport &Report);
112141

113142
} // End namespace SPIRV
114143

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
@@ -4852,6 +4852,137 @@ Instruction *SPIRVToLLVM::transRelational(SPIRVInstruction *I, BasicBlock *BB) {
48524852
&BtnInfo, &Attrs, /*TakeFuncName=*/true)));
48534853
}
48544854

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

204+
static cl::opt<bool>
205+
SPIRVPrintReport("spirv-print-report", cl::init(false),
206+
cl::desc("Display general information about the module "
207+
"(capabilities, extensions, version, memory model"
208+
" and addressing model)"));
209+
204210
static cl::opt<SPIRV::FPContractMode> FPCMode(
205211
"spirv-fp-contract", cl::desc("Set FP Contraction mode:"),
206212
cl::init(SPIRV::FPContractMode::On),
@@ -776,7 +782,7 @@ int main(int Ac, char **Av) {
776782
return convertSPIRV();
777783
#endif
778784

779-
if (!IsReverse && !IsRegularization && !SpecConstInfo)
785+
if (!IsReverse && !IsRegularization && !SpecConstInfo && !SPIRVPrintReport)
780786
return convertLLVMToSPIRV(Opts);
781787

782788
if (IsReverse && IsRegularization) {
@@ -802,5 +808,39 @@ int main(int Ac, char **Av) {
802808
std::cout << "Spec const id = " << SpecConst.first
803809
<< ", size in bytes = " << SpecConst.second << "\n";
804810
}
811+
812+
if (SPIRVPrintReport) {
813+
std::ifstream IFS(InputFile, std::ios::binary);
814+
int ErrCode = 0;
815+
llvm::Optional<SPIRV::SPIRVModuleReport> BinReport =
816+
SPIRV::getSpirvReport(IFS, ErrCode);
817+
if (!BinReport) {
818+
std::cerr << "Invalid SPIR-V binary, error code is " << ErrCode << "\n";
819+
return -1;
820+
}
821+
822+
SPIRV::SPIRVModuleTextReport TextReport =
823+
SPIRV::formatSpirvReport(BinReport.value());
824+
825+
std::cout << "SPIR-V module report:"
826+
<< "\n Version: " << TextReport.Version
827+
<< "\n Memory model: " << TextReport.MemoryModel
828+
<< "\n Addressing model: " << TextReport.AddrModel << "\n";
829+
830+
std::cout << " Number of capabilities: " << TextReport.Capabilities.size()
831+
<< "\n";
832+
for (auto &Capability : TextReport.Capabilities)
833+
std::cout << " Capability: " << Capability << "\n";
834+
835+
std::cout << " Number of extensions: " << TextReport.Extensions.size()
836+
<< "\n";
837+
for (auto &Extension : TextReport.Extensions)
838+
std::cout << " Extension: " << Extension << "\n";
839+
840+
std::cout << " Number of extended instruction sets: "
841+
<< TextReport.ExtendedInstructionSets.size() << "\n";
842+
for (auto &ExtendedInstructionSet : TextReport.ExtendedInstructionSets)
843+
std::cout << " Extended Instruction Set: " << ExtendedInstructionSet << "\n";
844+
}
805845
return 0;
806846
}

0 commit comments

Comments
 (0)