Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f42b6fa

Browse filesBrowse files
committed
Auto merge of #103299 - nikic:usub-overflow, r=wesleywiser
Don't use usub.with.overflow intrinsic The canonical form of a usub.with.overflow check in LLVM are separate sub + icmp instructions, rather than a usub.with.overflow intrinsic. Using usub.with.overflow will generally result in worse optimization potential. The backend will attempt to form usub.with.overflow when it comes to actual instruction selection. This is not fully reliable, but I believe this is a better tradeoff than using the intrinsic in IR. Fixes #103285.
2 parents 5ab7445 + 7833012 commit f42b6fa
Copy full SHA for f42b6fa

File tree

Expand file treeCollapse file tree

2 files changed

+24
-5
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+24
-5
lines changed

‎compiler/rustc_codegen_llvm/src/builder.rs

Copy file name to clipboardExpand all lines: compiler/rustc_codegen_llvm/src/builder.rs
+8-5Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,14 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
365365
Int(I64) => "llvm.ssub.with.overflow.i64",
366366
Int(I128) => "llvm.ssub.with.overflow.i128",
367367

368-
Uint(U8) => "llvm.usub.with.overflow.i8",
369-
Uint(U16) => "llvm.usub.with.overflow.i16",
370-
Uint(U32) => "llvm.usub.with.overflow.i32",
371-
Uint(U64) => "llvm.usub.with.overflow.i64",
372-
Uint(U128) => "llvm.usub.with.overflow.i128",
368+
Uint(_) => {
369+
// Emit sub and icmp instead of llvm.usub.with.overflow. LLVM considers these
370+
// to be the canonical form. It will attempt to reform llvm.usub.with.overflow
371+
// in the backend if profitable.
372+
let sub = self.sub(lhs, rhs);
373+
let cmp = self.icmp(IntPredicate::IntULT, lhs, rhs);
374+
return (sub, cmp);
375+
}
373376

374377
_ => unreachable!(),
375378
},
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// compile-flags: -O -C debug-assertions=yes
2+
3+
#![crate_type = "lib"]
4+
#![feature(strict_provenance)]
5+
6+
#[no_mangle]
7+
pub fn test(src: *const u8, dst: *const u8) -> usize {
8+
// CHECK-LABEL: @test(
9+
// CHECK-NOT: panic
10+
let src_usize = src.addr();
11+
let dst_usize = dst.addr();
12+
if src_usize > dst_usize {
13+
return src_usize - dst_usize;
14+
}
15+
return 0;
16+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.