Skip to content

Commit a92714a

Browse files
committed
Bug 1632067 part 6 - Simplify arguments length code. r=iain
Define the lengths directly in the Python script. Make CacheIRWriter assert the written arguments match this information to ensure everything stays in sync. Differential Revision: https://phabricator.services.mozilla.com/D71904 UltraBlame original commit: 1a37730d1bd1fe8df7e613edf04d18db5ce75f6c
1 parent b59745c commit a92714a

File tree

7 files changed

+87
-105
lines changed

7 files changed

+87
-105
lines changed

js/src/jit/BaselineInspector.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ static bool MaybeArgumentReader(ICStub* stub, CacheOp targetOp,
676676
CacheIRReader stubReader(GetCacheIRStubInfo(stub));
677677
while (stubReader.more()) {
678678
CacheOp op = stubReader.readOp();
679-
uint32_t argLength = CacheIROpFormat::ArgLengths[uint8_t(op)];
679+
uint32_t argLength = CacheIROpArgLengths[size_t(op)];
680680

681681
if (op == targetOp) {
682682
MOZ_ASSERT(argReader.isNothing(),

js/src/jit/CacheIR.cpp

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,47 +38,18 @@ const char* const js::jit::CacheKindNames[] = {
3838
#undef DEFINE_KIND
3939
};
4040

41-
const char* const js::jit::CacheIrOpNames[] = {
41+
const char* const js::jit::CacheIROpNames[] = {
4242
#define OPNAME(op, ...) #op,
4343
CACHE_IR_OPS(OPNAME)
4444
#undef OPNAME
4545
};
4646

47-
48-
49-
50-
namespace js::jit::CacheIROpFormat {
51-
52-
static constexpr uint32_t CacheIRArgLength(ArgType arg) {
53-
switch (arg) {
54-
case None:
55-
return 0;
56-
case Id:
57-
return sizeof(uint8_t);
58-
case Field:
59-
return sizeof(uint8_t);
60-
case Byte:
61-
return sizeof(uint8_t);
62-
case Int32:
63-
case UInt32:
64-
return sizeof(uint32_t);
65-
case Word:
66-
return sizeof(uintptr_t);
67-
}
68-
}
69-
template <typename... Args>
70-
static constexpr uint32_t CacheIRArgsLength(Args... args) {
71-
return (CacheIRArgLength(args) + ...);
72-
}
73-
74-
const uint32_t ArgLengths[] = {
75-
#define ARGLENGTH(op, ...) CacheIRArgsLength(__VA_ARGS__),
47+
const uint32_t js::jit::CacheIROpArgLengths[] = {
48+
#define ARGLENGTH(op, len) len,
7649
CACHE_IR_OPS(ARGLENGTH)
7750
#undef ARGLENGTH
7851
};
7952

80-
}
81-
8253
void CacheIRWriter::assertSameCompartment(JSObject* obj) {
8354
cx_->debugOnlyCheck(obj);
8455
}

js/src/jit/CacheIR.h

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -181,31 +181,14 @@ enum class CacheKind : uint8_t {
181181

182182
extern const char* const CacheKindNames[];
183183

184-
185-
186-
187-
188-
namespace CacheIROpFormat {
189-
enum ArgType {
190-
None,
191-
Id,
192-
Field,
193-
Byte,
194-
Int32,
195-
UInt32,
196-
Word,
197-
};
198-
199-
extern const uint32_t ArgLengths[];
200-
}
201-
202184
enum class CacheOp {
203185
#define DEFINE_OP(op, ...) op,
204186
CACHE_IR_OPS(DEFINE_OP)
205187
#undef DEFINE_OP
206188
};
207189

208-
extern const char* const CacheIrOpNames[];
190+
extern const char* const CacheIROpNames[];
191+
extern const uint32_t CacheIROpArgLengths[];
209192

210193
class StubField {
211194
public:
@@ -481,12 +464,33 @@ class MOZ_RAII CacheIRWriter : public JS::CustomAutoRooter {
481464
mutable uint32_t lastOffset_;
482465
mutable uint32_t lastIndex_;
483466

467+
#ifdef DEBUG
468+
469+
mozilla::Maybe<CacheOp> currentOp_;
470+
size_t currentOpArgsStart_ = 0;
471+
#endif
472+
484473
void assertSameCompartment(JSObject*);
485474

486475
void writeOp(CacheOp op) {
487476
MOZ_ASSERT(uint32_t(op) <= UINT8_MAX);
488477
buffer_.writeByte(uint32_t(op));
489478
nextInstructionId_++;
479+
#ifdef DEBUG
480+
MOZ_ASSERT(currentOp_.isNothing(), "Missing call to assertLengthMatches?");
481+
currentOp_.emplace(op);
482+
currentOpArgsStart_ = buffer_.length();
483+
#endif
484+
}
485+
486+
void assertLengthMatches() {
487+
#ifdef DEBUG
488+
489+
size_t expectedLen = CacheIROpArgLengths[size_t(*currentOp_)];
490+
MOZ_ASSERT_IF(!failed(),
491+
buffer_.length() - currentOpArgsStart_ == expectedLen);
492+
currentOp_.reset();
493+
#endif
490494
}
491495

492496
void writeOperandId(OperandId opId) {

js/src/jit/CacheIRSpewer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class MOZ_RAII CacheIROpsJitSpewer {
4545
CACHE_IR_SPEWER_GENERATED
4646

4747
void spewOp(CacheOp op) {
48-
const char* opName = CacheIrOpNames[size_t(op)];
48+
const char* opName = CacheIROpNames[size_t(op)];
4949
out_.printf("%s%-30s", prefix_, opName);
5050
}
5151
void spewOpEnd() { out_.printf("\n"); }
@@ -183,7 +183,7 @@ class MOZ_RAII CacheIROpsJSONSpewer {
183183
CACHE_IR_SPEWER_GENERATED
184184

185185
void spewOp(CacheOp op) {
186-
const char* opName = CacheIrOpNames[size_t(op)];
186+
const char* opName = CacheIROpNames[size_t(op)];
187187
j_.beginObject();
188188
j_.property("op", opName);
189189
j_.beginListProperty("args");

js/src/jit/GenerateCacheIRFiles.py

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ def gen_writer_method(name, args, custom_writer):
112112

113113

114114

115+
116+
117+
118+
115119

116120

117121
method_name = name[0].lower() + name[1:]
@@ -138,6 +142,7 @@ def gen_writer_method(name, args, custom_writer):
138142
code += '{} {}({}) {{\\\n'.format(ret_type, method_name, ', '.join(method_args))
139143
code += ' writeOp(CacheOp::{});\\\n'.format(name)
140144
code += args_code
145+
code += ' assertLengthMatches();\\\n'
141146
if ret_type != 'void':
142147
code += ' return result;\\\n'
143148
code += '}'
@@ -306,6 +311,53 @@ def gen_spewer_method(name, args):
306311
return code
307312

308313

314+
315+
316+
317+
arg_length = {
318+
'ValId': 1,
319+
'ObjId': 1,
320+
'StringId': 1,
321+
'SymbolId': 1,
322+
'Int32Id': 1,
323+
'NumberId': 1,
324+
'BigIntId': 1,
325+
'ValueTagId': 1,
326+
'RawId': 1,
327+
328+
'ShapeField': 1,
329+
'GroupField': 1,
330+
'ObjectField': 1,
331+
'StringField': 1,
332+
'AtomField': 1,
333+
'PropertyNameField': 1,
334+
'SymbolField': 1,
335+
'RawWordField': 1,
336+
'RawPointerField': 1,
337+
'DOMExpandoGenerationField': 1,
338+
'IdField': 1,
339+
'ValueField': 1,
340+
341+
'ByteImm': 1,
342+
'BoolImm': 1,
343+
'CallFlagsImm': 1,
344+
'TypedThingLayoutImm': 1,
345+
'ReferenceTypeImm': 1,
346+
'ScalarTypeImm': 1,
347+
'MetaTwoByteKindImm': 1,
348+
'JSOpImm': 1,
349+
'ValueTypeImm': 1,
350+
'GuardClassKindImm': 1,
351+
'JSWhyMagicImm': 1,
352+
353+
'Int32Imm': 4,
354+
'UInt32Imm': 4,
355+
356+
'JSNativeImm': 'sizeof(uintptr_t)',
357+
'StaticStringImm': 'sizeof(uintptr_t)',
358+
}
359+
360+
309361
def generate_cacheirops_header(c_out, yaml_path):
310362
"""Generate CacheIROpsGenerated.h from CacheIROps.yaml. The generated file
311363
contains a list of all CacheIR ops and generated source code for
@@ -315,51 +367,6 @@ def generate_cacheirops_header(c_out, yaml_path):
315367

316368

317369

318-
mapping = {
319-
'ValId': 'Id',
320-
'ObjId': 'Id',
321-
'StringId': 'Id',
322-
'SymbolId': 'Id',
323-
'Int32Id': 'Id',
324-
'NumberId': 'Id',
325-
'BigIntId': 'Id',
326-
'ValueTagId': 'Id',
327-
'RawId': 'Id',
328-
329-
'ShapeField': 'Field',
330-
'GroupField': 'Field',
331-
'ObjectField': 'Field',
332-
'StringField': 'Field',
333-
'AtomField': 'Field',
334-
'PropertyNameField': 'Field',
335-
'SymbolField': 'Field',
336-
'RawWordField': 'Field',
337-
'RawPointerField': 'Field',
338-
'DOMExpandoGenerationField': 'Field',
339-
'IdField': 'Field',
340-
'ValueField': 'Field',
341-
342-
'ByteImm': 'Byte',
343-
'BoolImm': 'Byte',
344-
'CallFlagsImm': 'Byte',
345-
'TypedThingLayoutImm': 'Byte',
346-
'ReferenceTypeImm': 'Byte',
347-
'ScalarTypeImm': 'Byte',
348-
'MetaTwoByteKindImm': 'Byte',
349-
'JSOpImm': 'Byte',
350-
'ValueTypeImm': 'Byte',
351-
'GuardClassKindImm': 'Byte',
352-
'JSWhyMagicImm': 'Byte',
353-
354-
'Int32Imm': 'Int32',
355-
356-
'UInt32Imm': 'UInt32',
357-
358-
'JSNativeImm': 'Word',
359-
'StaticStringImm': 'Word',
360-
}
361-
362-
363370
ops_items = []
364371

365372

@@ -385,10 +392,10 @@ def generate_cacheirops_header(c_out, yaml_path):
385392
assert isinstance(custom_writer, bool)
386393

387394
if args:
388-
args_str = ', '.join([mapping[v] for v in args.values()])
395+
args_length = ' + '.join([str(arg_length[v]) for v in args.values()])
389396
else:
390-
args_str = 'None'
391-
ops_items.append('_({}, {})'.format(name, args_str))
397+
args_length = '0'
398+
ops_items.append('_({}, {})'.format(name, args_length))
392399

393400
writer_methods.append(gen_writer_method(name, args, custom_writer))
394401
if shared:

js/src/jit/WarpCacheIRTranspiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ bool WarpCacheIRTranspiler::transpile(const MDefinitionStackVector& inputs) {
9898
#undef DEFINE_OP
9999

100100
default:
101-
fprintf(stderr, "Unsupported op: %s\n", CacheIrOpNames[size_t(op)]);
101+
fprintf(stderr, "Unsupported op: %s\n", CacheIROpNames[size_t(op)]);
102102
MOZ_CRASH("Unsupported op");
103103
}
104104
} while (reader.more());

js/src/jit/WarpOracle.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ AbortReasonOr<Ok> WarpOracle::maybeInlineIC(WarpOpSnapshotList& snapshots,
658658
CacheIRReader reader(stubInfo);
659659
while (reader.more()) {
660660
CacheOp op = reader.readOp();
661-
uint32_t argLength = CacheIROpFormat::ArgLengths[uint8_t(op)];
661+
uint32_t argLength = CacheIROpArgLengths[size_t(op)];
662662
reader.skip(argLength);
663663

664664
switch (op) {
@@ -671,7 +671,7 @@ AbortReasonOr<Ok> WarpOracle::maybeInlineIC(WarpOpSnapshotList& snapshots,
671671
default:
672672

673673
JitSpew(JitSpew_WarpTranspiler, "unsupported CacheIR opcode: %s",
674-
CacheIrOpNames[uint8_t(op)]);
674+
CacheIROpNames[size_t(op)]);
675675
return Ok();
676676
}
677677
}

0 commit comments

Comments
 (0)