Skip to content

mips: set llvm_args -mno-check-zero-division for all mips targets#157873

Open
xry111 wants to merge 1 commit into
rust-lang:mainfrom
xry111:mips-no-div-by-zero-trap
Open

mips: set llvm_args -mno-check-zero-division for all mips targets#157873
xry111 wants to merge 1 commit into
rust-lang:mainfrom
xry111:mips-no-div-by-zero-trap

Conversation

@xry111

@xry111 xry111 commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

In rust interger divide by zero is defined to panic, thus the inserted conditional trap should never trigger as the program should have panicked if the divisor is zero.

So disable the insertion of the redundant conditional trap.

@rustbot

rustbot commented Jun 13, 2026

Copy link
Copy Markdown
Collaborator

These commits modify compiler targets.
(See the Target Tier Policy.)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 13, 2026
@rustbot

rustbot commented Jun 13, 2026

Copy link
Copy Markdown
Collaborator

r? @tiif

rustbot has assigned @tiif.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 22 candidates

@folkertdev

Copy link
Copy Markdown
Contributor

Specifically https://doc.rust-lang.org/reference/expressions/operator-expr.html#r-expr.arith-logic.behavior documents:

† For integer types, division by zero panics.

Adding the flag does in fact have an effect on codegen (I'd have assumed that LLVM would be able to spot this, but I guess not)

https://rust.godbolt.org/z/evacM46bM

It's probably a good idea to capture that in an assembly-llvm codegen test using minicore

@xry111 xry111 force-pushed the mips-no-div-by-zero-trap branch from 1ae5e5c to 12b004e Compare June 15, 2026 03:40
@rustbot

This comment has been minimized.

@xry111

xry111 commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

It's probably a good idea to capture that in an assembly-llvm codegen test using minicore

Added (but I'm unsure if I wrote it correctly, I haven't written a codegen test before).

@rust-log-analyzer

This comment has been minimized.

@xry111 xry111 force-pushed the mips-no-div-by-zero-trap branch from 12b004e to 8bcda4f Compare June 15, 2026 07:39
@rustbot

This comment has been minimized.

@xry111 xry111 force-pushed the mips-no-div-by-zero-trap branch from 8bcda4f to 59bab36 Compare June 15, 2026 07:41
@rust-log-analyzer

This comment has been minimized.

@xry111 xry111 force-pushed the mips-no-div-by-zero-trap branch from 59bab36 to d444395 Compare June 15, 2026 07:55

@folkertdev folkertdev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

//@ assembly-output: emit-asm
//@ revisions: mips64el-unknown-linux-gnuabi64
//@[mips64el-unknown-linux-gnuabi64] compile-flags: --target=mips64el-unknown-linux-gnuabi64
//@[mips64el-unknown-linux-gnuabi64] needs-llvm-components: mips

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//@[mips64el-unknown-linux-gnuabi64] needs-llvm-components: mips
//@ needs-llvm-components: mips

All of these tests need mips, so I think you can factor this out.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also the code's been written already anyway, but testing all targets doesn't add that much value. it would be more interesting to test the (new) default versus a configuration where you enable the div by zero trap explicitly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm guess not

tidy [target-specific-tests (tests)]: /checkout/tests/assembly-llvm/mips-div-no-trap.rs: revision mipsel-unknown-netbsd should specify `needs-llvm-components: mips` as it has `--target` set
tidy [target-specific-tests (tests)]: /checkout/tests/assembly-llvm/mips-div-no-trap.rs: revision mipsel-unknown-none should specify `needs-llvm-components: mips` as it has `--target` set
tidy [target-specific-tests (tests)]: /checkout/tests/assembly-llvm/mips-div-no-trap.rs: revision mipsisa32r6-unknown-linux-gnu should specify `needs-llvm-components: mips` as it has `--target` set
tidy [target-specific-tests (tests)]: /checkout/tests/assembly-llvm/mips-div-no-trap.rs: revision mipsisa32r6el-unknown-linux-gnu should specify `needs-llvm-components: mips` as it has `--target` set
tidy [target-specific-tests (tests)]: /checkout/tests/assembly-llvm/mips-div-no-trap.rs: revision mipsisa64r6-unknown-linux-gnuabi64 should specify `needs-llvm-components: mips` as it has `--target` set
tidy [target-specific-tests (tests)]: /checkout/tests/assembly-llvm/mips-div-no-trap.rs: revision mipsisa64r6el-unknown-linux-gnuabi64 should specify `needs-llvm-components: mips` as it has `--target` set
tidy [target-specific-tests (tests)]: FAIL

@jieyouxu is there a reason that needs-llvm-components is not "inherited" by revisions? e.g. codegen-flags is I believe.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. Maybe when that was implemented it just wasn't "forwarded" for revisions?

#![crate_type = "lib"]
#![feature(no_core, lang_items)]
#![no_core]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use minicore instead (there are plenty of examples of it in this directory) to remove a bunch of these manual language items.

Comment thread tests/assembly-llvm/mips-div-no-trap.rs Outdated
Comment on lines +93 to +99
impl Div for i32 {
type Output = Self;

fn div(self, rhs: Self) -> Self {
self / rhs
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a recursive definition? My godbolt uses the intrinsic manually instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is how a "language item" works and the real core uses the same pattern. But I'll use the intrinsic anyway to get rid all the panic_* language items.

@rustbot rustbot assigned folkertdev and unassigned tiif Jun 15, 2026
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 15, 2026
@rustbot

rustbot commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@xry111 xry111 force-pushed the mips-no-div-by-zero-trap branch from d444395 to 7fd6d53 Compare June 15, 2026 12:47
@rust-log-analyzer

This comment has been minimized.

In rust interger divide by zero is defined to panic, thus the inserted
conditional trap should never trigger as the program should have
panicked if the divisor is zero.

So disable the insertion of the redundant conditional trap.
@xry111 xry111 force-pushed the mips-no-div-by-zero-trap branch from 7fd6d53 to 83d8c35 Compare June 15, 2026 13:48
@rustbot

rustbot commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants