================ @@ -0,0 +1,234 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "WebAssembly.h" +#include "WebAssemblyTargetMachine.h" +#include "llvm/CodeGen/AtomicExpand.h" +#include "llvm/CodeGen/IndirectBrExpand.h" +#include "llvm/CodeGen/MachineBlockPlacement.h" +#include "llvm/CodeGen/MachineCopyPropagation.h" +#include "llvm/CodeGen/MachineLateInstrsCleanup.h" +#include "llvm/CodeGen/PatchableFunction.h" +#include "llvm/CodeGen/PostRAMachineSink.h" +#include "llvm/CodeGen/PostRASchedulerList.h" +#include "llvm/CodeGen/RegisterCoalescerPass.h" +#include "llvm/CodeGen/RemoveLoadsIntoFakeUses.h" +#include "llvm/CodeGen/ShrinkWrap.h" +#include "llvm/CodeGen/UnreachableBlockElim.h" +#include "llvm/IR/PassInstrumentation.h" +#include "llvm/MC/MCStreamer.h" +#include "llvm/Passes/CodeGenPassBuilder.h" +#include "llvm/Passes/PassBuilder.h" +#include "llvm/Target/CGPassBuilderOption.h" +#include "llvm/Transforms/Utils/LowerGlobalDtors.h" +#include "llvm/Transforms/Utils/LowerInvoke.h" + +using namespace llvm; + +namespace WebAssembly { +extern cl::opt<bool> WasmDisableExplicitLocals; +extern cl::opt<bool> WasmEnableEH; +extern cl::opt<bool> WasmEnableEmEH; +extern cl::opt<bool> WasmEnableEmSjLj; +extern cl::opt<bool> WasmEnableSjLj; +} // namespace WebAssembly + +using llvm::WebAssembly::WasmDisableExplicitLocals; +using llvm::WebAssembly::WasmEnableEH; +using llvm::WebAssembly::WasmEnableEmEH; +using llvm::WebAssembly::WasmEnableEmSjLj; +using llvm::WebAssembly::WasmEnableSjLj; + +namespace { + +class WebAssemblyCodeGenPassBuilder + : public CodeGenPassBuilder<WebAssemblyCodeGenPassBuilder, + WebAssemblyTargetMachine> { + using Base = CodeGenPassBuilder<WebAssemblyCodeGenPassBuilder, + WebAssemblyTargetMachine>; + +public: + explicit WebAssemblyCodeGenPassBuilder(WebAssemblyTargetMachine &TM, + const CGPassBuilderOption &Opts, + PassInstrumentationCallbacks *PIC) + : CodeGenPassBuilder(TM, Opts, PIC) { + disablePass<RegisterCoalescerPass, MachineLateInstrsCleanupPass, + MachineCopyPropagationPass, PostRAMachineSinkingPass, + PostRASchedulerPass, FuncletLayoutPass, StackMapLivenessPass, + PatchableFunctionPass, ShrinkWrapPass, + RemoveLoadsIntoFakeUsesPass, MachineBlockPlacementPass>(); + } + + void addIRPasses(PassManagerWrapper &PMW) const; + void addISelPrepare(PassManagerWrapper &PMW) const; + Error addInstSelector(PassManagerWrapper &PMW) const; + void addPreEmitPass(PassManagerWrapper &PMW) const; +}; + +void WebAssemblyCodeGenPassBuilder::addIRPasses(PassManagerWrapper &PMW) const { + // Add signatures to prototype-less function declarations + // TODO(boomanaiden154): WebAssemblyAddMissingPrototypes + + // Lower .llvm.global_dtors into .llvm.global_ctors with __cxa_atexit calls. + flushFPMsToMPM(PMW); + addModulePass(LowerGlobalDtorsPass(), PMW); + + // Fix function bitcasts, as WebAssembly requires caller and callee signatures + // to match. + // TODO(boomanaiden154): WebAssemblyFixFunctionBitcasts + + // Optimize "returned" function attributes. + if (getOptLevel() != CodeGenOptLevel::None) { + // TODO(boomanaiden154): WebAssemblyOptimizeReturned + } + + // 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 that runs after these IR + // passes and Emscripten SjLj handling expects all invokes to be lowered + // before. + if (!WasmEnableEmEH && !WasmEnableEH) { + addFunctionPass(LowerInvokePass(), PMW); + // The lower invoke pass may create unreachable code. Remove it in order not + // to process dead blocks in setjmp/longjmp handling. + addFunctionPass(UnreachableBlockElimPass(), PMW); + } + + // Handle exceptions and setjmp/longjmp if enabled. Unlike Wasm EH preparation + // done in WasmEHPrepare pass, Wasm SjLj preparation shares libraries and + // transformation algorithms with Emscripten SjLj, so we run + // LowerEmscriptenEHSjLj pass also when Wasm SjLj is enabled. + if (WasmEnableEmEH || WasmEnableEmSjLj || WasmEnableSjLj) { + // TODO(boomanaiden154): WebAssemblyLowerEmscriptenEHSjLj + } + + // Expand indirectbr instructions to switches. + addFunctionPass(IndirectBrExpandPass(TM), PMW); + + // Try to expand `vecreduce_{and, or}` into `{any, all}_true`. + // TODO(boomanaiden154): WebAssemblyReduceToAnyAllTrue + + Base::addIRPasses(PMW); +} + +void WebAssemblyCodeGenPassBuilder::addISelPrepare( + PassManagerWrapper &PMW) const { + // We need to move reference type allocas to WASM_ADDRESS_SPACE_VAR so that + // loads and stores are promoted to local.gets/local.sets. + // TODO(boomanaiden154): WebAssemblyRefTypeMem2Local + // Lower atomics and TLS if necessary + // TODO(boomanaiden154): CoalesceFeaturesAndStripAtomics + + // This is a no-op if atomics are not used in the module + addFunctionPass(AtomicExpandPass(TM), PMW); + + Base::addISelPrepare(PMW); +} + +Error WebAssemblyCodeGenPassBuilder::addInstSelector( + PassManagerWrapper &PMW) const { + addMachineFunctionPass(WebAssemblyISelDAGToDAGPass(TM, getOptLevel()), PMW); ---------------- dschuff wrote:
There are a few passes in the [existing](https://github.com/llvm/llvm-project/blob/d76f677c075b71e2a5c2ee35539992d753258e2a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp#L551) config that should go here. https://github.com/llvm/llvm-project/pull/208983 _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
