This is an archive of the discontinued LLVM Phabricator instance.

[InstSimplify] Constant fold cast expressions with no already folded ops
AbandonedPublic

Authored by bjope on May 10 2021, 7:44 AM.

Details

Summary

The test case added in this commit includes reduced IR from a test
that failed when dumping the IR in textual form (e.g. when outputting
the .ll file). In general it is a known problem that one could end
up with huge constant expressions that aren't friendly to the
textual format at hand, see for example PR38538 for another example.
However, in the test case added here it should be possible to fold
the constant expression but there seem to be some problem with the
SimplifyCastInst having problems to fold the cast expression if the
operand isn't folded already. And some passes (such as SROA) seem to
generate constant expressions that aren't folded already.

Diff Detail

Event Timeline

bjope created this revision.May 10 2021, 7:44 AM
bjope requested review of this revision.May 10 2021, 7:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 10 2021, 7:44 AM
bjope added inline comments.May 10 2021, 7:56 AM
llvm/lib/Analysis/InstructionSimplify.cpp
4552

Maybe this is an ugly solution. But I could not really figure out what to do.

I had some problems with infinite recursion if doing this directly inside ConstantFoldCastOperand. That is why this patch is doing it before the call to ConstantFoldCastOperand. Should ConstantFoldCastOperand expect that operands already are folded (kind of like in this patch), and then the implementation of ConstantFoldCastOperand doesn't need to handle recursive folding?

nikic added a comment.May 10 2021, 8:33 AM

This looks somewhat suspect to me. Why is it important that InstSimplify specifically handles this? For example, I would expect InstCombine to take care of this, as it does operand constant folding.

bjope added a comment.May 10 2021, 8:43 AM

This looks somewhat suspect to me. Why is it important that InstSimplify specifically handles this? For example, I would expect InstCombine to take care of this, as it does operand constant folding.

Yes, instcombine would do this simplification. That is how I figured out that ConstantFoldConstant actually would do the trick. One could of course argue about the usefulness. In this case I guess it was about phase ordering and that one couldn't dump the IR without running instcombine first. Kind of making bisecting and fuzzy testing of single passes a bit more complicated.

lebedev.ri resigned from this revision.Jan 12 2023, 5:21 PM

This review seems to be stuck/dead, consider abandoning if no longer relevant.

Herald added a project: Restricted Project. · View Herald TranscriptJan 12 2023, 5:21 PM
Herald added a subscriber: StephenFan. · View Herald Transcript
bjope abandoned this revision.Nov 6 2023, 4:59 AM

Abandoning this. I don't think we can get that kind of constant expressions nowadays. And the reproducer stopped working in the beginning of August 2023 (even when using -passes=instsimplify).