llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Enable shrink-wrap optimization on RISC-V

Open preames opened this issue 4 years ago • 5 comments

ShrinkWrapping moves spill/fill code into the region which may require it. Classic example:

void foo(bool a) {
  if (a) bar(5, 6, 23, 0.0, 99);
}

Frame setup is only required on the path which calls bar.

This was previously enabled, but reverted in https://github.com/llvm/llvm-project/issues/53662. We need to revisit and fix the bug which triggered the revert.

preames avatar Jun 30 '22 20:06 preames

@llvm/issue-subscribers-backend-risc-v

llvmbot avatar Jun 30 '22 20:06 llvmbot

Shrink wrapping was re-enabled in 5744b9cb7998.

However, even slightly tweaked test cases continue to show opportunity for shrink wrapping which we don't get.

declare void @baz(i64, i64, i64)

define i64 @bar(i1 %c, i64 %a) {
  br i1 %c, label %do_call, label %return
do_call:
  tail call void @baz(i64 0, i64 1, i64 57)
  br label %return
return:
  ret i64 %a
}

Current assembly for this is:

	addi	sp, sp, -16
	.cfi_def_cfa_offset 16
	sd	ra, 8(sp)                       # 8-byte Folded Spill
	sd	s0, 0(sp)                       # 8-byte Folded Spill
	.cfi_offset ra, -8
	.cfi_offset s0, -16
	andi	a0, a0, 1
	mv	s0, a1
	beqz	a0, .LBB2_2
# %bb.1:                                # %do_call
	li	a1, 1
	li	a2, 57
	li	a0, 0
	call	baz@plt
.LBB2_2:                                # %return
	mv	a0, s0
	ld	ra, 8(sp)                       # 8-byte Folded Reload
	ld	s0, 0(sp)                       # 8-byte Folded Reload
	addi	sp, sp, 16
	ret

Note that the branch which bypasses the call does not clobber a1, and thus we should be able to still sink the stack manipulation into the call path.

preames avatar Jul 05 '22 17:07 preames

Calling baz redefines a1. Is it possible to sink stack manipulation into the call path?

luxufan avatar Jul 06 '22 08:07 luxufan

This it the MIR before the shrink wrap pass

# *** IR Dump Before Shrink Wrapping analysis (shrink-wrap) ***:
# Machine code for function bar: NoPHIs, TracksLiveness, NoVRegs, TiedOpsRewritten, TracksDebugUserValues
Function Live Ins: $x10, $x11

bb.0 (%ir-block.0):
  successors: %bb.1(0x40000000), %bb.2(0x40000000); %bb.1(50.00%), %bb.2(50.00%)
  liveins: $x10, $x11
  renamable $x10 = ANDI killed renamable $x10, 1
  renamable $x8 = COPY $x11
  BEQ killed renamable $x10, $x0, %bb.2
  PseudoBR %bb.1

bb.1.do_call:
; predecessors: %bb.0
  successors: %bb.2(0x80000000); %bb.2(100.00%)
  liveins: $x8
  ADJCALLSTACKDOWN 0, 0, implicit-def dead $x2, implicit $x2
  $x11 = ADDI $x0, 1
  $x12 = ADDI $x0, 57
  $x10 = COPY $x0
  PseudoCALL target-flags(riscv-call) @baz, <regmask $vlenb $x0 $x1 $x8 $x9 $x18 $x19 $x20 $x21 $x22 $x23 $x24 $x25 $x26 $x27 $x8_x9 $x18_x19 $x20_x21 $x22_x23 $x24_x25 $x26_x27>, implicit-def dead $x1, implicit $x10, implicit $x11, implicit killed $x12, implicit-def $x2
  ADJCALLSTACKUP 0, 0, implicit-def dead $x2, implicit $x2

bb.2.return:
; predecessors: %bb.0, %bb.1
  liveins: $x8
  $x10 = COPY killed renamable $x8
  PseudoRET implicit $x10

# End machine code for function bar.

The copy to X8 exists before the branch at that point. That copy would need to be moved below the branch and the copy at in bb.2.return would need to be moved into bb.1. I don't know if shrinking is capable of moving those instructions.

topperc avatar Feb 29 '24 23:02 topperc

This example might not be RISC-V specific? X86 doesn't optimize it either. https://godbolt.org/z/znnGbGz5K

bar:                                    # @bar
        push    rbx
        mov     rbx, rsi
        test    dil, 1
        je      .LBB0_2
        mov     esi, 1
        mov     edx, 57
        xor     edi, edi
        call    baz@PLT
.LBB0_2:                                # %return
        mov     rax, rbx
        pop     rbx
        ret

ChunyuLiao avatar May 22 '25 08:05 ChunyuLiao