This is an archive of the discontinued LLVM Phabricator instance.

[BPF] Fix a bug in peephole optimization
ClosedPublic

Authored by yonghong-song on Nov 20 2019, 2:17 PM.

Details

Reviewers
ast
Summary

One of current peephole optimiations is to remove SLL/SRL if
the sub register has been zero extended. This phase has two bugs
and one limitations.

First, for the physical subregister used in pseudo insn COPY
like below, it permits incorrect optimization.

%0:gpr32 = COPY $w0 
... 
%4:gpr = MOV_32_64 %0:gpr32
%5:gpr = SLL_ri %4:gpr(tied-def 0), 32
%6:gpr = SRA_ri %5:gpr(tied-def 0), 32

The $w0 could be from the return value of a previous function call
and its upper 32-bit value might contain some non-zero values.
The same applies to function arguments.

Second, the current code may permits removing SLL/SRA like below:

%0:gpr32 = COPY $w0 
%1:gpr32 = COPY %0:gpr32
... 
%4:gpr = MOV_32_64 %1:gpr32
%5:gpr = SLL_ri %4:gpr(tied-def 0), 32
%6:gpr = SRA_ri %5:gpr(tied-def 0), 32

The reason is that it did not follow def-use chain to skip all
intermediate 32bit-to-32bit COPY instructions.

The current implementation is also very conservative for PHI
instructions. If any PHI insn component is another PHI or COPY insn,
it will just permit SLL/SRA.

This patch fixed the issue as follows:

  • During def/use chain traversal, if any physical register is read, SLL/SRA will be preserved as these physical registers are mostly from function return values or current function arguments.
  • Recursively visit all COPY and PHI instructions.

Diff Detail

Event Timeline

yonghong-song created this revision.Nov 20 2019, 2:17 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 20 2019, 2:17 PM
ast accepted this revision.Nov 20 2019, 2:28 PM
This revision is now accepted and ready to land.Nov 20 2019, 2:28 PM

Should we backport this to the release/9.x branch?

@tstellar
About the impact of this bug:

  • to trigger the bug, the additional additional -mattr=+alu32 is needed.
  • with this bug, the program is most likely to be rejected by kernel verifier (based on what we know), so people likely just not using -mattr=+alu32 to make kernel verifier happy

That said, it would be still good to backport the change to 9.0.x if possible.

I just tried to cherry-pick to llvm monorepo release/9.x branch. There is a minor cherry-pick conflict as the
same file has been modified in 10.0.0. Let me know if you want me to do the work to prepare the patch.

yonghong-song closed this revision.Nov 20 2019, 3:25 PM

The patch is merged. Sorry I forgot to add the link to the patch.