Skip to content

Commit e1be3ea

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

File tree

10 files changed

+26
-3
lines changed

10 files changed

+26
-3
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_llvm/src/llvm_util.rs

+4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ fn require_inited() {
4646

4747
unsafe fn configure_llvm(sess: &Session) {
4848
let n_args = sess.opts.cg.llvm_args.len() + sess.target.llvm_args.len();
49+
let llvm_version = get_version();
4950
let mut llvm_c_strs = Vec::with_capacity(n_args + 1);
5051
let mut llvm_args = Vec::with_capacity(n_args + 1);
5152

@@ -90,6 +91,9 @@ unsafe fn configure_llvm(sess: &Session) {
9091
if sess.opts.unstable_opts.print_llvm_passes {
9192
add("-debug-pass=Structure", false);
9293
}
94+
if llvm_version >= (19, 0, 0) && sess.opts.unstable_opts.lint_llvm_ir {
95+
add("-lint-abort-on-error", false);
96+
}
9397
if sess.target.generate_arange_section
9498
&& !sess.opts.unstable_opts.no_generate_arange_section
9599
{

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,5 @@
1+
# `lint-llvm-ir`
2+
3+
---------------------
4+
5+
This flag will add `LintPass` to the start of the pipeline and set `-lint-abort-on-error`.

tests/codegen/cast-target-abi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ignore-tidy-linelength
22
//@ revisions:aarch64 loongarch64 powerpc64 sparc64 x86_64
33
// FIXME: Add `-Cllvm-args=--lint-abort-on-error` after LLVM 19
4-
//@ compile-flags: -O -C no-prepopulate-passes -C passes=lint
4+
//@ compile-flags: -O -C no-prepopulate-passes -Zlint-llvm-ir
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ revisions: linux apple
2-
//@ compile-flags: -C opt-level=0 -C no-prepopulate-passes -C passes=lint
2+
//@ compile-flags: -C opt-level=0 -C no-prepopulate-passes -Zlint-llvm-ir
33

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

0 commit comments

Comments
 (0)