[ORC] Add std::tuple support to SimplePackedSerialization.
[llvm-project.git] / llvm / lib / Target / WebAssembly / WebAssemblyTargetMachine.cpp
blobf98e9bf002691a8de8fa1f2f9c3d0377e7af6450
1 //===- WebAssemblyTargetMachine.cpp - Define TargetMachine for WebAssembly -==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file defines the WebAssembly-specific subclass of TargetMachine.
11 ///
12 //===----------------------------------------------------------------------===//
14 #include "WebAssemblyTargetMachine.h"
15 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
16 #include "TargetInfo/WebAssemblyTargetInfo.h"
17 #include "WebAssembly.h"
18 #include "WebAssemblyMachineFunctionInfo.h"
19 #include "WebAssemblyTargetObjectFile.h"
20 #include "WebAssemblyTargetTransformInfo.h"
21 #include "llvm/CodeGen/MIRParser/MIParser.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/CodeGen/RegAllocRegistry.h"
25 #include "llvm/CodeGen/TargetPassConfig.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/Support/TargetRegistry.h"
28 #include "llvm/Target/TargetOptions.h"
29 #include "llvm/Transforms/Scalar.h"
30 #include "llvm/Transforms/Scalar/LowerAtomic.h"
31 #include "llvm/Transforms/Utils.h"
32 using namespace llvm;
34 #define DEBUG_TYPE "wasm"
36 // Emscripten's asm.js-style exception handling
37 cl::opt<bool> WasmEnableEmException(
38 "enable-emscripten-cxx-exceptions",
39 cl::desc("WebAssembly Emscripten-style exception handling"),
40 cl::init(false));
42 // Emscripten's asm.js-style setjmp/longjmp handling
43 cl::opt<bool> WasmEnableEmSjLj(
44 "enable-emscripten-sjlj",
45 cl::desc("WebAssembly Emscripten-style setjmp/longjmp handling"),
46 cl::init(false));
48 // A command-line option to keep implicit locals
49 // for the purpose of testing with lit/llc ONLY.
50 // This produces output which is not valid WebAssembly, and is not supported
51 // by assemblers/disassemblers and other MC based tools.
52 static cl::opt<bool> WasmDisableExplicitLocals(
53 "wasm-disable-explicit-locals", cl::Hidden,
54 cl::desc("WebAssembly: output implicit locals in"
55 " instruction output for test purposes only."),
56 cl::init(false));
58 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyTarget() {
59 // Register the target.
60 RegisterTargetMachine<WebAssemblyTargetMachine> X(
61 getTheWebAssemblyTarget32());
62 RegisterTargetMachine<WebAssemblyTargetMachine> Y(
63 getTheWebAssemblyTarget64());
65 // Register backend passes
66 auto &PR = *PassRegistry::getPassRegistry();
67 initializeWebAssemblyAddMissingPrototypesPass(PR);
68 initializeWebAssemblyLowerEmscriptenEHSjLjPass(PR);
69 initializeLowerGlobalDtorsPass(PR);
70 initializeFixFunctionBitcastsPass(PR);
71 initializeOptimizeReturnedPass(PR);
72 initializeWebAssemblyArgumentMovePass(PR);
73 initializeWebAssemblySetP2AlignOperandsPass(PR);
74 initializeWebAssemblyReplacePhysRegsPass(PR);
75 initializeWebAssemblyPrepareForLiveIntervalsPass(PR);
76 initializeWebAssemblyOptimizeLiveIntervalsPass(PR);
77 initializeWebAssemblyMemIntrinsicResultsPass(PR);
78 initializeWebAssemblyRegStackifyPass(PR);
79 initializeWebAssemblyRegColoringPass(PR);
80 initializeWebAssemblyNullifyDebugValueListsPass(PR);
81 initializeWebAssemblyFixIrreducibleControlFlowPass(PR);
82 initializeWebAssemblyLateEHPreparePass(PR);
83 initializeWebAssemblyExceptionInfoPass(PR);
84 initializeWebAssemblyCFGSortPass(PR);
85 initializeWebAssemblyCFGStackifyPass(PR);
86 initializeWebAssemblyExplicitLocalsPass(PR);
87 initializeWebAssemblyLowerBrUnlessPass(PR);
88 initializeWebAssemblyRegNumberingPass(PR);
89 initializeWebAssemblyDebugFixupPass(PR);
90 initializeWebAssemblyPeepholePass(PR);
91 initializeWebAssemblyMCLowerPrePassPass(PR);
94 //===----------------------------------------------------------------------===//
95 // WebAssembly Lowering public interface.
96 //===----------------------------------------------------------------------===//
98 static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM,
99 const Triple &TT) {
100 if (!RM.hasValue()) {
101 // Default to static relocation model. This should always be more optimial
102 // than PIC since the static linker can determine all global addresses and
103 // assume direct function calls.
104 return Reloc::Static;
107 if (!TT.isOSEmscripten()) {
108 // Relocation modes other than static are currently implemented in a way
109 // that only works for Emscripten, so disable them if we aren't targeting
110 // Emscripten.
111 return Reloc::Static;
114 return *RM;
117 /// Create an WebAssembly architecture model.
119 WebAssemblyTargetMachine::WebAssemblyTargetMachine(
120 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
121 const TargetOptions &Options, Optional<Reloc::Model> RM,
122 Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
123 : LLVMTargetMachine(
125 TT.isArch64Bit()
126 ? (TT.isOSEmscripten()
127 ? "e-m:e-p:64:64-i64:64-f128:64-n32:64-S128-ni:1:10:20"
128 : "e-m:e-p:64:64-i64:64-n32:64-S128-ni:1:10:20")
129 : (TT.isOSEmscripten()
130 ? "e-m:e-p:32:32-i64:64-f128:64-n32:64-S128-ni:1:10:20"
131 : "e-m:e-p:32:32-i64:64-n32:64-S128-ni:1:10:20"),
132 TT, CPU, FS, Options, getEffectiveRelocModel(RM, TT),
133 getEffectiveCodeModel(CM, CodeModel::Large), OL),
134 TLOF(new WebAssemblyTargetObjectFile()) {
135 // WebAssembly type-checks instructions, but a noreturn function with a return
136 // type that doesn't match the context will cause a check failure. So we lower
137 // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's
138 // 'unreachable' instructions which is meant for that case.
139 this->Options.TrapUnreachable = true;
141 // WebAssembly treats each function as an independent unit. Force
142 // -ffunction-sections, effectively, so that we can emit them independently.
143 this->Options.FunctionSections = true;
144 this->Options.DataSections = true;
145 this->Options.UniqueSectionNames = true;
147 initAsmInfo();
149 // Note that we don't use setRequiresStructuredCFG(true). It disables
150 // optimizations than we're ok with, and want, such as critical edge
151 // splitting and tail merging.
154 WebAssemblyTargetMachine::~WebAssemblyTargetMachine() = default; // anchor.
156 const WebAssemblySubtarget *WebAssemblyTargetMachine::getSubtargetImpl() const {
157 return getSubtargetImpl(std::string(getTargetCPU()),
158 std::string(getTargetFeatureString()));
161 const WebAssemblySubtarget *
162 WebAssemblyTargetMachine::getSubtargetImpl(std::string CPU,
163 std::string FS) const {
164 auto &I = SubtargetMap[CPU + FS];
165 if (!I) {
166 I = std::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
168 return I.get();
171 const WebAssemblySubtarget *
172 WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
173 Attribute CPUAttr = F.getFnAttribute("target-cpu");
174 Attribute FSAttr = F.getFnAttribute("target-features");
176 std::string CPU =
177 CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
178 std::string FS =
179 FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
181 // This needs to be done before we create a new subtarget since any
182 // creation will depend on the TM and the code generation flags on the
183 // function that reside in TargetOptions.
184 resetTargetOptions(F);
186 return getSubtargetImpl(CPU, FS);
189 namespace {
191 class CoalesceFeaturesAndStripAtomics final : public ModulePass {
192 // Take the union of all features used in the module and use it for each
193 // function individually, since having multiple feature sets in one module
194 // currently does not make sense for WebAssembly. If atomics are not enabled,
195 // also strip atomic operations and thread local storage.
196 static char ID;
197 WebAssemblyTargetMachine *WasmTM;
199 public:
200 CoalesceFeaturesAndStripAtomics(WebAssemblyTargetMachine *WasmTM)
201 : ModulePass(ID), WasmTM(WasmTM) {}
203 bool runOnModule(Module &M) override {
204 FeatureBitset Features = coalesceFeatures(M);
206 std::string FeatureStr = getFeatureString(Features);
207 WasmTM->setTargetFeatureString(FeatureStr);
208 for (auto &F : M)
209 replaceFeatures(F, FeatureStr);
211 bool StrippedAtomics = false;
212 bool StrippedTLS = false;
214 if (!Features[WebAssembly::FeatureAtomics])
215 StrippedAtomics = stripAtomics(M);
217 if (!Features[WebAssembly::FeatureBulkMemory])
218 StrippedTLS = stripThreadLocals(M);
220 if (StrippedAtomics && !StrippedTLS)
221 stripThreadLocals(M);
222 else if (StrippedTLS && !StrippedAtomics)
223 stripAtomics(M);
225 recordFeatures(M, Features, StrippedAtomics || StrippedTLS);
227 // Conservatively assume we have made some change
228 return true;
231 private:
232 FeatureBitset coalesceFeatures(const Module &M) {
233 FeatureBitset Features =
234 WasmTM
235 ->getSubtargetImpl(std::string(WasmTM->getTargetCPU()),
236 std::string(WasmTM->getTargetFeatureString()))
237 ->getFeatureBits();
238 for (auto &F : M)
239 Features |= WasmTM->getSubtargetImpl(F)->getFeatureBits();
240 return Features;
243 std::string getFeatureString(const FeatureBitset &Features) {
244 std::string Ret;
245 for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) {
246 if (Features[KV.Value])
247 Ret += (StringRef("+") + KV.Key + ",").str();
249 return Ret;
252 void replaceFeatures(Function &F, const std::string &Features) {
253 F.removeFnAttr("target-features");
254 F.removeFnAttr("target-cpu");
255 F.addFnAttr("target-features", Features);
258 bool stripAtomics(Module &M) {
259 // Detect whether any atomics will be lowered, since there is no way to tell
260 // whether the LowerAtomic pass lowers e.g. stores.
261 bool Stripped = false;
262 for (auto &F : M) {
263 for (auto &B : F) {
264 for (auto &I : B) {
265 if (I.isAtomic()) {
266 Stripped = true;
267 goto done;
273 done:
274 if (!Stripped)
275 return false;
277 LowerAtomicPass Lowerer;
278 FunctionAnalysisManager FAM;
279 for (auto &F : M)
280 Lowerer.run(F, FAM);
282 return true;
285 bool stripThreadLocals(Module &M) {
286 bool Stripped = false;
287 for (auto &GV : M.globals()) {
288 if (GV.isThreadLocal()) {
289 Stripped = true;
290 GV.setThreadLocal(false);
293 return Stripped;
296 void recordFeatures(Module &M, const FeatureBitset &Features, bool Stripped) {
297 for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) {
298 if (Features[KV.Value]) {
299 // Mark features as used
300 std::string MDKey = (StringRef("wasm-feature-") + KV.Key).str();
301 M.addModuleFlag(Module::ModFlagBehavior::Error, MDKey,
302 wasm::WASM_FEATURE_PREFIX_USED);
305 // Code compiled without atomics or bulk-memory may have had its atomics or
306 // thread-local data lowered to nonatomic operations or non-thread-local
307 // data. In that case, we mark the pseudo-feature "shared-mem" as disallowed
308 // to tell the linker that it would be unsafe to allow this code ot be used
309 // in a module with shared memory.
310 if (Stripped) {
311 M.addModuleFlag(Module::ModFlagBehavior::Error, "wasm-feature-shared-mem",
312 wasm::WASM_FEATURE_PREFIX_DISALLOWED);
316 char CoalesceFeaturesAndStripAtomics::ID = 0;
318 /// WebAssembly Code Generator Pass Configuration Options.
319 class WebAssemblyPassConfig final : public TargetPassConfig {
320 public:
321 WebAssemblyPassConfig(WebAssemblyTargetMachine &TM, PassManagerBase &PM)
322 : TargetPassConfig(TM, PM) {}
324 WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const {
325 return getTM<WebAssemblyTargetMachine>();
328 FunctionPass *createTargetRegisterAllocator(bool) override;
330 void addIRPasses() override;
331 bool addInstSelector() override;
332 void addPostRegAlloc() override;
333 bool addGCPasses() override { return false; }
334 void addPreEmitPass() override;
335 bool addPreISel() override;
337 // No reg alloc
338 bool addRegAssignAndRewriteFast() override { return false; }
340 // No reg alloc
341 bool addRegAssignAndRewriteOptimized() override { return false; }
343 } // end anonymous namespace
345 TargetTransformInfo
346 WebAssemblyTargetMachine::getTargetTransformInfo(const Function &F) {
347 return TargetTransformInfo(WebAssemblyTTIImpl(this, F));
350 TargetPassConfig *
351 WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) {
352 return new WebAssemblyPassConfig(*this, PM);
355 FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
356 return nullptr; // No reg alloc
359 //===----------------------------------------------------------------------===//
360 // The following functions are called from lib/CodeGen/Passes.cpp to modify
361 // the CodeGen pass sequence.
362 //===----------------------------------------------------------------------===//
364 void WebAssemblyPassConfig::addIRPasses() {
365 // Lower atomics and TLS if necessary
366 addPass(new CoalesceFeaturesAndStripAtomics(&getWebAssemblyTargetMachine()));
368 // This is a no-op if atomics are not used in the module
369 addPass(createAtomicExpandPass());
371 // Add signatures to prototype-less function declarations
372 addPass(createWebAssemblyAddMissingPrototypes());
374 // Lower .llvm.global_dtors into .llvm_global_ctors with __cxa_atexit calls.
375 addPass(createWebAssemblyLowerGlobalDtors());
377 // Fix function bitcasts, as WebAssembly requires caller and callee signatures
378 // to match.
379 addPass(createWebAssemblyFixFunctionBitcasts());
381 // Optimize "returned" function attributes.
382 if (getOptLevel() != CodeGenOpt::None)
383 addPass(createWebAssemblyOptimizeReturned());
385 // If exception handling is not enabled and setjmp/longjmp handling is
386 // enabled, we lower invokes into calls and delete unreachable landingpad
387 // blocks. Lowering invokes when there is no EH support is done in
388 // TargetPassConfig::addPassesToHandleExceptions, but this runs after this
389 // function and SjLj handling expects all invokes to be lowered before.
390 if (!WasmEnableEmException &&
391 TM->Options.ExceptionModel == ExceptionHandling::None) {
392 addPass(createLowerInvokePass());
393 // The lower invoke pass may create unreachable code. Remove it in order not
394 // to process dead blocks in setjmp/longjmp handling.
395 addPass(createUnreachableBlockEliminationPass());
398 // Handle exceptions and setjmp/longjmp if enabled.
399 if (WasmEnableEmException || WasmEnableEmSjLj)
400 addPass(createWebAssemblyLowerEmscriptenEHSjLj(WasmEnableEmException,
401 WasmEnableEmSjLj));
403 // Expand indirectbr instructions to switches.
404 addPass(createIndirectBrExpandPass());
406 TargetPassConfig::addIRPasses();
409 bool WebAssemblyPassConfig::addInstSelector() {
410 (void)TargetPassConfig::addInstSelector();
411 addPass(
412 createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel()));
413 // Run the argument-move pass immediately after the ScheduleDAG scheduler
414 // so that we can fix up the ARGUMENT instructions before anything else
415 // sees them in the wrong place.
416 addPass(createWebAssemblyArgumentMove());
417 // Set the p2align operands. This information is present during ISel, however
418 // it's inconvenient to collect. Collect it now, and update the immediate
419 // operands.
420 addPass(createWebAssemblySetP2AlignOperands());
422 // Eliminate range checks and add default targets to br_table instructions.
423 addPass(createWebAssemblyFixBrTableDefaults());
425 return false;
428 void WebAssemblyPassConfig::addPostRegAlloc() {
429 // TODO: The following CodeGen passes don't currently support code containing
430 // virtual registers. Consider removing their restrictions and re-enabling
431 // them.
433 // These functions all require the NoVRegs property.
434 disablePass(&MachineCopyPropagationID);
435 disablePass(&PostRAMachineSinkingID);
436 disablePass(&PostRASchedulerID);
437 disablePass(&FuncletLayoutID);
438 disablePass(&StackMapLivenessID);
439 disablePass(&LiveDebugValuesID);
440 disablePass(&PatchableFunctionID);
441 disablePass(&ShrinkWrapID);
443 // This pass hurts code size for wasm because it can generate irreducible
444 // control flow.
445 disablePass(&MachineBlockPlacementID);
447 TargetPassConfig::addPostRegAlloc();
450 void WebAssemblyPassConfig::addPreEmitPass() {
451 TargetPassConfig::addPreEmitPass();
453 // Nullify DBG_VALUE_LISTs that we cannot handle.
454 addPass(createWebAssemblyNullifyDebugValueLists());
456 // Eliminate multiple-entry loops.
457 addPass(createWebAssemblyFixIrreducibleControlFlow());
459 // Do various transformations for exception handling.
460 // Every CFG-changing optimizations should come before this.
461 if (TM->Options.ExceptionModel == ExceptionHandling::Wasm)
462 addPass(createWebAssemblyLateEHPrepare());
464 // Now that we have a prologue and epilogue and all frame indices are
465 // rewritten, eliminate SP and FP. This allows them to be stackified,
466 // colored, and numbered with the rest of the registers.
467 addPass(createWebAssemblyReplacePhysRegs());
469 // Preparations and optimizations related to register stackification.
470 if (getOptLevel() != CodeGenOpt::None) {
471 // LiveIntervals isn't commonly run this late. Re-establish preconditions.
472 addPass(createWebAssemblyPrepareForLiveIntervals());
474 // Depend on LiveIntervals and perform some optimizations on it.
475 addPass(createWebAssemblyOptimizeLiveIntervals());
477 // Prepare memory intrinsic calls for register stackifying.
478 addPass(createWebAssemblyMemIntrinsicResults());
480 // Mark registers as representing wasm's value stack. This is a key
481 // code-compression technique in WebAssembly. We run this pass (and
482 // MemIntrinsicResults above) very late, so that it sees as much code as
483 // possible, including code emitted by PEI and expanded by late tail
484 // duplication.
485 addPass(createWebAssemblyRegStackify());
487 // Run the register coloring pass to reduce the total number of registers.
488 // This runs after stackification so that it doesn't consider registers
489 // that become stackified.
490 addPass(createWebAssemblyRegColoring());
493 // Sort the blocks of the CFG into topological order, a prerequisite for
494 // BLOCK and LOOP markers.
495 addPass(createWebAssemblyCFGSort());
497 // Insert BLOCK and LOOP markers.
498 addPass(createWebAssemblyCFGStackify());
500 // Insert explicit local.get and local.set operators.
501 if (!WasmDisableExplicitLocals)
502 addPass(createWebAssemblyExplicitLocals());
504 // Lower br_unless into br_if.
505 addPass(createWebAssemblyLowerBrUnless());
507 // Perform the very last peephole optimizations on the code.
508 if (getOptLevel() != CodeGenOpt::None)
509 addPass(createWebAssemblyPeephole());
511 // Create a mapping from LLVM CodeGen virtual registers to wasm registers.
512 addPass(createWebAssemblyRegNumbering());
514 // Fix debug_values whose defs have been stackified.
515 if (!WasmDisableExplicitLocals)
516 addPass(createWebAssemblyDebugFixup());
518 // Collect information to prepare for MC lowering / asm printing.
519 addPass(createWebAssemblyMCLowerPrePass());
522 bool WebAssemblyPassConfig::addPreISel() {
523 TargetPassConfig::addPreISel();
524 addPass(createWebAssemblyLowerRefTypesIntPtrConv());
525 return false;
528 yaml::MachineFunctionInfo *
529 WebAssemblyTargetMachine::createDefaultFuncInfoYAML() const {
530 return new yaml::WebAssemblyFunctionInfo();
533 yaml::MachineFunctionInfo *WebAssemblyTargetMachine::convertFuncInfoToYAML(
534 const MachineFunction &MF) const {
535 const auto *MFI = MF.getInfo<WebAssemblyFunctionInfo>();
536 return new yaml::WebAssemblyFunctionInfo(*MFI);
539 bool WebAssemblyTargetMachine::parseMachineFunctionInfo(
540 const yaml::MachineFunctionInfo &MFI, PerFunctionMIParsingState &PFS,
541 SMDiagnostic &Error, SMRange &SourceRange) const {
542 const auto &YamlMFI =
543 reinterpret_cast<const yaml::WebAssemblyFunctionInfo &>(MFI);
544 MachineFunction &MF = PFS.MF;
545 MF.getInfo<WebAssemblyFunctionInfo>()->initializeBaseYamlFields(YamlMFI);
546 return false;