@@ -1549,8 +1549,28 @@ void WasmBinaryWriter::trackExpressionDelimiter(Expression* curr,
1549
1549
}
1550
1550
1551
1551
std::optional<BufferWithRandomAccess> WasmBinaryWriter::writeCodeAnnotations () {
1552
- // Assemble the info for Branch Hinting: for each function, a vector of the
1553
- // hints.
1552
+ std::optional<BufferWithRandomAccess> ret;
1553
+
1554
+ auto append = [&](std::optional<BufferWithRandomAccess>&& temp) {
1555
+ if (temp) {
1556
+ if (!ret) {
1557
+ // This is the first section.
1558
+ ret = std::move (temp);
1559
+ } else {
1560
+ // This is a later section, append.
1561
+ ret->insert (ret->end (), temp->begin (), temp->end ());
1562
+ }
1563
+ }
1564
+ };
1565
+
1566
+ append (getBranchHintsBuffer ());
1567
+ return ret;
1568
+ }
1569
+
1570
+ template <typename HasFunc, typename EmitFunc>
1571
+ std::optional<BufferWithRandomAccess> WasmBinaryWriter::writeExpressionHints (
1572
+ Name sectionName, HasFunc has, EmitFunc emit) {
1573
+ // Assemble the info: for each function, a vector of the hints.
1554
1574
struct ExprHint {
1555
1575
Expression* expr;
1556
1576
// The offset we will write in the custom section.
@@ -1566,15 +1586,15 @@ std::optional<BufferWithRandomAccess> WasmBinaryWriter::writeCodeAnnotations() {
1566
1586
std::vector<FuncHints> funcHintsVec;
1567
1587
1568
1588
for (auto & func : wasm->functions ) {
1569
- // Collect the Branch Hints for this function.
1589
+ // Collect the hints for this function.
1570
1590
FuncHints funcHints;
1571
1591
1572
1592
// We compute the location of the function declaration area (where the
1573
1593
// locals are declared) the first time we need it.
1574
1594
BinaryLocation funcDeclarationsOffset = 0 ;
1575
1595
1576
1596
for (auto & [expr, annotation] : func->codeAnnotations ) {
1577
- if (annotation. branchLikely ) {
1597
+ if (has ( annotation) ) {
1578
1598
auto exprIter = binaryLocations.expressions .find (expr);
1579
1599
if (exprIter == binaryLocations.expressions .end ()) {
1580
1600
// No expression exists for this annotation - perhaps optimizations
@@ -1622,7 +1642,7 @@ std::optional<BufferWithRandomAccess> WasmBinaryWriter::writeCodeAnnotations() {
1622
1642
// We found data: emit the section.
1623
1643
buffer << uint8_t (BinaryConsts::Custom);
1624
1644
auto lebPos = buffer.writeU32LEBPlaceholder ();
1625
- buffer.writeInlineString (Annotations::BranchHint .str );
1645
+ buffer.writeInlineString (sectionName .str );
1626
1646
1627
1647
buffer << U32LEB (funcHintsVec.size ());
1628
1648
for (auto & funcHints : funcHintsVec) {
@@ -1632,14 +1652,7 @@ std::optional<BufferWithRandomAccess> WasmBinaryWriter::writeCodeAnnotations() {
1632
1652
for (auto & exprHint : funcHints.exprHints ) {
1633
1653
buffer << U32LEB (exprHint.offset );
1634
1654
1635
- // Hint size, always 1 for now.
1636
- buffer << U32LEB (1 );
1637
-
1638
- // We must only emit hints that are present.
1639
- assert (exprHint.hint ->branchLikely );
1640
-
1641
- // Hint contents: likely or not.
1642
- buffer << U32LEB (int (*exprHint.hint ->branchLikely ));
1655
+ emit (*exprHint.hint , buffer);
1643
1656
}
1644
1657
}
1645
1658
@@ -1651,6 +1664,25 @@ std::optional<BufferWithRandomAccess> WasmBinaryWriter::writeCodeAnnotations() {
1651
1664
return buffer;
1652
1665
}
1653
1666
1667
+ std::optional<BufferWithRandomAccess> WasmBinaryWriter::getBranchHintsBuffer () {
1668
+ return writeExpressionHints (
1669
+ Annotations::BranchHint,
1670
+ [](const Function::CodeAnnotation& annotation) {
1671
+ return annotation.branchLikely ;
1672
+ },
1673
+ [](const Function::CodeAnnotation& annotation,
1674
+ BufferWithRandomAccess& buffer) {
1675
+ // Hint size, always 1 for now.
1676
+ buffer << U32LEB (1 );
1677
+
1678
+ // We must only emit hints that are present.
1679
+ assert (annotation.branchLikely );
1680
+
1681
+ // Hint contents: likely or not.
1682
+ buffer << U32LEB (int (*annotation.branchLikely ));
1683
+ });
1684
+ }
1685
+
1654
1686
void WasmBinaryWriter::writeData (const char * data, size_t size) {
1655
1687
for (size_t i = 0 ; i < size; i++) {
1656
1688
o << int8_t (data[i]);
0 commit comments