Skip to content

Coalesce duplicate spills and reloads per CFG block#2892

Merged
milan-tom merged 29 commits intooxcaml:mainfrom
milan-tom:coalesce-block-duplicate-spills-and-reloads
Aug 14, 2024
Merged

Coalesce duplicate spills and reloads per CFG block#2892
milan-tom merged 29 commits intooxcaml:mainfrom
milan-tom:coalesce-block-duplicate-spills-and-reloads

Conversation

@milan-tom
Copy link
Copy Markdown
Contributor

@milan-tom milan-tom commented Aug 5, 2024

At present, if register allocation fails to register allocate a variable it is reloaded/spilled from the stack for every use throughout the duration of the whole program (either via creation of a new temporary register for that use or direct use in an instruction if the architecture supports this). This creates situations where even in a single CFG block the same variable may be reloaded and spilled many times. This can be avoided by using a register for that variable for the duration of a single block. This PR aims to do this by optimising the temporaries created after the first round of register allocation (only for this round to avoid infinite loops and the more rounds this optimisation is involved in the longer register allocation takes) to ensure at most one temporary register is used per variable in each CFG block.

Analysis of improvement (tested on compiler files)

Files

counter better (%) worse (%) equal (%)
reload 77.30 2.04 20.66
spill 32.92 8.06 59.02
block_duplicate_reload 70.15 2.04 27.81
block_duplicate_spill 37.00 0.00 63.00

Functions

counter better (%) worse (%) equal (%)
reload 30.02 0.93 69.05
spill 3.26 1.65 95.08
block_duplicate_reload 18.61 0.83 80.56
block_duplicate_spill 3.27 0.01 96.72

NB: Timing information is not analysed here as the files in the compiler are too small to do meaningful analyses of the worst cases.

The files/functions that have worse counts afterwards are due to assigning block temporaries for variables which have live ranges across a block that interfere with other registers which have already been register allocated. Future PR(s) will address this by disabling/tweaking the optimization in this case.

Full analysis (preview below)
comparison

@milan-tom milan-tom requested a review from xclerc August 5, 2024 15:20
@xclerc
Copy link
Copy Markdown
Contributor

xclerc commented Aug 6, 2024

(I have not read 96afa85 yet.)

@milan-tom milan-tom force-pushed the coalesce-block-duplicate-spills-and-reloads branch from 96afa85 to d6d4a48 Compare August 6, 2024 16:07
@milan-tom milan-tom requested a review from xclerc August 8, 2024 11:52
@milan-tom milan-tom requested a review from xclerc August 12, 2024 12:33
@TheNumbat TheNumbat self-requested a review August 12, 2024 17:49
Copy link
Copy Markdown
Member

@TheNumbat TheNumbat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Xavier's comments already cover my thoughts

@milan-tom milan-tom force-pushed the coalesce-block-duplicate-spills-and-reloads branch from ce5407b to dbe4135 Compare August 13, 2024 17:47
@milan-tom
Copy link
Copy Markdown
Contributor Author

Completed final checks and confirmed changes when optimization is disabled are only in files modified in this PR.

@milan-tom milan-tom merged commit 4cf708e into oxcaml:main Aug 14, 2024
@milan-tom milan-tom deleted the coalesce-block-duplicate-spills-and-reloads branch August 14, 2024 15:52
@xclerc xclerc mentioned this pull request Aug 16, 2024
@milan-tom milan-tom restored the coalesce-block-duplicate-spills-and-reloads branch September 4, 2024 12:36
@milan-tom milan-tom deleted the coalesce-block-duplicate-spills-and-reloads branch September 4, 2024 13:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants