[lld] [wasm-ld] Refactor WasmSym from static globals to per-link context (PR #134970)

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 8 23:21:08 PDT 2025


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff HEAD~1 HEAD --extensions cpp,h -- lld/wasm/Config.h lld/wasm/Driver.cpp lld/wasm/InputChunks.cpp lld/wasm/MarkLive.cpp lld/wasm/OutputSections.cpp lld/wasm/Symbols.cpp lld/wasm/Symbols.h lld/wasm/SyntheticSections.cpp lld/wasm/Writer.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index b43632d5e..38c12ce8b 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -964,7 +964,8 @@ static void createSyntheticSymbols() {
     // See:
     // https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md
     auto *globalType = is64 ? &globalTypeI64 : &globalTypeI32;
-    ctx.wasmSym->memoryBase = createUndefinedGlobal("__memory_base", globalType);
+    ctx.wasmSym->memoryBase =
+        createUndefinedGlobal("__memory_base", globalType);
     ctx.wasmSym->tableBase = createUndefinedGlobal("__table_base", globalType);
     ctx.wasmSym->memoryBase->markLive();
     ctx.wasmSym->tableBase->markLive();
@@ -980,9 +981,8 @@ static void createSyntheticSymbols() {
     ctx.wasmSym->tlsAlign = createGlobalVariable("__tls_align", false);
     ctx.wasmSym->initTLS = symtab->addSyntheticFunction(
         "__wasm_init_tls", WASM_SYMBOL_VISIBILITY_HIDDEN,
-        make<SyntheticFunction>(
-            is64 ? i64ArgSignature : i32ArgSignature,
-            "__wasm_init_tls"));
+        make<SyntheticFunction>(is64 ? i64ArgSignature : i32ArgSignature,
+                                "__wasm_init_tls"));
   }
 }
 
@@ -1001,8 +1001,10 @@ static void createOptionalSymbols() {
     ctx.wasmSym->globalBase = symtab->addOptionalDataSymbol("__global_base");
     ctx.wasmSym->heapBase = symtab->addOptionalDataSymbol("__heap_base");
     ctx.wasmSym->heapEnd = symtab->addOptionalDataSymbol("__heap_end");
-    ctx.wasmSym->definedMemoryBase = symtab->addOptionalDataSymbol("__memory_base");
-    ctx.wasmSym->definedTableBase = symtab->addOptionalDataSymbol("__table_base");
+    ctx.wasmSym->definedMemoryBase =
+        symtab->addOptionalDataSymbol("__memory_base");
+    ctx.wasmSym->definedTableBase =
+        symtab->addOptionalDataSymbol("__table_base");
   }
 
   ctx.wasmSym->firstPageEnd =
diff --git a/lld/wasm/InputChunks.cpp b/lld/wasm/InputChunks.cpp
index ac89f313c..1ce7cba5f 100644
--- a/lld/wasm/InputChunks.cpp
+++ b/lld/wasm/InputChunks.cpp
@@ -413,7 +413,8 @@ bool InputChunk::generateRelocationCode(raw_ostream &os) const {
       if (isTLS())
         writeUleb128(os, ctx.wasmSym->tlsBase->getGlobalIndex(), "tls_base");
       else
-        writeUleb128(os, ctx.wasmSym->memoryBase->getGlobalIndex(), "memory_base");
+        writeUleb128(os, ctx.wasmSym->memoryBase->getGlobalIndex(),
+                     "memory_base");
       writeU8(os, opcode_ptr_add, "ADD");
     }
 
@@ -436,7 +437,7 @@ bool InputChunk::generateRelocationCode(raw_ostream &os) const {
       }
     } else {
       assert(ctx.isPic);
-      const GlobalSymbol* baseSymbol = ctx.wasmSym->memoryBase;
+      const GlobalSymbol *baseSymbol = ctx.wasmSym->memoryBase;
       if (rel.Type == R_WASM_TABLE_INDEX_I32 ||
           rel.Type == R_WASM_TABLE_INDEX_I64)
         baseSymbol = ctx.wasmSym->tableBase;
diff --git a/lld/wasm/OutputSections.cpp b/lld/wasm/OutputSections.cpp
index 1864e4532..08f7fd361 100644
--- a/lld/wasm/OutputSections.cpp
+++ b/lld/wasm/OutputSections.cpp
@@ -137,7 +137,8 @@ void DataSection::finalizeContents() {
         if (ctx.isPic) {
           assert(segment->startVA == 0);
           initExpr.Inst.Opcode = WASM_OPCODE_GLOBAL_GET;
-          initExpr.Inst.Value.Global = ctx.wasmSym->memoryBase->getGlobalIndex();
+          initExpr.Inst.Value.Global =
+              ctx.wasmSym->memoryBase->getGlobalIndex();
         } else {
           initExpr = intConst(segment->startVA, is64);
         }
diff --git a/lld/wasm/SyntheticSections.cpp b/lld/wasm/SyntheticSections.cpp
index ce71e8f91..44890e4f8 100644
--- a/lld/wasm/SyntheticSections.cpp
+++ b/lld/wasm/SyntheticSections.cpp
@@ -456,7 +456,8 @@ void GlobalSection::generateRelocationCode(raw_ostream &os, bool TLS) const {
         continue;
       // Get __table_base
       writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
-      writeUleb128(os, ctx.wasmSym->tableBase->getGlobalIndex(), "__table_base");
+      writeUleb128(os, ctx.wasmSym->tableBase->getGlobalIndex(),
+                   "__table_base");
 
       // Add the table index to __table_base
       writeU8(os, opcode_ptr_const, "CONST");
diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp
index 3ab68339f..785a7980b 100644
--- a/lld/wasm/Writer.cpp
+++ b/lld/wasm/Writer.cpp
@@ -936,7 +936,8 @@ static void finalizeIndirectFunctionTable() {
 
   uint32_t tableSize = ctx.arg.tableBase + out.elemSec->numEntries();
   WasmLimits limits = {0, tableSize, 0, 0};
-  if (ctx.wasmSym->indirectFunctionTable->isDefined() && !ctx.arg.growableTable) {
+  if (ctx.wasmSym->indirectFunctionTable->isDefined() &&
+      !ctx.arg.growableTable) {
     limits.Flags |= WASM_LIMITS_FLAG_HAS_MAX;
     limits.Maximum = limits.Minimum;
   }
@@ -1182,8 +1183,7 @@ void Writer::createSyntheticInitFunctions() {
     if (llvm::any_of(segments, hasTLSRelocs)) {
       ctx.wasmSym->applyTLSRelocs = symtab->addSyntheticFunction(
           "__wasm_apply_tls_relocs", WASM_SYMBOL_VISIBILITY_HIDDEN,
-          make<SyntheticFunction>(nullSignature,
-                                  "__wasm_apply_tls_relocs"));
+          make<SyntheticFunction>(nullSignature, "__wasm_apply_tls_relocs"));
       ctx.wasmSym->applyTLSRelocs->markLive();
     }
   }
@@ -1286,7 +1286,8 @@ void Writer::createInitMemoryFunction() {
         writeUleb128(os, 2, "local count");
         writeU8(os, is64 ? WASM_TYPE_I64 : WASM_TYPE_I32, "address type");
         writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
-        writeUleb128(os, ctx.wasmSym->memoryBase->getGlobalIndex(), "memory_base");
+        writeUleb128(os, ctx.wasmSym->memoryBase->getGlobalIndex(),
+                     "memory_base");
         writePtrConst(os, flagAddress, is64, "flag address");
         writeU8(os, is64 ? WASM_OPCODE_I64_ADD : WASM_OPCODE_I32_ADD, "add");
         writeU8(os, WASM_OPCODE_LOCAL_SET, "local.set");

``````````

</details>


https://github.com/llvm/llvm-project/pull/134970


More information about the llvm-commits mailing list