Skip to content
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

[lld][WebAssembly] Reset context object after each link #78770

Merged
merged 1 commit into from
Jan 19, 2024

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Jan 19, 2024

This mirrors how the ELF linker works. I wasn't able to find anywhere where this is currently tested.

Followup to #78640, which triggered a regression.

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 19, 2024

@llvm/pr-subscribers-lld-wasm
@llvm/pr-subscribers-lld-elf

@llvm/pr-subscribers-lld

Author: Sam Clegg (sbc100)

Changes

This mirrors how the ELF linker works. I wasn't able to find anywhere where this is currently tested.

Followup to #78640, which triggered a regression.


Full diff: https://github.com/llvm/llvm-project/pull/78770.diff

3 Files Affected:

  • (modified) lld/ELF/Driver.cpp (+1-1)
  • (modified) lld/wasm/Config.h (+2)
  • (modified) lld/wasm/Driver.cpp (+13)
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 5ccc65600dcb910..9c040de1e47e66b 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -130,7 +130,7 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
 
   ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
   ctx->e.cleanupCallback = []() {
-    elf::ctx.reset();
+    //elf::ctx.reset();
     symtab = SymbolTable();
 
     outputSections.clear();
diff --git a/lld/wasm/Config.h b/lld/wasm/Config.h
index dc7ca265e9a2cb1..308f8ab3eb0ebed 100644
--- a/lld/wasm/Config.h
+++ b/lld/wasm/Config.h
@@ -138,6 +138,8 @@ struct Ctx {
   llvm::SmallVector<std::tuple<std::string, const InputFile *, const Symbol &>,
                     0>
       whyExtractRecords;
+
+  void reset();
 };
 
 extern Ctx ctx;
diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index 4a4f9a96227946d..96a214f76ebd70b 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -47,6 +47,16 @@ namespace lld::wasm {
 Configuration *config;
 Ctx ctx;
 
+void Ctx::reset() {
+  objectFiles.clear();
+  stubFiles.clear();
+  sharedFiles.clear();
+  bitcodeFiles.clear();
+  syntheticFunctions.clear();
+  syntheticGlobals.clear();
+  syntheticTables.clear();
+}
+
 namespace {
 
 // Create enum with OPT_xxx values for each option in Options.td
@@ -90,6 +100,9 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
   auto *ctx = new CommonLinkerContext;
 
   ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
+  ctx->e.cleanupCallback = []() {
+    wasm::ctx.reset();
+  };
   ctx->e.logName = args::getFilenameWithoutExe(args[0]);
   ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now (use "
                                  "-error-limit=0 to see all errors)";

@sbc100
Copy link
Collaborator Author

sbc100 commented Jan 19, 2024

@steven-johnson

@sbc100
Copy link
Collaborator Author

sbc100 commented Jan 19, 2024

I wonder if we can replace these elaborate reset() methods with a placement new in the static data location? Would seems less error prone.

Copy link

github-actions bot commented Jan 19, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@steven-johnson
Copy link

I wonder if we can replace these elaborate reset() methods with a placement new in the static data location? Would seems less error prone.

Or just ctx = Ctx(); ?

@sbc100
Copy link
Collaborator Author

sbc100 commented Jan 19, 2024

I wonder if we can replace these elaborate reset() methods with a placement new in the static data location? Would seems less error prone.

Or just ctx = Ctx(); ?

Yeah, I'm guessing there was some reason the original author didn't take that much simpler approach. I'll go with the same method for now for consistency and we can perhaps followup with a simplification.

@sbc100
Copy link
Collaborator Author

sbc100 commented Jan 19, 2024

I wonder if we can replace these elaborate reset() methods with a placement new in the static data location? Would seems less error prone.

Or just ctx = Ctx(); ?

Yeah, I'm guessing there was some reason the original author didn't take that much simpler approach. I'll go with the same method for now for consistency and we can perhaps followup with a simplification.

@MaskRay what would you think of such a simplification here? Maybe it would result in a little more allocator churn.. but that doesn't seem like a big concern if it only happens between lldMain operations (i.e. infrequently)

@steven-johnson
Copy link

I wonder if we can replace these elaborate reset() methods with a placement new in the static data location? Would seems less error prone.

Or just ctx = Ctx(); ?

Yeah, I'm guessing there was some reason the original author didn't take that much simpler approach. I'll go with the same method for now for consistency and we can perhaps followup with a simplification.

@MaskRay what would you think of such a simplification here? Maybe it would result in a little more allocator churn.. but that doesn't seem like a big concern if it only happens between lldMain operations (i.e. infrequently)

Hard to imagine that the single assignment would be anything more than noise in comparison to doing an object-linkage operation.

@@ -47,6 +47,16 @@ namespace lld::wasm {
Configuration *config;
Ctx ctx;

void Ctx::reset() {
objectFiles.clear();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: shouldn't we reset all the fields, including the bool fields? (And maybe initialize isPic to some explicit value rather than leaving it as uninited?)

Copy link
Member

@MaskRay MaskRay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM as this seems urgent.

wasm should have a unittest similar to unittests/AsLibELF/SomeDrivers.cpp for library users, which can be done in the future.

This mirrors who the ELF linker works.  I wasn't able to find anywhere
where this is currently tested.

Followup to llvm#78640, which triggered a regression.
@sbc100
Copy link
Collaborator Author

sbc100 commented Jan 19, 2024

Thanks for the pointer to unittests/AsLibELF/SomeDrivers.cpp. Will land this now and followup there.

@sbc100 sbc100 merged commit 2bfa5ca into llvm:main Jan 19, 2024
3 of 4 checks passed
@sbc100 sbc100 deleted the context_reset branch January 19, 2024 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants