Skip to content

Commit 54fdef7

Browse files
committed
Auto merge of rust-lang#121614 - clubby789:no-expect, r=saethlin
Don't emit `expect`/`assume` in opt-level=0 LLVM does not make use of expect/assume calls in `opt-level=0`, so we can simplify IR by not emitting them in this case.
2 parents 9c01301 + 5b96ae7 commit 54fdef7

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

compiler/rustc_codegen_llvm/src/intrinsic.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,7 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
187187
Some(instance),
188188
)
189189
}
190-
sym::likely => {
191-
self.call_intrinsic("llvm.expect.i1", &[args[0].immediate(), self.const_bool(true)])
192-
}
190+
sym::likely => self.expect(args[0].immediate(), true),
193191
sym::is_val_statically_known => {
194192
let intrinsic_type = args[0].layout.immediate_llvm_type(self.cx);
195193
let kind = self.type_kind(intrinsic_type);
@@ -210,8 +208,7 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
210208
self.const_bool(false)
211209
}
212210
}
213-
sym::unlikely => self
214-
.call_intrinsic("llvm.expect.i1", &[args[0].immediate(), self.const_bool(false)]),
211+
sym::unlikely => self.expect(args[0].immediate(), false),
215212
sym::select_unpredictable => {
216213
let cond = args[0].immediate();
217214
assert_eq!(args[1].layout, args[2].layout);
@@ -604,11 +601,17 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
604601
}
605602

606603
fn assume(&mut self, val: Self::Value) {
607-
self.call_intrinsic("llvm.assume", &[val]);
604+
if self.cx.sess().opts.optimize != rustc_session::config::OptLevel::No {
605+
self.call_intrinsic("llvm.assume", &[val]);
606+
}
608607
}
609608

610609
fn expect(&mut self, cond: Self::Value, expected: bool) -> Self::Value {
611-
self.call_intrinsic("llvm.expect.i1", &[cond, self.const_bool(expected)])
610+
if self.cx.sess().opts.optimize != rustc_session::config::OptLevel::No {
611+
self.call_intrinsic("llvm.expect.i1", &[cond, self.const_bool(expected)])
612+
} else {
613+
cond
614+
}
612615
}
613616

614617
fn type_test(&mut self, pointer: Self::Value, typeid: Self::Value) -> Self::Value {

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
382382
scalar: abi::Scalar,
383383
backend_ty: Bx::Type,
384384
) {
385-
if matches!(self.cx.sess().opts.optimize, OptLevel::No | OptLevel::Less)
385+
if matches!(self.cx.sess().opts.optimize, OptLevel::No)
386386
// For now, the critical niches are all over `Int`eger values.
387387
// Should floating-point values or pointers ever get more complex
388388
// niches, then this code will probably want to handle them too.

compiler/rustc_codegen_ssa/src/mir/statement.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use rustc_middle::mir::{self, NonDivergingIntrinsic};
22
use rustc_middle::span_bug;
3-
use rustc_session::config::OptLevel;
43
use tracing::instrument;
54

65
use super::{FunctionCx, LocalRef};
@@ -68,10 +67,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
6867
self.codegen_coverage(bx, kind, statement.source_info.scope);
6968
}
7069
mir::StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(ref op)) => {
71-
if !matches!(bx.tcx().sess.opts.optimize, OptLevel::No | OptLevel::Less) {
72-
let op_val = self.codegen_operand(bx, op);
73-
bx.assume(op_val.immediate());
74-
}
70+
let op_val = self.codegen_operand(bx, op);
71+
bx.assume(op_val.immediate());
7572
}
7673
mir::StatementKind::Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping(
7774
mir::CopyNonOverlapping { ref count, ref src, ref dst },

tests/codegen/intrinsics/likely.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ compile-flags: -C no-prepopulate-passes
1+
//@ compile-flags: -C no-prepopulate-passes -Copt-level=1
22

33
#![crate_type = "lib"]
44
#![feature(core_intrinsics)]

0 commit comments

Comments
 (0)