Skip to content

Commit 9b965b3

Browse files
committed
[lld][WebAssembly] Cleanup duplicate fields in Symbols.h. NFC
This avoids duplication and simplifies the code in several places without increasing the size of the symbol union (at least not above the assert'd limit of 120 bytes). Differential Revision: https://reviews.llvm.org/D106026
1 parent 2656af9 commit 9b965b3

File tree

3 files changed

+21
-50
lines changed

3 files changed

+21
-50
lines changed

lld/wasm/Symbols.h

+15-14
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ class Symbol {
172172
bool isStub : 1;
173173

174174
uint32_t flags;
175+
176+
llvm::Optional<StringRef> importName;
177+
llvm::Optional<StringRef> importModule;
175178
};
176179

177180
class FunctionSymbol : public Symbol {
@@ -222,15 +225,15 @@ class UndefinedFunction : public FunctionSymbol {
222225
const WasmSignature *type = nullptr,
223226
bool isCalledDirectly = true)
224227
: FunctionSymbol(name, UndefinedFunctionKind, flags, file, type),
225-
importName(importName), importModule(importModule),
226-
isCalledDirectly(isCalledDirectly) {}
228+
isCalledDirectly(isCalledDirectly) {
229+
this->importName = importName;
230+
this->importModule = importModule;
231+
}
227232

228233
static bool classof(const Symbol *s) {
229234
return s->kind() == UndefinedFunctionKind;
230235
}
231236

232-
llvm::Optional<StringRef> importName;
233-
llvm::Optional<StringRef> importModule;
234237
DefinedFunction *stubFunction = nullptr;
235238
bool isCalledDirectly;
236239
};
@@ -354,15 +357,14 @@ class UndefinedGlobal : public GlobalSymbol {
354357
llvm::Optional<StringRef> importModule, uint32_t flags,
355358
InputFile *file = nullptr,
356359
const WasmGlobalType *type = nullptr)
357-
: GlobalSymbol(name, UndefinedGlobalKind, flags, file, type),
358-
importName(importName), importModule(importModule) {}
360+
: GlobalSymbol(name, UndefinedGlobalKind, flags, file, type) {
361+
this->importName = importName;
362+
this->importModule = importModule;
363+
}
359364

360365
static bool classof(const Symbol *s) {
361366
return s->kind() == UndefinedGlobalKind;
362367
}
363-
364-
llvm::Optional<StringRef> importName;
365-
llvm::Optional<StringRef> importModule;
366368
};
367369

368370
class TableSymbol : public Symbol {
@@ -403,15 +405,14 @@ class UndefinedTable : public TableSymbol {
403405
UndefinedTable(StringRef name, llvm::Optional<StringRef> importName,
404406
llvm::Optional<StringRef> importModule, uint32_t flags,
405407
InputFile *file, const WasmTableType *type)
406-
: TableSymbol(name, UndefinedTableKind, flags, file, type),
407-
importName(importName), importModule(importModule) {}
408+
: TableSymbol(name, UndefinedTableKind, flags, file, type) {
409+
this->importName = importName;
410+
this->importModule = importModule;
411+
}
408412

409413
static bool classof(const Symbol *s) {
410414
return s->kind() == UndefinedTableKind;
411415
}
412-
413-
llvm::Optional<StringRef> importName;
414-
llvm::Optional<StringRef> importModule;
415416
};
416417

417418
// A tag is a general format to distinguish typed entities. Each tag has an

lld/wasm/SyntheticSections.cpp

+4-30
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,11 @@ void ImportSection::addGOTEntry(Symbol *sym) {
107107
gotSymbols.push_back(sym);
108108
}
109109

110-
template <typename UndefinedT>
111-
static void getImportModuleAndName(Symbol *sym, StringRef *outModule,
112-
StringRef *outName) {
113-
if (auto *undef = dyn_cast<UndefinedT>(sym)) {
114-
*outModule = undef->importModule ? *undef->importModule : defaultModule;
115-
*outName = undef->importName ? *undef->importName : sym->getName();
116-
} else {
117-
*outModule = defaultModule;
118-
*outName = sym->getName();
119-
}
120-
}
121-
122110
void ImportSection::addImport(Symbol *sym) {
123111
assert(!isSealed);
124-
StringRef module;
125-
StringRef name;
112+
StringRef module = sym->importModule.getValueOr(defaultModule);
113+
StringRef name = sym->importName.getValueOr(sym->getName());
126114
if (auto *f = dyn_cast<FunctionSymbol>(sym)) {
127-
getImportModuleAndName<UndefinedFunction>(sym, &module, &name);
128115
ImportKey<WasmSignature> key(*(f->getSignature()), module, name);
129116
auto entry = importedFunctions.try_emplace(key, numImportedFunctions);
130117
if (entry.second) {
@@ -134,7 +121,6 @@ void ImportSection::addImport(Symbol *sym) {
134121
f->setFunctionIndex(entry.first->second);
135122
}
136123
} else if (auto *g = dyn_cast<GlobalSymbol>(sym)) {
137-
getImportModuleAndName<UndefinedGlobal>(sym, &module, &name);
138124
ImportKey<WasmGlobalType> key(*(g->getGlobalType()), module, name);
139125
auto entry = importedGlobals.try_emplace(key, numImportedGlobals);
140126
if (entry.second) {
@@ -150,7 +136,6 @@ void ImportSection::addImport(Symbol *sym) {
150136
t->setTagIndex(numImportedTags++);
151137
} else {
152138
auto *table = cast<TableSymbol>(sym);
153-
getImportModuleAndName<UndefinedTable>(sym, &module, &name);
154139
ImportKey<WasmTableType> key(*(table->getTableType()), module, name);
155140
auto entry = importedTables.try_emplace(key, numImportedTables);
156141
if (entry.second) {
@@ -189,19 +174,8 @@ void ImportSection::writeBody() {
189174

190175
for (const Symbol *sym : importedSymbols) {
191176
WasmImport import;
192-
if (auto *f = dyn_cast<UndefinedFunction>(sym)) {
193-
import.Field = f->importName ? *f->importName : sym->getName();
194-
import.Module = f->importModule ? *f->importModule : defaultModule;
195-
} else if (auto *g = dyn_cast<UndefinedGlobal>(sym)) {
196-
import.Field = g->importName ? *g->importName : sym->getName();
197-
import.Module = g->importModule ? *g->importModule : defaultModule;
198-
} else if (auto *t = dyn_cast<UndefinedTable>(sym)) {
199-
import.Field = t->importName ? *t->importName : sym->getName();
200-
import.Module = t->importModule ? *t->importModule : defaultModule;
201-
} else {
202-
import.Field = sym->getName();
203-
import.Module = defaultModule;
204-
}
177+
import.Field = sym->importName.getValueOr(sym->getName());
178+
import.Module = sym->importModule.getValueOr(defaultModule);
205179

206180
if (auto *functionSym = dyn_cast<FunctionSymbol>(sym)) {
207181
import.Kind = WASM_EXTERNAL_FUNCTION;

lld/wasm/Writer.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -562,12 +562,8 @@ static bool shouldImport(Symbol *sym) {
562562
return true;
563563
if (config->allowUndefinedSymbols.count(sym->getName()) != 0)
564564
return true;
565-
if (auto *g = dyn_cast<UndefinedGlobal>(sym))
566-
return g->importName.hasValue();
567-
if (auto *f = dyn_cast<UndefinedFunction>(sym))
568-
return f->importName.hasValue();
569-
if (auto *t = dyn_cast<UndefinedTable>(sym))
570-
return t->importName.hasValue();
565+
if (sym->isUndefined())
566+
return sym->importName.hasValue();
571567

572568
return false;
573569
}

0 commit comments

Comments
 (0)