<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/96847>96847</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
FoldAndOrOfICmpsUsingRanges makes it hard to produce code compliant with BPF verifier
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Spikhalskiy
</td>
</tr>
</table>
<pre>
Currently when compiling code with BPF target, llvm makes optimizations that hurt further BPF verification and at the moment there are seemingly no real workarounds to this problem.
The problem is demonstrated by the following example:
Source code
```c
id = ctx->protocol;
if (id < 4 || id > 12)
return 0;
*(u64 *)((void *)v + id) = 0;
```
is getting translated into something like
```
/* pseudo IR */
id = ctx->protocol;
tmp = id;
tmp += -13;
if (tmp < 0xfffffff7) goto next; // <- tmp and defined bounds are discarded here
v += id;
*v = 0; // <- BPF verification fails because of unbounded variable
next:
```
which fails BPF verification.
This problem and the proposed solution were originally raised by @yonghong-song in
https://reviews.llvm.org/D147078
https://reviews.llvm.org/D147968
but there were seemingly no new developments in almost a year.
This problem was also raised in discussions like
https://lore.kernel.org/all/f9c2ab1f-42a9-f5eb-f9bf-63402c6a8a62@uliege.be/T/ https://lpc.events/event/17/contributions/1648/attachments/1242/2525/Generating%20BPF%20Verifier%20Friendly%20Code%20with%20Clang.pdf
This presents a major paint point because of the absence of a clear workaround, leading to time-sinking fights against LLVM. Could some workaround be implemented until a solid solution is discussed and implemented? It may take the form of a new pragma that would disable `FoldAndOrOfICmpsUsingRanges` transformation that seems to be causing this issue.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVl-vm7gT_TTOyygRMQTCQx5ucstPlfpTq7bb9wEP4MbYyDbkZj_9yiZpc--2q90oAv8Ze86MzxmDzslOEx3Y7sh2zyucfG_s4csozz0qd5bXVW3E9XCarCXt1RUuPWlozDBKJXUHjREEF-l7OH6qwKPtyDN-AqX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