This is an archive of the discontinued LLVM Phabricator instance.

[TargetLowering] Teach DemandedBits about VSCALE
ClosedPublic

Authored by benmxwl-arm on Nov 22 2022, 9:27 AM.

Details

Summary

This allows DemandedBits to see the result of VSCALE will be at most
VScaleMax * some compile-time constant. This relies on the vscale_range()
attribute being present on the function, with a max set. (This is done by
default when clang is targeting AArch64+SVE).

Using this various redundant operations (zexts, sexts, ands, ors, etc)
can be eliminated.

Diff Detail

Event Timeline

benmxwl-arm created this revision.Nov 22 2022, 9:27 AM
benmxwl-arm requested review of this revision.Nov 22 2022, 9:27 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 22 2022, 9:27 AM
Matt added a subscriber: Matt.Nov 22 2022, 10:59 AM

@dmgreen Thanks a lot for your review of D138424. Just adding you here as it's the natural continuation.

paulwalker-arm added inline comments.Dec 12 2022, 6:10 AM
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
1137–1141

ISD::VSCALE's operand is guaranteed to be a constant so you can use Op.getConstantOperandAPInt(0) * (*MaxVScale) without need to test it first.

Remove unnecessary dyn_cast<ConstantSDNode>

benmxwl-arm marked an inline comment as done.Dec 12 2022, 8:28 AM

Done (and now rebased on main fixing up some Optional -> std::optional changes)

paulwalker-arm added inline comments.Dec 13 2022, 4:59 AM
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
1140

Would this be more accurate as ~? Also means we don't need to worry about the negation overflowing, which I doubt is a real concern anyway.

Replaced negation with ~ and added a new test case where this makes a difference.
(Previosuly the redundant or in the new test case would not be removed)

benmxwl-arm marked an inline comment as done.Dec 13 2022, 5:50 AM
paulwalker-arm accepted this revision.Dec 13 2022, 7:05 AM
This revision is now accepted and ready to land.Dec 13 2022, 7:05 AM
This revision was landed with ongoing or failed builds.Dec 14 2022, 7:52 AM
This revision was automatically updated to reflect the committed changes.
uabelho added inline comments.
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
1137

With an UBSan build we get this

../lib/CodeGen/SelectionDAG/TargetLowering.cpp:1137:20: runtime error: signed integer overflow: 16 * -9223372036854775808 cannot be represented in type 'long'

when running lit tests

Failed Tests (1):
  LLVM :: CodeGen/AArch64/sve-gather-scatter-addr-opts.ll

I get it also on latest main (d2b070d3c95c5).

I'm seeing a potential miscompilation on RISCV64 with this particular change: https://github.com/llvm/llvm-project/issues/59594

Given the UB and potential miscompile, reverted in 9b92f70d4758f75903ce93feaba5098130820d40.