This is an archive of the discontinued LLVM Phabricator instance.

[ConstantFold] Fold bitcasts on scalable vector splats.
Needs ReviewPublic

Authored by sdesmalen on May 12 2022, 12:32 AM.

Details

Summary

This patch also contains changes to InstCombine to avoid a regression for
scalable-const-fp-splat.ll which otherwise would no longer be folded
because the expression is not an explicit ConstExpr any more, but a folded
splat instead.

This patch additionally fixes

https://github.com/llvm/llvm-project/issues/55348.

where one of the operands to be sunk was a ConstExpr. Because the m_pattern()
functions also match ConstExpr's, the cast<Instruction> following the match
led to an assertion failure. Because we now fold the ConstExpr, there is no
longer a way to represent a sext/zext as a ConstExpr and the patterns no
longer match.

Diff Detail

Event Timeline

sdesmalen created this revision.May 12 2022, 12:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 12 2022, 12:32 AM
sdesmalen requested review of this revision.May 12 2022, 12:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 12 2022, 12:32 AM

From what I gather about the bug report, RISCV doesn't suffer from it as we always dyn_cast to Instruction. Is that right? Just wondering if we should add a similar test - I don't think we have constant expr coverage in sink-splat-operands.ll.

llvm/lib/IR/ConstantFold.cpp
406

DestVecTy == DestVTy, no?

409

cast<VectorType>(DestTy) == DestVTy.

llvm/test/Transforms/InstCombine/addrspacecast.ll
206 ↗(On Diff #428865)

This one strikes me as peculiar. Is it all less canonical than it was before?

sdesmalen updated this revision to Diff 428934.May 12 2022, 7:05 AM
sdesmalen marked 3 inline comments as done.
  • Addressed comments.
  • Reverted the fixed-width change (leading to different addrspacecast constant)

From what I gather about the bug report, RISCV doesn't suffer from it as we always dyn_cast to Instruction. Is that right? Just wondering if we should add a similar test - I don't think we have constant expr coverage in sink-splat-operands.ll.

That's what I did initially, but I couldn't write a test with a ZExt/SExt as ConstExpr that wouldn't fold, so that would mean I'd rewrite the code without being able to test it.

llvm/lib/IR/ConstantFold.cpp
406

You're right, I moved this code around a few times and didn't spot this.

llvm/test/Transforms/InstCombine/addrspacecast.ll
206 ↗(On Diff #428865)

You're right, that's probably not desired, because addrspacecast's don't fold any further. I've reverted that change now.

craig.topper added inline comments.May 12 2022, 11:33 AM
llvm/lib/IR/ConstantFold.cpp
402

Is it always a bitcast or any cast?

Matt added a subscriber: Matt.Jun 28 2022, 2:20 PM