forked from iree-org/iree
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a busybox style
iree-lld
tool. (iree-org#7186)
* I'm not sure this is a good idea. But also not sure it is bad. * The intent here is that for distribution (i.e. as part of the Python API), we can have a standalone LLD tool which links against the common compiler .so/.dll, saving quite a bit of binary size (i.e. a standalone lld is on the same order of magnitude of ~everything else whereas bundling adds 20-30% on disk and potentially saves shared memory, etc). * I don't think this is out of bounds of the upstream policy of not supporting LLD-as-a-library: we still only use it as a standalone process, just allow it to be linked with everything. * Doesn't actually use the tool yet: would need some plumbing to make sure the various things select it and consistently pass -flavor vs relying on name-based sniffing.
- Loading branch information
1 parent
1f33d06
commit 76138f4
Showing
6 changed files
with
156 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2021 The IREE Authors | ||
// | ||
// Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
// See llvm-project/lld/tools/lld/lld.cpp. Much of that is scaffolding for | ||
// supporting symlink based lld which auto-detects the flavor. Instead, we | ||
// duplicate the flavor parsing and invoke the backend directly similar to | ||
// what the lldMain() does. | ||
|
||
#include <cstdlib> | ||
#include <vector> | ||
|
||
#include "iree-compiler-c/Tools.h" | ||
#include "lld/Common/Driver.h" | ||
#include "lld/Common/ErrorHandler.h" | ||
#include "lld/Common/Memory.h" | ||
#include "llvm/ADT/STLExtras.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/ADT/StringSwitch.h" | ||
#include "llvm/ADT/Triple.h" | ||
#include "llvm/ADT/Twine.h" | ||
#include "llvm/Support/CommandLine.h" | ||
#include "llvm/Support/CrashRecoveryContext.h" | ||
#include "llvm/Support/Host.h" | ||
#include "llvm/Support/InitLLVM.h" | ||
#include "llvm/Support/Path.h" | ||
#include "llvm/Support/PluginLoader.h" | ||
#include "llvm/Support/Process.h" | ||
|
||
using namespace lld; | ||
using namespace llvm; | ||
using namespace llvm::sys; | ||
|
||
enum Flavor { | ||
Invalid, | ||
Gnu, // -flavor gnu | ||
WinLink, // -flavor link | ||
Darwin, // -flavor darwin | ||
Wasm, // -flavor wasm | ||
}; | ||
|
||
[[noreturn]] static void die(const Twine &s) { | ||
llvm::errs() << s << "\n"; | ||
exit(1); | ||
} | ||
|
||
static Flavor getFlavor(StringRef s) { | ||
return StringSwitch<Flavor>(s) | ||
.CasesLower("ld", "ld.lld", "gnu", Gnu) | ||
.CasesLower("wasm", "ld-wasm", Wasm) | ||
.CaseLower("link", WinLink) | ||
.CasesLower("ld64", "ld64.lld", "darwin", "darwinnew", | ||
"ld64.lld.darwinnew", Darwin) | ||
.Default(Invalid); | ||
} | ||
|
||
static Flavor parseFlavor(std::vector<const char *> &v) { | ||
// Parse -flavor option. | ||
if (v.size() > 1 && v[1] == StringRef("-flavor")) { | ||
if (v.size() <= 2) die("missing arg value for '-flavor'"); | ||
Flavor f = getFlavor(v[2]); | ||
if (f == Invalid) die("Unknown flavor: " + StringRef(v[2])); | ||
v.erase(v.begin() + 1, v.begin() + 3); | ||
return f; | ||
} | ||
die("Expected -flavor <gnu|link|darwin|wasm>"); | ||
} | ||
|
||
int ireeCompilerRunLldMain(int argc, char **argv) { | ||
InitLLVM x(argc, argv); | ||
sys::Process::UseANSIEscapeCodes(true); | ||
bool exitEarly = true; | ||
llvm::raw_ostream &stdoutOS = llvm::outs(); | ||
llvm::raw_ostream &stderrOS = llvm::errs(); | ||
|
||
std::vector<const char *> args(argv, argv + argc); | ||
switch (parseFlavor(args)) { | ||
case Gnu: | ||
return !elf::link(args, exitEarly, stdoutOS, stderrOS); | ||
case WinLink: | ||
return !coff::link(args, exitEarly, stdoutOS, stderrOS); | ||
case Darwin: | ||
return !macho::link(args, exitEarly, stdoutOS, stderrOS); | ||
case Wasm: | ||
return !lld::wasm::link(args, exitEarly, stdoutOS, stderrOS); | ||
default: | ||
die("lld is a generic driver.\n" | ||
"Invoke ld.lld (Unix), ld64.lld (macOS), lld-link (Windows), wasm-ld" | ||
" (WebAssembly) instead"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright 2021 The IREE Authors | ||
// | ||
// Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#include "iree-compiler-c/Tools.h" | ||
|
||
int main(int argc, char **argv) { return ireeCompilerRunLldMain(argc, argv); } |