summaryrefslogtreecommitdiff
path: root/clang/lib/Driver/ToolChains/WebAssembly.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2020-01-17 20:45:01 +0000
committerDimitry Andric <dim@FreeBSD.org>2020-01-17 20:45:01 +0000
commit706b4fc47bbc608932d3b491ae19a3b9cde9497b (patch)
tree4adf86a776049cbf7f69a1929c4babcbbef925eb /clang/lib/Driver/ToolChains/WebAssembly.cpp
parent7cc9cf2bf09f069cb2dd947ead05d0b54301fb71 (diff)
Notes
Diffstat (limited to 'clang/lib/Driver/ToolChains/WebAssembly.cpp')
-rw-r--r--clang/lib/Driver/ToolChains/WebAssembly.cpp64
1 files changed, 57 insertions, 7 deletions
diff --git a/clang/lib/Driver/ToolChains/WebAssembly.cpp b/clang/lib/Driver/ToolChains/WebAssembly.cpp
index 3add913b700f..907f86b8233c 100644
--- a/clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ b/clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -8,6 +8,7 @@
#include "WebAssembly.h"
#include "CommonArgs.h"
+#include "clang/Basic/Version.h"
#include "clang/Config/config.h"
#include "clang/Driver/Compilation.h"
#include "clang/Driver/Driver.h"
@@ -90,6 +91,39 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back(Output.getFilename());
C.addCommand(std::make_unique<Command>(JA, *this, Linker, CmdArgs, Inputs));
+
+ // When optimizing, if wasm-opt is available, run it.
+ if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
+ auto WasmOptPath = getToolChain().GetProgramPath("wasm-opt");
+ if (WasmOptPath != "wasm-opt") {
+ StringRef OOpt = "s";
+ if (A->getOption().matches(options::OPT_O4) ||
+ A->getOption().matches(options::OPT_Ofast))
+ OOpt = "4";
+ else if (A->getOption().matches(options::OPT_O0))
+ OOpt = "0";
+ else if (A->getOption().matches(options::OPT_O))
+ OOpt = A->getValue();
+
+ if (OOpt != "0") {
+ const char *WasmOpt = Args.MakeArgString(WasmOptPath);
+ ArgStringList CmdArgs;
+ CmdArgs.push_back(Output.getFilename());
+ CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
+ CmdArgs.push_back("-o");
+ CmdArgs.push_back(Output.getFilename());
+ C.addCommand(std::make_unique<Command>(JA, *this, WasmOpt, CmdArgs, Inputs));
+ }
+ }
+ }
+}
+
+/// Given a base library directory, append path components to form the
+/// LTO directory.
+static std::string AppendLTOLibDir(const std::string &Dir) {
+ // The version allows the path to be keyed to the specific version of
+ // LLVM in used, as the bitcode format is not stable.
+ return Dir + "/llvm-lto/" LLVM_VERSION_STRING;
}
WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
@@ -100,16 +134,24 @@ WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
getProgramPaths().push_back(getDriver().getInstalledDir());
+ auto SysRoot = getDriver().SysRoot;
if (getTriple().getOS() == llvm::Triple::UnknownOS) {
// Theoretically an "unknown" OS should mean no standard libraries, however
// it could also mean that a custom set of libraries is in use, so just add
// /lib to the search path. Disable multiarch in this case, to discourage
// paths containing "unknown" from acquiring meanings.
- getFilePaths().push_back(getDriver().SysRoot + "/lib");
+ getFilePaths().push_back(SysRoot + "/lib");
} else {
const std::string MultiarchTriple =
- getMultiarchTriple(getDriver(), Triple, getDriver().SysRoot);
- getFilePaths().push_back(getDriver().SysRoot + "/lib/" + MultiarchTriple);
+ getMultiarchTriple(getDriver(), Triple, SysRoot);
+ if (D.isUsingLTO()) {
+ // For LTO, enable use of lto-enabled sysroot libraries too, if available.
+ // Note that the directory is keyed to the LLVM revision, as LLVM's
+ // bitcode format is not stable.
+ auto Dir = AppendLTOLibDir(SysRoot + "/lib/" + MultiarchTriple);
+ getFilePaths().push_back(Dir);
+ }
+ getFilePaths().push_back(SysRoot + "/lib/" + MultiarchTriple);
}
}
@@ -137,9 +179,9 @@ bool WebAssembly::HasNativeLLVMSupport() const { return true; }
void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
ArgStringList &CC1Args,
Action::OffloadKind) const {
- if (DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
- options::OPT_fno_use_init_array, true))
- CC1Args.push_back("-fuse-init-array");
+ if (!DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
+ options::OPT_fno_use_init_array, true))
+ CC1Args.push_back("-fno-use-init-array");
// '-pthread' implies atomics, bulk-memory, mutable-globals, and sign-ext
if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread,
@@ -181,6 +223,12 @@ void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
getDriver().Diag(diag::err_drv_argument_not_allowed_with)
<< "-fwasm-exceptions"
<< "-mno-exception-handling";
+ // '-fwasm-exceptions' is not compatible with '-mno-reference-types'
+ if (DriverArgs.hasFlag(options::OPT_mno_reference_types,
+ options::OPT_mexception_handing, false))
+ getDriver().Diag(diag::err_drv_argument_not_allowed_with)
+ << "-fwasm-exceptions"
+ << "-mno-reference-types";
// '-fwasm-exceptions' is not compatible with
// '-mllvm -enable-emscripten-cxx-exceptions'
for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
@@ -189,9 +237,11 @@ void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
<< "-fwasm-exceptions"
<< "-mllvm -enable-emscripten-cxx-exceptions";
}
- // '-fwasm-exceptions' implies exception-handling
+ // '-fwasm-exceptions' implies exception-handling and reference-types
CC1Args.push_back("-target-feature");
CC1Args.push_back("+exception-handling");
+ CC1Args.push_back("-target-feature");
+ CC1Args.push_back("+reference-types");
}
}