Skip to content

Commit fe6f97b

Browse files
authored
[NFC] Refactor a generic function for binary reading of a code annotations section (#7593)
This will save code with future annotations like inline hints.
1 parent 23a9e69 commit fe6f97b

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

src/wasm-binary.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,13 @@ class WasmBinaryReader {
16991699
// Scans ahead in the binary to check certain conditions like
17001700
// needCodeLocations.
17011701
void preScan();
1702+
1703+
// Internal helper for reading a code annotation section for a hint that is
1704+
// expression offset based. Receives the section name, payload length of the
1705+
// section and a function to read a single hint (receiving the annotation to
1706+
// update).
1707+
template<typename ReadFunc>
1708+
void readExpressionHints(Name sectionName, size_t payloadLen, ReadFunc read);
17021709
};
17031710

17041711
} // namespace wasm

src/wasm/wasm-binary.cpp

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5211,14 +5211,17 @@ void WasmBinaryReader::readDylink0(size_t payloadLen) {
52115211
}
52125212
}
52135213

5214-
void WasmBinaryReader::readBranchHints(size_t payloadLen) {
5214+
template<typename ReadFunc>
5215+
void WasmBinaryReader::readExpressionHints(Name sectionName,
5216+
size_t payloadLen,
5217+
ReadFunc read) {
52155218
auto sectionPos = pos;
52165219

52175220
auto numFuncs = getU32LEB();
52185221
for (Index i = 0; i < numFuncs; i++) {
52195222
auto funcIndex = getU32LEB();
52205223
if (funcIndex >= wasm.functions.size()) {
5221-
throwError("bad BranchHint function");
5224+
throwError("bad function in " + sectionName.toString());
52225225
}
52235226

52245227
auto& func = wasm.functions[funcIndex];
@@ -5243,22 +5246,11 @@ void WasmBinaryReader::readBranchHints(size_t payloadLen) {
52435246

52445247
auto iter = locationsMap.find(absoluteOffset);
52455248
if (iter == locationsMap.end()) {
5246-
throwError("bad BranchHint offset");
5249+
throwError("bad offset in " + sectionName.toString());
52475250
}
52485251
auto* expr = iter->second;
52495252

5250-
auto size = getU32LEB();
5251-
if (size != 1) {
5252-
throwError("bad BranchHint size");
5253-
}
5254-
5255-
auto likely = getU32LEB();
5256-
if (likely != 0 && likely != 1) {
5257-
throwError("bad BranchHint value");
5258-
}
5259-
5260-
// Apply the valid hint.
5261-
func->codeAnnotations[expr].branchLikely = likely;
5253+
read(func->codeAnnotations[expr]);
52625254
}
52635255
}
52645256

@@ -5267,6 +5259,25 @@ void WasmBinaryReader::readBranchHints(size_t payloadLen) {
52675259
}
52685260
}
52695261

5262+
void WasmBinaryReader::readBranchHints(size_t payloadLen) {
5263+
readExpressionHints(Annotations::BranchHint,
5264+
payloadLen,
5265+
[&](Function::CodeAnnotation& annotation) {
5266+
auto size = getU32LEB();
5267+
if (size != 1) {
5268+
throwError("bad BranchHint size");
5269+
}
5270+
5271+
auto likely = getU32LEB();
5272+
if (likely != 0 && likely != 1) {
5273+
throwError("bad BranchHint value");
5274+
}
5275+
5276+
// Apply the valid hint.
5277+
annotation.branchLikely = likely;
5278+
});
5279+
}
5280+
52705281
Index WasmBinaryReader::readMemoryAccess(Address& alignment, Address& offset) {
52715282
auto rawAlignment = getU32LEB();
52725283
bool hasMemIdx = false;

0 commit comments

Comments
 (0)