* [PATCH 0/2] Avoid -Wconstant-logical-operand in nsecs_to_jiffies_timeout()
@ 2023-07-18 21:44 Nathan Chancellor
2023-07-18 21:44 ` [PATCH 1/2] drm/v3d: " Nathan Chancellor
` (2 more replies)
0 siblings, 3 replies; 9+ messages in thread
From: Nathan Chancellor @ 2023-07-18 21:44 UTC (permalink / raw)
To: jani.nikula, joonas.lahtinen, rodrigo.vivi, tvrtko.ursulin, emma,
mwen
Cc: ndesaulniers, justinstitt, trix, intel-gfx, dri-devel, llvm,
patches, Nathan Chancellor
Hi all,
A proposed update to clang's -Wconstant-logical-operand [1] to warn when
the left hand side is a constant as well now triggers with the modulo
expression in nsecs_to_jiffies_timeout() when NSEC_PER_SEC is not a
multiple of HZ, such as CONFIG_HZ=300:
drivers/gpu/drm/i915/gem/i915_gem_wait.c:189:24: warning: use of logical '&&' with constant operand [-Wconstant-logical-operand]
189 | if (NSEC_PER_SEC % HZ &&
| ~~~~~~~~~~~~~~~~~ ^
drivers/gpu/drm/i915/gem/i915_gem_wait.c:189:24: note: use '&' for a bitwise operation
189 | if (NSEC_PER_SEC % HZ &&
| ^~
| &
drivers/gpu/drm/i915/gem/i915_gem_wait.c:189:24: note: remove constant to silence this warning
1 warning generated.
In file included from drivers/gpu/drm/v3d/v3d_debugfs.c:12:
drivers/gpu/drm/v3d/v3d_drv.h:343:24: warning: use of logical '&&' with constant operand [-Wconstant-logical-operand]
343 | if (NSEC_PER_SEC % HZ &&
| ~~~~~~~~~~~~~~~~~ ^
drivers/gpu/drm/v3d/v3d_drv.h:343:24: note: use '&' for a bitwise operation
343 | if (NSEC_PER_SEC % HZ &&
| ^~
| &
drivers/gpu/drm/v3d/v3d_drv.h:343:24: note: remove constant to silence this warning
1 warning generated.
These patches add an explicit comparison to zero to make the
expression a boolean, which clears up the warning.
The patches have no real dependency on each other but I felt like they
made send together since it is the same code.
If these could go into mainline sooner rather than later to avoid
breaking builds that can hit this with CONFIG_WERROR, that would be
nice, but I won't insist since I don't think our own CI has builds that
has those conditions, but others might.
---
Nathan Chancellor (2):
drm/v3d: Avoid -Wconstant-logical-operand in nsecs_to_jiffies_timeout()
drm/i915: Avoid -Wconstant-logical-operand in nsecs_to_jiffies_timeout()
drivers/gpu/drm/i915/gem/i915_gem_wait.c | 2 +-
drivers/gpu/drm/v3d/v3d_drv.h | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
---
base-commit: fdf0eaf11452d72945af31804e2a1048ee1b574c
change-id: 20230718-nsecs_to_jiffies_timeout-constant-logical-operand-4a944690f3e9
Best regards,
--
Nathan Chancellor <nathan@kernel.org>
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH 1/2] drm/v3d: Avoid -Wconstant-logical-operand in nsecs_to_jiffies_timeout()
2023-07-18 21:44 [PATCH 0/2] Avoid -Wconstant-logical-operand in nsecs_to_jiffies_timeout() Nathan Chancellor
@ 2023-07-18 21:44 ` Nathan Chancellor
2023-07-21 18:36 ` Nick Desaulniers
2023-07-27 14:01 ` Maira Canal
2023-07-18 21:44 ` [PATCH 2/2] drm/i915: " Nathan Chancellor
2023-07-27 16:08 ` [PATCH 0/2] " Maira Canal
2 siblings, 2 replies; 9+ messages in thread
From: Nathan Chancellor @ 2023-07-18 21:44 UTC (permalink / raw)
To: jani.nikula, joonas.lahtinen, rodrigo.vivi, tvrtko.ursulin, emma,
mwen
Cc: ndesaulniers, justinstitt, trix, intel-gfx, dri-devel, llvm,
patches, Nathan Chancellor
A proposed update to clang's -Wconstant-logical-operand to warn when the
left hand side is a constant shows the following instance in
nsecs_to_jiffies_timeout() when NSEC_PER_SEC is not a multiple of HZ,
such as CONFIG_HZ=300:
In file included from drivers/gpu/drm/v3d/v3d_debugfs.c:12:
drivers/gpu/drm/v3d/v3d_drv.h:343:24: warning: use of logical '&&' with constant operand [-Wconstant-logical-operand]
343 | if (NSEC_PER_SEC % HZ &&
| ~~~~~~~~~~~~~~~~~ ^
drivers/gpu/drm/v3d/v3d_drv.h:343:24: note: use '&' for a bitwise operation
343 | if (NSEC_PER_SEC % HZ &&
| ^~
| &
drivers/gpu/drm/v3d/v3d_drv.h:343:24: note: remove constant to silence this warning
1 warning generated.
Turn this into an explicit comparison against zero to make the
expression a boolean to make it clear this should be a logical check,
not a bitwise one.
Link: https://reviews.llvm.org/D142609
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
---
drivers/gpu/drm/v3d/v3d_drv.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/v3d/v3d_drv.h b/drivers/gpu/drm/v3d/v3d_drv.h
index b74b1351bfc8..7f664a4b2a75 100644
--- a/drivers/gpu/drm/v3d/v3d_drv.h
+++ b/drivers/gpu/drm/v3d/v3d_drv.h
@@ -340,7 +340,7 @@ struct v3d_submit_ext {
static inline unsigned long nsecs_to_jiffies_timeout(const u64 n)
{
/* nsecs_to_jiffies64() does not guard against overflow */
- if (NSEC_PER_SEC % HZ &&
+ if ((NSEC_PER_SEC % HZ) != 0 &&
div_u64(n, NSEC_PER_SEC) >= MAX_JIFFY_OFFSET / HZ)
return MAX_JIFFY_OFFSET;
--
2.41.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH 1/2] drm/v3d: Avoid -Wconstant-logical-operand in nsecs_to_jiffies_timeout()
2023-07-18 21:44 ` [PATCH 1/2] drm/v3d: " Nathan Chancellor
@ 2023-07-21 18:36 ` Nick Desaulniers
2023-07-27 14:01 ` Maira Canal
1 sibling, 0 replies; 9+ messages in thread
From: Nick Desaulniers @ 2023-07-21 18:36 UTC (permalink / raw)
To: Nathan Chancellor
Cc: jani.nikula, joonas.lahtinen, rodrigo.vivi, tvrtko.ursulin, emma,
mwen, justinstitt, trix, intel-gfx, dri-devel, llvm, patches
On Tue, Jul 18, 2023 at 2:44 PM Nathan Chancellor <nathan@kernel.org> wrote:
>
> A proposed update to clang's -Wconstant-logical-operand to warn when the
> left hand side is a constant shows the following instance in
> nsecs_to_jiffies_timeout() when NSEC_PER_SEC is not a multiple of HZ,
> such as CONFIG_HZ=300:
>
> In file included from drivers/gpu/drm/v3d/v3d_debugfs.c:12:
> drivers/gpu/drm/v3d/v3d_drv.h:343:24: warning: use of logical '&&' with constant operand [-Wconstant-logical-operand]
> 343 | if (NSEC_PER_SEC % HZ &&
> | ~~~~~~~~~~~~~~~~~ ^
> drivers/gpu/drm/v3d/v3d_drv.h:343:24: note: use '&' for a bitwise operation
> 343 | if (NSEC_PER_SEC % HZ &&
> | ^~
> | &
> drivers/gpu/drm/v3d/v3d_drv.h:343:24: note: remove constant to silence this warning
> 1 warning generated.
>
> Turn this into an explicit comparison against zero to make the
> expression a boolean to make it clear this should be a logical check,
> not a bitwise one.
>
> Link: https://reviews.llvm.org/D142609
> Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Thanks for the patch!
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
> ---
> drivers/gpu/drm/v3d/v3d_drv.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/drm/v3d/v3d_drv.h b/drivers/gpu/drm/v3d/v3d_drv.h
> index b74b1351bfc8..7f664a4b2a75 100644
> --- a/drivers/gpu/drm/v3d/v3d_drv.h
> +++ b/drivers/gpu/drm/v3d/v3d_drv.h
> @@ -340,7 +340,7 @@ struct v3d_submit_ext {
> static inline unsigned long nsecs_to_jiffies_timeout(const u64 n)
> {
> /* nsecs_to_jiffies64() does not guard against overflow */
> - if (NSEC_PER_SEC % HZ &&
> + if ((NSEC_PER_SEC % HZ) != 0 &&
> div_u64(n, NSEC_PER_SEC) >= MAX_JIFFY_OFFSET / HZ)
> return MAX_JIFFY_OFFSET;
>
>
> --
> 2.41.0
>
--
Thanks,
~Nick Desaulniers
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH 1/2] drm/v3d: Avoid -Wconstant-logical-operand in nsecs_to_jiffies_timeout()
2023-07-18 21:44 ` [PATCH 1/2] drm/v3d: " Nathan Chancellor
2023-07-21 18:36 ` Nick Desaulniers
@ 2023-07-27 14:01 ` Maira Canal
2023-07-27 14:41 ` Nathan Chancellor
1 sibling, 1 reply; 9+ messages in thread
From: Maira Canal @ 2023-07-27 14:01 UTC (permalink / raw)
To: Nathan Chancellor, jani.nikula, joonas.lahtinen, rodrigo.vivi,
tvrtko.ursulin, emma, mwen
Cc: trix, intel-gfx, llvm, ndesaulniers, patches, dri-devel,
justinstitt
Hi Nathan,
On 7/18/23 18:44, Nathan Chancellor wrote:
> A proposed update to clang's -Wconstant-logical-operand to warn when the
> left hand side is a constant shows the following instance in
> nsecs_to_jiffies_timeout() when NSEC_PER_SEC is not a multiple of HZ,
> such as CONFIG_HZ=300:
>
> In file included from drivers/gpu/drm/v3d/v3d_debugfs.c:12:
> drivers/gpu/drm/v3d/v3d_drv.h:343:24: warning: use of logical '&&' with constant operand [-Wconstant-logical-operand]
> 343 | if (NSEC_PER_SEC % HZ &&
> | ~~~~~~~~~~~~~~~~~ ^
> drivers/gpu/drm/v3d/v3d_drv.h:343:24: note: use '&' for a bitwise operation
> 343 | if (NSEC_PER_SEC % HZ &&
> | ^~
> | &
> drivers/gpu/drm/v3d/v3d_drv.h:343:24: note: remove constant to silence this warning
> 1 warning generated.
>
> Turn this into an explicit comparison against zero to make the
> expression a boolean to make it clear this should be a logical check,
> not a bitwise one.
>
> Link: https://reviews.llvm.org/D142609
> Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Reviewed-by: Maíra Canal <mcanal@igalia.com>
Thanks for all the hard work with clang! Let me know if you need someone
to push it to drm-misc-next.
Best Regards,
- Maíra
> ---
> drivers/gpu/drm/v3d/v3d_drv.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/drm/v3d/v3d_drv.h b/drivers/gpu/drm/v3d/v3d_drv.h
> index b74b1351bfc8..7f664a4b2a75 100644
> --- a/drivers/gpu/drm/v3d/v3d_drv.h
> +++ b/drivers/gpu/drm/v3d/v3d_drv.h
> @@ -340,7 +340,7 @@ struct v3d_submit_ext {
> static inline unsigned long nsecs_to_jiffies_timeout(const u64 n)
> {
> /* nsecs_to_jiffies64() does not guard against overflow */
> - if (NSEC_PER_SEC % HZ &&
> + if ((NSEC_PER_SEC % HZ) != 0 &&
> div_u64(n, NSEC_PER_SEC) >= MAX_JIFFY_OFFSET / HZ)
> return MAX_JIFFY_OFFSET;
>
>
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH 1/2] drm/v3d: Avoid -Wconstant-logical-operand in nsecs_to_jiffies_timeout()
2023-07-27 14:01 ` Maira Canal
@ 2023-07-27 14:41 ` Nathan Chancellor
0 siblings, 0 replies; 9+ messages in thread
From: Nathan Chancellor @ 2023-07-27 14:41 UTC (permalink / raw)
To: Maira Canal
Cc: jani.nikula, joonas.lahtinen, rodrigo.vivi, tvrtko.ursulin, emma,
mwen, trix, intel-gfx, llvm, ndesaulniers, patches, dri-devel,
justinstitt
Hi Maira,
On Thu, Jul 27, 2023 at 11:01:27AM -0300, Maira Canal wrote:
> Hi Nathan,
>
> On 7/18/23 18:44, Nathan Chancellor wrote:
> > A proposed update to clang's -Wconstant-logical-operand to warn when the
> > left hand side is a constant shows the following instance in
> > nsecs_to_jiffies_timeout() when NSEC_PER_SEC is not a multiple of HZ,
> > such as CONFIG_HZ=300:
> >
> > In file included from drivers/gpu/drm/v3d/v3d_debugfs.c:12:
> > drivers/gpu/drm/v3d/v3d_drv.h:343:24: warning: use of logical '&&' with constant operand [-Wconstant-logical-operand]
> > 343 | if (NSEC_PER_SEC % HZ &&
> > | ~~~~~~~~~~~~~~~~~ ^
> > drivers/gpu/drm/v3d/v3d_drv.h:343:24: note: use '&' for a bitwise operation
> > 343 | if (NSEC_PER_SEC % HZ &&
> > | ^~
> > | &
> > drivers/gpu/drm/v3d/v3d_drv.h:343:24: note: remove constant to silence this warning
> > 1 warning generated.
> >
> > Turn this into an explicit comparison against zero to make the
> > expression a boolean to make it clear this should be a logical check,
> > not a bitwise one.
> >
> > Link: https://reviews.llvm.org/D142609
> > Signed-off-by: Nathan Chancellor <nathan@kernel.org>
>
> Reviewed-by: Maíra Canal <mcanal@igalia.com>
>
> Thanks for all the hard work with clang! Let me know if you need someone
> to push it to drm-misc-next.
Yes I will, I do not have drm commit rights. I think the i915 patch can
go to drm-misc as well with Tvrtko's ack. Thanks a lot for taking a
look!
Cheers,
Nathan
> Best Regards,
> - Maíra
>
> > ---
> > drivers/gpu/drm/v3d/v3d_drv.h | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/drivers/gpu/drm/v3d/v3d_drv.h b/drivers/gpu/drm/v3d/v3d_drv.h
> > index b74b1351bfc8..7f664a4b2a75 100644
> > --- a/drivers/gpu/drm/v3d/v3d_drv.h
> > +++ b/drivers/gpu/drm/v3d/v3d_drv.h
> > @@ -340,7 +340,7 @@ struct v3d_submit_ext {
> > static inline unsigned long nsecs_to_jiffies_timeout(const u64 n)
> > {
> > /* nsecs_to_jiffies64() does not guard against overflow */
> > - if (NSEC_PER_SEC % HZ &&
> > + if ((NSEC_PER_SEC % HZ) != 0 &&
> > div_u64(n, NSEC_PER_SEC) >= MAX_JIFFY_OFFSET / HZ)
> > return MAX_JIFFY_OFFSET;
> >
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 2/2] drm/i915: Avoid -Wconstant-logical-operand in nsecs_to_jiffies_timeout()
2023-07-18 21:44 [PATCH 0/2] Avoid -Wconstant-logical-operand in nsecs_to_jiffies_timeout() Nathan Chancellor
2023-07-18 21:44 ` [PATCH 1/2] drm/v3d: " Nathan Chancellor
@ 2023-07-18 21:44 ` Nathan Chancellor
2023-07-20 8:43 ` Tvrtko Ursulin
2023-07-27 16:08 ` [PATCH 0/2] " Maira Canal
2 siblings, 1 reply; 9+ messages in thread
From: Nathan Chancellor @ 2023-07-18 21:44 UTC (permalink / raw)
To: jani.nikula, joonas.lahtinen, rodrigo.vivi, tvrtko.ursulin, emma,
mwen
Cc: ndesaulniers, justinstitt, trix, intel-gfx, dri-devel, llvm,
patches, Nathan Chancellor
A proposed update to clang's -Wconstant-logical-operand to warn when the
left hand side is a constant shows the following instance in
nsecs_to_jiffies_timeout() when NSEC_PER_SEC is not a multiple of HZ,
such as CONFIG_HZ=300:
drivers/gpu/drm/i915/gem/i915_gem_wait.c:189:24: warning: use of logical '&&' with constant operand [-Wconstant-logical-operand]
189 | if (NSEC_PER_SEC % HZ &&
| ~~~~~~~~~~~~~~~~~ ^
drivers/gpu/drm/i915/gem/i915_gem_wait.c:189:24: note: use '&' for a bitwise operation
189 | if (NSEC_PER_SEC % HZ &&
| ^~
| &
drivers/gpu/drm/i915/gem/i915_gem_wait.c:189:24: note: remove constant to silence this warning
1 warning generated.
Turn this into an explicit comparison against zero to make the
expression a boolean to make it clear this should be a logical check,
not a bitwise one.
Link: https://reviews.llvm.org/D142609
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
---
drivers/gpu/drm/i915/gem/i915_gem_wait.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/i915/gem/i915_gem_wait.c b/drivers/gpu/drm/i915/gem/i915_gem_wait.c
index 4a33ad2d122b..d4b918fb11ce 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_wait.c
+++ b/drivers/gpu/drm/i915/gem/i915_gem_wait.c
@@ -186,7 +186,7 @@ i915_gem_object_wait(struct drm_i915_gem_object *obj,
static inline unsigned long nsecs_to_jiffies_timeout(const u64 n)
{
/* nsecs_to_jiffies64() does not guard against overflow */
- if (NSEC_PER_SEC % HZ &&
+ if ((NSEC_PER_SEC % HZ) != 0 &&
div_u64(n, NSEC_PER_SEC) >= MAX_JIFFY_OFFSET / HZ)
return MAX_JIFFY_OFFSET;
--
2.41.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH 2/2] drm/i915: Avoid -Wconstant-logical-operand in nsecs_to_jiffies_timeout()
2023-07-18 21:44 ` [PATCH 2/2] drm/i915: " Nathan Chancellor
@ 2023-07-20 8:43 ` Tvrtko Ursulin
2023-07-20 15:16 ` Nathan Chancellor
0 siblings, 1 reply; 9+ messages in thread
From: Tvrtko Ursulin @ 2023-07-20 8:43 UTC (permalink / raw)
To: Nathan Chancellor, jani.nikula, joonas.lahtinen, rodrigo.vivi,
emma, mwen
Cc: ndesaulniers, justinstitt, trix, intel-gfx, dri-devel, llvm,
patches
On 18/07/2023 22:44, Nathan Chancellor wrote:
> A proposed update to clang's -Wconstant-logical-operand to warn when the
> left hand side is a constant shows the following instance in
> nsecs_to_jiffies_timeout() when NSEC_PER_SEC is not a multiple of HZ,
> such as CONFIG_HZ=300:
>
> drivers/gpu/drm/i915/gem/i915_gem_wait.c:189:24: warning: use of logical '&&' with constant operand [-Wconstant-logical-operand]
> 189 | if (NSEC_PER_SEC % HZ &&
> | ~~~~~~~~~~~~~~~~~ ^
> drivers/gpu/drm/i915/gem/i915_gem_wait.c:189:24: note: use '&' for a bitwise operation
> 189 | if (NSEC_PER_SEC % HZ &&
> | ^~
> | &
> drivers/gpu/drm/i915/gem/i915_gem_wait.c:189:24: note: remove constant to silence this warning
> 1 warning generated.
>
> Turn this into an explicit comparison against zero to make the
> expression a boolean to make it clear this should be a logical check,
> not a bitwise one.
So -Wconstant-logical-operand only triggers when it is a constant but
not zero constant? Why does that make sense is not a kludge to avoid too
much noise?
Personally, it all feels a bit over the top as a warning, since code in
both cases should optimise away. And we may end up papering over it if
it becomes a default.
Then again this patch IMO does make the code more readable, so I am
happy to take this one via our tree. Or either give ack to bring it in
via drm-misc-next:
Acked-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Let me know which route works best.
Regards,
Tvrtko
> Link: https://reviews.llvm.org/D142609
> Signed-off-by: Nathan Chancellor <nathan@kernel.org>
> ---
> drivers/gpu/drm/i915/gem/i915_gem_wait.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/drm/i915/gem/i915_gem_wait.c b/drivers/gpu/drm/i915/gem/i915_gem_wait.c
> index 4a33ad2d122b..d4b918fb11ce 100644
> --- a/drivers/gpu/drm/i915/gem/i915_gem_wait.c
> +++ b/drivers/gpu/drm/i915/gem/i915_gem_wait.c
> @@ -186,7 +186,7 @@ i915_gem_object_wait(struct drm_i915_gem_object *obj,
> static inline unsigned long nsecs_to_jiffies_timeout(const u64 n)
> {
> /* nsecs_to_jiffies64() does not guard against overflow */
> - if (NSEC_PER_SEC % HZ &&
> + if ((NSEC_PER_SEC % HZ) != 0 &&
> div_u64(n, NSEC_PER_SEC) >= MAX_JIFFY_OFFSET / HZ)
> return MAX_JIFFY_OFFSET;
>
>
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH 2/2] drm/i915: Avoid -Wconstant-logical-operand in nsecs_to_jiffies_timeout()
2023-07-20 8:43 ` Tvrtko Ursulin
@ 2023-07-20 15:16 ` Nathan Chancellor
0 siblings, 0 replies; 9+ messages in thread
From: Nathan Chancellor @ 2023-07-20 15:16 UTC (permalink / raw)
To: Tvrtko Ursulin
Cc: jani.nikula, joonas.lahtinen, rodrigo.vivi, emma, mwen,
ndesaulniers, justinstitt, trix, intel-gfx, dri-devel, llvm,
patches
On Thu, Jul 20, 2023 at 09:43:05AM +0100, Tvrtko Ursulin wrote:
>
> On 18/07/2023 22:44, Nathan Chancellor wrote:
> > A proposed update to clang's -Wconstant-logical-operand to warn when the
> > left hand side is a constant shows the following instance in
> > nsecs_to_jiffies_timeout() when NSEC_PER_SEC is not a multiple of HZ,
> > such as CONFIG_HZ=300:
> >
> > drivers/gpu/drm/i915/gem/i915_gem_wait.c:189:24: warning: use of logical '&&' with constant operand [-Wconstant-logical-operand]
> > 189 | if (NSEC_PER_SEC % HZ &&
> > | ~~~~~~~~~~~~~~~~~ ^
> > drivers/gpu/drm/i915/gem/i915_gem_wait.c:189:24: note: use '&' for a bitwise operation
> > 189 | if (NSEC_PER_SEC % HZ &&
> > | ^~
> > | &
> > drivers/gpu/drm/i915/gem/i915_gem_wait.c:189:24: note: remove constant to silence this warning
> > 1 warning generated.
> >
> > Turn this into an explicit comparison against zero to make the
> > expression a boolean to make it clear this should be a logical check,
> > not a bitwise one.
>
> So -Wconstant-logical-operand only triggers when it is a
> constant but not zero constant? Why does that make sense is not
> a kludge to avoid too much noise?
Yes, the warning purposefully does not trigger when the constant is a 1
or 0 (as those are usually indicative of an intentional logical
operation):
https://github.com/llvm/llvm-project/blob/dfdfd306cfaf54fbc43e2d5eb36489dac3eb9976/clang/lib/Sema/SemaExpr.cpp#L13917-L13919
In this case, it is 100, so I kind of understand why this might be
ambiguous to the compiler.
> Personally, it all feels a bit over the top as a warning,
> since code in both cases should optimise away. And we may end
I do not necessarily disagree, as you can see from the differential
review that I linked in the message, but I also understand it is a fine
line to tread when writing compiler warnings between wanting to catch
as many potential problems as possible and having too much noise for
developers to sift through. I think this is erring on the side of
caution.
> up papering over it if it becomes a default.
diagtool tree tells me this warning is already on by default.
> Then again this patch IMO does make the code more readable, so
I think so too.
> I am happy to take this one via our tree. Or either give ack to
> bring it in via drm-misc-next:
>
> Acked-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
>
> Let me know which route works best.
Thanks for the feedback! Either route is fine with me but if the v3d
patch is going to go in via drm-misc-next, it seems like it would not be
too much trouble to push this one with it.
Cheers,
Nathan
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 0/2] Avoid -Wconstant-logical-operand in nsecs_to_jiffies_timeout()
2023-07-18 21:44 [PATCH 0/2] Avoid -Wconstant-logical-operand in nsecs_to_jiffies_timeout() Nathan Chancellor
2023-07-18 21:44 ` [PATCH 1/2] drm/v3d: " Nathan Chancellor
2023-07-18 21:44 ` [PATCH 2/2] drm/i915: " Nathan Chancellor
@ 2023-07-27 16:08 ` Maira Canal
2 siblings, 0 replies; 9+ messages in thread
From: Maira Canal @ 2023-07-27 16:08 UTC (permalink / raw)
To: Nathan Chancellor, jani.nikula, joonas.lahtinen, rodrigo.vivi,
tvrtko.ursulin, emma, mwen
Cc: trix, intel-gfx, llvm, ndesaulniers, patches, dri-devel,
justinstitt
On 7/18/23 18:44, Nathan Chancellor wrote:
> Hi all,
>
> A proposed update to clang's -Wconstant-logical-operand [1] to warn when
> the left hand side is a constant as well now triggers with the modulo
> expression in nsecs_to_jiffies_timeout() when NSEC_PER_SEC is not a
> multiple of HZ, such as CONFIG_HZ=300:
>
> drivers/gpu/drm/i915/gem/i915_gem_wait.c:189:24: warning: use of logical '&&' with constant operand [-Wconstant-logical-operand]
> 189 | if (NSEC_PER_SEC % HZ &&
> | ~~~~~~~~~~~~~~~~~ ^
> drivers/gpu/drm/i915/gem/i915_gem_wait.c:189:24: note: use '&' for a bitwise operation
> 189 | if (NSEC_PER_SEC % HZ &&
> | ^~
> | &
> drivers/gpu/drm/i915/gem/i915_gem_wait.c:189:24: note: remove constant to silence this warning
> 1 warning generated.
>
> In file included from drivers/gpu/drm/v3d/v3d_debugfs.c:12:
> drivers/gpu/drm/v3d/v3d_drv.h:343:24: warning: use of logical '&&' with constant operand [-Wconstant-logical-operand]
> 343 | if (NSEC_PER_SEC % HZ &&
> | ~~~~~~~~~~~~~~~~~ ^
> drivers/gpu/drm/v3d/v3d_drv.h:343:24: note: use '&' for a bitwise operation
> 343 | if (NSEC_PER_SEC % HZ &&
> | ^~
> | &
> drivers/gpu/drm/v3d/v3d_drv.h:343:24: note: remove constant to silence this warning
> 1 warning generated.
>
> These patches add an explicit comparison to zero to make the
> expression a boolean, which clears up the warning.
>
> The patches have no real dependency on each other but I felt like they
> made send together since it is the same code.
>
> If these could go into mainline sooner rather than later to avoid
> breaking builds that can hit this with CONFIG_WERROR, that would be
> nice, but I won't insist since I don't think our own CI has builds that
> has those conditions, but others might.
>
> ---
> Nathan Chancellor (2):
> drm/v3d: Avoid -Wconstant-logical-operand in nsecs_to_jiffies_timeout()
> drm/i915: Avoid -Wconstant-logical-operand in nsecs_to_jiffies_timeout()
Applied both patches to drm-misc/drm-misc-next!
Best Regards,
- Maíra
>
> drivers/gpu/drm/i915/gem/i915_gem_wait.c | 2 +-
> drivers/gpu/drm/v3d/v3d_drv.h | 2 +-
> 2 files changed, 2 insertions(+), 2 deletions(-)
> ---
> base-commit: fdf0eaf11452d72945af31804e2a1048ee1b574c
> change-id: 20230718-nsecs_to_jiffies_timeout-constant-logical-operand-4a944690f3e9
>
> Best regards,
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2023-07-27 16:08 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-18 21:44 [PATCH 0/2] Avoid -Wconstant-logical-operand in nsecs_to_jiffies_timeout() Nathan Chancellor
2023-07-18 21:44 ` [PATCH 1/2] drm/v3d: " Nathan Chancellor
2023-07-21 18:36 ` Nick Desaulniers
2023-07-27 14:01 ` Maira Canal
2023-07-27 14:41 ` Nathan Chancellor
2023-07-18 21:44 ` [PATCH 2/2] drm/i915: " Nathan Chancellor
2023-07-20 8:43 ` Tvrtko Ursulin
2023-07-20 15:16 ` Nathan Chancellor
2023-07-27 16:08 ` [PATCH 0/2] " Maira Canal
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox