This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Fixing stack offset for RVV object with vararg in stack.
ClosedPublic

Authored by kito-cheng on Apr 5 2022, 9:16 PM.

Details

Summary

We found LLVM generate wrong stack offset for RVV object when stack
having variable argument, that cause by we didn't count vaarg part during
calculate RVV stack objects.

Also update the stack layout diagram for including vaarg in the diagram.

Stack layout ref:
https://github.com/gcc-mirror/gcc/blob/master/gcc/config/riscv/riscv.cc#L3941

Diff Detail

Event Timeline

kito-cheng created this revision.Apr 5 2022, 9:16 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 5 2022, 9:16 PM
kito-cheng requested review of this revision.Apr 5 2022, 9:16 PM
kito-cheng edited the summary of this revision. (Show Details)Apr 5 2022, 9:18 PM

Note, I didn't use update_mir_test_checks.py for the testcase since I try to keep stack layout in the check, and that script will generate checks for function body only.

llvm/test/CodeGen/RISCV/wrong-stack-offset-for-rvv-object.mir
23 ↗(On Diff #420700)

See detailed stack layout diagram here.

167 ↗(On Diff #420700)

So 40 is apparently wrong offset according the stack layout diagram.

I guess this fix might need to consider back port LLVM 14.

kito-cheng updated this revision to Diff 421079.Apr 6 2022, 8:44 PM

Changes:

  • Move test case into test/CodeGen/RISCV/rvv.
rogfer01 accepted this revision.Apr 7 2022, 4:27 AM

This looks good to me, based on what we discussed on D123179 with this change the stack looks like this

The assembly emitted is as follows, the offset of v8 is now correctly computed.

asm_fprintf:                            # @asm_fprintf
# %bb.0:                                # %entry
	addi	sp, sp, -64
	sd	ra, 40(sp)                      # 8-byte Folded Spill
	sd	s0, 32(sp)                      # 8-byte Folded Spill
	sd	s1, 24(sp)                      # 8-byte Folded Spill
	csrr	a0, vlenb
	sub	sp, sp, a0
	mv	s0, a4
	mv	s1, a1
	csrr	a0, vlenb
	add	a0, sp, a0
	sd	a7, 56(a0)
	csrr	a0, vlenb
	add	a0, sp, a0
	sd	a6, 48(a0)
	vsetivli	zero, 2, e8, mf8, ta, mu
	vmv.v.i	v8, 0
	addi	a0, sp, 24
	vs1r.v	v8, (a0)                        # Unknown-size Folded Spill
.LBB0_1:                                # %while.cond
                                        # =>This Inner Loop Header: Depth=1
	bnez	zero, .LBB0_1
# %bb.2:                                # %sw.bb
                                        #   in Loop: Header=BB0_1 Depth=1
	vsetivli	zero, 2, e8, mf8, ta, mu
	addi	a0, sp, 24
	vl1r.v	v8, (a0)                        # Unknown-size Folded Reload
	vse8.v	v8, (s0)
	mv	a0, s1
	call	fprintf@plt
	j	.LBB0_1

I've also checked cases with FP and BP but they seem unaffected by the original bug.

I have no objection with this change. Thanks @kito-cheng.

This revision is now accepted and ready to land.Apr 7 2022, 4:27 AM
This revision was landed with ongoing or failed builds.Apr 7 2022, 9:01 PM
This revision was automatically updated to reflect the committed changes.