<commit_before>//===- MarkLive.cpp -------------------------------------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements --gc-sections, which is a feature to remove unused
// chunks from the output. Unused chunks are those that are not reachable from
// known root symbols or chunks. This feature is implemented as a mark-sweep
// garbage collector.
//
// Here's how it works. Each InputChunk has a "Live" bit. The bit is off by
// default. Starting with the GC-roots, visit all reachable chunks and set their
// Live bits. The Writer will then ignore chunks whose Live bits are off, so
// that such chunk are not appear in the output.
//
//===----------------------------------------------------------------------===//

#include "MarkLive.h"
#include "Config.h"
#include "InputChunks.h"
#include "SymbolTable.h"
#include "Symbols.h"

#define DEBUG_TYPE "lld"

using namespace llvm;
using namespace llvm::wasm;
using namespace lld;
using namespace lld::wasm;

void lld::wasm::markLive() {
  if (!Config->GcSections)
    return;

  DEBUG(dbgs() << "markLive\n");
  SmallVector<InputChunk *, 256> Q;

  auto Enqueue = [&](Symbol *Sym) {
    if (!Sym)
      return;
    InputChunk *Chunk = Sym->getChunk();
    if (!Chunk || Chunk->Live)
      return;
    Chunk->Live = true;
    Q.push_back(Chunk);
  };

  // Add GC root symbols.
  if (!Config->Entry.empty())
    Enqueue(Symtab->find(Config->Entry));
  Enqueue(WasmSym::CallCtors);

  // By default we export all non-hidden, so they are gc roots too
  for (Symbol *Sym : Symtab->getSymbols())
    if (!Sym->isHidden())
      Enqueue(Sym);

  // The ctor fuctions are all used the synthetic __wasm_call_ctors function,
  // but since this function is created in-place it doesn't contain reloctations
  // which mean we have to manually mark the ctors.
  for (const ObjFile *Obj : Symtab->ObjectFiles) {
    const WasmLinkingData &L = Obj->getWasmObj()->linkingData();
    for (const WasmInitFunc &F : L.InitFunctions)
      Enqueue(Obj->getFunctionSymbol(F.FunctionIndex));
  }

  auto EnqueueSuccessors = [Enqueue](InputChunk &Chunk) {
    for (const WasmRelocation Reloc : Chunk.getRelocations()) {
      switch (Reloc.Type) {
      case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
      case R_WEBASSEMBLY_TABLE_INDEX_I32:<commit_msg>[WebAssembly] Fix typos in comment. NFC.<commit_after>//===- MarkLive.cpp -------------------------------------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements --gc-sections, which is a feature to remove unused
// chunks from the output. Unused chunks are those that are not reachable from
// known root symbols or chunks. This feature is implemented as a mark-sweep
// garbage collector.
//
// Here's how it works. Each InputChunk has a "Live" bit. The bit is off by
// default. Starting with the GC-roots, visit all reachable chunks and set their
// Live bits. The Writer will then ignore chunks whose Live bits are off, so
// that such chunk are not appear in the output.
//
//===----------------------------------------------------------------------===//

#include "MarkLive.h"
#include "Config.h"
#include "InputChunks.h"
#include "SymbolTable.h"
#include "Symbols.h"

#define DEBUG_TYPE "lld"

using namespace llvm;
using namespace llvm::wasm;
using namespace lld;
using namespace lld::wasm;

void lld::wasm::markLive() {
  if (!Config->GcSections)
    return;

  DEBUG(dbgs() << "markLive\n");
  SmallVector<InputChunk *, 256> Q;

  auto Enqueue = [&](Symbol *Sym) {
    if (!Sym)
      return;
    InputChunk *Chunk = Sym->getChunk();
    if (!Chunk || Chunk->Live)
      return;
    Chunk->Live = true;
    Q.push_back(Chunk);
  };

  // Add GC root symbols.
  if (!Config->Entry.empty())
    Enqueue(Symtab->find(Config->Entry));
  Enqueue(WasmSym::CallCtors);

  // By default we export all non-hidden, so they are gc roots too
  for (Symbol *Sym : Symtab->getSymbols())
    if (!Sym->isHidden())
      Enqueue(Sym);

  // The ctor functions are all used in the synthetic __wasm_call_ctors
  // function, but since this function is created in-place it doesn't contain
  // reloctations which mean we have to manually mark the ctors.
  for (const ObjFile *Obj : Symtab->ObjectFiles) {
    const WasmLinkingData &L = Obj->getWasmObj()->linkingData();
    for (const WasmInitFunc &F : L.InitFunctions)
      Enqueue(Obj->getFunctionSymbol(F.FunctionIndex));
  }

  auto EnqueueSuccessors = [Enqueue](InputChunk &Chunk) {
    for (const WasmRelocation Reloc : Chunk.getRelocations()) {
      switch (Reloc.Type) {
      case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
      case R_WEBASSEMBLY_TABLE_INDEX_I32:
{'fraction_non_alphanumeric': 0.1937512281391236, 'fraction_numerical': 0.0019650225977598744, 'mean_word_length': 2.391072618254497, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4181088', 'n_tokens_mistral': 1514, 'n_tokens_neox': 1469, 'n_words': 501}