-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[WASM] wasm-ld: split up __wasm_apply_data_relocs #129007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dmjio
wants to merge
2
commits into
llvm:main
Choose a base branch
from
dmjio:wasm-data-relocs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1459,20 +1459,21 @@ void Writer::createStartFunction() { | |
void Writer::createApplyDataRelocationsFunction() { | ||
LLVM_DEBUG(dbgs() << "createApplyDataRelocationsFunction\n"); | ||
// First write the body's contents to a string. | ||
std::string bodyContent; | ||
std::vector<std::string> funcs; | ||
{ | ||
raw_string_ostream os(bodyContent); | ||
writeUleb128(os, 0, "num locals"); | ||
bool generated = false; | ||
for (const OutputSegment *seg : segments) | ||
if (!ctx.arg.sharedMemory || !seg->isTLS()) | ||
for (const InputChunk *inSeg : seg->inputSegments) | ||
generated |= inSeg->generateRelocationCode(os); | ||
inSeg->generateRelocationCode(funcs); | ||
} | ||
|
||
if (!generated) { | ||
LLVM_DEBUG(dbgs() << "skipping empty __wasm_apply_data_relocs\n"); | ||
return; | ||
} | ||
if (funcs.empty()) { | ||
LLVM_DEBUG(dbgs() << "skipping empty __wasm_apply_data_relocs\n"); | ||
return; | ||
} | ||
|
||
for (auto &func : funcs) { | ||
raw_string_ostream os(func); | ||
writeU8(os, WASM_OPCODE_END, "END"); | ||
} | ||
|
||
|
@@ -1485,24 +1486,67 @@ void Writer::createApplyDataRelocationsFunction() { | |
make<SyntheticFunction>(nullSignature, "__wasm_apply_data_relocs")); | ||
def->markLive(); | ||
|
||
createFunction(def, bodyContent); | ||
if (funcs.size() == 1) { | ||
createFunction(def, funcs.back()); | ||
return; | ||
} | ||
|
||
std::string body; | ||
{ | ||
raw_string_ostream os(body); | ||
writeUleb128(os, 0, "num locals"); | ||
|
||
for (std::size_t i = 0; i < funcs.size(); ++i) { | ||
auto &name = | ||
*make<std::string>("__wasm_apply_data_relocs_" + std::to_string(i)); | ||
auto *func = make<SyntheticFunction>(nullSignature, name); | ||
auto *def = symtab->addSyntheticFunction( | ||
name, WASM_SYMBOL_VISIBILITY_HIDDEN, func); | ||
def->markLive(); | ||
// Normally this shouldn't be called manually for a synthetic | ||
// function, since the function indices in | ||
// ctx.syntheticFunctions will be calculated later (check | ||
// functionSec->addFunction call hierarchy for details). | ||
// However, at this point we already need the correct index. The | ||
// solution is to place the new synthetic function eagerly, and | ||
// also making addFunction idempotent by skipping when there's | ||
// already a function index. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess that alternative to this would be to somehow generate relocations here. I wonder how this works for other synthetic functions that can call each such as |
||
out.functionSec->addFunction(func); | ||
createFunction(def, funcs[i]); | ||
|
||
writeU8(os, WASM_OPCODE_CALL, "CALL"); | ||
writeUleb128(os, def->getFunctionIndex(), "function index"); | ||
} | ||
|
||
writeU8(os, WASM_OPCODE_END, "END"); | ||
} | ||
createFunction(def, body); | ||
} | ||
|
||
void Writer::createApplyTLSRelocationsFunction() { | ||
LLVM_DEBUG(dbgs() << "createApplyTLSRelocationsFunction\n"); | ||
std::string bodyContent; | ||
std::vector<std::string> funcs; | ||
{ | ||
raw_string_ostream os(bodyContent); | ||
writeUleb128(os, 0, "num locals"); | ||
for (const OutputSegment *seg : segments) | ||
if (seg->isTLS()) | ||
for (const InputChunk *inSeg : seg->inputSegments) | ||
inSeg->generateRelocationCode(os); | ||
inSeg->generateRelocationCode(funcs); | ||
} | ||
|
||
if (funcs.empty()) { | ||
funcs.emplace_back(std::string()); | ||
raw_string_ostream os(funcs.back()); | ||
writeUleb128(os, 0, "num locals"); | ||
} | ||
|
||
for (auto &func : funcs) { | ||
raw_string_ostream os(func); | ||
writeU8(os, WASM_OPCODE_END, "END"); | ||
} | ||
|
||
createFunction(WasmSym::applyTLSRelocs, bodyContent); | ||
assert(funcs.size() == 1); | ||
|
||
createFunction(WasmSym::applyTLSRelocs, funcs.back()); | ||
} | ||
|
||
// Similar to createApplyDataRelocationsFunction but generates relocation code | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious why this was needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
assert(!hasFunctionIndex());
was being triggered insetFunctionIndex
, andif (func->hasFunctionIndex()) return;
was introduced to keep program termination from occurring.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, it's described below
https://github.com/llvm/llvm-project/pull/129007/files#diff-e826be2acc8b58c5d040525dc8a509e90810d3edcd93190d4810e476919ef9aaR1509-R1513
addFunction
ormarkLive
is setting the index already it seems. So this explains theif (func->hasFunctionIndex()) return;
(since theassert
invariant no longer holds)