From: "Jose E. Marchesi" <jose.marchesi@oracle.com>
To: Yonghong Song <yonghong.song@linux.dev>
Cc: Eduard Zingerman <eddyz87@gmail.com>,
bpf@vger.kernel.org, Yonghong Song <yhs@meta.com>,
Alexei Starovoitov <alexei.starovoitov@gmail.com>,
david.faust@oracle.com, cupertino.miranda@oracle.com
Subject: Re: [PATCH bpf-next] bpf: abstract loop unrolling pragmas in BPF selftests
Date: Thu, 08 Feb 2024 19:59:33 +0100 [thread overview]
Message-ID: <87a5oadboq.fsf@oracle.com> (raw)
In-Reply-To: <514b171d-8a3c-4134-a0b4-9b6531b3fc38@linux.dev> (Yonghong Song's message of "Thu, 8 Feb 2024 10:35:55 -0800")
> On 2/8/24 10:04 AM, Yonghong Song wrote:
>>
>> On 2/8/24 8:51 AM, Jose E. Marchesi wrote:
>>>> On Thu, 2024-02-08 at 16:35 +0100, Jose E. Marchesi wrote:
>>>> [...]
>>>>
>>>>> If the compiler generates assembly code the same code for
>>>>> profile2.c for
>>>>> before and after, that means that the loop does _not_ get
>>>>> unrolled when
>>>>> profiler.inc.h is built with -O2 but without #pragma unroll.
>>>>>
>>>>> But what if #pragma unroll is used? If it unrolls then, that
>>>>> would mean
>>>>> that the pragma does something more than -funroll-loops/-O2.
>>>>>
>>>>> Sorry if I am not making sense. Stuff like this confuses me to no end
>>>>> ;)
>>>> Sorry, I messed up while switching branches :(
>>>> Here are the correct stats:
>>>>
>>>> | File | insn # | insn # |
>>>> | | before | after |
>>>> |-----------------+--------+--------|
>>>> | profiler1.bpf.o | 16716 | 4813 |
>>> This means:
>>>
>>> - With both `#pragma unroll' and -O2 we get 16716 instructions.
>>> - Without `#pragma unroll' and with -O2 we get 4813 instructions.
>>>
>>> Weird.
>>
>> Thanks for the analysis. I can reproduce with vs. without '#pragma
>> unroll' at -O2
>> level, the number of generated insns is indeed different, quite
>> dramatically
>> as the above numbers. I will do some checking in compiler.
>
> Okay, a quick checking compiler found that
> - with "#pragma unroll" means no profitability test and do full
> unroll as instructed
I don't think clang's `#pragma unroll' does full unroll.
On one side, AFAIK `pragma unroll' is supposed to be equivalent to
`pragma clang loop(enable)', which is different to `pragma clang loop
unroll(full)'.
On the other, if you replace `pragma unroll' with `pragma clang loop
unroll(full)' in the BPF selftests you will get branch instruction
overflows.
What criteria `pragma unroll' in clang uses in order to determine how
much it unrolls the loop, compared to -O2|-funroll-loops, I don't know.
> - without "#pragma unroll" mean compiler will do profitability for full unroll,
> if compiler thinks full unroll is not profitable, there will be no unrolling.
>
> So for gcc, even users saying '#pragma unroll', gcc still do
> profitability test?
GCC doesn't support `#pragma unroll'.
Hence in my original patch the macro __pragma_unroll expands to nothing
with GCC. That will lead to the compiler perhaps not unrolling the loop
even with -O2|-funroll-loops.
>
>>
>>>
>>>> | profiler2.bpf.o | 2088 | 2050 |
>>> - Without `#pragma unroll' and with -O2 we get 2088 instructions.
>>> - With `#pragma loop unroll(disable)' and with -O2 we get 2050
>>> instructions.
>>>
>>> Also surprising.
>>>
>>>> | profiler3.bpf.o | 4465 | 1690 |
>>
next prev parent reply other threads:[~2024-02-08 18:59 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-07 10:12 [PATCH bpf-next] bpf: abstract loop unrolling pragmas in BPF selftests Jose E. Marchesi
2024-02-07 21:45 ` Yonghong Song
2024-02-08 11:32 ` Jose E. Marchesi
2024-02-08 12:55 ` Jose E. Marchesi
2024-02-08 14:18 ` Eduard Zingerman
2024-02-08 15:05 ` Jose E. Marchesi
2024-02-08 15:28 ` Eduard Zingerman
2024-02-08 15:35 ` Jose E. Marchesi
2024-02-08 15:53 ` Eduard Zingerman
2024-02-08 16:51 ` Jose E. Marchesi
2024-02-08 18:04 ` Yonghong Song
2024-02-08 18:35 ` Yonghong Song
2024-02-08 18:59 ` Jose E. Marchesi [this message]
2024-02-08 19:03 ` Jose E. Marchesi
2024-02-08 19:34 ` Eduard Zingerman
2024-02-08 19:44 ` Yonghong Song
2024-02-08 19:49 ` Yonghong Song
2024-02-08 20:06 ` Jose E. Marchesi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87a5oadboq.fsf@oracle.com \
--to=jose.marchesi@oracle.com \
--cc=alexei.starovoitov@gmail.com \
--cc=bpf@vger.kernel.org \
--cc=cupertino.miranda@oracle.com \
--cc=david.faust@oracle.com \
--cc=eddyz87@gmail.com \
--cc=yhs@meta.com \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox