Skip to content

Commit 9ad7d8f

Browse files
authored
[Statepoint] Optimize Location structure size (#78600)
Reduce its size from 24 to 12 bytes. Improves memory consumption when dealing with statepoint-heavy code.
1 parent 1ab418b commit 9ad7d8f

File tree

3 files changed

+28
-34
lines changed

3 files changed

+28
-34
lines changed

llvm/include/llvm/CodeGen/StackMaps.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ class StatepointOpers {
259259
class StackMaps {
260260
public:
261261
struct Location {
262-
enum LocationType {
262+
enum LocationType : unsigned short {
263263
Unprocessed,
264264
Register,
265265
Direct,
@@ -268,12 +268,13 @@ class StackMaps {
268268
ConstantIndex
269269
};
270270
LocationType Type = Unprocessed;
271-
unsigned Size = 0;
272-
unsigned Reg = 0;
273-
int64_t Offset = 0;
271+
unsigned short Size = 0;
272+
unsigned short Reg = 0;
273+
int32_t Offset = 0;
274274

275275
Location() = default;
276-
Location(LocationType Type, unsigned Size, unsigned Reg, int64_t Offset)
276+
Location(LocationType Type, unsigned short Size, unsigned short Reg,
277+
int32_t Offset)
277278
: Type(Type), Size(Size), Reg(Reg), Offset(Offset) {}
278279
};
279280

@@ -369,7 +370,7 @@ class StackMaps {
369370
MachineInstr::const_mop_iterator
370371
parseOperand(MachineInstr::const_mop_iterator MOI,
371372
MachineInstr::const_mop_iterator MOE, LocationVec &Locs,
372-
LiveOutVec &LiveOuts) const;
373+
LiveOutVec &LiveOuts);
373374

374375
/// Specialized parser of statepoint operands.
375376
/// They do not directly correspond to StackMap record entries.

llvm/lib/CodeGen/StackMaps.cpp

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ static unsigned getDwarfRegNum(unsigned Reg, const TargetRegisterInfo *TRI) {
207207
MachineInstr::const_mop_iterator
208208
StackMaps::parseOperand(MachineInstr::const_mop_iterator MOI,
209209
MachineInstr::const_mop_iterator MOE, LocationVec &Locs,
210-
LiveOutVec &LiveOuts) const {
210+
LiveOutVec &LiveOuts) {
211211
const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo();
212212
if (MOI->isImm()) {
213213
switch (MOI->getImm()) {
@@ -238,7 +238,22 @@ StackMaps::parseOperand(MachineInstr::const_mop_iterator MOI,
238238
++MOI;
239239
assert(MOI->isImm() && "Expected constant operand.");
240240
int64_t Imm = MOI->getImm();
241-
Locs.emplace_back(Location::Constant, sizeof(int64_t), 0, Imm);
241+
if (isInt<32>(Imm)) {
242+
Locs.emplace_back(Location::Constant, sizeof(int64_t), 0, Imm);
243+
} else {
244+
// ConstPool is intentionally a MapVector of 'uint64_t's (as
245+
// opposed to 'int64_t's). We should never be in a situation
246+
// where we have to insert either the tombstone or the empty
247+
// keys into a map, and for a DenseMap<uint64_t, T> these are
248+
// (uint64_t)0 and (uint64_t)-1. They can be and are
249+
// represented using 32 bit integers.
250+
assert((uint64_t)Imm != DenseMapInfo<uint64_t>::getEmptyKey() &&
251+
(uint64_t)Imm != DenseMapInfo<uint64_t>::getTombstoneKey() &&
252+
"empty and tombstone keys should fit in 32 bits!");
253+
auto Result = ConstPool.insert(std::make_pair(Imm, Imm));
254+
Locs.emplace_back(Location::ConstantIndex, sizeof(int64_t), 0,
255+
Result.first - ConstPool.begin());
256+
}
242257
break;
243258
}
244259
}
@@ -497,27 +512,6 @@ void StackMaps::recordStackMapOpers(const MCSymbol &MILabel,
497512
while (MOI != MOE)
498513
MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
499514

500-
// Move large constants into the constant pool.
501-
for (auto &Loc : Locations) {
502-
// Constants are encoded as sign-extended integers.
503-
// -1 is directly encoded as .long 0xFFFFFFFF with no constant pool.
504-
if (Loc.Type == Location::Constant && !isInt<32>(Loc.Offset)) {
505-
Loc.Type = Location::ConstantIndex;
506-
// ConstPool is intentionally a MapVector of 'uint64_t's (as
507-
// opposed to 'int64_t's). We should never be in a situation
508-
// where we have to insert either the tombstone or the empty
509-
// keys into a map, and for a DenseMap<uint64_t, T> these are
510-
// (uint64_t)0 and (uint64_t)-1. They can be and are
511-
// represented using 32 bit integers.
512-
assert((uint64_t)Loc.Offset != DenseMapInfo<uint64_t>::getEmptyKey() &&
513-
(uint64_t)Loc.Offset !=
514-
DenseMapInfo<uint64_t>::getTombstoneKey() &&
515-
"empty and tombstone keys should fit in 32 bits!");
516-
auto Result = ConstPool.insert(std::make_pair(Loc.Offset, Loc.Offset));
517-
Loc.Offset = Result.first - ConstPool.begin();
518-
}
519-
}
520-
521515
// Create an expression to calculate the offset of the callsite from function
522516
// entry.
523517
const MCExpr *CSOffsetExpr = MCBinaryExpr::createSub(

llvm/test/CodeGen/X86/statepoint-fixup-undef.mir

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,11 @@ body: |
124124
; STACKMAP-NEXT: .byte 0
125125
; STACKMAP-NEXT: .short 0
126126
; STACKMAP-NEXT: .long 1
127-
; STACKMAP-NEXT: .long 1
127+
; STACKMAP-NEXT: .long 0
128128
; STACKMAP-NEXT: .long 1
129129
; STACKMAP-NEXT: .quad test_undef
130130
; STACKMAP-NEXT: .quad 88
131131
; STACKMAP-NEXT: .quad 1
132-
; STACKMAP-NEXT: .quad 4278124286
133132
; STACKMAP-NEXT: .quad 2
134133
; STACKMAP-NEXT: .long .Ltmp0-test_undef
135134
; STACKMAP-NEXT: .short 0
@@ -182,13 +181,13 @@ body: |
182181
; STACKMAP-NEXT: .short 3
183182
; STACKMAP-NEXT: .short 0
184183
; STACKMAP-NEXT: .long 0
185-
; This is entry we're looking for, reference to constant pool entry 0xFEFEFEFE
186-
; STACKMAP-NEXT: .byte 5
184+
; This is a constant 0xFEFEFEFE we are looking for
185+
; STACKMAP-NEXT: .byte 4
187186
; STACKMAP-NEXT: .byte 0
188187
; STACKMAP-NEXT: .short 8
189188
; STACKMAP-NEXT: .short 0
190189
; STACKMAP-NEXT: .short 0
191-
; STACKMAP-NEXT: .long 0
190+
; STACKMAP-NEXT: .long -16843010
192191
; STACKMAP-NEXT: .byte 4
193192
; STACKMAP-NEXT: .byte 0
194193
; STACKMAP-NEXT: .short 8

0 commit comments

Comments
 (0)