This is an archive of the discontinued LLVM Phabricator instance.

[CodeGen][BasicBlockSections] Fix wrong alignment directive placement in basic block section cases
ClosedPublic

Authored by sinan on Nov 7 2022, 12:25 AM.

Details

Summary

MachineBlockPlacement pass sets an alignment attribute to the loop header MBB and this attribute will lead to an alignment directive during emitting asm. In the case of the basic block section, the alignment directive is put before the section label, and thus the alignment is set to the predecessor of the loop header, which is not what we expect and increases the code size (both inserting nop and set section alignment).

e.g. reify.__part.2 is the loop header and .p2align 4, 0x90 should be moved below .section .text.reify.reify.__part.2.

	.section	.text.reify.reify.__part.1,"ax",@progbits
reify.__part.1:                         # %if.end
	.cfi_startproc
	.cfi_def_cfa %rsp, 8
	movq	(%rdi), %rcx
	movq	24(%rcx), %rdx
	leaq	1(%rdx), %rax
.Ltmp1:
	#DEBUG_VALUE: reify:key <- $rax
	cmpq	16(%rcx), %rdx
	jle	reify.__part.3
	jmp	reify.__part.2
.LBB_END0_1:
	.size	reify.__part.1, .LBB_END0_1-reify.__part.1
	.cfi_endproc
	.p2align	4, 0x90
	.section	.text.reify.reify.__part.2,"ax",@progbits
        # .p2align is expected to be put here!
reify.__part.2:                         # %while.body
                                        # =>This Inner Loop Header: Depth=1
	.cfi_startproc
	.cfi_def_cfa %rsp, 8
[Nr] Name                             Type                   Address                       Off         Size      ES   Flg Lk Inf Al
[ 3] .text.reify                        PROGBITS        0000000000000000 000040 00000f 00  AX  0   0 16
[ 5] .text.reify.reify.__part.1 PROGBITS        0000000000000000 000050 000020 00  AX  0   0 16
[ 7] .text.reify.reify.__part.2 PROGBITS        0000000000000000 000070 000024 00  AX  0   0  1

Diff Detail

Event Timeline

sinan created this revision.Nov 7 2022, 12:25 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 7 2022, 12:25 AM
sinan requested review of this revision.Nov 7 2022, 12:25 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 7 2022, 12:25 AM
sinan edited the summary of this revision. (Show Details)Nov 7 2022, 12:40 AM
sinan added reviewers: NickGuy, rahmanl, SjoerdMeijer.

Thanks for this fix.

llvm/test/CodeGen/X86/align-basic-block-sections.ll
1 ↗(On Diff #473583)

Can you please simplify the test and check the assembly only to make sure the alignment directive is placed at the right position?
You might want to use -align-all-blocks or -align-all-nofallthru-blocks and then use a simple if-then-else statement to trigger alignment.

sinan updated this revision to Diff 474411.Nov 9 2022, 6:43 PM

update the test case. This test case is generated after basic-block-sections pass and the name mapping between the basic block(bb.2.entry (align 256, bbsections 2)) and the label name(test.__part.2) from the assembly can be clearer in this way.

rahmanl accepted this revision.Nov 16 2022, 2:13 PM

Thank you.

This revision is now accepted and ready to land.Nov 16 2022, 2:13 PM
This revision was landed with ongoing or failed builds.Nov 16 2022, 11:02 PM
This revision was automatically updated to reflect the committed changes.