Skip to content

Commit 1f3f774

Browse files
committed
[WebAssembly] Implement --trace and --trace-symbol
Differential Revision: https://reviews.llvm.org/D57725 llvm-svn: 353264
1 parent c1950aa commit 1f3f774

File tree

9 files changed

+119
-14
lines changed

9 files changed

+119
-14
lines changed

lld/test/wasm/trace-symbol.ll

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; RUN: llc -filetype=obj %p/Inputs/ret32.ll -o %t.ret32.o
2+
; RUN: llc -filetype=obj -o %t.o %s
3+
; RUN: wasm-ld -o %t.wasm %t.o %t.ret32.o -y ret32 -y _start 2>&1 | FileCheck %s -check-prefix=BOTH
4+
5+
; check alias
6+
; RUN: wasm-ld -o %t.wasm %t.o %t.ret32.o -trace-symbol=_start 2>&1 | FileCheck %s -check-prefixes=JUST-START
7+
8+
target triple = "wasm32-unknown-unknown"
9+
10+
declare i32 @ret32(float %arg)
11+
12+
define void @_start() {
13+
entry:
14+
%call1 = call i32 @ret32(float 0.0)
15+
ret void
16+
}
17+
18+
; BOTH: .o: definition of _start
19+
; BOTH: .o: reference to ret32
20+
; BOTH: .ret32.o: definition of ret32
21+
22+
; JUST-START: .o: definition of _start
23+
; JUST-START-NOT: ret32

lld/test/wasm/trace.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
RUN: llc -filetype=obj %p/Inputs/start.ll -o %t.foo.o
2+
3+
# Check -t
4+
RUN: wasm-ld %t.foo.o -o out.wasm -t 2>&1 | FileCheck %s
5+
CHECK: {{.*}}.foo.o
6+
7+
# Check --trace alias
8+
RUN: wasm-ld %t.foo.o -o out.wasm --trace 2>&1 | FileCheck %s

lld/wasm/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ struct Configuration {
3838
bool StripAll;
3939
bool StripDebug;
4040
bool StackFirst;
41+
bool Trace;
4142
uint32_t GlobalBase;
4243
uint32_t InitialMemory;
4344
uint32_t MaxMemory;

lld/wasm/Driver.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ static void setConfigs(opt::InputArgList &Args) {
371371
Config->StripAll = Args.hasArg(OPT_strip_all);
372372
Config->StripDebug = Args.hasArg(OPT_strip_debug);
373373
Config->StackFirst = Args.hasArg(OPT_stack_first);
374+
Config->Trace = Args.hasArg(OPT_trace);
374375
Config->ThinLTOCacheDir = Args.getLastArgValue(OPT_thinlto_cache_dir);
375376
Config->ThinLTOCachePolicy = CHECK(
376377
parseCachePruningPolicy(Args.getLastArgValue(OPT_thinlto_cache_policy)),
@@ -556,6 +557,10 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
556557
Config->AllowUndefined = true;
557558
}
558559

560+
// Handle --trace-symbol.
561+
for (auto *Arg : Args.filtered(OPT_trace_symbol))
562+
Symtab->trace(Arg->getValue());
563+
559564
if (!Config->Relocatable)
560565
createSyntheticSymbols();
561566

lld/wasm/Options.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ def strip_debug: F<"strip-debug">, HelpText<"Strip debugging information">;
9393

9494
def threads: F<"threads">, HelpText<"Run the linker multi-threaded">;
9595

96+
def trace: F<"trace">, HelpText<"Print the names of the input files">;
97+
98+
defm trace_symbol: Eq<"trace-symbol", "Trace references to symbols">;
99+
96100
defm undefined: Eq<"undefined", "Force undefined symbol during linking">;
97101

98102
def v: Flag<["-"], "v">, HelpText<"Display the version number">;
@@ -160,6 +164,8 @@ def: Flag<["-"], "m">, Alias<max_memory>;
160164
def: Flag<["-"], "r">, Alias<relocatable>;
161165
def: Flag<["-"], "s">, Alias<strip_all>, HelpText<"Alias for --strip-all">;
162166
def: Flag<["-"], "S">, Alias<strip_debug>, HelpText<"Alias for --strip-debug">;
167+
def: Flag<["-"], "t">, Alias<trace>, HelpText<"Alias for --trace">;
168+
def: JoinedOrSeparate<["-"], "y">, Alias<trace_symbol>, HelpText<"Alias for --trace-symbol">;
163169
def: JoinedOrSeparate<["-"], "u">, Alias<undefined>;
164170

165171
// LTO-related options.

lld/wasm/SymbolTable.cpp

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ SymbolTable *lld::wasm::Symtab;
2828

2929
void SymbolTable::addFile(InputFile *File) {
3030
log("Processing: " + toString(File));
31+
if (Config->Trace)
32+
message(toString(File));
3133
File->parse();
3234

3335
// LLVM bitcode file
@@ -73,21 +75,42 @@ void SymbolTable::reportRemainingUndefines() {
7375
}
7476

7577
Symbol *SymbolTable::find(StringRef Name) {
76-
return SymMap.lookup(CachedHashStringRef(Name));
78+
auto It = SymMap.find(CachedHashStringRef(Name));
79+
if (It == SymMap.end() || It->second == -1)
80+
return nullptr;
81+
return SymVector[It->second];
7782
}
7883

79-
std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, InputFile *File) {
80-
bool Inserted = false;
81-
Symbol *&Sym = SymMap[CachedHashStringRef(Name)];
82-
if (!Sym) {
83-
Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
84-
Sym->IsUsedInRegularObj = false;
85-
SymVector.emplace_back(Sym);
86-
Inserted = true;
84+
std::pair<Symbol *, bool> SymbolTable::insertName(StringRef Name) {
85+
bool Trace = false;
86+
auto P = SymMap.insert({CachedHashStringRef(Name), (int)SymVector.size()});
87+
int &SymIndex = P.first->second;
88+
bool IsNew = P.second;
89+
if (SymIndex == -1) {
90+
SymIndex = SymVector.size();
91+
Trace = true;
92+
IsNew = true;
8793
}
94+
95+
if (!IsNew)
96+
return {SymVector[SymIndex], false};
97+
98+
Symbol *Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
99+
Sym->IsUsedInRegularObj = false;
100+
Sym->Traced = Trace;
101+
SymVector.emplace_back(Sym);
102+
return {Sym, true};
103+
}
104+
105+
std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, InputFile *File) {
106+
Symbol *S;
107+
bool WasInserted;
108+
std::tie(S, WasInserted) = insertName(Name);
109+
88110
if (!File || File->kind() == InputFile::ObjectKind)
89-
Sym->IsUsedInRegularObj = true;
90-
return {Sym, Inserted};
111+
S->IsUsedInRegularObj = true;
112+
113+
return {S, WasInserted};
91114
}
92115

93116
static void reportTypeError(const Symbol *Existing, const InputFile *File,
@@ -409,3 +432,9 @@ void SymbolTable::addLazy(ArchiveFile *File, const Archive::Symbol *Sym) {
409432
bool SymbolTable::addComdat(StringRef Name) {
410433
return Comdats.insert(CachedHashStringRef(Name)).second;
411434
}
435+
436+
// Set a flag for --trace-symbol so that we can print out a log message
437+
// if a new symbol with the same name is inserted into the symbol table.
438+
void SymbolTable::trace(StringRef Name) {
439+
SymMap.insert({CachedHashStringRef(Name), -1});
440+
}

lld/wasm/SymbolTable.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ class SymbolTable {
4646
void reportRemainingUndefines();
4747

4848
ArrayRef<Symbol *> getSymbols() const { return SymVector; }
49+
4950
Symbol *find(StringRef Name);
5051

52+
void trace(StringRef Name);
53+
5154
Symbol *addDefinedFunction(StringRef Name, uint32_t Flags, InputFile *File,
5255
InputFunction *Function);
5356
Symbol *addDefinedData(StringRef Name, uint32_t Flags, InputFile *File,
@@ -76,8 +79,12 @@ class SymbolTable {
7679

7780
private:
7881
std::pair<Symbol *, bool> insert(StringRef Name, InputFile *File);
82+
std::pair<Symbol *, bool> insertName(StringRef Name);
7983

80-
llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> SymMap;
84+
// Maps symbol names to index into the SymVector. -1 means that symbols
85+
// is to not yet in the vector but it should have tracing enabled if it is
86+
// ever added.
87+
llvm::DenseMap<llvm::CachedHashStringRef, int> SymMap;
8188
std::vector<Symbol *> SymVector;
8289

8390
llvm::DenseSet<llvm::CachedHashStringRef> Comdats;

lld/wasm/Symbols.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,16 @@ std::string lld::toString(wasm::Symbol::Kind Kind) {
293293
}
294294
llvm_unreachable("invalid symbol kind");
295295
}
296+
297+
// Print out a log message for --trace-symbol.
298+
void lld::wasm::printTraceSymbol(Symbol *Sym) {
299+
std::string S;
300+
if (Sym->isUndefined())
301+
S = ": reference to ";
302+
else if (Sym->isLazy())
303+
S = ": lazy definition of ";
304+
else
305+
S = ": definition of ";
306+
307+
message(toString(Sym->getFile()) + S + Sym->getName());
308+
}

lld/wasm/Symbols.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,14 @@ class Symbol {
100100
unsigned IsUsedInRegularObj : 1;
101101
unsigned ForceExport : 1;
102102

103+
// True if this symbol is specified by --trace-symbol option.
104+
unsigned Traced : 1;
105+
103106
protected:
104107
Symbol(StringRef Name, Kind K, uint32_t Flags, InputFile *F)
105-
: IsUsedInRegularObj(false), ForceExport(false), Name(Name),
106-
SymbolKind(K), Flags(Flags), File(F), Referenced(!Config->GcSections) {}
108+
: IsUsedInRegularObj(false), ForceExport(false), Traced(false),
109+
Name(Name), SymbolKind(K), Flags(Flags), File(F),
110+
Referenced(!Config->GcSections) {}
107111

108112
StringRef Name;
109113
Kind SymbolKind;
@@ -402,6 +406,8 @@ union SymbolUnion {
402406
alignas(SectionSymbol) char I[sizeof(SectionSymbol)];
403407
};
404408

409+
void printTraceSymbol(Symbol *Sym);
410+
405411
template <typename T, typename... ArgT>
406412
T *replaceSymbol(Symbol *S, ArgT &&... Arg) {
407413
static_assert(std::is_trivially_destructible<T>(),
@@ -417,6 +423,13 @@ T *replaceSymbol(Symbol *S, ArgT &&... Arg) {
417423
T *S2 = new (S) T(std::forward<ArgT>(Arg)...);
418424
S2->IsUsedInRegularObj = SymCopy.IsUsedInRegularObj;
419425
S2->ForceExport = SymCopy.ForceExport;
426+
S2->Traced = SymCopy.Traced;
427+
428+
// Print out a log message if --trace-symbol was specified.
429+
// This is for debugging.
430+
if (S2->Traced)
431+
printTraceSymbol(S2);
432+
420433
return S2;
421434
}
422435

0 commit comments

Comments
 (0)