Skip to content

Conversation

@marc-hb
Copy link
Collaborator

@marc-hb marc-hb commented Apr 30, 2021

[#4002 was a completely different and rejected solution to this problem , please start the review there]

For compatibility with Zephyr, MIN and MAX macros are downgraded to the
basic, compile-time, standard compliant and unsafe implementation.

Fixes zephyrproject-rtos/zephyr#33567

See much longer commit messages.

@marc-hb
Copy link
Collaborator Author

marc-hb commented Apr 30, 2021

https://sof-ci.01.org/sofpr/PR4119/build8880/devicetest is all green

The checkpatch warnings about unsafe macros are... valid https://sof-ci.01.org/sofpr/PR4119/build8880/checkpatch

Copy link
Member

@lgirdwood lgirdwood left a comment

Choose a reason for hiding this comment

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

Thanks @marc-hb this moves us closer and closer to Zephyr. One thing that is also needed here is that we should have in numbers.h and other places.

#if !__ZEPHYR__
#define MIN and MAX sof version
#endif 

This will make sure we really are using Zephyr versions. These checks can all be removed when we remove xtos.

@marc-hb
Copy link
Collaborator Author

marc-hb commented Apr 30, 2021

#if !__ZEPHYR__
#define MIN and MAX sof version
#endif

The current #ifdef #undef looked strange to me, I wasn't sure about it so thanks for clarifying that it should go.

I will replace it with the above after one successful daily test run on this merged PR (EDIT: #4130)

marc-hb added 2 commits April 30, 2021 18:52
For consistency with Zephyr, MIN and MAX macros are "downgraded" to the
basic, compile-time, standard compliant and unsafe implementation that
Zephyr uses. "Unsafe" because it can evaluate its arguments twice which
is obviously not desired when the argument has side-effects:
https://wiki.sei.cmu.edu/confluence/display/c/PRE31-C.+Avoid+side+effects+in+arguments+to+unsafe+macros

I reviewed every single invocation of MIN() and MAX() and found no
increment or assignment but I found a number of function calls in their
arguments. This commit removes them all. Note I did _not_ check for
volatile variables.

Most of these functions do not appear to have side-effects _but_ it's
much simpler and faster for the reader or static analyzer not to even
have to wonder about the possibility of side-effects and it's also more
future-proof; no risk of a function accidentally gaining side effects.

Note the following files are not compiled in the default configurations
and the changes in them were NOT compile-tested locally:

src/audio/codec_adapter/codec_adapter.c
src/audio/drc/drc_generic.c

Signed-off-by: Marc Herbert <marc.herbert@intel.com>
For compatibility with Zephyr, MIN and MAX macros are downgraded to the
basic, compile-time, standard compliant and unsafe implementation.

This means the code will now be the same whether it's compiled with Zephyr
versus not.

Fixes zephyrproject-rtos/zephyr#33567 which
means it's now possible to use MIN() or MAX() in either Zephyr's
BUILD_ASSERT() or in SOF's equivalent STATIC_ASSERT() or in C11's
_Static_assert()

This implementation is unsafe because it can evaluate its arguments
twice which is obviously not desired when the argument has side-effects:

https://wiki.sei.cmu.edu/confluence/display/c/PRE31-C.+Avoid+side+effects+in+arguments+to+unsafe+macros

Signed-off-by: Marc Herbert <marc.herbert@intel.com>
@marc-hb
Copy link
Collaborator Author

marc-hb commented Apr 30, 2021

https://sof-ci.01.org/sof-pr-viewer/#/build/PR4119/build6376934 failed on ICL, TGL and CNL because I made a (fixed) typo in src/audio/codec_adapter/codec_adapter.c that I can't compile with the information I have. Compilation was successful for APL, JSL and TGH in the same run and github and Jenkins tests were successful on all platforms.

@mrajwa , @dbaluta , @cujomalainey , @lgirdwood : is there some open-source codec we could use in some open-source configuration accessible and added to Github Actions? So we don't have to wait for Quickbuild, sometimes it takes days to run or does not run at all.

@marc-hb
Copy link
Collaborator Author

marc-hb commented Apr 30, 2021

Of course when https://sof-ci.01.org/sof-pr-viewer/#/build/PR4119/build6380518 is green then it's the turn of one system in https://sof-ci.01.org/sofpr/PR4119/build8897/devicetest/ to have a glitch. Can't have all planets aligned at the same time!
CML_HEL_RT5682 did not boot at all, cannot be related to this PR. Moreover the older https://sof-ci.01.org/sofpr/PR4119/build8880/devicetest with just a typo difference was all green.

=> Test are all passing.

@lgirdwood lgirdwood merged commit cf85f21 into thesofproject:main May 3, 2021
@marc-hb marc-hb deleted the downgrade-max branch May 3, 2021 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sof: framework is redefnining MAX, MIN to version with limited capabilities

2 participants