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

cmake: compiler: add double promotion warning #57154

Merged

Conversation

XenuIsWatching
Copy link
Member

@XenuIsWatching XenuIsWatching commented Apr 22, 2023

Too many times, code is pushed that includes floats that really becomes doubles and C implicit promotion rules will want to make floats into doubles very easily. As Zephyr primarily targets low-end processors that may not have a double precision floating point unit, enable this flag globally for now.

DEPENDS ON

west.yml also needed to be updated to include the fixes above with the fixes merged in to their "Zephyr forks"

tejlmand
tejlmand previously approved these changes Apr 25, 2023
Copy link
Collaborator

@keith-packard keith-packard left a comment

Choose a reason for hiding this comment

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

Yes, we definitely want this warning enabled by default. Building picolibc with -Werror=double-promotion yielded dozens of fixes and made the resulting library free of double operations when using only float values.

However, fixes to make this work should probably land in separate PR/PRs, but having them appear here first so that we can let CI check to make sure they're sufficient seems like a good plan. Should we mark this as DNM until then?

Once the patches that fix the promotion bugs have landed, I'd be happy to approve this.

@XenuIsWatching
Copy link
Member Author

XenuIsWatching commented Apr 26, 2023

Yes, we definitely want this warning enabled by default. Building picolibc with -Werror=double-promotion yielded dozens of fixes and made the resulting library free of double operations when using only float values.

However, fixes to make this work should probably land in separate PR/PRs, but having them appear here first so that we can let CI check to make sure they're sufficient seems like a good plan. Should we mark this as DNM until then?

Once the patches that fix the promotion bugs have landed, I'd be happy to approve this.

Agreed, it should be marked as DNM until all the little double promotion bugs are squashed in seperate PRs.

My only worry is with modules that have double-promotion issues, that's what stopped me the last time I try to integrated this flag by default to in zephyr, but some may of been fixed since then....
#38958

speaking of which, we got our first hit (it's just that with how large and expansive zephyr is, it's gonna be easier and quicker for me to rely on the server running the test cases, as my local machine is gonna take a loooong time to get through them all with this flag enabled)

/__w/zephyr/modules/lib/zcbor/src/zcbor_encode.c: In function 'zcbor_float16_put':
/__w/zephyr/modules/lib/zcbor/src/zcbor_encode.c:604:23: error: implicit conversion from 'float' to 'double' to match other operand of binary expression [-Werror=double-promotion]
  604 |         if (abs_input <= (F16_MIN / 2)) {
      |                       ^~
/__w/zephyr/modules/lib/zcbor/src/zcbor_encode.c:606:30: error: implicit conversion from 'float' to 'double' to match other operand of binary expression [-Werror=double-promotion]
  606 |         } else if (abs_input < F16_MIN) {
      |                              ^
/__w/zephyr/modules/lib/zcbor/src/zcbor_encode.c:609:30: error: implicit conversion from 'float' to 'double' to match other operand of binary expression [-Werror=double-promotion]
  609 |         } else if (abs_input < F16_MIN_NORM) {
      |                              ^
cc1: all warnings being treated as errors

@XenuIsWatching XenuIsWatching changed the title cmake: compiler: add double promotion warning DNM: cmake: compiler: add double promotion warning Apr 26, 2023
@zephyrbot zephyrbot requested a review from jciupis April 26, 2023 02:21
@keith-packard
Copy link
Collaborator

My only worry is with modules that have double-promotion issues, that's what stopped me the last time I try to integrated this flag by default to in zephyr, but some may of been fixed since then.... #38958

Yeah, it's tedious but there is a pretty reasonable process. Post a PR to the module repo, then post a PR to the zephyr repo that pulls the module branch. It's documented somewhere even ...

@keith-packard keith-packard added the DNM This PR should not be merged (Do Not Merge) label Apr 26, 2023
@XenuIsWatching XenuIsWatching dismissed stale reviews from tejlmand and cfriedt via 9f938e7 January 26, 2024 05:04
@XenuIsWatching XenuIsWatching changed the title DNM: cmake: compiler: add double promotion warning cmake: compiler: add double promotion warning Jan 26, 2024
@XenuIsWatching
Copy link
Member Author

XenuIsWatching commented Jan 26, 2024

@fabiobaltieri Can you merge this into zephyr-testing again? I really hope this is the last time as it now appears all the checkboxes are now checked 😄

@fabiobaltieri
Copy link
Member

Copy link
Member

@cfriedt cfriedt left a comment

Choose a reason for hiding this comment

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

Copy link
Member

@fabiobaltieri fabiobaltieri left a comment

Choose a reason for hiding this comment

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

Let's go!

Copy link
Collaborator

@keith-packard keith-packard left a comment

Choose a reason for hiding this comment

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

Let's get this merged before something breaks again :-)

@cfriedt cfriedt merged commit 018dbcf into zephyrproject-rtos:main Jan 27, 2024
22 checks passed
yashi added a commit to yashi/scsat1-fsw that referenced this pull request Jan 31, 2024
In C, a literal value like "2.5" is considered a double. When you
multiply a float by a double, implicit promotion occurs, performing
the calculation in 64 bits. This unnecessarily consumes CPU power on
MCUs.

   [118/166] Building C object .../sc_dstrx3.c.obj
   In file included from .../logging/log.h:11,
                    from .../sc_dstrx3.c:9:
   .../sc_dstrx3.c: In function 'sc_dstrx3_get_hk_telemetry':
   .../sc_dstrx3.c:132:83: warning: implicit conversion from 'float'
   to 'double' to match other operand of binary expression [-Wdouble-promotion]
     132 |         LOG_DBG("VOLTAGE          : %f [v] (raw: %d)", ((float)hk->voltage / 256) * 2.5,
         |                                                                                   ^

The goal is to perform all calculations under float. Then, for this
particular case, convert it to double because the value is passed to a
variadic function, z_log_minimal_printk().

Related resources:
- https://en.cppreference.com/w/c/language/conversion
- Link to PR: zephyrproject-rtos/zephyr#57154

Signed-off-by: Yasushi SHOJI <yashi@spacecubics.com>
yashi added a commit to yashi/scsat1-fsw that referenced this pull request Feb 1, 2024
In C, a literal value like "2.5" is considered a double. When you
multiply a float by a double, implicit promotion occurs, performing
the calculation in 64 bits. This unnecessarily consumes CPU power on
MCUs.

   [118/166] Building C object .../sc_dstrx3.c.obj
   In file included from .../logging/log.h:11,
                    from .../sc_dstrx3.c:9:
   .../sc_dstrx3.c: In function 'sc_dstrx3_get_hk_telemetry':
   .../sc_dstrx3.c:132:83: warning: implicit conversion from 'float'
   to 'double' to match other operand of binary expression [-Wdouble-promotion]
     132 |         LOG_DBG("VOLTAGE          : %f [v] (raw: %d)", ((float)hk->voltage / 256) * 2.5,
         |                                                                                   ^

The goal is to perform all calculations under float. Then, for this
particular case, convert it to double because the value is passed to a
variadic function, z_log_minimal_printk().

Related resources:
- https://en.cppreference.com/w/c/language/conversion
- Link to PR: zephyrproject-rtos/zephyr#57154

Signed-off-by: Yasushi SHOJI <yashi@spacecubics.com>
yashi added a commit to spacecubics/scsat1-fsw that referenced this pull request Feb 1, 2024
In C, a literal value like "2.5" is considered a double. When you
multiply a float by a double, implicit promotion occurs, performing
the calculation in 64 bits. This unnecessarily consumes CPU power on
MCUs.

   [118/166] Building C object .../sc_dstrx3.c.obj
   In file included from .../logging/log.h:11,
                    from .../sc_dstrx3.c:9:
   .../sc_dstrx3.c: In function 'sc_dstrx3_get_hk_telemetry':
   .../sc_dstrx3.c:132:83: warning: implicit conversion from 'float'
   to 'double' to match other operand of binary expression [-Wdouble-promotion]
     132 |         LOG_DBG("VOLTAGE          : %f [v] (raw: %d)", ((float)hk->voltage / 256) * 2.5,
         |                                                                                   ^

The goal is to perform all calculations under float. Then, for this
particular case, convert it to double because the value is passed to a
variadic function, z_log_minimal_printk().

Related resources:
- https://en.cppreference.com/w/c/language/conversion
- Link to PR: zephyrproject-rtos/zephyr#57154

Signed-off-by: Yasushi SHOJI <yashi@spacecubics.com>
@rlubos rlubos mentioned this pull request Mar 7, 2024
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.