Skip to content

Commit e99f7d8

Browse files
preamesmemfrob
authored and
memfrob
committed
[GC] Remove redundant entiries in stackmap section (and test it this time)
This is a reimplementation of the optimization removed in D75964. The actual spill/fill optimization is handled by D76013, this one just worries about reducing the stackmap section size itself by eliminating redundant entries. As noted in the comments, we could go a lot further here, but avoiding the degenerate invoke case as we did before is probably "enough" in practice. Differential Revision: https://reviews.llvm.org/D76021
1 parent f1f253c commit e99f7d8

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

llvm/lib/CodeGen/SelectionDAG/StatepointLowering.cpp

+22-3
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ SDValue SelectionDAGBuilder::LowerAsSTATEPOINT(
603603
// Clear state
604604
StatepointLowering.startNewStatepoint(*this);
605605
assert(SI.Bases.size() == SI.Ptrs.size() &&
606-
SI.Ptrs.size() == SI.GCRelocates.size());
606+
SI.Ptrs.size() <= SI.GCRelocates.size());
607607

608608
#ifndef NDEBUG
609609
for (auto *Reloc : SI.GCRelocates)
@@ -823,10 +823,29 @@ SelectionDAGBuilder::LowerStatepoint(ImmutableStatepoint ISP,
823823
ISP.getNumCallArgs(), ActualCallee,
824824
ISP.getActualReturnType(), false /* IsPatchPoint */);
825825

826+
// There may be duplication in the gc.relocate list; such as two copies of
827+
// each relocation on normal and exceptional path for an invoke. We only
828+
// need to spill once and record one copy in the stackmap, but we need to
829+
// reload once per gc.relocate. (Dedupping gc.relocates is trickier and best
830+
// handled as a CSE problem elsewhere.)
831+
// TODO: There a couple of major stackmap size optimizations we could do
832+
// here if we wished.
833+
// 1) If we've encountered a derived pair {B, D}, we don't need to actually
834+
// record {B,B} if it's seen later.
835+
// 2) Due to rematerialization, actual derived pointers are somewhat rare;
836+
// given that, we could change the format to record base pointer relocations
837+
// separately with half the space. This would require a format rev and a
838+
// fairly major rework of the STATEPOINT node though.
839+
SmallSet<SDValue, 8> Seen;
826840
for (const GCRelocateInst *Relocate : ISP.getRelocates()) {
827841
SI.GCRelocates.push_back(Relocate);
828-
SI.Bases.push_back(Relocate->getBasePtr());
829-
SI.Ptrs.push_back(Relocate->getDerivedPtr());
842+
843+
SDValue BaseSD = getValue(Relocate->getBasePtr());
844+
SDValue DerivedSD = getValue(Relocate->getDerivedPtr());
845+
if (Seen.insert(DerivedSD).second) {
846+
SI.Bases.push_back(Relocate->getBasePtr());
847+
SI.Ptrs.push_back(Relocate->getDerivedPtr());
848+
}
830849
}
831850

832851
SI.GCArgs = ArrayRef<const Use>(ISP.gc_args_begin(), ISP.gc_args_end());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
; RUN: llc -verify-machineinstrs < %s | fgrep -A 10000 .llvm_stackmaps | wc -l | FileCheck %s
2+
3+
; Without removal of duplicate entries, the size is 62 lines
4+
; CHECK: 50
5+
6+
target triple = "x86_64-pc-linux-gnu"
7+
8+
declare void @func()
9+
10+
define i1 @test1(i32 addrspace(1)* %arg) gc "statepoint-example" {
11+
entry:
12+
%safepoint_token = call token (i64, i32, void ()*, i32, i32, ...) @llvm.experimental.gc.statepoint.p0f_isVoidf(i64 0, i32 0, void ()* @func, i32 0, i32 0, i32 0, i32 0, i32 addrspace(1)* %arg)
13+
%reloc1 = call i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %safepoint_token, i32 7, i32 7)
14+
%reloc2 = call i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %safepoint_token, i32 7, i32 7)
15+
%cmp1 = icmp eq i32 addrspace(1)* %reloc1, null
16+
%cmp2 = icmp eq i32 addrspace(1)* %reloc2, null
17+
%cmp = and i1 %cmp1, %cmp2
18+
ret i1 %cmp
19+
}
20+
21+
declare token @llvm.experimental.gc.statepoint.p0f_isVoidf(i64, i32, void ()*, i32, i32, ...)
22+
declare i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token, i32, i32)

0 commit comments

Comments
 (0)