This is an archive of the discontinued LLVM Phabricator instance.

BPF: avoid NE/EQ loop exit condition
ClosedPublic

Authored by yonghong-song on Aug 4 2021, 10:38 AM.

Details

Summary

Kuniyuki Iwashima reported reported in [1] that llvm compiler may
convert a loop exit condition with "i < bound" to "i != bound", where
"i" is the loop index variable and "bound" is the upper bound.
In case that "bound" is not a constant, verifier will always have "i != bound"
true, which will cause verifier failure since to verifier this is
an infinite loop.

The fix is to avoid transforming "i < bound" to "i != bound".
In llvm, the transformation is done by IndVarSimplify pass.
The compiler checks loop condition cost (i = i + 1) and if the
cost is lower, it may transform "i < bound" to "i != bound".
This patch implemented getArithmeticInstrCost() in BPF TargetTransformInfo
class to return a higher cost for such an operation, which
will prevent the transformation for the test case
added in this patch.

[1] https://lore.kernel.org/netdev/1994df05-8f01-371f-3c3b-d33d7836878c@fb.com/

Diff Detail

Event Timeline

yonghong-song created this revision.Aug 4 2021, 10:38 AM
yonghong-song requested review of this revision.Aug 4 2021, 10:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 4 2021, 10:38 AM
anakryiko accepted this revision.Aug 4 2021, 1:27 PM

I've hit similar issue in the past and it's a very frustrating experience having to work around that. So thumbs up for preventing this!

This revision is now accepted and ready to land.Aug 4 2021, 1:27 PM
This revision was landed with ongoing or failed builds.Aug 4 2021, 4:54 PM
This revision was automatically updated to reflect the committed changes.