Skip to content

Commit

Permalink
[TargetVerifier][AMDGPU] Add TargetVerifier.
Browse files Browse the repository at this point in the history
This pass verifies the IR for an individual backend. This is
different than Lint because it consolidates all checks for a
given backend in a single pass. A check for Lint may be
undefined behavior across all targets, whereas a check in
TargetVerifier would only pertain to the specified target
but can check more than just undefined behavior such are IR
validity. A use case of this would be to reject programs
with invalid IR while fuzzing.
  • Loading branch information
jofrn committed Jan 20, 2025
1 parent 1c5b122 commit 053aac8
Show file tree
Hide file tree
Showing 10 changed files with 631 additions and 7 deletions.
4 changes: 4 additions & 0 deletions llvm/include/llvm/IR/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ class LLVM_ABI Module {
/// @name Constructors
/// @{
public:
/// Is this Module valid as determined by one of the verification passes
/// i.e. Lint, Verifier, TargetVerifier.
bool IsValid = true;

/// Is this Module using intrinsics to record the position of debugging
/// information, or non-intrinsic records? See IsNewDbgInfoFormat in
/// \ref BasicBlock.
Expand Down
82 changes: 82 additions & 0 deletions llvm/include/llvm/Target/TargetVerifier.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//===-- llvm/Target/TargetVerifier.h - LLVM IR Target Verifier ---*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines target verifier interfaces that can be used for some
// validation of input to the system, and for checking that transformations
// haven't done something bad. In contrast to the Verifier or Lint, the
// TargetVerifier looks for constructions invalid to a particular target
// machine.
//
// To see what specifically is checked, look at TargetVerifier.cpp or an
// individual backend's TargetVerifier.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_TARGET_VERIFIER_H
#define LLVM_TARGET_VERIFIER_H

#include "llvm/IR/PassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/TargetParser/Triple.h"

namespace llvm {

class Function;

class TargetVerifierPass : public PassInfoMixin<TargetVerifierPass> {
public:
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) {}
};

class TargetVerify {
protected:
void WriteValues(ArrayRef<const Value *> Vs) {
for (const Value *V : Vs) {
if (!V)
continue;
if (isa<Instruction>(V)) {
MessagesStr << *V << '\n';
} else {
V->printAsOperand(MessagesStr, true, Mod);
MessagesStr << '\n';
}
}
}

/// A check failed, so printout out the condition and the message.
///
/// This provides a nice place to put a breakpoint if you want to see why
/// something is not correct.
void CheckFailed(const Twine &Message) { MessagesStr << Message << '\n'; }

/// A check failed (with values to print).
///
/// This calls the Message-only version so that the above is easier to set
/// a breakpoint on.
template <typename T1, typename... Ts>
void CheckFailed(const Twine &Message, const T1 &V1, const Ts &... Vs) {
CheckFailed(Message);
WriteValues({V1, Vs...});
}
public:
Module *Mod;
Triple TT;

std::string Messages;
raw_string_ostream MessagesStr;

TargetVerify(Module *Mod)
: Mod(Mod), TT(Triple::normalize(Mod->getTargetTriple())),
MessagesStr(Messages) {}

void run(Function &F) {};
};

} // namespace llvm

#endif // LLVM_TARGET_VERIFIER_H
36 changes: 36 additions & 0 deletions llvm/include/llvm/Target/TargetVerify/AMDGPUTargetVerifier.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//===-- llvm/Target/TargetVerify/AMDGPUTargetVerifier.h - AMDGPU ---*- C++ -*-===//
////
//// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
//// See https://llvm.org/LICENSE.txt for license information.
//// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
////
////===----------------------------------------------------------------------===//
////
//// This file defines target verifier interfaces that can be used for some
//// validation of input to the system, and for checking that transformations
//// haven't done something bad. In contrast to the Verifier or Lint, the
//// TargetVerifier looks for constructions invalid to a particular target
//// machine.
////
//// To see what specifically is checked, look at an individual backend's
//// TargetVerifier.
////
////===----------------------------------------------------------------------===//

#ifndef LLVM_AMDGPU_TARGET_VERIFIER_H
#define LLVM_AMDGPU_TARGET_VERIFIER_H

#include "llvm/Target/TargetVerifier.h"

namespace llvm {

class Function;

class AMDGPUTargetVerifierPass : public TargetVerifierPass {
public:
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
};

} // namespace llvm

#endif // LLVM_AMDGPU_TARGET_VERIFIER_H
9 changes: 6 additions & 3 deletions llvm/lib/Analysis/Lint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -747,10 +747,13 @@ PreservedAnalyses LintPass::run(Function &F, FunctionAnalysisManager &AM) {
Lint L(Mod, DL, AA, AC, DT, TLI);
L.visit(F);
dbgs() << L.MessagesStr.str();
if (LintAbortOnError && !L.MessagesStr.str().empty())
report_fatal_error(Twine("Linter found errors, aborting. (enabled by --") +
if (!L.MessagesStr.str().empty()) {
F.getParent()->IsValid = false;
if (LintAbortOnError)
report_fatal_error(Twine("Linter found errors, aborting. (enabled by --") +
LintAbortOnErrorArgName + ")",
false);
false);
}
return PreservedAnalyses::all();
}

Expand Down
18 changes: 14 additions & 4 deletions llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ static cl::opt<bool> VerifyNoAliasScopeDomination(
cl::desc("Ensure that llvm.experimental.noalias.scope.decl for identical "
"scopes are not dominating"));

static cl::opt<bool>
VerifyAbortOnError("verifier-abort-on-error", cl::init(false),
cl::desc("In the Verifier pass, abort on errors."));

namespace llvm {

struct VerifierSupport {
Expand Down Expand Up @@ -7739,16 +7743,22 @@ VerifierAnalysis::Result VerifierAnalysis::run(Function &F,

PreservedAnalyses VerifierPass::run(Module &M, ModuleAnalysisManager &AM) {
auto Res = AM.getResult<VerifierAnalysis>(M);
if (FatalErrors && (Res.IRBroken || Res.DebugInfoBroken))
report_fatal_error("Broken module found, compilation aborted!");
if (Res.IRBroken || Res.DebugInfoBroken) {
M.IsValid = false;
if (VerifyAbortOnError && FatalErrors)
report_fatal_error("Broken module found, compilation aborted!");
}

return PreservedAnalyses::all();
}

PreservedAnalyses VerifierPass::run(Function &F, FunctionAnalysisManager &AM) {
auto res = AM.getResult<VerifierAnalysis>(F);
if (res.IRBroken && FatalErrors)
report_fatal_error("Broken function found, compilation aborted!");
if (res.IRBroken) {
F.getParent()->IsValid = false;
if (VerifyAbortOnError && FatalErrors)
report_fatal_error("Broken function found, compilation aborted!");
}

return PreservedAnalyses::all();
}
Loading

0 comments on commit 053aac8

Please sign in to comment.