Skip to content

Commit d5c40d0

Browse files
Rollup merge of #128970 - DianQK:lint-llvm-ir, r=nikic
Add `-Zlint-llvm-ir` This flag is similar to `-Zverify-llvm-ir` and allows us to lint the generated IR. r? compiler
2 parents 4b08b2e + 9589eb9 commit d5c40d0

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
@@ -571,6 +571,7 @@ pub(crate) unsafe fn llvm_optimize(
571571
cgcx.opts.cg.linker_plugin_lto.enabled(),
572572
config.no_prepopulate_passes,
573573
config.verify_llvm_ir,
574+
config.lint_llvm_ir,
574575
using_thin_buffers,
575576
config.merge_functions,
576577
unroll_loops,

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2225,6 +2225,7 @@ unsafe extern "C" {
22252225
IsLinkerPluginLTO: bool,
22262226
NoPrepopulatePasses: bool,
22272227
VerifyIR: bool,
2228+
LintIR: bool,
22282229
UseThinLTOBuffers: bool,
22292230
MergeFunctions: bool,
22302231
UnrollLoops: bool,

compiler/rustc_codegen_ssa/src/back/write.rs

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

239240
verify_llvm_ir: sess.verify_llvm_ir(),
241+
lint_llvm_ir: sess.opts.unstable_opts.lint_llvm_ir,
240242
no_prepopulate_passes: sess.opts.cg.no_prepopulate_passes,
241243
no_builtins: no_builtins || sess.target.no_builtins,
242244

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,7 @@ fn test_unstable_options_tracking_hash() {
795795
tracked!(instrument_xray, Some(InstrumentXRay::default()));
796796
tracked!(link_directives, false);
797797
tracked!(link_only, true);
798+
tracked!(lint_llvm_ir, true);
798799
tracked!(llvm_module_flag, vec![("bar".to_string(), 123, "max".to_string())]);
799800
tracked!(llvm_plugins, vec![String::from("plugin_name")]);
800801
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
@@ -1811,6 +1811,8 @@ options! {
18111811
"link the `.rlink` file generated by `-Z no-link` (default: no)"),
18121812
linker_features: LinkerFeaturesCli = (LinkerFeaturesCli::default(), parse_linker_features, [UNTRACKED],
18131813
"a comma-separated list of linker features to enable (+) or disable (-): `lld`"),
1814+
lint_llvm_ir: bool = (false, parse_bool, [TRACKED],
1815+
"lint LLVM IR (default: no)"),
18141816
lint_mir: bool = (false, parse_bool, [UNTRACKED],
18151817
"lint MIR before and after each transformation"),
18161818
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)