rust/tests/codegen-llvm/overflow-checks.rs
Zhongyao Chen eef09cfb7d tests: Fix overflow-checks test for RISC-V target
The overflow-checks codegen test was failing on riscv64gc target
because the FileCheck pattern did not account for ABI extension
attributes. RISC-V LP64 ABI requires integer types smaller than
XLEN (64-bit) to be zero-extended or sign-extended to register width.

For u8 parameters, RISC-V generates:
  i8 noundef zeroext %a, i8 noundef zeroext %b

While x86_64 and aarch64 generate:
  i8 noundef %a, i8 noundef %b

The original CHECK pattern only matched the format without the
`zeroext` attribute, causing test failures on RISC-V.

This patch makes the zeroext attribute optional in the FileCheck
pattern using `{{( zeroext)?}}`, allowing the test to pass on
architectures that add ABI extension attributes (e.g., RISC-V).

Test results before fix:
- x86_64-unknown-linux-gnu: 3 passed
- aarch64-unknown-linux-gnu: 3 passed
- riscv64gc-unknown-linux-gnu: 1 passed, 2 failed

Test results after fix:
- x86_64-unknown-linux-gnu: 3 passed
- aarch64-unknown-linux-gnu: 3 passed
- riscv64gc-unknown-linux-gnu: 3 passed
2025-11-12 17:52:14 +08:00

29 lines
1.2 KiB
Rust

// With -Coverflow-checks=yes (enabled by default by -Cdebug-assertions=yes) we will produce a
// runtime check that panics when an operation would result in integer overflow.
//
// This test ensures that such a runtime check is *not* emitted when debug-assertions are enabled,
// but overflow-checks are explicitly disabled. It also ensures that even if a dependency is
// compiled with overflow checks, `intrinsics::overflow_checks()` will be treated with the
// overflow-checks setting of the current crate (when `#[rustc_inherit_overflow_checks]`) is used.
//@ aux-build:overflow_checks_add.rs
//@ revisions: DEBUG NOCHECKS
//@ compile-flags: -O -Cdebug-assertions=yes
//@ [NOCHECKS] compile-flags: -Coverflow-checks=no
#![crate_type = "lib"]
extern crate overflow_checks_add;
// CHECK-LABEL: @add(
#[no_mangle]
pub unsafe fn add(a: u8, b: u8) -> u8 {
// CHECK: i8 noundef{{( zeroext)?}} %a, i8 noundef{{( zeroext)?}} %b
// CHECK: add i8 %b, %a
// DEBUG: icmp ult i8 [[zero:[^,]+]], %a
// DEBUG: call core::num::overflow_panic::add
// DEBUG: unreachable
// NOCHECKS-NOT: unreachable
// NOCHECKS: ret i8 %0
overflow_checks_add::add(a, b)
}