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

Revert "sys/pm_layered: pm_(un)block add attribute optimize(3)" #19155

Merged
merged 1 commit into from Jan 16, 2023

Conversation

Teufelchen1
Copy link
Contributor

Revert "sys/pm_layered: pm_(un)block add attribute optimize(3) -shortens hotpath"

This reverts commit 5447203.

Contribution description

Compiling examples/gnrc_networking_mac using TOOLCHAIN=llvm yields the following error:

RIOT/sys/pm_layered/pm.c:77:16: error: unknown attribute 'optimize' ignored [-Werror,-Wunknown-attributes]
__attribute__((optimize(3)))

As indicated, this is because the attribute optimize is GCC only and not present in LLVM.
Compare the manpages of GCC and LLVM.

Testing procedure

Since this should only affect performance and not behavior, no special testing is needed. I am not aware of any tests in RIOT which could verify that assumption.

Issues/PRs references

Introduced in #18846

There is another instance of this attribute being used in shell_lock.c. Since the usage is security related, I omit it from this PR.

@github-actions github-actions bot added the Area: sys Area: System label Jan 16, 2023
Copy link
Contributor

@benpicco benpicco left a comment

Choose a reason for hiding this comment

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

@kfessel what difference doesn -O3 even make here?
I can't think of that many different ways to optimize this.

I would assume most optimizations came from the proper alignment?

@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jan 16, 2023
@benpicco
Copy link
Contributor

There is another instance of this attribute being used in shell_lock.c. Since the usage is security related, I omit it from this PR.

shell_lock is more of a tamper protection than real security. There is a wanrning about this in the doc.

@benpicco
Copy link
Contributor

bors merge

@bors
Copy link
Contributor

bors bot commented Jan 16, 2023

🕐 Waiting for PR status (GitHub check) to be set, probably by CI. Bors will automatically try to run when all required PR statuses are set.

@riot-ci
Copy link

riot-ci commented Jan 16, 2023

Murdock results

✔️ PASSED

571026d Revert "sys/pm_layered: pm_(un)block add attribute optimize(3) -shortens hotpath"

Success Failures Total Runtime
6785 0 6785 12m:39s

Artifacts

@bors
Copy link
Contributor

bors bot commented Jan 16, 2023

Already running a review

bors bot added a commit that referenced this pull request Jan 16, 2023
18477: gnrc_static: add static network configuration r=miri64 a=benpicco



19155: Revert "sys/pm_layered: pm_(un)block add attribute optimize(3)" r=benpicco a=Teufelchen1

Revert "sys/pm_layered: pm_(un)block add attribute optimize(3) -shortens hotpath"

This reverts commit 5447203.

### Contribution description

Compiling `examples/gnrc_networking_mac` using `TOOLCHAIN=llvm` yields the following error:
```
RIOT/sys/pm_layered/pm.c:77:16: error: unknown attribute 'optimize' ignored [-Werror,-Wunknown-attributes]
__attribute__((optimize(3)))
```
As indicated, this is because the attribute `optimize` is GCC only and not present in LLVM.
Compare the manpages of [GCC](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html) and [LLVM](https://clang.llvm.org/docs/AttributeReference.html).


### Testing procedure

Since this should only affect performance and not behavior, no special testing is needed. I am not aware of any tests in RIOT which could verify that assumption.

### Issues/PRs references

Introduced in #18846

There is another instance of this attribute being used in[ shell_lock.c](https://github.com/RIOT-OS/RIOT/blob/6fb340d654ac8da07759cb9199c6aaa478589aa7/sys/shell_lock/shell_lock.c#L80). Since the usage is security related, I omit it from this PR.


Co-authored-by: Benjamin Valentin <benjamin.valentin@ml-pa.com>
Co-authored-by: Teufelchen1 <bennet.blischke@haw-hamburg.de>
@maribu
Copy link
Member

maribu commented Jan 16, 2023

bors cancel
bors merge

(This should allow another PR to hop onto the merge train 🚂 🚋 🚋 🚋)

@bors
Copy link
Contributor

bors bot commented Jan 16, 2023

Canceled.

@bors
Copy link
Contributor

bors bot commented Jan 16, 2023

Already running a review

bors bot added a commit that referenced this pull request Jan 16, 2023
18477: gnrc_static: add static network configuration r=miri64 a=benpicco



19155: Revert "sys/pm_layered: pm_(un)block add attribute optimize(3)" r=maribu a=Teufelchen1

Revert "sys/pm_layered: pm_(un)block add attribute optimize(3) -shortens hotpath"

This reverts commit 5447203.

### Contribution description

Compiling `examples/gnrc_networking_mac` using `TOOLCHAIN=llvm` yields the following error:
```
RIOT/sys/pm_layered/pm.c:77:16: error: unknown attribute 'optimize' ignored [-Werror,-Wunknown-attributes]
__attribute__((optimize(3)))
```
As indicated, this is because the attribute `optimize` is GCC only and not present in LLVM.
Compare the manpages of [GCC](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html) and [LLVM](https://clang.llvm.org/docs/AttributeReference.html).


### Testing procedure

Since this should only affect performance and not behavior, no special testing is needed. I am not aware of any tests in RIOT which could verify that assumption.

### Issues/PRs references

Introduced in #18846

There is another instance of this attribute being used in[ shell_lock.c](https://github.com/RIOT-OS/RIOT/blob/6fb340d654ac8da07759cb9199c6aaa478589aa7/sys/shell_lock/shell_lock.c#L80). Since the usage is security related, I omit it from this PR.


Co-authored-by: Benjamin Valentin <benjamin.valentin@ml-pa.com>
Co-authored-by: Teufelchen1 <bennet.blischke@haw-hamburg.de>
@maribu
Copy link
Member

maribu commented Jan 16, 2023

One last try to pack the train:

bors cancel
bors merge

@bors
Copy link
Contributor

bors bot commented Jan 16, 2023

Canceled.

@benpicco
Copy link
Contributor

bors merge

@bors
Copy link
Contributor

bors bot commented Jan 16, 2023

Already running a review

@kfessel
Copy link
Contributor

kfessel commented Jan 16, 2023

@kfessel what difference doesn -O3 even make here? I can't think of that many different ways to optimize this.

I would assume most optimizations came from the proper alignment?

the attribute shortens the hotpath by reordering instructions preferring the common case of not failing the assertion -> less jumps -> less loss of pipeline

btw this is not an error but an elevated warning

@benpicco
Copy link
Contributor

Uh sounds like we need

#define likely(x)       __builtin_expect((x),1)
#define unlikely(x)     __builtin_expect((x),0)

like Linux, then assert() would be

#define assert(cond) (likely(cond) ? (void)0 : _assert_panic())

@kfessel
Copy link
Contributor

kfessel commented Jan 16, 2023

we could use #pragma GCC optimize(3) (not per function but for the whole file) to achieve the same optimization without clang complaining

@kfessel
Copy link
Contributor

kfessel commented Jan 16, 2023

another fix would be not to warn for unknown attributes in clang

@maribu
Copy link
Member

maribu commented Jan 16, 2023

Maybe we should just add -Wno-error=unknown-attributes when compiling with LLVM?

Otherwise this likely will be a whack-a-mole game, as we only compile-test with GCC.

@bors
Copy link
Contributor

bors bot commented Jan 16, 2023

Build succeeded:

@bors bors bot merged commit 211db05 into RIOT-OS:master Jan 16, 2023
bors bot added a commit that referenced this pull request Jan 17, 2023
19157: sys/shell_lock: do not call strlen, less jumpy r=kaspar030 a=kfessel

### Contribution description

the current `_safe_strcmp` depends on not being optimized and not being inlined (implicitly given by the -O0), this new one does less (would say not since even O3 had compile results that should not return early or show different runtimes for different secrets).

the runtime of strlen depend on the length of the string (password) therefor it is removed. `ifs` are very jumpy and depend on the contend of password, this avoids `ifs` sadly clang still compiles some boolean statements to if.  

with this PR the `__attribute__((optimize("O0")))` should be removable. 

### Testing procedure

see https://godbolt.org/z/x35b85cEx
and run the `<RIOT>/tests/shell_lock` 

### Issues/PRs references
#19155 made me aware of this function 

Co-authored-by: Karl Fessel <karl.fessel@ovgu.de>
bors bot added a commit that referenced this pull request Jan 17, 2023
19157: sys/shell_lock: do not call strlen, less jumpy r=benpicco a=kfessel

### Contribution description

the current `_safe_strcmp` depends on not being optimized and not being inlined (implicitly given by the -O0), this new one does less (would say not since even O3 had compile results that should not return early or show different runtimes for different secrets).

the runtime of strlen depend on the length of the string (password) therefor it is removed. `ifs` are very jumpy and depend on the contend of password, this avoids `ifs` sadly clang still compiles some boolean statements to if.  

with this PR the `__attribute__((optimize("O0")))` should be removable. 

### Testing procedure

see https://godbolt.org/z/x35b85cEx
and run the `<RIOT>/tests/shell_lock` 

### Issues/PRs references
#19155 made me aware of this function 

Co-authored-by: Karl Fessel <karl.fessel@ovgu.de>
@jia200x jia200x added this to the Release 2023.04 milestone Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: sys Area: System CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants