Skip to content

[clang][LoongArch] Too many instructions are generated for const-string copy with -mstrict-align #101295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
heiher opened this issue Jul 31, 2024 · 1 comment · Fixed by #101309

Comments

@heiher
Copy link
Member

heiher commented Jul 31, 2024

Command line options:

clang -target loongarch64 -O3 -S -o - align.c -mstrict-align

Minimal reproduction:

#include <stdio.h>

extern void outs(char *str);

void test(void)
{
    char msgbuf[64];
    sprintf(msgbuf, "preemption imbalance ");
    outs(msgbuf);
}

LoongArch machine code (clang 20.0.0git):

	.text
	.file	"align.c"
	.globl	test                            # -- Begin function test
	.p2align	5
	.type	test,@function
test:                                   # @test
# %bb.0:                                # %entry
	addi.d	$sp, $sp, -80
	st.d	$ra, $sp, 72                    # 8-byte Folded Spill
	pcalau12i	$a0, %pc_hi20(.L.str)
	addi.d	$a0, $a0, %pc_lo12(.L.str)
	ld.b	$a1, $a0, 21
	ld.bu	$a2, $a0, 20
	slli.d	$a1, $a1, 8
	or	$a1, $a1, $a2
	st.h	$a1, $sp, 28
	ld.bu	$a1, $a0, 17
	ld.bu	$a2, $a0, 16
	ld.bu	$a3, $a0, 18
	ld.b	$a4, $a0, 19
	slli.d	$a1, $a1, 8
	or	$a1, $a1, $a2
	slli.d	$a2, $a3, 16
	slli.d	$a3, $a4, 24
	or	$a2, $a3, $a2
	or	$a1, $a2, $a1
	st.w	$a1, $sp, 24
	ld.bu	$a1, $a0, 9
	ld.bu	$a2, $a0, 8
	ld.bu	$a3, $a0, 10
	ld.bu	$a4, $a0, 11
	slli.d	$a1, $a1, 8
	or	$a1, $a1, $a2
	slli.d	$a2, $a3, 16
	slli.d	$a3, $a4, 24
	or	$a2, $a3, $a2
	or	$a1, $a2, $a1
	ld.bu	$a2, $a0, 13
	ld.bu	$a3, $a0, 12
	ld.bu	$a4, $a0, 14
	ld.bu	$a5, $a0, 15
	slli.d	$a2, $a2, 8
	or	$a2, $a2, $a3
	slli.d	$a3, $a4, 16
	slli.d	$a4, $a5, 24
	or	$a3, $a4, $a3
	or	$a2, $a3, $a2
	slli.d	$a2, $a2, 32
	or	$a1, $a2, $a1
	st.d	$a1, $sp, 16
	ld.bu	$a1, $a0, 1
	ld.bu	$a2, $a0, 0
	ld.bu	$a3, $a0, 2
	ld.bu	$a4, $a0, 3
	slli.d	$a1, $a1, 8
	or	$a1, $a1, $a2
	slli.d	$a2, $a3, 16
	slli.d	$a3, $a4, 24
	or	$a2, $a3, $a2
	or	$a1, $a2, $a1
	ld.bu	$a2, $a0, 5
	ld.bu	$a3, $a0, 4
	ld.bu	$a4, $a0, 6
	ld.bu	$a0, $a0, 7
	slli.d	$a2, $a2, 8
	or	$a2, $a2, $a3
	slli.d	$a3, $a4, 16
	slli.d	$a0, $a0, 24
	or	$a0, $a0, $a3
	or	$a0, $a0, $a2
	slli.d	$a0, $a0, 32
	or	$a0, $a0, $a1
	st.d	$a0, $sp, 8
	addi.d	$a0, $sp, 8
	bl	%plt(outs)
	ld.d	$ra, $sp, 72                    # 8-byte Folded Reload
	addi.d	$sp, $sp, 80
	ret
.Lfunc_end0:
	.size	test, .Lfunc_end0-test
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"preemption imbalance "
	.size	.L.str, 22

	.ident	"clang version 20.0.0git (https://github.com/llvm/llvm-project 84ad292f343e0b0b821fc18447a20c59530f9ec7)"
	.section	".note.GNU-stack","",@progbits
	.addrsig

LoongArch machine code (gcc 14.1.0):

	.file	"align.c"
	.text
	.section	.rodata.str1.8,"aMS",@progbits,1
	.align	3
.LC0:
	.ascii	"preemption imbalance \000"
	.text
	.align	2
	.align	3
	.globl	test
	.type	test, @function
test:
.LFB0 = .
	.cfi_startproc
	la.local	$r12,.LC0
	ldptr.d	$r15,$r12,0
	ld.d	$r14,$r12,8
	ldptr.w	$r13,$r12,16
	ld.hu	$r12,$r12,20
	addi.d	$r3,$r3,-80
	.cfi_def_cfa_offset 80
	or	$r4,$r3,$r0
	st.d	$r1,$r3,72
	.cfi_offset 1, -8
	stptr.d	$r15,$r3,0
	st.d	$r14,$r3,8
	st.w	$r13,$r3,16
	st.h	$r12,$r3,20
	bl	%plt(outs)
	ld.d	$r1,$r3,72
	.cfi_restore 1
	addi.d	$r3,$r3,80
	.cfi_def_cfa_offset 0
	jr	$r1
	.cfi_endproc
.LFE0:
	.size	test, .-test


	.ident	"GCC: (crosstool-NG UNKNOWN) 14.1.0"
	.section	.note.GNU-stack,"",@progbits
@heiher heiher self-assigned this Jul 31, 2024
@github-actions github-actions bot added the clang Clang issues not falling into any other category label Jul 31, 2024
@heiher
Copy link
Member Author

heiher commented Jul 31, 2024

LoongArch machine code (clang 20.0.0git + #101309):

	.text
	.file	"align.c"
	.globl	test                            # -- Begin function test
	.p2align	5
	.type	test,@function
test:                                   # @test
# %bb.0:                                # %entry
	addi.d	$sp, $sp, -80
	st.d	$ra, $sp, 72                    # 8-byte Folded Spill
	pcalau12i	$a0, %pc_hi20(.L.str)
	addi.d	$a0, $a0, %pc_lo12(.L.str)
	ld.h	$a1, $a0, 20
	ld.w	$a2, $a0, 16
	ld.d	$a3, $a0, 8
	ld.d	$a0, $a0, 0
	st.h	$a1, $sp, 28
	st.w	$a2, $sp, 24
	st.d	$a3, $sp, 16
	st.d	$a0, $sp, 8
	addi.d	$a0, $sp, 8
	bl	%plt(outs)
	ld.d	$ra, $sp, 72                    # 8-byte Folded Reload
	addi.d	$sp, $sp, 80
	ret
.Lfunc_end0:
	.size	test, .Lfunc_end0-test
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.8,"aMS",@progbits,1
	.p2align	3, 0x0
.L.str:
	.asciz	"preemption imbalance "
	.size	.L.str, 22

	.ident	"clang version 20.0.0git (https://github.com/llvm/llvm-project 37f06dda98eed9495f44e787fdaeb162ab7baec3)"
	.section	".note.GNU-stack","",@progbits
	.addrsig

@EugeneZelenko EugeneZelenko added missed-optimization backend:loongarch and removed clang Clang issues not falling into any other category labels Jul 31, 2024
@heiher heiher closed this as completed in 8b26c02 Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants