Skip to content

Lint: Replace -lint-abort-on-error cl::opt with pass parameter #132933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions llvm/include/llvm/Analysis/Lint.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,20 @@ class Function;
///
/// This should only be used for debugging, because it plays games with
/// PassManagers and stuff.
void lintModule(const Module &M);
void lintModule(const Module &M, bool AbortOnError = false);

// Lint a function.
void lintFunction(const Function &F);
void lintFunction(const Function &F, bool AbortOnError = false);

class LintPass : public PassInfoMixin<LintPass> {
const bool AbortOnError;

public:
LintPass(bool AbortOnError) : AbortOnError(AbortOnError) {}
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);

void printPipeline(raw_ostream &OS,
function_ref<StringRef(StringRef)> MapClassName2PassName);
};

} // namespace llvm
Expand Down
28 changes: 15 additions & 13 deletions llvm/lib/Analysis/Lint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@

using namespace llvm;

static const char LintAbortOnErrorArgName[] = "lint-abort-on-error";
static cl::opt<bool>
LintAbortOnError(LintAbortOnErrorArgName, cl::init(false),
cl::desc("In the Lint pass, abort on errors."));

namespace {
namespace MemRef {
static const unsigned Read = 1;
Expand Down Expand Up @@ -747,20 +742,27 @@ 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 --") +
LintAbortOnErrorArgName + ")",
false);
if (AbortOnError && !L.MessagesStr.str().empty())
report_fatal_error(
"linter found errors, aborting. (enabled by abort-on-error)", false);
return PreservedAnalyses::all();
}

void LintPass::printPipeline(
raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
static_cast<PassInfoMixin<LintPass> *>(this)->printPipeline(
OS, MapClassName2PassName);
if (AbortOnError)
OS << "<abort-on-error>";
}

//===----------------------------------------------------------------------===//
// Implement the public interfaces to this file...
//===----------------------------------------------------------------------===//

/// lintFunction - Check a function for errors, printing messages on stderr.
///
void llvm::lintFunction(const Function &f) {
void llvm::lintFunction(const Function &f, bool AbortOnError) {
Function &F = const_cast<Function &>(f);
assert(!F.isDeclaration() && "Cannot lint external functions");

Expand All @@ -775,14 +777,14 @@ void llvm::lintFunction(const Function &f) {
AA.registerFunctionAnalysis<TypeBasedAA>();
return AA;
});
LintPass().run(F, FAM);
LintPass(AbortOnError).run(F, FAM);
}

/// lintModule - Check a module for errors, printing messages on stderr.
///
void llvm::lintModule(const Module &M) {
void llvm::lintModule(const Module &M, bool AbortOnError) {
for (const Function &F : M) {
if (!F.isDeclaration())
lintFunction(F);
lintFunction(F, AbortOnError);
}
}
6 changes: 6 additions & 0 deletions llvm/lib/Passes/PassBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,12 @@ Expected<HardwareLoopOptions> parseHardwareLoopOptions(StringRef Params) {
return HardwareLoopOpts;
}

/// Parser of parameters for Lint pass.
Expected<bool> parseLintOptions(StringRef Params) {
return PassBuilder::parseSinglePassOption(Params, "abort-on-error",
"LintPass");
}

/// Parser of parameters for LoopUnroll pass.
Expected<LoopUnrollOptions> parseLoopUnrollOptions(StringRef Params) {
LoopUnrollOptions UnrollOpts;
Expand Down
6 changes: 5 additions & 1 deletion llvm/lib/Passes/PassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ FUNCTION_PASS("kcfi", KCFIPass())
FUNCTION_PASS("kernel-info", KernelInfoPrinter(TM))
FUNCTION_PASS("lcssa", LCSSAPass())
FUNCTION_PASS("libcalls-shrinkwrap", LibCallsShrinkWrapPass())
FUNCTION_PASS("lint", LintPass())
FUNCTION_PASS("load-store-vectorizer", LoadStoreVectorizerPass())
FUNCTION_PASS("loop-data-prefetch", LoopDataPrefetchPass())
FUNCTION_PASS("loop-distribute", LoopDistributePass())
Expand Down Expand Up @@ -543,6 +542,11 @@ FUNCTION_PASS_WITH_PARAMS(
parseInstCombineOptions,
"no-use-loop-info;use-loop-info;no-verify-fixpoint;verify-fixpoint;"
"max-iterations=N")
FUNCTION_PASS_WITH_PARAMS(
"lint", "LintPass",
[](bool AbortOnError) { return LintPass(AbortOnError); },
parseLintOptions,
"abort-on-error")
FUNCTION_PASS_WITH_PARAMS(
"loop-unroll", "LoopUnrollPass",
[](LoopUnrollOptions Opts) { return LoopUnrollPass(Opts); },
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Analysis/Lint/abort-on-error.ll
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
; RUN: not opt -passes=lint -disable-output --lint-abort-on-error %s 2>&1 | FileCheck %s
; RUN: not opt -passes='lint<abort-on-error>' -disable-output %s 2>&1 | FileCheck %s

; CHECK: Undefined behavior: Division by zero
; CHECK-NEXT: %b = sdiv i32 %a, 0
; CHECK-NEXT: LLVM ERROR: Linter found errors, aborting. (enabled by --lint-abort-on-error)
; CHECK-NEXT: LLVM ERROR: linter found errors, aborting. (enabled by abort-on-error)

define i32 @sdiv_by_zero(i32 %a) {
%b = sdiv i32 %a, 0
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Analysis/Lint/const-store.ll
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; RUN: not opt --mtriple=amdgcn --passes=lint --lint-abort-on-error %s -disable-output 2>&1 | FileCheck %s
; RUN: not opt --mtriple=amdgcn --passes='lint<abort-on-error>' %s -disable-output 2>&1 | FileCheck %s
; RUN: opt --mtriple=amdgcn --mcpu=gfx1030 --passes=lint %s -disable-output 2>&1 | FileCheck %s --check-prefixes=CHECK,CHECK0
; RUN: opt --mtriple=x86_64 --passes=lint --lint-abort-on-error %s -disable-output 2>&1 | FileCheck %s --allow-empty --check-prefix=NOERR
; RUN: opt --mtriple=x86_64 --passes='lint<abort-on-error>' %s -disable-output 2>&1 | FileCheck %s --allow-empty --check-prefix=NOERR
; NOERR: {{^$}}

define amdgpu_kernel void @store_const(ptr addrspace(4) %out, i32 %a, i32 %b) {
Expand Down