Skip to contentSkip to conversation

static_assert, sfc, and clang issues

DISCUSSION Stephen Hemminger 4 participants 7 messages Jan 16, 2024 – Jan 17, 2024

lore
Share & tools
lore
#0 lore raw

Ran into a corner case issue, so sending to mailing list for wider discussion.

One improvement to DPDK code base planned is getting rid of variable length arrays. VLA's can cause bugs and are not supported by the Windows compiler. Gcc and Clang have a flag to warn on use of VLA's (-Wvla).

In DPDK one use of VLA's is in the RTE_BUILD_BUG_ON macro. The best path is to replace the undefined array access with a better static_assert() which is builtin to compilers.

Using static_assert() is relatively easy, there are a few places where it does detect use of non-constant expressions in existing code but these are fixable.

But there is one case where use static_assert() runs into a Clang bug which is not fixed in distros:

https://github.com/llvm/llvm-project/issues/55821

The code in question is in the sfc driver which for better or worse has lots of assertions. The problem is that clang (except in unreleased trunk) can not handle static_assert in a switch leg.

		switch (actions->type) {
		case RTE_FLOW_ACTION_TYPE_VOID:
			SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_VOID,
					       actions_set);
			break;

../drivers/net/sfc/sfc_flow.c:1635:4: error: expected expression

                        SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_VOID,
                        ^
../drivers/net/sfc/sfc_flow.h:36:2: note: expanded from macro 'SFC_BUILD_SET_OVERFLOW'
        RTE_BUILD_BUG_ON((_action) >= sizeof(_set) * CHAR_BIT)
        ^

There are some workarounds:
0. Ignore it, works on Gcc, and Clang fix is pending.
1. Remove many of these RTE_BUILD_BUG_ON's. They are really not that helpful.
2. Add additional { } to these switch cases so they become basic blocks which works around the bug.
3. Move the assertions out of the switch

My preference is #2 but open to other options.

#1 lore raw
Quoted 37 lines · Stephen Hemminger

Ran into a corner case issue, so sending to mailing list for wider discussion.

One improvement to DPDK code base planned is getting rid of variable length arrays. VLA's can cause bugs and are not supported by the Windows compiler. Gcc and Clang have a flag to warn on use of VLA's (-Wvla).

In DPDK one use of VLA's is in the RTE_BUILD_BUG_ON macro. The best path is to replace the undefined array access with a better static_assert() which is builtin to compilers.

Using static_assert() is relatively easy, there are a few places where it does detect use of non-constant expressions in existing code but these are fixable.

But there is one case where use static_assert() runs into a Clang bug which is not fixed in distros:

https://github.com/llvm/llvm-project/issues/55821

The code in question is in the sfc driver which for better or worse has lots of assertions. The problem is that clang (except in unreleased trunk) can not handle static_assert in a switch leg.

		switch (actions->type) {
		case RTE_FLOW_ACTION_TYPE_VOID:
			SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_VOID,
					       actions_set);
			break;

../drivers/net/sfc/sfc_flow.c:1635:4: error: expected expression

                        SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_VOID,
                        ^
../drivers/net/sfc/sfc_flow.h:36:2: note: expanded from macro 'SFC_BUILD_SET_OVERFLOW'
        RTE_BUILD_BUG_ON((_action) >= sizeof(_set) * CHAR_BIT)
        ^
Quoted 8 lines

There are some workarounds:
0. Ignore it, works on Gcc, and Clang fix is pending.
1. Remove many of these RTE_BUILD_BUG_ON's. They are really not that helpful.
2. Add additional { } to these switch cases so they become basic blocks which works around the bug.
3. Move the assertions out of the switch

My preference is #2 but open to other options.

+1 for #2 just make it a block.

#2 lore raw
Quoted 52 lines

From: Tyler Retzlaff [mailto:[email protected]]
Sent: Tuesday, 16 January 2024 22.51

Quoted 1 line · Stephen Hemminger

Ran into a corner case issue, so sending to mailing list for wider

discussion.

Quoted 2 lines

One improvement to DPDK code base planned is getting rid of variable

length arrays.

Quoted 8 lines

VLA's can cause bugs and are not supported by the Windows compiler. Gcc and Clang have a flag to warn on use of VLA's (-Wvla).

In DPDK one use of VLA's is in the RTE_BUILD_BUG_ON macro. The best path is to replace the undefined array access with a better static_assert() which is builtin to compilers.

Using static_assert() is relatively easy, there are a few places

where

Quoted 1 line

it does detect use of non-constant expressions in existing code but

these

Quoted 9 lines

are fixable.

But there is one case where use static_assert() runs into a Clang bug which is not fixed in distros:

https://github.com/llvm/llvm-project/issues/55821

The code in question is in the sfc driver which for better or worse has lots of assertions. The problem is that clang (except in

unreleased trunk)

Quoted 10 lines

can not handle static_assert in a switch leg.

		switch (actions->type) {
		case RTE_FLOW_ACTION_TYPE_VOID:
			SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_VOID,
					       actions_set);
			break;

../drivers/net/sfc/sfc_flow.c:1635:4: error: expected expression

SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_VOID,

Quoted 2 lines
                        ^

../drivers/net/sfc/sfc_flow.h:36:2: note: expanded from macro

'SFC_BUILD_SET_OVERFLOW'

Quoted 7 lines
        RTE_BUILD_BUG_ON((_action) >= sizeof(_set) * CHAR_BIT)
        ^

There are some workarounds:
0. Ignore it, works on Gcc, and Clang fix is pending.
1. Remove many of these RTE_BUILD_BUG_ON's. They are really not

that helpful.

Quoted 8 lines
Quoted 1 line

2. Add additional { } to these switch cases so they become basic

blocks

Quoted 4 lines

which works around the bug.
3. Move the assertions out of the switch

My preference is #2 but open to other options.

+1 for #2 just make it a block.

I prefer that you implement the workaround in the RTE_BUILD_BUG_ON() macro, by surrounding it by "do { } while (0)", like this:

#define RTE_BUILD_BUG_ON(condition) do { static_assert(!(condition), #condition); } while (0)

And please mention the workaround reason, with the reference to the clang bug, in the source code comment describing the function.

The godbolt link in the clang issue seems happy with this workaround.

#3 lore raw
Quoted 67 lines

From: Morten Brørup [mailto:[email protected]]
Sent: Tuesday, 16 January 2024 23.15

Quoted 8 lines

From: Tyler Retzlaff [mailto:[email protected]]
Sent: Tuesday, 16 January 2024 22.51

Quoted 1 line · Stephen Hemminger

Ran into a corner case issue, so sending to mailing list for wider

discussion.

Quoted 2 lines

One improvement to DPDK code base planned is getting rid of

variable

Quoted 6 lines

length arrays.

Quoted 5 lines

VLA's can cause bugs and are not supported by the Windows compiler. Gcc and Clang have a flag to warn on use of VLA's (-Wvla).

In DPDK one use of VLA's is in the RTE_BUILD_BUG_ON macro. The best path is to replace the undefined array access with a

better

Quoted 9 lines
Quoted 3 lines

static_assert() which is builtin to compilers.

Using static_assert() is relatively easy, there are a few places

where

Quoted 1 line

it does detect use of non-constant expressions in existing code but

these

Quoted 3 lines

are fixable.

But there is one case where use static_assert() runs into a Clang

bug

Quoted 29 lines
Quoted 6 lines

which is not fixed in distros:

https://github.com/llvm/llvm-project/issues/55821

The code in question is in the sfc driver which for better or worse has lots of assertions. The problem is that clang (except in

unreleased trunk)

Quoted 10 lines

can not handle static_assert in a switch leg.

		switch (actions->type) {
		case RTE_FLOW_ACTION_TYPE_VOID:
			SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_VOID,
					       actions_set);
			break;

../drivers/net/sfc/sfc_flow.c:1635:4: error: expected expression

SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_VOID,

Quoted 2 lines
                        ^

../drivers/net/sfc/sfc_flow.h:36:2: note: expanded from macro

'SFC_BUILD_SET_OVERFLOW'

Quoted 7 lines
        RTE_BUILD_BUG_ON((_action) >= sizeof(_set) * CHAR_BIT)
        ^

There are some workarounds:
0. Ignore it, works on Gcc, and Clang fix is pending.
1. Remove many of these RTE_BUILD_BUG_ON's. They are really not

that helpful.

Quoted 8 lines
Quoted 1 line

2. Add additional { } to these switch cases so they become basic

blocks

Quoted 4 lines

which works around the bug.
3. Move the assertions out of the switch

My preference is #2 but open to other options.

+1 for #2 just make it a block.

Quoted 8 lines

I prefer that you implement the workaround in the RTE_BUILD_BUG_ON() macro, by surrounding it by "do { } while (0)", like this:

#define RTE_BUILD_BUG_ON(condition) do { static_assert(!(condition), #condition); } while (0)

And please mention the workaround reason, with the reference to the clang bug, in the source code comment describing the function.

Typo: "describing the function" -> "describing the RTE_BUILD_BUG_ON macro"

Quoted 2 lines

The godbolt link in the clang issue seems happy with this workaround.

#4 lore raw
In reply to Morten Brørup
Quoted 9 lines · On Tue, 16 Jan 2024 23:14:36 +0100 Morten Brørup <[email protected]>…
Quoted 1 line

+1 for #2 just make it a block.

I prefer that you implement the workaround in the RTE_BUILD_BUG_ON() macro, by surrounding it by "do { } while (0)", like this:

#define RTE_BUILD_BUG_ON(condition) do { static_assert(!(condition), #condition); } while (0)

And please mention the workaround reason, with the reference to the clang bug, in the source code comment describing the function.

The godbolt link in the clang issue seems happy with this workaround.

In the patch series sent, already did that. Didn't need to do { } while(0) hack to make it work. Reference is in commit message.

#5 lore raw
Quoted 7 lines · Stephen Hemminger
Quoted 4 lines · On Tue, 16 Jan 2024 23:14:36 +0100 Morten Brørup <[email protected]>…
Quoted 1 line

+1 for #2 just make it a block.

I prefer that you implement the workaround in the RTE_BUILD_BUG_ON() macro, by surrounding it by "do { } while (0)", like this:

Quoted 8 lines
Quoted 5 lines

#define RTE_BUILD_BUG_ON(condition) do { static_assert(!(condition), #condition); } while (0)

And please mention the workaround reason, with the reference to the clang bug, in the source code comment describing the function.

The godbolt link in the clang issue seems happy with this workaround.

In the patch series sent, already did that. Didn't need to do { } while(0) hack to make it work. Reference is in commit message.

I'm OK with the solution in the patch series. Thanks a lot.

#6 lore raw
In reply to Stephen Hemminger
Quoted 14 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Tuesday, 16 January 2024 23.50

Quoted 3 lines · On Tue, 16 Jan 2024 23:14:36 +0100 Morten Brørup <[email protected]>…
Quoted 1 line

+1 for #2 just make it a block.

I prefer that you implement the workaround in the RTE_BUILD_BUG_ON()

macro, by surrounding it by "do { } while (0)", like this:

Quoted 2 lines

#define RTE_BUILD_BUG_ON(condition) do { static_assert(!(condition),

#condition); } while (0)

Quoted 1 line
Quoted 7 lines
Quoted 1 line

And please mention the workaround reason, with the reference to the

clang bug, in the source code comment describing the function.

Quoted 2 lines

The godbolt link in the clang issue seems happy with this workaround.

In the patch series sent, already did that. Didn't need to do { } while(0) hack to make it work.

It works in the problematic file, but not generally.

Without the do/while, "RTE_BUILD_BUG_ON(condition);" will expand to two lines:

{ static_assert(...); }
;

... which may be problematic when used with if/else without curly braces.

However, I do admit that this problem is probably purely theoretical.

Quoted 1 line

Reference is in commit message.

Yes, but if someone only looks at the source code, the workaround looks strange and unnecessary.

Personally, I prefer not having to dig into commit logs when reviewing code. Maybe it's just me. I'll leave the decision to you.