Skip to content

Commit 9589eb9

Browse files
committed
Add -Zlint-llvm-ir
1 parent c9bd03c commit 9589eb9

File tree

9 files changed

+26
-4
lines changed

9 files changed

+26
-4
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

+1
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ pub(crate) unsafe fn llvm_optimize(
573573
cgcx.opts.cg.linker_plugin_lto.enabled(),
574574
config.no_prepopulate_passes,
575575
config.verify_llvm_ir,
576+
config.lint_llvm_ir,
576577
using_thin_buffers,
577578
config.merge_functions,
578579
unroll_loops,

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2217,6 +2217,7 @@ extern "C" {
22172217
IsLinkerPluginLTO: bool,
22182218
NoPrepopulatePasses: bool,
22192219
VerifyIR: bool,
2220+
LintIR: bool,
22202221
UseThinLTOBuffers: bool,
22212222
MergeFunctions: bool,
22222223
UnrollLoops: bool,

compiler/rustc_codegen_ssa/src/back/write.rs

+2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ pub struct ModuleConfig {
111111
// Miscellaneous flags. These are mostly copied from command-line
112112
// options.
113113
pub verify_llvm_ir: bool,
114+
pub lint_llvm_ir: bool,
114115
pub no_prepopulate_passes: bool,
115116
pub no_builtins: bool,
116117
pub time_module: bool,
@@ -236,6 +237,7 @@ impl ModuleConfig {
236237
bc_cmdline: sess.target.bitcode_llvm_cmdline.to_string(),
237238

238239
verify_llvm_ir: sess.verify_llvm_ir(),
240+
lint_llvm_ir: sess.opts.unstable_opts.lint_llvm_ir,
239241
no_prepopulate_passes: sess.opts.cg.no_prepopulate_passes,
240242
no_builtins: no_builtins || sess.target.no_builtins,
241243

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,7 @@ fn test_unstable_options_tracking_hash() {
793793
tracked!(instrument_xray, Some(InstrumentXRay::default()));
794794
tracked!(link_directives, false);
795795
tracked!(link_only, true);
796+
tracked!(lint_llvm_ir, true);
796797
tracked!(llvm_module_flag, vec![("bar".to_string(), 123, "max".to_string())]);
797798
tracked!(llvm_plugins, vec![String::from("plugin_name")]);
798799
tracked!(location_detail, LocationDetail { file: true, line: false, column: false });

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ extern "C" LLVMRustResult LLVMRustOptimize(
713713
LLVMModuleRef ModuleRef, LLVMTargetMachineRef TMRef,
714714
LLVMRustPassBuilderOptLevel OptLevelRust, LLVMRustOptStage OptStage,
715715
bool IsLinkerPluginLTO, bool NoPrepopulatePasses, bool VerifyIR,
716-
bool UseThinLTOBuffers, bool MergeFunctions, bool UnrollLoops,
716+
bool LintIR, bool UseThinLTOBuffers, bool MergeFunctions, bool UnrollLoops,
717717
bool SLPVectorize, bool LoopVectorize, bool DisableSimplifyLibCalls,
718718
bool EmitLifetimeMarkers, LLVMRustSanitizerOptions *SanitizerOptions,
719719
const char *PGOGenPath, const char *PGOUsePath, bool InstrumentCoverage,
@@ -842,6 +842,13 @@ extern "C" LLVMRustResult LLVMRustOptimize(
842842
});
843843
}
844844

845+
if (LintIR) {
846+
PipelineStartEPCallbacks.push_back(
847+
[](ModulePassManager &MPM, OptimizationLevel Level) {
848+
MPM.addPass(createModuleToFunctionPassAdaptor(LintPass()));
849+
});
850+
}
851+
845852
if (InstrumentGCOV) {
846853
PipelineStartEPCallbacks.push_back(
847854
[](ModulePassManager &MPM, OptimizationLevel Level) {

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,8 @@ options! {
17951795
"link the `.rlink` file generated by `-Z no-link` (default: no)"),
17961796
linker_features: LinkerFeaturesCli = (LinkerFeaturesCli::default(), parse_linker_features, [UNTRACKED],
17971797
"a comma-separated list of linker features to enable (+) or disable (-): `lld`"),
1798+
lint_llvm_ir: bool = (false, parse_bool, [TRACKED],
1799+
"lint LLVM IR (default: no)"),
17981800
lint_mir: bool = (false, parse_bool, [UNTRACKED],
17991801
"lint MIR before and after each transformation"),
18001802
llvm_module_flag: Vec<(String, u32, String)> = (Vec::new(), parse_llvm_module_flag, [TRACKED],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# `lint-llvm-ir`
2+
3+
---------------------
4+
5+
This flag will add `LintPass` to the start of the pipeline.
6+
You can use it to check for common errors in the LLVM IR generated by `rustc`.
7+
You can add `-Cllvm-args=-lint-abort-on-error` to abort the process if errors were found.

tests/codegen/cast-target-abi.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ignore-tidy-linelength
22
//@ revisions:aarch64 loongarch64 powerpc64 sparc64 x86_64
3-
// FIXME: Add `-Cllvm-args=--lint-abort-on-error` after LLVM 19
4-
//@ compile-flags: -O -C no-prepopulate-passes -C passes=lint
3+
//@ min-llvm-version: 19
4+
//@ compile-flags: -O -Cno-prepopulate-passes -Zlint-llvm-ir -Cllvm-args=-lint-abort-on-error
55

66
//@[aarch64] compile-flags: --target aarch64-unknown-linux-gnu
77
//@[aarch64] needs-llvm-components: arm

tests/codegen/cffi/ffi-out-of-bounds-loads.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//@ revisions: linux apple
2-
//@ compile-flags: -C opt-level=0 -C no-prepopulate-passes -C passes=lint
2+
//@ min-llvm-version: 19
3+
//@ compile-flags: -Copt-level=0 -Cno-prepopulate-passes -Zlint-llvm-ir -Cllvm-args=-lint-abort-on-error
34

45
//@[linux] compile-flags: --target x86_64-unknown-linux-gnu
56
//@[linux] needs-llvm-components: x86

0 commit comments

Comments
 (0)