This is an archive of the discontinued LLVM Phabricator instance.

X86: completely refactor `X86FrameLowering::emitPrologue`
Needs ReviewPublic

Authored by compnerd on Feb 3 2023, 10:17 AM.

Details

Summary

This function has had a long overdue need for refactoring. The current implementation works across the matrix of:

{IP32, IP64, I64P32} x {Function,Funclet x {Managed,Unmanaged}}

This makes the code extremely brittle and difficult to follow. Introduce the FrameBuilder that constructs the shared context to emit the various operations. The builder has a number of emitters which follow the general pattern of a series of guard statements preventing the emission if the conditions do not hold. We then perform the build. This allows the build operation to become entirely free of control flow - we perform the operations unconditionally. Furthermore, it allows us to split up the operations so that we no longer have a single 600-line function that emits the prologue for every configuration.

This was initially started in D104557. This applies the full transformation as a single patch (though it was performed through ~42 individual commits).

Diff Detail

Event Timeline

compnerd created this revision.Feb 3 2023, 10:17 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 3 2023, 10:18 AM
compnerd requested review of this revision.Feb 3 2023, 10:18 AM
compnerd edited the summary of this revision. (Show Details)Feb 3 2023, 10:21 AM
rnk edited reviewers, added: hans, mstorsjo, pengfei, LuoYuanke; removed: rnk.May 10 2023, 11:28 AM

Let me add some other reviewers. Broadly speaking, this code is super complicated, and any attempt to improve is positive.

LuoYuanke added a subscriber: skan.May 10 2023, 4:08 PM

is there any intended change to functionality? Or just a pure refactor?

FrameBuilder class should be stateless. I.e. all member functions should be const (and there should be no mutable variables).

llvm/lib/Target/X86/X86FrameLowering.cpp
1512

Method names should start with a lowercase letter.

1549

Destructors should not have side effects.

1567

This renaming does not looks necessary, avoiding it would reduce the patch size.

barannikov88 added inline comments.May 13 2023, 5:07 AM
llvm/lib/Target/X86/X86FrameLowering.cpp
1429

This variable is used only in one place. Just call TLI->hasStackProbeSymbol(MF) directly.
The same is true for most of the other variables.

1688

Such kind of checks should be done at the call site.

barannikov88 added inline comments.May 13 2023, 5:18 AM
llvm/lib/Target/X86/X86FrameLowering.cpp
1429

This variable is used only in one place.

I was wrong, it is used in several places.

I'm not sure whether I should proceed with further comments. Once all of the
already expressed concerns are addressed, the result will resemble the original
code before refactoring, with the exception that some code was extracted into
small functions. I'll let X86 maintainers to take a look first.


I'm not an X86 guy, but I studied this code because X86 and my target share
a few concepts. Even though the function is large, I personally found it easy
to follow. It can still benefit from factoring out pieces of code.

rnk added a subscriber: rnk.May 16 2023, 8:20 AM