2015-06-30 07:51:55 +08:00
|
|
|
//===- WebAssemblyTargetMachine.cpp - Define TargetMachine for WebAssembly -==//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, 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
|
2015-06-30 07:51:55 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-01 23:54:18 +08:00
|
|
|
/// This file defines the WebAssembly-specific subclass of TargetMachine.
|
2015-06-30 07:51:55 +08:00
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "WebAssemblyTargetMachine.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
|
2019-05-15 09:03:00 +08:00
|
|
|
#include "TargetInfo/WebAssemblyTargetInfo.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "WebAssembly.h"
|
2019-03-27 01:35:35 +08:00
|
|
|
#include "WebAssemblyMachineFunctionInfo.h"
|
2015-12-17 12:55:44 +08:00
|
|
|
#include "WebAssemblyTargetObjectFile.h"
|
2015-06-30 07:51:55 +08:00
|
|
|
#include "WebAssemblyTargetTransformInfo.h"
|
2019-03-27 01:35:35 +08:00
|
|
|
#include "llvm/CodeGen/MIRParser/MIParser.h"
|
2015-06-30 07:51:55 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/CodeGen/RegAllocRegistry.h"
|
2016-05-10 11:21:59 +08:00
|
|
|
#include "llvm/CodeGen/TargetPassConfig.h"
|
2015-06-30 07:51:55 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2015-07-02 07:41:25 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
[WebAssembly] Merge used feature sets, update atomics linkage policy
Summary:
It does not currently make sense to use WebAssembly features in some functions
but not others, so this CL adds an IR pass that takes the union of all used
feature sets and applies it to each function in the module. This allows us to
prevent atomics from being lowered away if some function has opted in to using
them. When atomics is not enabled anywhere, we detect whether there exists any
atomic operations or thread local storage that would be stripped and disallow
linking with objects that contain atomics if and only if atomics or tls are
stripped. When atomics is enabled, mark it as used but do not require it of
other objects in the link. These changes allow libraries that do not use atomics
to be built once and linked into both single-threaded and multithreaded
binaries.
Reviewers: aheejin, sbc100, dschuff
Subscribers: jgravelle-google, hiraditya, sunfish, jfb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59625
llvm-svn: 357226
2019-03-29 08:14:01 +08:00
|
|
|
#include "llvm/Transforms/Scalar/LowerAtomic.h"
|
2018-03-29 01:44:36 +08:00
|
|
|
#include "llvm/Transforms/Utils.h"
|
2015-06-30 07:51:55 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "wasm"
|
|
|
|
|
2016-08-02 05:34:04 +08:00
|
|
|
// Emscripten's asm.js-style exception handling
|
2016-08-18 23:27:25 +08:00
|
|
|
static cl::opt<bool> EnableEmException(
|
2016-08-09 08:29:55 +08:00
|
|
|
"enable-emscripten-cxx-exceptions",
|
2016-08-02 05:34:04 +08:00
|
|
|
cl::desc("WebAssembly Emscripten-style exception handling"),
|
|
|
|
cl::init(false));
|
|
|
|
|
2016-08-18 23:27:25 +08:00
|
|
|
// Emscripten's asm.js-style setjmp/longjmp handling
|
|
|
|
static cl::opt<bool> EnableEmSjLj(
|
|
|
|
"enable-emscripten-sjlj",
|
|
|
|
cl::desc("WebAssembly Emscripten-style setjmp/longjmp handling"),
|
|
|
|
cl::init(false));
|
|
|
|
|
2020-05-01 02:41:39 +08:00
|
|
|
// A command-line option to keep implicit locals
|
|
|
|
// for the purpose of testing with lit/llc ONLY.
|
|
|
|
// This produces output which is not valid WebAssembly, and is not supported
|
|
|
|
// by assemblers/disassemblers and other MC based tools.
|
|
|
|
static cl::opt<bool> WasmDisableExplicitLocals(
|
|
|
|
"wasm-disable-explicit-locals", cl::Hidden,
|
|
|
|
cl::desc("WebAssembly: output implicit locals in"
|
|
|
|
" instruction output for test purposes only."),
|
|
|
|
cl::init(false));
|
|
|
|
|
CMake: Make most target symbols hidden by default
Summary:
For builds with LLVM_BUILD_LLVM_DYLIB=ON and BUILD_SHARED_LIBS=OFF
this change makes all symbols in the target specific libraries hidden
by default.
A new macro called LLVM_EXTERNAL_VISIBILITY has been added to mark symbols in these
libraries public, which is mainly needed for the definitions of the
LLVMInitialize* functions.
This patch reduces the number of public symbols in libLLVM.so by about
25%. This should improve load times for the dynamic library and also
make abi checker tools, like abidiff require less memory when analyzing
libLLVM.so
One side-effect of this change is that for builds with
LLVM_BUILD_LLVM_DYLIB=ON and LLVM_LINK_LLVM_DYLIB=ON some unittests that
access symbols that are no longer public will need to be statically linked.
Before and after public symbol counts (using gcc 8.2.1, ld.bfd 2.31.1):
nm before/libLLVM-9svn.so | grep ' [A-Zuvw] ' | wc -l
36221
nm after/libLLVM-9svn.so | grep ' [A-Zuvw] ' | wc -l
26278
Reviewers: chandlerc, beanz, mgorny, rnk, hans
Reviewed By: rnk, hans
Subscribers: merge_guards_bot, luismarques, smeenai, ldionne, lenary, s.egerton, pzheng, sameer.abuasal, MaskRay, wuzish, echristo, Jim, hiraditya, michaelplatings, chapuni, jholewinski, arsenm, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, javed.absar, sbc100, jgravelle-google, aheejin, kbarton, fedor.sergeev, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, zzheng, edward-jones, mgrang, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, PkmX, jocewei, kristina, jsji, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D54439
2020-01-15 11:15:07 +08:00
|
|
|
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyTarget() {
|
2015-06-30 07:51:55 +08:00
|
|
|
// Register the target.
|
2016-10-10 07:00:34 +08:00
|
|
|
RegisterTargetMachine<WebAssemblyTargetMachine> X(
|
|
|
|
getTheWebAssemblyTarget32());
|
|
|
|
RegisterTargetMachine<WebAssemblyTargetMachine> Y(
|
|
|
|
getTheWebAssemblyTarget64());
|
2016-08-02 05:34:04 +08:00
|
|
|
|
2018-03-31 04:36:58 +08:00
|
|
|
// Register backend passes
|
|
|
|
auto &PR = *PassRegistry::getPassRegistry();
|
2018-07-11 12:29:36 +08:00
|
|
|
initializeWebAssemblyAddMissingPrototypesPass(PR);
|
2018-03-31 04:36:58 +08:00
|
|
|
initializeWebAssemblyLowerEmscriptenEHSjLjPass(PR);
|
|
|
|
initializeLowerGlobalDtorsPass(PR);
|
|
|
|
initializeFixFunctionBitcastsPass(PR);
|
|
|
|
initializeOptimizeReturnedPass(PR);
|
|
|
|
initializeWebAssemblyArgumentMovePass(PR);
|
|
|
|
initializeWebAssemblySetP2AlignOperandsPass(PR);
|
|
|
|
initializeWebAssemblyReplacePhysRegsPass(PR);
|
|
|
|
initializeWebAssemblyPrepareForLiveIntervalsPass(PR);
|
|
|
|
initializeWebAssemblyOptimizeLiveIntervalsPass(PR);
|
2019-01-09 06:35:18 +08:00
|
|
|
initializeWebAssemblyMemIntrinsicResultsPass(PR);
|
2018-03-31 04:36:58 +08:00
|
|
|
initializeWebAssemblyRegStackifyPass(PR);
|
|
|
|
initializeWebAssemblyRegColoringPass(PR);
|
|
|
|
initializeWebAssemblyFixIrreducibleControlFlowPass(PR);
|
2018-06-25 09:07:11 +08:00
|
|
|
initializeWebAssemblyLateEHPreparePass(PR);
|
2018-06-25 09:20:21 +08:00
|
|
|
initializeWebAssemblyExceptionInfoPass(PR);
|
2018-03-31 04:36:58 +08:00
|
|
|
initializeWebAssemblyCFGSortPass(PR);
|
|
|
|
initializeWebAssemblyCFGStackifyPass(PR);
|
2019-03-30 17:29:57 +08:00
|
|
|
initializeWebAssemblyExplicitLocalsPass(PR);
|
2018-03-31 04:36:58 +08:00
|
|
|
initializeWebAssemblyLowerBrUnlessPass(PR);
|
|
|
|
initializeWebAssemblyRegNumberingPass(PR);
|
2020-05-01 02:41:39 +08:00
|
|
|
initializeWebAssemblyDebugFixupPass(PR);
|
2018-03-31 04:36:58 +08:00
|
|
|
initializeWebAssemblyPeepholePass(PR);
|
2015-06-30 07:51:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// WebAssembly Lowering public interface.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-06-06 04:01:01 +08:00
|
|
|
static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM,
|
|
|
|
const Triple &TT) {
|
2018-11-17 02:59:51 +08:00
|
|
|
if (!RM.hasValue()) {
|
|
|
|
// Default to static relocation model. This should always be more optimial
|
|
|
|
// than PIC since the static linker can determine all global addresses and
|
|
|
|
// assume direct function calls.
|
|
|
|
return Reloc::Static;
|
|
|
|
}
|
2019-06-06 04:01:01 +08:00
|
|
|
|
|
|
|
if (!TT.isOSEmscripten()) {
|
|
|
|
// Relocation modes other than static are currently implemented in a way
|
|
|
|
// that only works for Emscripten, so disable them if we aren't targeting
|
|
|
|
// Emscripten.
|
|
|
|
return Reloc::Static;
|
|
|
|
}
|
|
|
|
|
2016-05-19 11:00:05 +08:00
|
|
|
return *RM;
|
|
|
|
}
|
|
|
|
|
2015-06-30 07:51:55 +08:00
|
|
|
/// Create an WebAssembly architecture model.
|
|
|
|
///
|
|
|
|
WebAssemblyTargetMachine::WebAssemblyTargetMachine(
|
|
|
|
const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
|
2016-05-19 11:00:05 +08:00
|
|
|
const TargetOptions &Options, Optional<Reloc::Model> RM,
|
2017-08-03 13:15:53 +08:00
|
|
|
Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
|
2017-10-13 06:57:28 +08:00
|
|
|
: LLVMTargetMachine(T,
|
|
|
|
TT.isArch64Bit() ? "e-m:e-p:64:64-i64:64-n32:64-S128"
|
|
|
|
: "e-m:e-p:32:32-i64:64-n32:64-S128",
|
2019-06-06 04:01:01 +08:00
|
|
|
TT, CPU, FS, Options, getEffectiveRelocModel(RM, TT),
|
2018-12-07 20:10:23 +08:00
|
|
|
getEffectiveCodeModel(CM, CodeModel::Large), OL),
|
2018-07-17 07:09:29 +08:00
|
|
|
TLOF(new WebAssemblyTargetObjectFile()) {
|
2016-10-04 06:43:53 +08:00
|
|
|
// WebAssembly type-checks instructions, but a noreturn function with a return
|
2015-11-10 08:30:57 +08:00
|
|
|
// type that doesn't match the context will cause a check failure. So we lower
|
|
|
|
// LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's
|
2016-10-04 06:43:53 +08:00
|
|
|
// 'unreachable' instructions which is meant for that case.
|
2015-11-10 08:30:57 +08:00
|
|
|
this->Options.TrapUnreachable = true;
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
// WebAssembly treats each function as an independent unit. Force
|
|
|
|
// -ffunction-sections, effectively, so that we can emit them independently.
|
2018-07-17 07:09:29 +08:00
|
|
|
this->Options.FunctionSections = true;
|
|
|
|
this->Options.DataSections = true;
|
|
|
|
this->Options.UniqueSectionNames = true;
|
2017-02-25 07:18:00 +08:00
|
|
|
|
2015-06-30 07:51:55 +08:00
|
|
|
initAsmInfo();
|
|
|
|
|
2016-02-18 14:32:53 +08:00
|
|
|
// Note that we don't use setRequiresStructuredCFG(true). It disables
|
|
|
|
// optimizations than we're ok with, and want, such as critical edge
|
|
|
|
// splitting and tail merging.
|
2015-06-30 07:51:55 +08:00
|
|
|
}
|
|
|
|
|
[WebAssembly] clang-tidy (NFC)
Summary:
This patch fixes clang-tidy warnings on wasm-only files.
The list of checks used is:
`-*,clang-diagnostic-*,llvm-*,misc-*,-misc-unused-parameters,readability-identifier-naming,modernize-*`
(LLVM's default .clang-tidy list is the same except it does not have
`modernize-*`. But I've seen in multiple CLs in LLVM the modernize style
was recommended and code was fixed based on the style, so I added it as
well.)
The common fixes are:
- Variable names start with an uppercase letter
- Function names start with a lowercase letter
- Use `auto` when you use casts so the type is evident
- Use inline initialization for class member variables
- Use `= default` for empty constructors / destructors
- Use `using` in place of `typedef`
Reviewers: sbc100, tlively, aardappel
Subscribers: dschuff, sunfish, jgravelle-google, yurydelendik, kripken, MatzeB, mgorny, rupprecht, llvm-commits
Differential Revision: https://reviews.llvm.org/D57500
llvm-svn: 353075
2019-02-05 03:13:39 +08:00
|
|
|
WebAssemblyTargetMachine::~WebAssemblyTargetMachine() = default; // anchor.
|
2015-06-30 07:51:55 +08:00
|
|
|
|
[WebAssembly] Remove uses of ThreadModel
Summary:
In the clang UI, replaces -mthread-model posix with -matomics as the
source of truth on threading. In the backend, replaces
-thread-model=posix with the atomics target feature, which is now
collected on the WebAssemblyTargetMachine along with all other used
features. These collected features will also be used to emit the
target features section in the future.
The default configuration for the backend is thread-model=posix and no
atomics, which was previously an invalid configuration. This change
makes the default valid because the thread model is ignored.
A side effect of this change is that objects are never emitted with
passive segments. It will instead be up to the linker to decide
whether sections should be active or passive based on whether atomics
are used in the final link.
Reviewers: aheejin, sbc100, dschuff
Subscribers: mehdi_amini, jgravelle-google, hiraditya, sunfish, steven_wu, dexonsmith, rupprecht, jfb, jdoerfert, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D58742
llvm-svn: 355112
2019-03-01 02:39:08 +08:00
|
|
|
const WebAssemblySubtarget *
|
|
|
|
WebAssemblyTargetMachine::getSubtargetImpl(std::string CPU,
|
|
|
|
std::string FS) const {
|
|
|
|
auto &I = SubtargetMap[CPU + FS];
|
|
|
|
if (!I) {
|
2019-08-15 23:54:37 +08:00
|
|
|
I = std::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
|
[WebAssembly] Remove uses of ThreadModel
Summary:
In the clang UI, replaces -mthread-model posix with -matomics as the
source of truth on threading. In the backend, replaces
-thread-model=posix with the atomics target feature, which is now
collected on the WebAssemblyTargetMachine along with all other used
features. These collected features will also be used to emit the
target features section in the future.
The default configuration for the backend is thread-model=posix and no
atomics, which was previously an invalid configuration. This change
makes the default valid because the thread model is ignored.
A side effect of this change is that objects are never emitted with
passive segments. It will instead be up to the linker to decide
whether sections should be active or passive based on whether atomics
are used in the final link.
Reviewers: aheejin, sbc100, dschuff
Subscribers: mehdi_amini, jgravelle-google, hiraditya, sunfish, steven_wu, dexonsmith, rupprecht, jfb, jdoerfert, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D58742
llvm-svn: 355112
2019-03-01 02:39:08 +08:00
|
|
|
}
|
|
|
|
return I.get();
|
|
|
|
}
|
|
|
|
|
2015-06-30 07:51:55 +08:00
|
|
|
const WebAssemblySubtarget *
|
|
|
|
WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
|
|
|
|
Attribute CPUAttr = F.getFnAttribute("target-cpu");
|
|
|
|
Attribute FSAttr = F.getFnAttribute("target-features");
|
|
|
|
|
|
|
|
std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
|
|
|
|
? CPUAttr.getValueAsString().str()
|
|
|
|
: TargetCPU;
|
|
|
|
std::string FS = !FSAttr.hasAttribute(Attribute::None)
|
|
|
|
? FSAttr.getValueAsString().str()
|
|
|
|
: TargetFS;
|
|
|
|
|
[WebAssembly] Remove uses of ThreadModel
Summary:
In the clang UI, replaces -mthread-model posix with -matomics as the
source of truth on threading. In the backend, replaces
-thread-model=posix with the atomics target feature, which is now
collected on the WebAssemblyTargetMachine along with all other used
features. These collected features will also be used to emit the
target features section in the future.
The default configuration for the backend is thread-model=posix and no
atomics, which was previously an invalid configuration. This change
makes the default valid because the thread model is ignored.
A side effect of this change is that objects are never emitted with
passive segments. It will instead be up to the linker to decide
whether sections should be active or passive based on whether atomics
are used in the final link.
Reviewers: aheejin, sbc100, dschuff
Subscribers: mehdi_amini, jgravelle-google, hiraditya, sunfish, steven_wu, dexonsmith, rupprecht, jfb, jdoerfert, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D58742
llvm-svn: 355112
2019-03-01 02:39:08 +08:00
|
|
|
// This needs to be done before we create a new subtarget since any
|
|
|
|
// creation will depend on the TM and the code generation flags on the
|
|
|
|
// function that reside in TargetOptions.
|
|
|
|
resetTargetOptions(F);
|
|
|
|
|
|
|
|
return getSubtargetImpl(CPU, FS);
|
2015-06-30 07:51:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
[WebAssembly] Merge used feature sets, update atomics linkage policy
Summary:
It does not currently make sense to use WebAssembly features in some functions
but not others, so this CL adds an IR pass that takes the union of all used
feature sets and applies it to each function in the module. This allows us to
prevent atomics from being lowered away if some function has opted in to using
them. When atomics is not enabled anywhere, we detect whether there exists any
atomic operations or thread local storage that would be stripped and disallow
linking with objects that contain atomics if and only if atomics or tls are
stripped. When atomics is enabled, mark it as used but do not require it of
other objects in the link. These changes allow libraries that do not use atomics
to be built once and linked into both single-threaded and multithreaded
binaries.
Reviewers: aheejin, sbc100, dschuff
Subscribers: jgravelle-google, hiraditya, sunfish, jfb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59625
llvm-svn: 357226
2019-03-29 08:14:01 +08:00
|
|
|
|
|
|
|
class CoalesceFeaturesAndStripAtomics final : public ModulePass {
|
|
|
|
// Take the union of all features used in the module and use it for each
|
|
|
|
// function individually, since having multiple feature sets in one module
|
|
|
|
// currently does not make sense for WebAssembly. If atomics are not enabled,
|
|
|
|
// also strip atomic operations and thread local storage.
|
2018-03-21 06:01:32 +08:00
|
|
|
static char ID;
|
[WebAssembly] Merge used feature sets, update atomics linkage policy
Summary:
It does not currently make sense to use WebAssembly features in some functions
but not others, so this CL adds an IR pass that takes the union of all used
feature sets and applies it to each function in the module. This allows us to
prevent atomics from being lowered away if some function has opted in to using
them. When atomics is not enabled anywhere, we detect whether there exists any
atomic operations or thread local storage that would be stripped and disallow
linking with objects that contain atomics if and only if atomics or tls are
stripped. When atomics is enabled, mark it as used but do not require it of
other objects in the link. These changes allow libraries that do not use atomics
to be built once and linked into both single-threaded and multithreaded
binaries.
Reviewers: aheejin, sbc100, dschuff
Subscribers: jgravelle-google, hiraditya, sunfish, jfb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59625
llvm-svn: 357226
2019-03-29 08:14:01 +08:00
|
|
|
WebAssemblyTargetMachine *WasmTM;
|
2018-03-21 06:01:32 +08:00
|
|
|
|
2018-09-05 09:27:38 +08:00
|
|
|
public:
|
[WebAssembly] Merge used feature sets, update atomics linkage policy
Summary:
It does not currently make sense to use WebAssembly features in some functions
but not others, so this CL adds an IR pass that takes the union of all used
feature sets and applies it to each function in the module. This allows us to
prevent atomics from being lowered away if some function has opted in to using
them. When atomics is not enabled anywhere, we detect whether there exists any
atomic operations or thread local storage that would be stripped and disallow
linking with objects that contain atomics if and only if atomics or tls are
stripped. When atomics is enabled, mark it as used but do not require it of
other objects in the link. These changes allow libraries that do not use atomics
to be built once and linked into both single-threaded and multithreaded
binaries.
Reviewers: aheejin, sbc100, dschuff
Subscribers: jgravelle-google, hiraditya, sunfish, jfb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59625
llvm-svn: 357226
2019-03-29 08:14:01 +08:00
|
|
|
CoalesceFeaturesAndStripAtomics(WebAssemblyTargetMachine *WasmTM)
|
|
|
|
: ModulePass(ID), WasmTM(WasmTM) {}
|
|
|
|
|
2018-03-21 06:01:32 +08:00
|
|
|
bool runOnModule(Module &M) override {
|
[WebAssembly] Merge used feature sets, update atomics linkage policy
Summary:
It does not currently make sense to use WebAssembly features in some functions
but not others, so this CL adds an IR pass that takes the union of all used
feature sets and applies it to each function in the module. This allows us to
prevent atomics from being lowered away if some function has opted in to using
them. When atomics is not enabled anywhere, we detect whether there exists any
atomic operations or thread local storage that would be stripped and disallow
linking with objects that contain atomics if and only if atomics or tls are
stripped. When atomics is enabled, mark it as used but do not require it of
other objects in the link. These changes allow libraries that do not use atomics
to be built once and linked into both single-threaded and multithreaded
binaries.
Reviewers: aheejin, sbc100, dschuff
Subscribers: jgravelle-google, hiraditya, sunfish, jfb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59625
llvm-svn: 357226
2019-03-29 08:14:01 +08:00
|
|
|
FeatureBitset Features = coalesceFeatures(M);
|
|
|
|
|
|
|
|
std::string FeatureStr = getFeatureString(Features);
|
|
|
|
for (auto &F : M)
|
|
|
|
replaceFeatures(F, FeatureStr);
|
|
|
|
|
[WebAssembly] Implement thread-local storage (local-exec model)
Summary:
Thread local variables are placed inside a `.tdata` segment. Their symbols are
offsets from the start of the segment. The address of a thread local variable
is computed as `__tls_base` + the offset from the start of the segment.
`.tdata` segment is a passive segment and `memory.init` is used once per thread
to initialize the thread local storage.
`__tls_base` is a wasm global. Since each thread has its own wasm instance,
it is effectively thread local. Currently, `__tls_base` must be initialized
at thread startup, and so cannot be used with dynamic libraries.
`__tls_base` is to be initialized with a new linker-synthesized function,
`__wasm_init_tls`, which takes as an argument a block of memory to use as the
storage for thread locals. It then initializes the block of memory and sets
`__tls_base`. As `__wasm_init_tls` will handle the memory initialization,
the memory does not have to be zeroed.
To help allocating memory for thread-local storage, a new compiler intrinsic
is introduced: `__builtin_wasm_tls_size()`. This instrinsic function returns
the size of the thread-local storage for the current function.
The expected usage is to run something like the following upon thread startup:
__wasm_init_tls(malloc(__builtin_wasm_tls_size()));
Reviewers: tlively, aheejin, kripken, sbc100
Subscribers: dschuff, jgravelle-google, hiraditya, sunfish, jfb, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D64537
llvm-svn: 366272
2019-07-17 06:00:45 +08:00
|
|
|
bool StrippedAtomics = false;
|
|
|
|
bool StrippedTLS = false;
|
|
|
|
|
|
|
|
if (!Features[WebAssembly::FeatureAtomics])
|
|
|
|
StrippedAtomics = stripAtomics(M);
|
|
|
|
|
|
|
|
if (!Features[WebAssembly::FeatureBulkMemory])
|
|
|
|
StrippedTLS = stripThreadLocals(M);
|
|
|
|
|
|
|
|
if (StrippedAtomics && !StrippedTLS)
|
|
|
|
stripThreadLocals(M);
|
|
|
|
else if (StrippedTLS && !StrippedAtomics)
|
|
|
|
stripAtomics(M);
|
[WebAssembly] Merge used feature sets, update atomics linkage policy
Summary:
It does not currently make sense to use WebAssembly features in some functions
but not others, so this CL adds an IR pass that takes the union of all used
feature sets and applies it to each function in the module. This allows us to
prevent atomics from being lowered away if some function has opted in to using
them. When atomics is not enabled anywhere, we detect whether there exists any
atomic operations or thread local storage that would be stripped and disallow
linking with objects that contain atomics if and only if atomics or tls are
stripped. When atomics is enabled, mark it as used but do not require it of
other objects in the link. These changes allow libraries that do not use atomics
to be built once and linked into both single-threaded and multithreaded
binaries.
Reviewers: aheejin, sbc100, dschuff
Subscribers: jgravelle-google, hiraditya, sunfish, jfb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59625
llvm-svn: 357226
2019-03-29 08:14:01 +08:00
|
|
|
|
[WebAssembly] Implement thread-local storage (local-exec model)
Summary:
Thread local variables are placed inside a `.tdata` segment. Their symbols are
offsets from the start of the segment. The address of a thread local variable
is computed as `__tls_base` + the offset from the start of the segment.
`.tdata` segment is a passive segment and `memory.init` is used once per thread
to initialize the thread local storage.
`__tls_base` is a wasm global. Since each thread has its own wasm instance,
it is effectively thread local. Currently, `__tls_base` must be initialized
at thread startup, and so cannot be used with dynamic libraries.
`__tls_base` is to be initialized with a new linker-synthesized function,
`__wasm_init_tls`, which takes as an argument a block of memory to use as the
storage for thread locals. It then initializes the block of memory and sets
`__tls_base`. As `__wasm_init_tls` will handle the memory initialization,
the memory does not have to be zeroed.
To help allocating memory for thread-local storage, a new compiler intrinsic
is introduced: `__builtin_wasm_tls_size()`. This instrinsic function returns
the size of the thread-local storage for the current function.
The expected usage is to run something like the following upon thread startup:
__wasm_init_tls(malloc(__builtin_wasm_tls_size()));
Reviewers: tlively, aheejin, kripken, sbc100
Subscribers: dschuff, jgravelle-google, hiraditya, sunfish, jfb, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D64537
llvm-svn: 366272
2019-07-17 06:00:45 +08:00
|
|
|
recordFeatures(M, Features, StrippedAtomics || StrippedTLS);
|
[WebAssembly] Merge used feature sets, update atomics linkage policy
Summary:
It does not currently make sense to use WebAssembly features in some functions
but not others, so this CL adds an IR pass that takes the union of all used
feature sets and applies it to each function in the module. This allows us to
prevent atomics from being lowered away if some function has opted in to using
them. When atomics is not enabled anywhere, we detect whether there exists any
atomic operations or thread local storage that would be stripped and disallow
linking with objects that contain atomics if and only if atomics or tls are
stripped. When atomics is enabled, mark it as used but do not require it of
other objects in the link. These changes allow libraries that do not use atomics
to be built once and linked into both single-threaded and multithreaded
binaries.
Reviewers: aheejin, sbc100, dschuff
Subscribers: jgravelle-google, hiraditya, sunfish, jfb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59625
llvm-svn: 357226
2019-03-29 08:14:01 +08:00
|
|
|
|
|
|
|
// Conservatively assume we have made some change
|
2018-03-21 06:01:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
[WebAssembly] Merge used feature sets, update atomics linkage policy
Summary:
It does not currently make sense to use WebAssembly features in some functions
but not others, so this CL adds an IR pass that takes the union of all used
feature sets and applies it to each function in the module. This allows us to
prevent atomics from being lowered away if some function has opted in to using
them. When atomics is not enabled anywhere, we detect whether there exists any
atomic operations or thread local storage that would be stripped and disallow
linking with objects that contain atomics if and only if atomics or tls are
stripped. When atomics is enabled, mark it as used but do not require it of
other objects in the link. These changes allow libraries that do not use atomics
to be built once and linked into both single-threaded and multithreaded
binaries.
Reviewers: aheejin, sbc100, dschuff
Subscribers: jgravelle-google, hiraditya, sunfish, jfb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59625
llvm-svn: 357226
2019-03-29 08:14:01 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
FeatureBitset coalesceFeatures(const Module &M) {
|
|
|
|
FeatureBitset Features =
|
|
|
|
WasmTM
|
2020-01-29 03:23:46 +08:00
|
|
|
->getSubtargetImpl(std::string(WasmTM->getTargetCPU()),
|
|
|
|
std::string(WasmTM->getTargetFeatureString()))
|
[WebAssembly] Merge used feature sets, update atomics linkage policy
Summary:
It does not currently make sense to use WebAssembly features in some functions
but not others, so this CL adds an IR pass that takes the union of all used
feature sets and applies it to each function in the module. This allows us to
prevent atomics from being lowered away if some function has opted in to using
them. When atomics is not enabled anywhere, we detect whether there exists any
atomic operations or thread local storage that would be stripped and disallow
linking with objects that contain atomics if and only if atomics or tls are
stripped. When atomics is enabled, mark it as used but do not require it of
other objects in the link. These changes allow libraries that do not use atomics
to be built once and linked into both single-threaded and multithreaded
binaries.
Reviewers: aheejin, sbc100, dschuff
Subscribers: jgravelle-google, hiraditya, sunfish, jfb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59625
llvm-svn: 357226
2019-03-29 08:14:01 +08:00
|
|
|
->getFeatureBits();
|
|
|
|
for (auto &F : M)
|
|
|
|
Features |= WasmTM->getSubtargetImpl(F)->getFeatureBits();
|
|
|
|
return Features;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getFeatureString(const FeatureBitset &Features) {
|
|
|
|
std::string Ret;
|
|
|
|
for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) {
|
|
|
|
if (Features[KV.Value])
|
|
|
|
Ret += (StringRef("+") + KV.Key + ",").str();
|
|
|
|
}
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void replaceFeatures(Function &F, const std::string &Features) {
|
|
|
|
F.removeFnAttr("target-features");
|
|
|
|
F.removeFnAttr("target-cpu");
|
|
|
|
F.addFnAttr("target-features", Features);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool stripAtomics(Module &M) {
|
|
|
|
// Detect whether any atomics will be lowered, since there is no way to tell
|
|
|
|
// whether the LowerAtomic pass lowers e.g. stores.
|
|
|
|
bool Stripped = false;
|
|
|
|
for (auto &F : M) {
|
|
|
|
for (auto &B : F) {
|
|
|
|
for (auto &I : B) {
|
|
|
|
if (I.isAtomic()) {
|
|
|
|
Stripped = true;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (!Stripped)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
LowerAtomicPass Lowerer;
|
|
|
|
FunctionAnalysisManager FAM;
|
|
|
|
for (auto &F : M)
|
|
|
|
Lowerer.run(F, FAM);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool stripThreadLocals(Module &M) {
|
|
|
|
bool Stripped = false;
|
|
|
|
for (auto &GV : M.globals()) {
|
|
|
|
if (GV.getThreadLocalMode() !=
|
|
|
|
GlobalValue::ThreadLocalMode::NotThreadLocal) {
|
|
|
|
Stripped = true;
|
|
|
|
GV.setThreadLocalMode(GlobalValue::ThreadLocalMode::NotThreadLocal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Stripped;
|
|
|
|
}
|
|
|
|
|
|
|
|
void recordFeatures(Module &M, const FeatureBitset &Features, bool Stripped) {
|
|
|
|
for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) {
|
[WebAssembly] Disallow 'shared-mem' rather than 'atomics'
Summary:
The WebAssembly backend automatically lowers atomic operations and TLS
to nonatomic operations and non-TLS data when either are present and
the atomics or bulk-memory features are not present, respectively. The
resulting object is no longer thread-safe, so the linker has to be
told not to allow it to be linked into a module with shared
memory. This was previously done by disallowing the 'atomics' feature,
which prevented any objct with its atomic operations or TLS removed
from being linked with any object containing atomics or TLS, and
therefore preventing it from being linked into a module with shared
memory since shared memory requires atomics.
However, as of https://github.com/WebAssembly/threads/issues/144, the
validation rules are relaxed to allow atomic operations to validate
with unshared memories, which makes it perfectly safe to link an
object with stripped atomics and TLS with another object that still
contains TLS and atomics as long as the resulting module has an
unshared memory. To allow this kind of link, this patch disallows a
pseudo-feature 'shared-mem' rather than 'atomics' to communicate to
the linker that the object is not thread-safe. This means that the
'atomics' feature is available to accurately reflect whether or not an
object has atomics enabled.
As a drive-by tweak, this change also requires that bulk-memory be
enabled in addition to atomics in order to use shared memory. This is
because initializing shared memories requires bulk-memory operations.
Reviewers: aheejin, sbc100
Subscribers: dschuff, jgravelle-google, hiraditya, sunfish, jfb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D79542
2020-05-07 10:33:24 +08:00
|
|
|
if (Features[KV.Value]) {
|
|
|
|
// Mark features as used
|
|
|
|
std::string MDKey = (StringRef("wasm-feature-") + KV.Key).str();
|
[WebAssembly] Merge used feature sets, update atomics linkage policy
Summary:
It does not currently make sense to use WebAssembly features in some functions
but not others, so this CL adds an IR pass that takes the union of all used
feature sets and applies it to each function in the module. This allows us to
prevent atomics from being lowered away if some function has opted in to using
them. When atomics is not enabled anywhere, we detect whether there exists any
atomic operations or thread local storage that would be stripped and disallow
linking with objects that contain atomics if and only if atomics or tls are
stripped. When atomics is enabled, mark it as used but do not require it of
other objects in the link. These changes allow libraries that do not use atomics
to be built once and linked into both single-threaded and multithreaded
binaries.
Reviewers: aheejin, sbc100, dschuff
Subscribers: jgravelle-google, hiraditya, sunfish, jfb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59625
llvm-svn: 357226
2019-03-29 08:14:01 +08:00
|
|
|
M.addModuleFlag(Module::ModFlagBehavior::Error, MDKey,
|
|
|
|
wasm::WASM_FEATURE_PREFIX_USED);
|
|
|
|
}
|
|
|
|
}
|
[WebAssembly] Disallow 'shared-mem' rather than 'atomics'
Summary:
The WebAssembly backend automatically lowers atomic operations and TLS
to nonatomic operations and non-TLS data when either are present and
the atomics or bulk-memory features are not present, respectively. The
resulting object is no longer thread-safe, so the linker has to be
told not to allow it to be linked into a module with shared
memory. This was previously done by disallowing the 'atomics' feature,
which prevented any objct with its atomic operations or TLS removed
from being linked with any object containing atomics or TLS, and
therefore preventing it from being linked into a module with shared
memory since shared memory requires atomics.
However, as of https://github.com/WebAssembly/threads/issues/144, the
validation rules are relaxed to allow atomic operations to validate
with unshared memories, which makes it perfectly safe to link an
object with stripped atomics and TLS with another object that still
contains TLS and atomics as long as the resulting module has an
unshared memory. To allow this kind of link, this patch disallows a
pseudo-feature 'shared-mem' rather than 'atomics' to communicate to
the linker that the object is not thread-safe. This means that the
'atomics' feature is available to accurately reflect whether or not an
object has atomics enabled.
As a drive-by tweak, this change also requires that bulk-memory be
enabled in addition to atomics in order to use shared memory. This is
because initializing shared memories requires bulk-memory operations.
Reviewers: aheejin, sbc100
Subscribers: dschuff, jgravelle-google, hiraditya, sunfish, jfb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D79542
2020-05-07 10:33:24 +08:00
|
|
|
// Code compiled without atomics or bulk-memory may have had its atomics or
|
|
|
|
// thread-local data lowered to nonatomic operations or non-thread-local
|
|
|
|
// data. In that case, we mark the pseudo-feature "shared-mem" as disallowed
|
|
|
|
// to tell the linker that it would be unsafe to allow this code ot be used
|
|
|
|
// in a module with shared memory.
|
|
|
|
if (Stripped) {
|
|
|
|
M.addModuleFlag(Module::ModFlagBehavior::Error, "wasm-feature-shared-mem",
|
|
|
|
wasm::WASM_FEATURE_PREFIX_DISALLOWED);
|
|
|
|
}
|
[WebAssembly] Merge used feature sets, update atomics linkage policy
Summary:
It does not currently make sense to use WebAssembly features in some functions
but not others, so this CL adds an IR pass that takes the union of all used
feature sets and applies it to each function in the module. This allows us to
prevent atomics from being lowered away if some function has opted in to using
them. When atomics is not enabled anywhere, we detect whether there exists any
atomic operations or thread local storage that would be stripped and disallow
linking with objects that contain atomics if and only if atomics or tls are
stripped. When atomics is enabled, mark it as used but do not require it of
other objects in the link. These changes allow libraries that do not use atomics
to be built once and linked into both single-threaded and multithreaded
binaries.
Reviewers: aheejin, sbc100, dschuff
Subscribers: jgravelle-google, hiraditya, sunfish, jfb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59625
llvm-svn: 357226
2019-03-29 08:14:01 +08:00
|
|
|
}
|
2018-03-21 06:01:32 +08:00
|
|
|
};
|
[WebAssembly] Merge used feature sets, update atomics linkage policy
Summary:
It does not currently make sense to use WebAssembly features in some functions
but not others, so this CL adds an IR pass that takes the union of all used
feature sets and applies it to each function in the module. This allows us to
prevent atomics from being lowered away if some function has opted in to using
them. When atomics is not enabled anywhere, we detect whether there exists any
atomic operations or thread local storage that would be stripped and disallow
linking with objects that contain atomics if and only if atomics or tls are
stripped. When atomics is enabled, mark it as used but do not require it of
other objects in the link. These changes allow libraries that do not use atomics
to be built once and linked into both single-threaded and multithreaded
binaries.
Reviewers: aheejin, sbc100, dschuff
Subscribers: jgravelle-google, hiraditya, sunfish, jfb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59625
llvm-svn: 357226
2019-03-29 08:14:01 +08:00
|
|
|
char CoalesceFeaturesAndStripAtomics::ID = 0;
|
2018-03-21 06:01:32 +08:00
|
|
|
|
2015-06-30 07:51:55 +08:00
|
|
|
/// WebAssembly Code Generator Pass Configuration Options.
|
|
|
|
class WebAssemblyPassConfig final : public TargetPassConfig {
|
|
|
|
public:
|
2017-05-31 05:36:41 +08:00
|
|
|
WebAssemblyPassConfig(WebAssemblyTargetMachine &TM, PassManagerBase &PM)
|
2015-06-30 07:51:55 +08:00
|
|
|
: TargetPassConfig(TM, PM) {}
|
|
|
|
|
|
|
|
WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const {
|
|
|
|
return getTM<WebAssemblyTargetMachine>();
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *createTargetRegisterAllocator(bool) override;
|
|
|
|
|
|
|
|
void addIRPasses() override;
|
|
|
|
bool addInstSelector() override;
|
|
|
|
void addPostRegAlloc() override;
|
2016-03-29 01:05:30 +08:00
|
|
|
bool addGCPasses() override { return false; }
|
2015-06-30 07:51:55 +08:00
|
|
|
void addPreEmitPass() override;
|
2019-03-20 03:33:12 +08:00
|
|
|
|
|
|
|
// No reg alloc
|
|
|
|
bool addRegAssignmentFast() override { return false; }
|
|
|
|
|
|
|
|
// No reg alloc
|
|
|
|
bool addRegAssignmentOptimized() override { return false; }
|
2015-06-30 07:51:55 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
(Re-landing) Expose a TargetMachine::getTargetTransformInfo function
Re-land r321234. It had to be reverted because it broke the shared
library build. The shared library build broke because there was a
missing LLVMBuild dependency from lib/Passes (which calls
TargetMachine::getTargetIRAnalysis) to lib/Target. As far as I can
tell, this problem was always there but was somehow masked
before (perhaps because TargetMachine::getTargetIRAnalysis was a
virtual function).
Original commit message:
This makes the TargetMachine interface a bit simpler. We still need
the std::function in TargetIRAnalysis to avoid having to add a
dependency from Analysis to Target.
See discussion:
http://lists.llvm.org/pipermail/llvm-dev/2017-December/119749.html
I avoided adding all of the backend owners to this review since the
change is simple, but let me know if you feel differently about this.
Reviewers: echristo, MatzeB, hfinkel
Reviewed By: hfinkel
Subscribers: jholewinski, jfb, arsenm, dschuff, mcrosier, sdardis, nemanjai, nhaehnle, javed.absar, sbc100, jgravelle-google, aheejin, kbarton, llvm-commits
Differential Revision: https://reviews.llvm.org/D41464
llvm-svn: 321375
2017-12-23 02:21:59 +08:00
|
|
|
TargetTransformInfo
|
|
|
|
WebAssemblyTargetMachine::getTargetTransformInfo(const Function &F) {
|
|
|
|
return TargetTransformInfo(WebAssemblyTTIImpl(this, F));
|
2015-06-30 07:51:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TargetPassConfig *
|
|
|
|
WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) {
|
2017-05-31 05:36:41 +08:00
|
|
|
return new WebAssemblyPassConfig(*this, PM);
|
2015-06-30 07:51:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
|
|
|
|
return nullptr; // No reg alloc
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// The following functions are called from lib/CodeGen/Passes.cpp to modify
|
|
|
|
// the CodeGen pass sequence.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void WebAssemblyPassConfig::addIRPasses() {
|
[WebAssembly] Merge used feature sets, update atomics linkage policy
Summary:
It does not currently make sense to use WebAssembly features in some functions
but not others, so this CL adds an IR pass that takes the union of all used
feature sets and applies it to each function in the module. This allows us to
prevent atomics from being lowered away if some function has opted in to using
them. When atomics is not enabled anywhere, we detect whether there exists any
atomic operations or thread local storage that would be stripped and disallow
linking with objects that contain atomics if and only if atomics or tls are
stripped. When atomics is enabled, mark it as used but do not require it of
other objects in the link. These changes allow libraries that do not use atomics
to be built once and linked into both single-threaded and multithreaded
binaries.
Reviewers: aheejin, sbc100, dschuff
Subscribers: jgravelle-google, hiraditya, sunfish, jfb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59625
llvm-svn: 357226
2019-03-29 08:14:01 +08:00
|
|
|
// Runs LowerAtomicPass if necessary
|
|
|
|
addPass(new CoalesceFeaturesAndStripAtomics(&getWebAssemblyTargetMachine()));
|
|
|
|
|
|
|
|
// This is a no-op if atomics are not used in the module
|
|
|
|
addPass(createAtomicExpandPass());
|
2015-06-30 07:51:55 +08:00
|
|
|
|
2018-07-11 12:29:36 +08:00
|
|
|
// Add signatures to prototype-less function declarations
|
|
|
|
addPass(createWebAssemblyAddMissingPrototypes());
|
|
|
|
|
2017-12-15 08:17:10 +08:00
|
|
|
// Lower .llvm.global_dtors into .llvm_global_ctors with __cxa_atexit calls.
|
|
|
|
addPass(createWebAssemblyLowerGlobalDtors());
|
|
|
|
|
2017-01-07 08:34:54 +08:00
|
|
|
// Fix function bitcasts, as WebAssembly requires caller and callee signatures
|
|
|
|
// to match.
|
|
|
|
addPass(createWebAssemblyFixFunctionBitcasts());
|
|
|
|
|
2015-11-26 00:55:01 +08:00
|
|
|
// Optimize "returned" function attributes.
|
2016-01-19 22:55:02 +08:00
|
|
|
if (getOptLevel() != CodeGenOpt::None)
|
|
|
|
addPass(createWebAssemblyOptimizeReturned());
|
2015-11-26 00:55:01 +08:00
|
|
|
|
2016-09-02 05:05:15 +08:00
|
|
|
// If exception handling is not enabled and setjmp/longjmp handling is
|
|
|
|
// enabled, we lower invokes into calls and delete unreachable landingpad
|
|
|
|
// blocks. Lowering invokes when there is no EH support is done in
|
|
|
|
// TargetPassConfig::addPassesToHandleExceptions, but this runs after this
|
|
|
|
// function and SjLj handling expects all invokes to be lowered before.
|
2018-02-24 08:40:50 +08:00
|
|
|
if (!EnableEmException &&
|
|
|
|
TM->Options.ExceptionModel == ExceptionHandling::None) {
|
2016-09-02 05:05:15 +08:00
|
|
|
addPass(createLowerInvokePass());
|
|
|
|
// The lower invoke pass may create unreachable code. Remove it in order not
|
|
|
|
// to process dead blocks in setjmp/longjmp handling.
|
|
|
|
addPass(createUnreachableBlockEliminationPass());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle exceptions and setjmp/longjmp if enabled.
|
2016-08-18 23:27:25 +08:00
|
|
|
if (EnableEmException || EnableEmSjLj)
|
|
|
|
addPass(createWebAssemblyLowerEmscriptenEHSjLj(EnableEmException,
|
|
|
|
EnableEmSjLj));
|
2016-08-02 05:34:04 +08:00
|
|
|
|
2019-07-04 07:54:06 +08:00
|
|
|
// Expand indirectbr instructions to switches.
|
|
|
|
addPass(createIndirectBrExpandPass());
|
|
|
|
|
2015-06-30 07:51:55 +08:00
|
|
|
TargetPassConfig::addIRPasses();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebAssemblyPassConfig::addInstSelector() {
|
2015-12-06 03:24:17 +08:00
|
|
|
(void)TargetPassConfig::addInstSelector();
|
2015-06-30 07:51:55 +08:00
|
|
|
addPass(
|
|
|
|
createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel()));
|
2015-12-10 00:23:59 +08:00
|
|
|
// Run the argument-move pass immediately after the ScheduleDAG scheduler
|
|
|
|
// so that we can fix up the ARGUMENT instructions before anything else
|
|
|
|
// sees them in the wrong place.
|
|
|
|
addPass(createWebAssemblyArgumentMove());
|
2016-01-26 11:39:31 +08:00
|
|
|
// Set the p2align operands. This information is present during ISel, however
|
|
|
|
// it's inconvenient to collect. Collect it now, and update the immediate
|
|
|
|
// operands.
|
|
|
|
addPass(createWebAssemblySetP2AlignOperands());
|
2015-06-30 07:51:55 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-01 01:53:38 +08:00
|
|
|
void WebAssemblyPassConfig::addPostRegAlloc() {
|
2015-11-26 02:13:18 +08:00
|
|
|
// TODO: The following CodeGen passes don't currently support code containing
|
|
|
|
// virtual registers. Consider removing their restrictions and re-enabling
|
|
|
|
// them.
|
2016-03-29 01:05:30 +08:00
|
|
|
|
2016-08-25 09:27:13 +08:00
|
|
|
// These functions all require the NoVRegs property.
|
2015-08-01 01:53:38 +08:00
|
|
|
disablePass(&MachineCopyPropagationID);
|
[CodeGen]Add NoVRegs property on PostRASink and ShrinkWrap
Summary:
This change declare that PostRAMachineSinking and ShrinkWrap require NoVRegs
property, so now the MachineFunctionPass can enforce this check.
These passes are disabled in NVPTX & WebAssembly.
Reviewers: dschuff, jlebar, tra, jgravelle-google, MatzeB, sebpop, thegameg, mcrosier
Reviewed By: dschuff, thegameg
Subscribers: jholewinski, jfb, sbc100, aheejin, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D45183
llvm-svn: 329095
2018-04-04 02:17:34 +08:00
|
|
|
disablePass(&PostRAMachineSinkingID);
|
2016-03-29 06:52:20 +08:00
|
|
|
disablePass(&PostRASchedulerID);
|
|
|
|
disablePass(&FuncletLayoutID);
|
|
|
|
disablePass(&StackMapLivenessID);
|
|
|
|
disablePass(&LiveDebugValuesID);
|
2016-04-19 14:24:58 +08:00
|
|
|
disablePass(&PatchableFunctionID);
|
[CodeGen]Add NoVRegs property on PostRASink and ShrinkWrap
Summary:
This change declare that PostRAMachineSinking and ShrinkWrap require NoVRegs
property, so now the MachineFunctionPass can enforce this check.
These passes are disabled in NVPTX & WebAssembly.
Reviewers: dschuff, jlebar, tra, jgravelle-google, MatzeB, sebpop, thegameg, mcrosier
Reviewed By: dschuff, thegameg
Subscribers: jholewinski, jfb, sbc100, aheejin, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D45183
llvm-svn: 329095
2018-04-04 02:17:34 +08:00
|
|
|
disablePass(&ShrinkWrapID);
|
2015-09-17 00:51:30 +08:00
|
|
|
|
2019-03-06 04:35:34 +08:00
|
|
|
// This pass hurts code size for wasm because it can generate irreducible
|
|
|
|
// control flow.
|
|
|
|
disablePass(&MachineBlockPlacementID);
|
|
|
|
|
2015-12-06 03:24:17 +08:00
|
|
|
TargetPassConfig::addPostRegAlloc();
|
2015-08-01 01:53:38 +08:00
|
|
|
}
|
2015-06-30 07:51:55 +08:00
|
|
|
|
2015-09-17 00:51:30 +08:00
|
|
|
void WebAssemblyPassConfig::addPreEmitPass() {
|
2015-12-06 03:24:17 +08:00
|
|
|
TargetPassConfig::addPreEmitPass();
|
2015-12-17 09:39:00 +08:00
|
|
|
|
2019-01-08 09:25:12 +08:00
|
|
|
// Eliminate multiple-entry loops.
|
|
|
|
addPass(createWebAssemblyFixIrreducibleControlFlow());
|
|
|
|
|
|
|
|
// Do various transformations for exception handling.
|
[WebAssembly] Exception handling: Switch to the new proposal
Summary:
This switches the EH implementation to the new proposal:
https://github.com/WebAssembly/exception-handling/blob/master/proposals/Exceptions.md
(The previous proposal was
https://github.com/WebAssembly/exception-handling/blob/master/proposals/old/Exceptions.md)
- Instruction changes
- Now we have one single `catch` instruction that returns a except_ref
value
- `throw` now can take variable number of operations
- `rethrow` does not have 'depth' argument anymore
- `br_on_exn` queries an except_ref to see if it matches the tag and
branches to the given label if true.
- `extract_exception` is a pseudo instruction that simulates popping
values from wasm stack. This is to make `br_on_exn`, a very special
instruction, work: `br_on_exn` puts values onto the stack only if it
is taken, and the # of values can vay depending on the tag.
- Now there's only one `catch` per `try`, this patch removes all special
handling for terminate pad with a call to `__clang_call_terminate`.
Before it was the only case there are two catch clauses (a normal
`catch` and `catch_all` per `try`).
- Make `rethrow` act as a terminator like `throw`. This splits BB after
`rethrow` in WasmEHPrepare, and deletes an unnecessary `unreachable`
after `rethrow` in LateEHPrepare.
- Now we stop at all catchpads (because we add wasm `catch` instruction
that catches all exceptions), this creates new
`findWasmUnwindDestinations` function in SelectionDAGBuilder.
- Now we use `br_on_exn` instrution to figure out if an except_ref
matches the current tag or not, LateEHPrepare generates this sequence
for catch pads:
```
catch
block i32
br_on_exn $__cpp_exception
end_block
extract_exception
```
- Branch analysis for `br_on_exn` in WebAssemblyInstrInfo
- Other various misc. changes to switch to the new proposal.
Reviewers: dschuff
Subscribers: sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D57134
llvm-svn: 352598
2019-01-30 11:21:57 +08:00
|
|
|
// Every CFG-changing optimizations should come before this.
|
2019-01-08 09:25:12 +08:00
|
|
|
addPass(createWebAssemblyLateEHPrepare());
|
|
|
|
|
2019-01-31 06:44:45 +08:00
|
|
|
// Now that we have a prologue and epilogue and all frame indices are
|
|
|
|
// rewritten, eliminate SP and FP. This allows them to be stackified,
|
|
|
|
// colored, and numbered with the rest of the registers.
|
|
|
|
addPass(createWebAssemblyReplacePhysRegs());
|
|
|
|
|
[WebAssembly] Exception handling: Switch to the new proposal
Summary:
This switches the EH implementation to the new proposal:
https://github.com/WebAssembly/exception-handling/blob/master/proposals/Exceptions.md
(The previous proposal was
https://github.com/WebAssembly/exception-handling/blob/master/proposals/old/Exceptions.md)
- Instruction changes
- Now we have one single `catch` instruction that returns a except_ref
value
- `throw` now can take variable number of operations
- `rethrow` does not have 'depth' argument anymore
- `br_on_exn` queries an except_ref to see if it matches the tag and
branches to the given label if true.
- `extract_exception` is a pseudo instruction that simulates popping
values from wasm stack. This is to make `br_on_exn`, a very special
instruction, work: `br_on_exn` puts values onto the stack only if it
is taken, and the # of values can vay depending on the tag.
- Now there's only one `catch` per `try`, this patch removes all special
handling for terminate pad with a call to `__clang_call_terminate`.
Before it was the only case there are two catch clauses (a normal
`catch` and `catch_all` per `try`).
- Make `rethrow` act as a terminator like `throw`. This splits BB after
`rethrow` in WasmEHPrepare, and deletes an unnecessary `unreachable`
after `rethrow` in LateEHPrepare.
- Now we stop at all catchpads (because we add wasm `catch` instruction
that catches all exceptions), this creates new
`findWasmUnwindDestinations` function in SelectionDAGBuilder.
- Now we use `br_on_exn` instrution to figure out if an except_ref
matches the current tag or not, LateEHPrepare generates this sequence
for catch pads:
```
catch
block i32
br_on_exn $__cpp_exception
end_block
extract_exception
```
- Branch analysis for `br_on_exn` in WebAssemblyInstrInfo
- Other various misc. changes to switch to the new proposal.
Reviewers: dschuff
Subscribers: sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D57134
llvm-svn: 352598
2019-01-30 11:21:57 +08:00
|
|
|
// Preparations and optimizations related to register stackification.
|
2016-05-10 12:24:02 +08:00
|
|
|
if (getOptLevel() != CodeGenOpt::None) {
|
|
|
|
// LiveIntervals isn't commonly run this late. Re-establish preconditions.
|
|
|
|
addPass(createWebAssemblyPrepareForLiveIntervals());
|
|
|
|
|
|
|
|
// Depend on LiveIntervals and perform some optimizations on it.
|
|
|
|
addPass(createWebAssemblyOptimizeLiveIntervals());
|
|
|
|
|
2019-01-09 06:35:18 +08:00
|
|
|
// Prepare memory intrinsic calls for register stackifying.
|
|
|
|
addPass(createWebAssemblyMemIntrinsicResults());
|
2016-05-10 12:24:02 +08:00
|
|
|
|
2016-10-04 06:43:53 +08:00
|
|
|
// Mark registers as representing wasm's value stack. This is a key
|
2016-05-10 12:24:02 +08:00
|
|
|
// code-compression technique in WebAssembly. We run this pass (and
|
2019-01-09 06:35:18 +08:00
|
|
|
// MemIntrinsicResults above) very late, so that it sees as much code as
|
|
|
|
// possible, including code emitted by PEI and expanded by late tail
|
|
|
|
// duplication.
|
2016-05-10 12:24:02 +08:00
|
|
|
addPass(createWebAssemblyRegStackify());
|
|
|
|
|
|
|
|
// Run the register coloring pass to reduce the total number of registers.
|
|
|
|
// This runs after stackification so that it doesn't consider registers
|
|
|
|
// that become stackified.
|
|
|
|
addPass(createWebAssemblyRegColoring());
|
|
|
|
}
|
|
|
|
|
2017-02-28 06:38:58 +08:00
|
|
|
// Sort the blocks of the CFG into topological order, a prerequisite for
|
|
|
|
// BLOCK and LOOP markers.
|
|
|
|
addPass(createWebAssemblyCFGSort());
|
|
|
|
|
|
|
|
// Insert BLOCK and LOOP markers.
|
2015-09-17 00:51:30 +08:00
|
|
|
addPass(createWebAssemblyCFGStackify());
|
2015-11-26 05:32:06 +08:00
|
|
|
|
2019-03-30 17:29:57 +08:00
|
|
|
// Insert explicit local.get and local.set operators.
|
2020-05-01 02:41:39 +08:00
|
|
|
if (!WasmDisableExplicitLocals)
|
|
|
|
addPass(createWebAssemblyExplicitLocals());
|
2019-03-30 17:29:57 +08:00
|
|
|
|
2015-12-05 11:03:35 +08:00
|
|
|
// Lower br_unless into br_if.
|
|
|
|
addPass(createWebAssemblyLowerBrUnless());
|
|
|
|
|
2015-11-26 05:32:06 +08:00
|
|
|
// Perform the very last peephole optimizations on the code.
|
2016-01-19 22:55:02 +08:00
|
|
|
if (getOptLevel() != CodeGenOpt::None)
|
|
|
|
addPass(createWebAssemblyPeephole());
|
2016-05-21 08:21:56 +08:00
|
|
|
|
|
|
|
// Create a mapping from LLVM CodeGen virtual registers to wasm registers.
|
|
|
|
addPass(createWebAssemblyRegNumbering());
|
2020-05-01 02:41:39 +08:00
|
|
|
|
|
|
|
// Fix debug_values whose defs have been stackified.
|
|
|
|
if (!WasmDisableExplicitLocals)
|
|
|
|
addPass(createWebAssemblyDebugFixup());
|
2015-09-17 00:51:30 +08:00
|
|
|
}
|
2019-03-27 01:35:35 +08:00
|
|
|
|
|
|
|
yaml::MachineFunctionInfo *
|
|
|
|
WebAssemblyTargetMachine::createDefaultFuncInfoYAML() const {
|
|
|
|
return new yaml::WebAssemblyFunctionInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
yaml::MachineFunctionInfo *WebAssemblyTargetMachine::convertFuncInfoToYAML(
|
|
|
|
const MachineFunction &MF) const {
|
|
|
|
const auto *MFI = MF.getInfo<WebAssemblyFunctionInfo>();
|
|
|
|
return new yaml::WebAssemblyFunctionInfo(*MFI);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebAssemblyTargetMachine::parseMachineFunctionInfo(
|
|
|
|
const yaml::MachineFunctionInfo &MFI, PerFunctionMIParsingState &PFS,
|
|
|
|
SMDiagnostic &Error, SMRange &SourceRange) const {
|
|
|
|
const auto &YamlMFI =
|
|
|
|
reinterpret_cast<const yaml::WebAssemblyFunctionInfo &>(MFI);
|
|
|
|
MachineFunction &MF = PFS.MF;
|
|
|
|
MF.getInfo<WebAssemblyFunctionInfo>()->initializeBaseYamlFields(YamlMFI);
|
|
|
|
return false;
|
|
|
|
}
|