Skip to content

Commit 911be97

Browse files
AlexeySachkovvmaksimo
authored andcommitted
Avoid verifying module in release builds (#712)
This is achieved by guarding most (only covered LLVM IR -> SPIR-V passes) `verifyModule` calls by corresponding command line option, which is disabled/enabled in accordance with _SPIRVDBG define. Made _SPIRVDBG define disabled in release builds by default. Adjusted SPIRVErrorLog::CheckError to not use `spvdbg` for printing information about errors.
1 parent 6476837 commit 911be97

12 files changed

+108
-114
lines changed

llvm-spirv/lib/SPIRV/OCL20ToSPIRV.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,15 @@
4040
#include "OCLTypeToSPIRV.h"
4141
#include "OCLUtil.h"
4242
#include "SPIRVInternal.h"
43+
#include "libSPIRV/SPIRVDebug.h"
4344

4445
#include "llvm/ADT/StringSwitch.h"
4546
#include "llvm/Analysis/ValueTracking.h"
4647
#include "llvm/IR/IRBuilder.h"
4748
#include "llvm/IR/InstVisitor.h"
4849
#include "llvm/IR/Instruction.h"
4950
#include "llvm/IR/Instructions.h"
50-
#include "llvm/IR/Verifier.h"
5151
#include "llvm/Pass.h"
52-
#include "llvm/Support/Debug.h"
5352

5453
#include <algorithm>
5554
#include <set>
@@ -349,11 +348,8 @@ bool OCL20ToSPIRV::runOnModule(Module &Module) {
349348
eraseUselessFunctions(M); // remove unused functions declarations
350349
LLVM_DEBUG(dbgs() << "After OCL20ToSPIRV:\n" << *M);
351350

352-
std::string Err;
353-
raw_string_ostream ErrorOS(Err);
354-
if (verifyModule(*M, &ErrorOS)) {
355-
LLVM_DEBUG(errs() << "Fails to verify module: " << ErrorOS.str());
356-
}
351+
verifyRegularizationPass(*M, "OCL20ToSPIRV");
352+
357353
return true;
358354
}
359355

llvm-spirv/lib/SPIRV/OCL21ToSPIRV.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@
3939

4040
#include "OCLUtil.h"
4141
#include "SPIRVInternal.h"
42+
#include "libSPIRV/SPIRVDebug.h"
43+
4244
#include "llvm/ADT/StringSwitch.h"
4345
#include "llvm/IR/IRBuilder.h"
4446
#include "llvm/IR/InstVisitor.h"
4547
#include "llvm/IR/Instructions.h"
46-
#include "llvm/IR/Verifier.h"
4748
#include "llvm/Pass.h"
48-
#include "llvm/Support/Debug.h"
4949

5050
#include <set>
5151

@@ -54,7 +54,6 @@ using namespace SPIRV;
5454
using namespace OCLUtil;
5555

5656
namespace SPIRV {
57-
5857
class OCL21ToSPIRV : public ModulePass, public InstVisitor<OCL21ToSPIRV> {
5958
public:
6059
OCL21ToSPIRV() : ModulePass(ID), M(nullptr), Ctx(nullptr), CLVer(0) {
@@ -122,11 +121,8 @@ bool OCL21ToSPIRV::runOnModule(Module &Module) {
122121
GV->eraseFromParent();
123122

124123
LLVM_DEBUG(dbgs() << "After OCL21ToSPIRV:\n" << *M);
125-
std::string Err;
126-
raw_string_ostream ErrorOS(Err);
127-
if (verifyModule(*M, &ErrorOS)) {
128-
LLVM_DEBUG(errs() << "Fails to verify module: " << ErrorOS.str());
129-
}
124+
verifyRegularizationPass(*M, "OCL21ToSPIRV");
125+
130126
return true;
131127
}
132128

llvm-spirv/lib/SPIRV/OCLUtil.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
#include "llvm/IR/IRBuilder.h"
4747
#include "llvm/IR/InstVisitor.h"
4848
#include "llvm/IR/Instructions.h"
49-
#include "llvm/IR/Verifier.h"
5049
#include "llvm/Pass.h"
5150
#include "llvm/Support/CommandLine.h"
5251
#include "llvm/Support/Debug.h"

llvm-spirv/lib/SPIRV/PreprocessMetadata.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@
4343
#include "SPIRVMDBuilder.h"
4444
#include "SPIRVMDWalker.h"
4545
#include "VectorComputeUtil.h"
46+
#include "libSPIRV/SPIRVDebug.h"
4647

4748
#include "llvm/ADT/Triple.h"
4849
#include "llvm/IR/IRBuilder.h"
4950
#include "llvm/IR/InstVisitor.h"
50-
#include "llvm/IR/Verifier.h"
5151
#include "llvm/Pass.h"
52-
#include "llvm/Support/CommandLine.h"
53-
#include "llvm/Support/Debug.h"
5452

5553
using namespace llvm;
5654
using namespace SPIRV;
@@ -88,13 +86,10 @@ bool PreprocessMetadata::runOnModule(Module &Module) {
8886

8987
LLVM_DEBUG(dbgs() << "Enter PreprocessMetadata:\n");
9088
visit(M);
91-
9289
LLVM_DEBUG(dbgs() << "After PreprocessMetadata:\n" << *M);
93-
std::string Err;
94-
raw_string_ostream ErrorOS(Err);
95-
if (verifyModule(*M, &ErrorOS)) {
96-
LLVM_DEBUG(errs() << "Fails to verify module: " << ErrorOS.str());
97-
}
90+
91+
verifyRegularizationPass(*M, "PreprocessMetadata");
92+
9893
return true;
9994
}
10095

llvm-spirv/lib/SPIRV/SPIRVLowerBool.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,17 @@
3838
#define DEBUG_TYPE "spvbool"
3939

4040
#include "SPIRVInternal.h"
41+
#include "libSPIRV/SPIRVDebug.h"
42+
4143
#include "llvm/IR/IRBuilder.h"
4244
#include "llvm/IR/InstVisitor.h"
4345
#include "llvm/IR/Instructions.h"
44-
#include "llvm/IR/Verifier.h"
4546
#include "llvm/Pass.h"
46-
#include "llvm/Support/CommandLine.h"
47-
#include "llvm/Support/Debug.h"
4847

4948
using namespace llvm;
5049
using namespace SPIRV;
5150

5251
namespace SPIRV {
53-
cl::opt<bool> SPIRVLowerBoolValidate(
54-
"spvbool-validate",
55-
cl::desc("Validate module after lowering boolean instructions for SPIR-V"));
56-
5752
class SPIRVLowerBool : public ModulePass, public InstVisitor<SPIRVLowerBool> {
5853
public:
5954
SPIRVLowerBool() : ModulePass(ID), Context(nullptr) {
@@ -119,15 +114,7 @@ class SPIRVLowerBool : public ModulePass, public InstVisitor<SPIRVLowerBool> {
119114
Context = &M.getContext();
120115
visit(M);
121116

122-
if (SPIRVLowerBoolValidate) {
123-
LLVM_DEBUG(dbgs() << "After SPIRVLowerBool:\n" << M);
124-
std::string Err;
125-
raw_string_ostream ErrorOS(Err);
126-
if (verifyModule(M, &ErrorOS)) {
127-
Err = std::string("Fails to verify module: ") + Err;
128-
report_fatal_error(Err.c_str(), false);
129-
}
130-
}
117+
verifyRegularizationPass(M, "SPIRVLowerBool");
131118
return true;
132119
}
133120

llvm-spirv/lib/SPIRV/SPIRVLowerConstExpr.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,14 @@
4141
#include "SPIRVInternal.h"
4242
#include "SPIRVMDBuilder.h"
4343
#include "SPIRVMDWalker.h"
44+
#include "libSPIRV/SPIRVDebug.h"
4445

4546
#include "llvm/ADT/StringSwitch.h"
4647
#include "llvm/ADT/Triple.h"
4748
#include "llvm/IR/IRBuilder.h"
4849
#include "llvm/IR/InstVisitor.h"
4950
#include "llvm/IR/Instructions.h"
50-
#include "llvm/IR/Verifier.h"
5151
#include "llvm/Pass.h"
52-
#include "llvm/Support/CommandLine.h"
53-
#include "llvm/Support/Debug.h"
5452

5553
#include <list>
5654
#include <set>
@@ -93,12 +91,8 @@ bool SPIRVLowerConstExpr::runOnModule(Module &Module) {
9391
LLVM_DEBUG(dbgs() << "Enter SPIRVLowerConstExpr:\n");
9492
visit(M);
9593

96-
LLVM_DEBUG(dbgs() << "After SPIRVLowerConstExpr:\n" << *M);
97-
std::string Err;
98-
raw_string_ostream ErrorOS(Err);
99-
if (verifyModule(*M, &ErrorOS)) {
100-
LLVM_DEBUG(errs() << "Fails to verify module: " << ErrorOS.str());
101-
}
94+
verifyRegularizationPass(*M, "SPIRVLowerConstExpr");
95+
10296
return true;
10397
}
10498

llvm-spirv/lib/SPIRV/SPIRVLowerMemmove.cpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,18 @@
3838
#define DEBUG_TYPE "spvmemmove"
3939

4040
#include "SPIRVInternal.h"
41+
#include "libSPIRV/SPIRVDebug.h"
42+
4143
#include "llvm/IR/IRBuilder.h"
4244
#include "llvm/IR/InstVisitor.h"
4345
#include "llvm/IR/IntrinsicInst.h"
4446
#include "llvm/IR/Module.h"
45-
#include "llvm/IR/Verifier.h"
4647
#include "llvm/Pass.h"
47-
#include "llvm/Support/CommandLine.h"
48-
#include "llvm/Support/Debug.h"
4948

5049
using namespace llvm;
5150
using namespace SPIRV;
5251

5352
namespace SPIRV {
54-
cl::opt<bool> SPIRVLowerMemmoveValidate(
55-
"spvmemmove-validate",
56-
cl::desc("Validate module after lowering llvm.memmove instructions into "
57-
"llvm.memcpy"));
58-
5953
class SPIRVLowerMemmove : public ModulePass,
6054
public InstVisitor<SPIRVLowerMemmove> {
6155
public:
@@ -119,15 +113,7 @@ class SPIRVLowerMemmove : public ModulePass,
119113
Mod = &M;
120114
visit(M);
121115

122-
if (SPIRVLowerMemmoveValidate) {
123-
LLVM_DEBUG(dbgs() << "After SPIRVLowerMemmove:\n" << M);
124-
std::string Err;
125-
raw_string_ostream ErrorOS(Err);
126-
if (verifyModule(M, &ErrorOS)) {
127-
Err = std::string("Fails to verify module: ") + Err;
128-
report_fatal_error(Err.c_str(), false);
129-
}
130-
}
116+
verifyRegularizationPass(M, "SPIRVLowerMemmove");
131117
return true;
132118
}
133119

llvm-spirv/lib/SPIRV/SPIRVLowerSaddWithOverflow.cpp

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,23 @@
3939
//===----------------------------------------------------------------------===//
4040
#define DEBUG_TYPE "spv-lower-llvm_sadd_with_overflow"
4141

42-
#include "LLVMSPIRVLib.h"
4342
#include "LLVMSaddWithOverflow.h"
43+
44+
#include "LLVMSPIRVLib.h"
4445
#include "SPIRVError.h"
46+
#include "libSPIRV/SPIRVDebug.h"
47+
4548
#include "llvm/IR/InstVisitor.h"
4649
#include "llvm/IR/IntrinsicInst.h"
4750
#include "llvm/IR/Module.h"
48-
#include "llvm/IR/Verifier.h"
4951
#include "llvm/IRReader/IRReader.h"
5052
#include "llvm/Linker/Linker.h"
5153
#include "llvm/Pass.h"
52-
#include "llvm/Support/CommandLine.h"
53-
#include "llvm/Support/Debug.h"
5454

5555
using namespace llvm;
5656
using namespace SPIRV;
5757

5858
namespace SPIRV {
59-
cl::opt<bool> SPIRVLowerSaddWithOverflowValidate(
60-
"spv-lower-saddwithoverflow-validate",
61-
cl::desc("Validate module after lowering llvm.sadd.with.overflow.*"
62-
"intrinsics"));
63-
6459
class SPIRVLowerSaddWithOverflow
6560
: public ModulePass,
6661
public InstVisitor<SPIRVLowerSaddWithOverflow> {
@@ -125,15 +120,7 @@ class SPIRVLowerSaddWithOverflow
125120
Mod = &M;
126121
visit(M);
127122

128-
if (SPIRVLowerSaddWithOverflowValidate) {
129-
LLVM_DEBUG(dbgs() << "After SPIRVLowerSaddWithOverflow:\n" << M);
130-
std::string Err;
131-
raw_string_ostream ErrorOS(Err);
132-
if (verifyModule(M, &ErrorOS)) {
133-
Err = std::string("Fails to verify module: ") + Err;
134-
report_fatal_error(Err.c_str(), false);
135-
}
136-
}
123+
verifyRegularizationPass(M, "SPIRVLowerSaddWithOverflow");
137124
return TheModuleIsModified;
138125
}
139126

llvm-spirv/lib/SPIRV/SPIRVRegularizeLLVM.cpp

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@
3939

4040
#include "OCLUtil.h"
4141
#include "SPIRVInternal.h"
42+
#include "libSPIRV/SPIRVDebug.h"
4243

4344
#include "llvm/IR/InstVisitor.h"
4445
#include "llvm/IR/Instructions.h"
4546
#include "llvm/IR/Operator.h"
46-
#include "llvm/IR/Verifier.h"
4747
#include "llvm/Pass.h"
48-
#include "llvm/Support/Debug.h"
4948

5049
#include <set>
5150
#include <vector>
@@ -90,13 +89,10 @@ bool SPIRVRegularizeLLVM::runOnModule(Module &Module) {
9089

9190
LLVM_DEBUG(dbgs() << "Enter SPIRVRegularizeLLVM:\n");
9291
regularize();
93-
9492
LLVM_DEBUG(dbgs() << "After SPIRVRegularizeLLVM:\n" << *M);
95-
std::string Err;
96-
raw_string_ostream ErrorOS(Err);
97-
if (verifyModule(*M, &ErrorOS)) {
98-
LLVM_DEBUG(errs() << "Fails to verify module: " << ErrorOS.str());
99-
}
93+
94+
verifyRegularizationPass(*M, "SPIRVRegularizeLLVM");
95+
10096
return true;
10197
}
10298

@@ -206,13 +202,6 @@ bool SPIRVRegularizeLLVM::regularize() {
206202
}
207203
}
208204

209-
std::string Err;
210-
raw_string_ostream ErrorOS(Err);
211-
if (verifyModule(*M, &ErrorOS)) {
212-
SPIRVDBG(errs() << "Fails to verify module: " << ErrorOS.str();)
213-
return false;
214-
}
215-
216205
if (SPIRVDbgSaveRegularizedModule)
217206
saveLLVMModule(M, RegularizedModuleTmpFile);
218207
return true;

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVDebug.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,33 @@
3939

4040
#include "SPIRVDebug.h"
4141

42+
#include "llvm/IR/Verifier.h"
43+
#include "llvm/Support/raw_ostream.h"
44+
45+
#define DEBUG_TYPE "spirv-regularization"
46+
4247
using namespace SPIRV;
4348

4449
bool SPIRV::SPIRVDbgEnable = false;
4550
SPIRV::SPIRVDbgErrorHandlingKinds SPIRV::SPIRVDbgError =
4651
SPIRVDbgErrorHandlingKinds::Exit;
4752
bool SPIRV::SPIRVDbgErrorMsgIncludesSourceInfo = true;
53+
54+
llvm::cl::opt<bool> SPIRV::VerifyRegularizationPasses(
55+
"spirv-verify-regularize-passes", llvm::cl::init(_SPIRVDBG),
56+
llvm::cl::desc(
57+
"Verify module after each pass in LLVM regularization phase"));
58+
59+
namespace SPIRV {
60+
void verifyRegularizationPass(llvm::Module &M, const std::string &PassName) {
61+
if (VerifyRegularizationPasses) {
62+
std::string Err;
63+
llvm::raw_string_ostream ErrorOS(Err);
64+
if (llvm::verifyModule(M, &ErrorOS)) {
65+
LLVM_DEBUG(llvm::errs()
66+
<< "Failed to verify module after pass: " << PassName << "\n"
67+
<< ErrorOS.str());
68+
}
69+
}
70+
}
71+
} // namespace SPIRV

0 commit comments

Comments
 (0)