Skip to content

Commit ee041f8

Browse files
committed
Emit assume(false) as store i1 true, ptr poison, align 1
Store to poison is the canonical form for non-terminator unreachable.
1 parent d9284af commit ee041f8

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

compiler/rustc_codegen_llvm/src/intrinsic.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,20 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
566566
}
567567

568568
fn assume(&mut self, val: Self::Value) {
569-
self.call_intrinsic("llvm.assume", &[val]);
569+
match self.const_to_opt_uint(val) {
570+
Some(0) => {
571+
// See https://github.com/llvm/llvm-project/blob/1347b9a3aa671d610e812579ab5e5f05870586cf/llvm/docs/Frontend/PerformanceTips.rst?plain=1#L204-L211.
572+
self.store(
573+
self.const_bool(true),
574+
self.const_poison(self.type_ptr()),
575+
self.tcx().data_layout.i1_align.abi,
576+
);
577+
}
578+
Some(_) => {}
579+
None => {
580+
self.call_intrinsic("llvm.assume", &[val]);
581+
}
582+
}
570583
}
571584

572585
fn expect(&mut self, cond: Self::Value, expected: bool) -> Self::Value {

tests/codegen/assume-false.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ compile-flags: -Cno-prepopulate-passes -Zmir-opt-level=0 -O -Zinline-mir
2+
3+
#![crate_type = "lib"]
4+
5+
#[no_mangle]
6+
pub fn unwrap_unchecked(x: Option<i32>) -> i32 {
7+
// CHECK-LABEL: define{{.*}} i32 @unwrap_unchecked
8+
// CHECK-NOT: call void @llvm.assume(i1 false)
9+
// CHECK: store i1 true, ptr poison, align 1
10+
// CHECK-NOT: call void @llvm.assume(i1 false)
11+
// CHECK: ret
12+
// CHECK }
13+
unsafe { x.unwrap_unchecked() }
14+
}

0 commit comments

Comments
 (0)