Skip to content

Commit cff7ad5

Browse files
authored
[SandboxVec][Utils] Implement Utils::verifyFunction() (llvm#124356)
This patch implements a wrapper function for the LLVM IR verifier for functions, and calls it (flag-guarded) within the bottom-up-vectorizer for finding IR bugs as soon as they happen.
1 parent 05fd4d5 commit cff7ad5

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

llvm/include/llvm/SandboxIR/Utils.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "llvm/Analysis/MemoryLocation.h"
1818
#include "llvm/Analysis/ScalarEvolution.h"
1919
#include "llvm/Analysis/ValueTracking.h"
20+
#include "llvm/IR/Verifier.h"
21+
#include "llvm/SandboxIR/Function.h"
2022
#include "llvm/SandboxIR/Instruction.h"
2123
#include <optional>
2224

@@ -122,6 +124,13 @@ class Utils {
122124
const std::optional<MemoryLocation> &OptLoc) {
123125
return BatchAA.getModRefInfo(cast<llvm::Instruction>(I->Val), OptLoc);
124126
}
127+
128+
/// Equivalent to llvm::verifyFunction().
129+
/// \Returns true if the IR is broken.
130+
static bool verifyFunction(const Function *F, raw_ostream &OS) {
131+
const auto &LLVMF = *cast<llvm::Function>(F->Val);
132+
return llvm::verifyFunction(LLVMF, &OS);
133+
}
125134
};
126135

127136
} // namespace llvm::sandboxir

llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ static cl::opt<bool>
2727
AllowNonPow2("sbvec-allow-non-pow2", cl::init(false), cl::Hidden,
2828
cl::desc("Allow non-power-of-2 vectorization."));
2929

30+
#ifndef NDEBUG
31+
static cl::opt<bool>
32+
AlwaysVerify("sbvec-always-verify", cl::init(false), cl::Hidden,
33+
cl::desc("Helps find bugs by verifying the IR whenever we "
34+
"emit new instructions (*very* expensive)."));
35+
#endif // NDEBUG
36+
3037
namespace sandboxir {
3138

3239
BottomUpVec::BottomUpVec(StringRef Pipeline)
@@ -365,6 +372,17 @@ Value *BottomUpVec::vectorizeRec(ArrayRef<Value *> Bndl,
365372
break;
366373
}
367374
}
375+
#ifndef NDEBUG
376+
if (AlwaysVerify) {
377+
// This helps find broken IR by constantly verifying the function. Note that
378+
// this is very expensive and should only be used for debugging.
379+
Instruction *I0 = isa<Instruction>(Bndl[0])
380+
? cast<Instruction>(Bndl[0])
381+
: cast<Instruction>(UserBndl[0]);
382+
assert(!Utils::verifyFunction(I0->getParent()->getParent(), dbgs()) &&
383+
"Broken function!");
384+
}
385+
#endif
368386
return NewVec;
369387
}
370388

0 commit comments

Comments
 (0)