Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent unnecessary bounds check in SCB::{get_priority, set_priority} #202

Merged
merged 3 commits into from
Mar 18, 2020
Merged

Prevent unnecessary bounds check in SCB::{get_priority, set_priority} #202

merged 3 commits into from
Mar 18, 2020

Conversation

qwerty19106
Copy link
Contributor

SystemHandler.index() gives true index for SCB.shpr array.
But now it produce unnecessary bounds check. For example, SCB::set_priority:

48: sym.cortex_m::peripheral::scb::__impl_cortex_m::peripheral::SCB_::set_priority::h622cd21bcc3321be ();
0x08000376      push    {r7, lr}
0x08000378      mov     r7, sp
0x0800037a      bl      cortex_m::peripheral::scb::SystemHandler::index::hdaf1a31aa5a6a8c5 ; sym.cortex_m::peripheral::scb::SystemHandler::index::hdaf1a31aa5a6a8c5
0x0800037e      subs    r0, 4
0x08000380      uxtb    r0, r0
0x08000382      cmp     r0, 0xb    ; 11
0x08000384      itttt   ls
0x08000386      movw    r1, 0xed18
0x0800038a      movt    r1, 0xe000
0x0800038e      movs    r2, 0xff
0x08000390      strb    r2, [r0, r1]
0x08000392      it      ls
0x08000394      pop     {r7, pc}
0x08000396      movw    r2, 0x1dc0
0x0800039a      movs    r1, 0xc
0x0800039c      movt    r2, 0x800
0x080003a0      bl      core::panicking::panic_bounds_check::h5181f47ae17a04a5 ; sym.core::panicking::panic_bounds_check::h5181f47ae17a04a5
0x080003a4      trap

This PR fix it.

@qwerty19106 qwerty19106 requested a review from a team as a code owner March 17, 2020 09:44
@rust-highfive
Copy link

r? @therealprof

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-cortex-m labels Mar 17, 2020
Copy link
Contributor

@therealprof therealprof left a comment

Choose a reason for hiding this comment

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

The code change looks good to me, I'm not quite happy about the unsafety comments, though.

prevent unnecessary bounds check!

I don't think we need to provide a motivation here. The use of get_unchecked makes it quite clear that the motivation here is to get rid of bound checks.

Index of shpr array is true by SystemHandler design.

I don't know what true means in this context. Should rather say something along lines of "index is bounded to [x,y] by SystemHandler design".

Alternatively we could also throw in an assert to ensure that this invariant is never violated and point to that instead. Not sure how that would fare with code generation though.

@qwerty19106
Copy link
Contributor Author

I tried adding assert!(index <16), but the compiler could not optimize it.

Is there a reason why we cannot change SystemHandler definition to:

pub enum SystemHandler {
    MemoryManagement = 4,
    ...

?

therealprof added a commit that referenced this pull request Mar 17, 2020
This is an alternative proposal to #202 as suggested in
#202 (comment)

Signed-off-by: Daniel Egger <[email protected]>
@therealprof
Copy link
Contributor

Let's see how that goes.

bors bot added a commit that referenced this pull request Mar 17, 2020
203: Use u8 repr for enum instead of custom method r=jonas-schievink a=therealprof

This is an alternative proposal to #202 as suggested in
#202 (comment)

Signed-off-by: Daniel Egger <[email protected]>

Co-authored-by: Daniel Egger <[email protected]>
@therealprof
Copy link
Contributor

@qwerty19106 Can you check whether #203 does the trick on its own or whether we still need to change use get_unchecked, too, to achieve better code?

@qwerty19106
Copy link
Contributor Author

On #203 the code just got a little easier, but panic_bounds_check is still present.

With temporary disabled #[inline] and armv7m target:

40: sym.cortex_m::peripheral::scb::__impl_cortex_m::peripheral::SCB_::set_priority::h7df537b11de4024c (int16_t arg2, int16_t arg3);
; arg int16_t arg2 @ r1
; arg int16_t arg3 @ r2
0x08000efc      push    {r7, lr}
0x08000efe      mov     r7, sp
0x08000f00      subs    r0, r1, 4  ; arg2
0x08000f02      uxtb    r0, r0
0x08000f04      cmp     r0, 0xb    ; 11
0x08000f06      itttt   ls
0x08000f08      movw    r1, 0xed18
0x08000f0c      movt    r1, 0xe000
0x08000f10      strb    r2, [r0, r1] ; arg3
0x08000f12      pop     {r7, pc}
0x08000f14      movw    r2, 0x2414
0x08000f18      movs    r1, 0xc
0x08000f1a      movt    r2, 0x800
0x08000f1e      bl      core::panicking::panic_bounds_check::h5181f47ae17a04a5 ; sym.core::panicking::panic_bounds_check::h5181f47ae17a04a5
0x08000f22      trap

I updated unsafe comments. Now it produce this code:

16: sym.cortex_m::peripheral::scb::__impl_cortex_m::peripheral::SCB_::set_priority::hd3dc9949907d960c (int16_t arg2, int16_t arg3);
; arg int16_t arg2 @ r1
; arg int16_t arg3 @ r2
0x08000e18      subs    r0, r1, 4  ; arg2
0x08000e1a      movw    r1, 0xed18
0x08000e1e      movt    r1, 0xe000
0x08000e22      uxtb    r0, r0
0x08000e24      strb    r2, [r0, r1] ; arg3
0x08000e26      bx      lr

I think one should to create issue in the Rust repository, since #203 should give enough information for compiler. I could do it if you don't mind.

@jonas-schievink
Copy link
Contributor

I think one should to create issue in the Rust repository, since #203 should give enough information for compiler. I could do it if you don't mind.

Agreed, this looks like a missed optimization. I'll file an issue.

@jonas-schievink
Copy link
Contributor

Looks like it's an old issue: rust-lang/rust#13926

@therealprof
Copy link
Contributor

@jonas-schievink Do you have a strong opinion on whether we should make this change in the interim?

therealprof
therealprof previously approved these changes Mar 17, 2020
Copy link
Contributor

@therealprof therealprof left a comment

Choose a reason for hiding this comment

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

LGTM, thanks.

@jonas-schievink
Copy link
Contributor

I don't have anything against merging this, but the comments should mention the upstream issue.

@qwerty19106
Copy link
Contributor Author

Added TODO comment about rust-lang/rust#13926

Copy link
Contributor

@therealprof therealprof left a comment

Choose a reason for hiding this comment

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

Excellent, thanks you.

@therealprof
Copy link
Contributor

bors r+

@bors
Copy link
Contributor

bors bot commented Mar 18, 2020

Build succeeded

@bors bors bot merged commit 6a0432a into rust-embedded:master Mar 18, 2020
adamgreig pushed a commit that referenced this pull request Jan 12, 2022
202: Enable building semihosting for Armv8m r=korken89 a=aurabindo

Semihosting protocol has not changed for Arvmv8m and hence it
need not be disabled for this architecture. 

Co-authored-by: Aurabindo Jayamohanan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-cortex-m
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants