Skip to content

Commit 4c6df0b

Browse files
[JSC] B3 can use undefined bits or not defined required bits when spilling
https://bugs.webkit.org/show_bug.cgi?id=156486 Patch by Benjamin Poulain <[email protected]> on 2016-04-11 Reviewed by Filip Pizlo. Spilling had issues when replacing arguments in place. The problems are: 1) If we have a 32bit stackslot, a x86 instruction could still try to load 64bits from it. 2) If we have a 64bit stackslot, Move32 would only set half the bits. 3) We were reducing Move to Move32 even if the top bits are read from the stack slot. The case 1 appear with something like this: Move32 %tmp0, %tmp1 Op64 %tmp1, %tmp2, %tmp3 When we spill %tmp1, the stack slot is 32bit, Move32 sets 32bits but Op64 supports addressing for %tmp1. When we substitute %tmp1 in Op64, we are creating a 64bit read for a 32bit stack slot. The case 2 is an other common one. If we have: BB#1 Move32 %tmp0, %tmp1 Jump WebPlatformForEmbedded#3 BB#2 Op64 %tmp0, %tmp1 Jump WebPlatformForEmbedded#3 BB#3 Use64 %tmp1 We have a stack slot of 64bits. When spilling %tmp1 in WebPlatformForEmbedded#1, we are effectively doing a 32bit store on the stack slot, leaving the top bits undefined. Case 3 is pretty much the same as 2 but we create the Move32 ourself because the source is a 32bit with ZDef. Case (1) is solved by requiring that the stack slot is at least as large as the largest use/def of that tmp. Case (2) and (3) are solved by not replacing a Tmp by an Address if the Def is smaller than the stack slot. * b3/air/AirIteratedRegisterCoalescing.cpp: * b3/testb3.cpp: (JSC::B3::testSpillDefSmallerThanUse): (JSC::B3::testSpillUseLargerThanDef): (JSC::B3::run): git-svn-id: http://svn.webkit.org/repository/webkit/trunk@199337 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent 7d9b5f7 commit 4c6df0b

4 files changed

Lines changed: 175 additions & 8 deletions

File tree

‎Source/JavaScriptCore/ChangeLog‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,52 @@
1+
2016-04-11 Benjamin Poulain <[email protected]>
2+
3+
[JSC] B3 can use undefined bits or not defined required bits when spilling
4+
https://bugs.webkit.org/show_bug.cgi?id=156486
5+
6+
Reviewed by Filip Pizlo.
7+
8+
Spilling had issues when replacing arguments in place.
9+
10+
The problems are:
11+
1) If we have a 32bit stackslot, a x86 instruction could still try to load 64bits from it.
12+
2) If we have a 64bit stackslot, Move32 would only set half the bits.
13+
3) We were reducing Move to Move32 even if the top bits are read from the stack slot.
14+
15+
The case 1 appear with something like this:
16+
Move32 %tmp0, %tmp1
17+
Op64 %tmp1, %tmp2, %tmp3
18+
When we spill %tmp1, the stack slot is 32bit, Move32 sets 32bits
19+
but Op64 supports addressing for %tmp1. When we substitute %tmp1 in Op64,
20+
we are creating a 64bit read for a 32bit stack slot.
21+
22+
The case 2 is an other common one. If we have:
23+
BB#1
24+
Move32 %tmp0, %tmp1
25+
Jump #3
26+
BB#2
27+
Op64 %tmp0, %tmp1
28+
Jump #3
29+
BB#3
30+
Use64 %tmp1
31+
32+
We have a stack slot of 64bits. When spilling %tmp1 in #1, we are
33+
effectively doing a 32bit store on the stack slot, leaving the top bits undefined.
34+
35+
Case 3 is pretty much the same as 2 but we create the Move32 ourself
36+
because the source is a 32bit with ZDef.
37+
38+
Case (1) is solved by requiring that the stack slot is at least as large as the largest
39+
use/def of that tmp.
40+
41+
Case (2) and (3) are solved by not replacing a Tmp by an Address if the Def
42+
is smaller than the stack slot.
43+
44+
* b3/air/AirIteratedRegisterCoalescing.cpp:
45+
* b3/testb3.cpp:
46+
(JSC::B3::testSpillDefSmallerThanUse):
47+
(JSC::B3::testSpillUseLargerThanDef):
48+
(JSC::B3::run):
49+
150
2016-04-11 Brian Burg <[email protected]>
251

352
Web Inspector: get rid of InspectorBasicValue and InspectorString subclasses

‎Source/JavaScriptCore/b3/air/AirIteratedRegisterCoalescing.cpp‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,11 @@ class IteratedRegisterCoalescing {
14191419
}
14201420
}
14211421

1422+
static unsigned stackSlotMinimumWidth(Arg::Width width)
1423+
{
1424+
return width <= Arg::Width32 ? 4 : 8;
1425+
}
1426+
14221427
template<Arg::Type type>
14231428
void addSpillAndFill(const ColoringAllocator<type>& allocator, HashSet<unsigned>& unspillableTmps)
14241429
{
@@ -1429,7 +1434,7 @@ class IteratedRegisterCoalescing {
14291434

14301435
// Allocate stack slot for each spilled value.
14311436
StackSlot* stackSlot = m_code.addStackSlot(
1432-
m_tmpWidth.width(tmp) <= Arg::Width32 ? 4 : 8, StackSlotKind::Spill);
1437+
stackSlotMinimumWidth(m_tmpWidth.requiredWidth(tmp)), StackSlotKind::Spill);
14331438
bool isNewTmp = stackSlots.add(tmp, stackSlot).isNewEntry;
14341439
ASSERT_UNUSED(isNewTmp, isNewTmp);
14351440
}
@@ -1447,30 +1452,39 @@ class IteratedRegisterCoalescing {
14471452
// only claim to read 32 bits from the source if only 32 bits of the destination are
14481453
// read. Note that we only apply this logic if this turns into a load or store, since
14491454
// Move is the canonical way to move data between GPRs.
1450-
bool forceMove32IfDidSpill = false;
1455+
bool canUseMove32IfDidSpill = false;
14511456
bool didSpill = false;
14521457
if (type == Arg::GP && inst.opcode == Move) {
14531458
if ((inst.args[0].isTmp() && m_tmpWidth.width(inst.args[0].tmp()) <= Arg::Width32)
14541459
|| (inst.args[1].isTmp() && m_tmpWidth.width(inst.args[1].tmp()) <= Arg::Width32))
1455-
forceMove32IfDidSpill = true;
1460+
canUseMove32IfDidSpill = true;
14561461
}
14571462

14581463
// Try to replace the register use by memory use when possible.
14591464
inst.forEachArg(
1460-
[&] (Arg& arg, Arg::Role, Arg::Type argType, Arg::Width width) {
1465+
[&] (Arg& arg, Arg::Role role, Arg::Type argType, Arg::Width width) {
14611466
if (arg.isTmp() && argType == type && !arg.isReg()) {
14621467
auto stackSlotEntry = stackSlots.find(arg.tmp());
14631468
if (stackSlotEntry != stackSlots.end()
14641469
&& inst.admitsStack(arg)) {
1470+
1471+
Arg::Width spillWidth = m_tmpWidth.requiredWidth(arg.tmp());
1472+
if (Arg::isAnyDef(role) && width < spillWidth)
1473+
return;
1474+
ASSERT(inst.opcode == Move || !(Arg::isAnyUse(role) && width > spillWidth));
1475+
1476+
if (spillWidth != Arg::Width32)
1477+
canUseMove32IfDidSpill = false;
1478+
14651479
stackSlotEntry->value->ensureSize(
1466-
forceMove32IfDidSpill ? 4 : Arg::bytes(width));
1480+
canUseMove32IfDidSpill ? 4 : Arg::bytes(width));
14671481
arg = Arg::stack(stackSlotEntry->value);
14681482
didSpill = true;
14691483
}
14701484
}
14711485
});
14721486

1473-
if (didSpill && forceMove32IfDidSpill)
1487+
if (didSpill && canUseMove32IfDidSpill)
14741488
inst.opcode = Move32;
14751489

14761490
// For every other case, add Load/Store as needed.
@@ -1488,9 +1502,9 @@ class IteratedRegisterCoalescing {
14881502
return;
14891503
}
14901504

1491-
Arg arg = Arg::stack(stackSlotEntry->value);
1505+
Arg::Width spillWidth = m_tmpWidth.requiredWidth(tmp);
14921506
Opcode move = Oops;
1493-
switch (stackSlotEntry->value->byteSize()) {
1507+
switch (stackSlotMinimumWidth(spillWidth)) {
14941508
case 4:
14951509
move = type == Arg::GP ? Move32 : MoveFloat;
14961510
break;
@@ -1505,6 +1519,7 @@ class IteratedRegisterCoalescing {
15051519
tmp = m_code.newTmp(type);
15061520
unspillableTmps.add(AbsoluteTmpMapper<type>::absoluteIndex(tmp));
15071521

1522+
Arg arg = Arg::stack(stackSlotEntry->value);
15081523
if (Arg::isAnyUse(role) && role != Arg::Scratch)
15091524
insertionSet.insert(instIndex, move, inst.origin, arg, tmp);
15101525
if (Arg::isAnyDef(role))

‎Source/JavaScriptCore/b3/air/AirTmpWidth.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ class TmpWidth {
6060
return std::min(iter->value.use, iter->value.def);
6161
}
6262

63+
// Return the minimum required width for all defs/uses of this Tmp.
64+
Arg::Width requiredWidth(Tmp tmp)
65+
{
66+
auto iter = m_width.find(tmp);
67+
if (iter == m_width.end())
68+
return Arg::minimumWidth(Arg(tmp).type());
69+
return std::max(iter->value.use, iter->value.def);
70+
}
71+
6372
// This indirectly tells you how much of the tmp's high bits are guaranteed to be zero. The number of
6473
// high bits that are zero are:
6574
//

‎Source/JavaScriptCore/b3/testb3.cpp‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11408,6 +11408,97 @@ void testPatchpointDoubleRegs()
1140811408
CHECK(invoke<double>(*code, 42.5) == 42.5);
1140911409
}
1141011410

11411+
void testSpillDefSmallerThanUse()
11412+
{
11413+
Procedure proc;
11414+
BasicBlock* root = proc.addBlock();
11415+
11416+
// Move32.
11417+
Value* arg32 = root->appendNew<Value>(
11418+
proc, Trunc, Origin(),
11419+
root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0));
11420+
Value* arg64 = root->appendNew<Value>(proc, ZExt32, Origin(), arg32);
11421+
11422+
// Make sure arg64 is on the stack.
11423+
PatchpointValue* forceSpill = root->appendNew<PatchpointValue>(proc, Int64, Origin());
11424+
RegisterSet clobberSet = RegisterSet::allGPRs();
11425+
clobberSet.exclude(RegisterSet::stackRegisters());
11426+
clobberSet.exclude(RegisterSet::reservedHardwareRegisters());
11427+
clobberSet.clear(GPRInfo::returnValueGPR); // Force the return value for aliasing below.
11428+
forceSpill->clobberLate(clobberSet);
11429+
forceSpill->setGenerator(
11430+
[&] (CCallHelpers& jit, const StackmapGenerationParams& params) {
11431+
AllowMacroScratchRegisterUsage allowScratch(jit);
11432+
jit.xor64(params[0].gpr(), params[0].gpr());
11433+
});
11434+
11435+
// On x86, Sub admit an address for any operand. If it uses the stack, the top bits must be zero.
11436+
Value* result = root->appendNew<Value>(proc, Sub, Origin(), forceSpill, arg64);
11437+
root->appendNew<ControlValue>(proc, Return, Origin(), result);
11438+
11439+
auto code = compile(proc);
11440+
CHECK(invoke<int64_t>(*code, 0xffffffff00000000) == 0);
11441+
}
11442+
11443+
void testSpillUseLargerThanDef()
11444+
{
11445+
Procedure proc;
11446+
BasicBlock* root = proc.addBlock();
11447+
BasicBlock* thenCase = proc.addBlock();
11448+
BasicBlock* elseCase = proc.addBlock();
11449+
BasicBlock* tail = proc.addBlock();
11450+
11451+
RegisterSet clobberSet = RegisterSet::allGPRs();
11452+
clobberSet.exclude(RegisterSet::stackRegisters());
11453+
clobberSet.exclude(RegisterSet::reservedHardwareRegisters());
11454+
11455+
Value* condition = root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0);
11456+
Value* argument = root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR1);
11457+
root->appendNew<ControlValue>(
11458+
proc, Branch, Origin(),
11459+
root->appendNew<Value>(
11460+
proc, Trunc, Origin(),
11461+
condition),
11462+
FrequentedBlock(thenCase), FrequentedBlock(elseCase));
11463+
11464+
Value* truncated = thenCase->appendNew<Value>(proc, ZExt32, Origin(),
11465+
thenCase->appendNew<Value>(proc, Trunc, Origin(), argument));
11466+
UpsilonValue* thenResult = thenCase->appendNew<UpsilonValue>(proc, Origin(), truncated);
11467+
thenCase->appendNew<ControlValue>(proc, Jump, Origin(), FrequentedBlock(tail));
11468+
11469+
UpsilonValue* elseResult = elseCase->appendNew<UpsilonValue>(proc, Origin(), argument);
11470+
elseCase->appendNew<ControlValue>(proc, Jump, Origin(), FrequentedBlock(tail));
11471+
11472+
for (unsigned i = 0; i < 100; ++i) {
11473+
PatchpointValue* preventTailDuplication = tail->appendNew<PatchpointValue>(proc, Void, Origin());
11474+
preventTailDuplication->clobberLate(clobberSet);
11475+
preventTailDuplication->setGenerator([] (CCallHelpers&, const StackmapGenerationParams&) { });
11476+
}
11477+
11478+
PatchpointValue* forceSpill = tail->appendNew<PatchpointValue>(proc, Void, Origin());
11479+
forceSpill->clobberLate(clobberSet);
11480+
forceSpill->setGenerator(
11481+
[&] (CCallHelpers& jit, const StackmapGenerationParams&) {
11482+
AllowMacroScratchRegisterUsage allowScratch(jit);
11483+
clobberSet.forEach([&] (Reg reg) {
11484+
jit.move(CCallHelpers::TrustedImm64(0xffffffffffffffff), reg.gpr());
11485+
});
11486+
});
11487+
11488+
Value* phi = tail->appendNew<Value>(proc, Phi, Int64, Origin());
11489+
thenResult->setPhi(phi);
11490+
elseResult->setPhi(phi);
11491+
tail->appendNew<ControlValue>(proc, Return, Origin(), phi);
11492+
11493+
auto code = compile(proc);
11494+
CHECK(invoke<uint64_t>(*code, 1, 0xffffffff00000000) == 0);
11495+
CHECK(invoke<uint64_t>(*code, 0, 0xffffffff00000000) == 0xffffffff00000000);
11496+
11497+
// A second time since the previous run is still on the stack.
11498+
CHECK(invoke<uint64_t>(*code, 1, 0xffffffff00000000) == 0);
11499+
11500+
}
11501+
1141111502
// Make sure the compiler does not try to optimize anything out.
1141211503
NEVER_INLINE double zero()
1141311504
{
@@ -12784,6 +12875,9 @@ void run(const char* filter)
1278412875

1278512876
RUN(testPatchpointDoubleRegs());
1278612877

12878+
RUN(testSpillDefSmallerThanUse());
12879+
RUN(testSpillUseLargerThanDef());
12880+
1278712881
if (tasks.isEmpty())
1278812882
usage();
1278912883

0 commit comments

Comments
 (0)