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 //===----------------------------------------------------------------------===// 13 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; 33 34 #define DEBUG_TYPE "wasm" 35 36 // Emscripten's asm.js-style exception handling 37 cl::opt<bool> EnableEmException( 38 "enable-emscripten-cxx-exceptions", 39 cl::desc("WebAssembly Emscripten-style exception handling"), 40 cl::init(false)); 41 42 // Emscripten's asm.js-style setjmp/longjmp handling 43 cl::opt<bool> EnableEmSjLj( 44 "enable-emscripten-sjlj", 45 cl::desc("WebAssembly Emscripten-style setjmp/longjmp handling"), 46 cl::init(false)); 47 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)); 57 58 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyTarget() { 59 // Register the target. 60 RegisterTargetMachine<WebAssemblyTargetMachine> X( 61 getTheWebAssemblyTarget32()); 62 RegisterTargetMachine<WebAssemblyTargetMachine> Y( 63 getTheWebAssemblyTarget64()); 64 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 initializeWebAssemblyFixIrreducibleControlFlowPass(PR); 81 initializeWebAssemblyLateEHPreparePass(PR); 82 initializeWebAssemblyExceptionInfoPass(PR); 83 initializeWebAssemblyCFGSortPass(PR); 84 initializeWebAssemblyCFGStackifyPass(PR); 85 initializeWebAssemblyExplicitLocalsPass(PR); 86 initializeWebAssemblyLowerBrUnlessPass(PR); 87 initializeWebAssemblyRegNumberingPass(PR); 88 initializeWebAssemblyDebugFixupPass(PR); 89 initializeWebAssemblyPeepholePass(PR); 90 } 91 92 //===----------------------------------------------------------------------===// 93 // WebAssembly Lowering public interface. 94 //===----------------------------------------------------------------------===// 95 96 static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM, 97 const Triple &TT) { 98 if (!RM.hasValue()) { 99 // Default to static relocation model. This should always be more optimial 100 // than PIC since the static linker can determine all global addresses and 101 // assume direct function calls. 102 return Reloc::Static; 103 } 104 105 if (!TT.isOSEmscripten()) { 106 // Relocation modes other than static are currently implemented in a way 107 // that only works for Emscripten, so disable them if we aren't targeting 108 // Emscripten. 109 return Reloc::Static; 110 } 111 112 return *RM; 113 } 114 115 /// Create an WebAssembly architecture model. 116 /// 117 WebAssemblyTargetMachine::WebAssemblyTargetMachine( 118 const Target &T, const Triple &TT, StringRef CPU, StringRef FS, 119 const TargetOptions &Options, Optional<Reloc::Model> RM, 120 Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT) 121 : LLVMTargetMachine(T, 122 TT.isArch64Bit() ? "e-m:e-p:64:64-i64:64-n32:64-S128" 123 : "e-m:e-p:32:32-i64:64-n32:64-S128", 124 TT, CPU, FS, Options, getEffectiveRelocModel(RM, TT), 125 getEffectiveCodeModel(CM, CodeModel::Large), OL), 126 TLOF(new WebAssemblyTargetObjectFile()) { 127 // WebAssembly type-checks instructions, but a noreturn function with a return 128 // type that doesn't match the context will cause a check failure. So we lower 129 // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's 130 // 'unreachable' instructions which is meant for that case. 131 this->Options.TrapUnreachable = true; 132 133 // WebAssembly treats each function as an independent unit. Force 134 // -ffunction-sections, effectively, so that we can emit them independently. 135 this->Options.FunctionSections = true; 136 this->Options.DataSections = true; 137 this->Options.UniqueSectionNames = true; 138 139 initAsmInfo(); 140 141 // Note that we don't use setRequiresStructuredCFG(true). It disables 142 // optimizations than we're ok with, and want, such as critical edge 143 // splitting and tail merging. 144 } 145 146 WebAssemblyTargetMachine::~WebAssemblyTargetMachine() = default; // anchor. 147 148 const WebAssemblySubtarget *WebAssemblyTargetMachine::getSubtargetImpl() const { 149 return getSubtargetImpl(std::string(getTargetCPU()), 150 std::string(getTargetFeatureString())); 151 } 152 153 const WebAssemblySubtarget * 154 WebAssemblyTargetMachine::getSubtargetImpl(std::string CPU, 155 std::string FS) const { 156 auto &I = SubtargetMap[CPU + FS]; 157 if (!I) { 158 I = std::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this); 159 } 160 return I.get(); 161 } 162 163 const WebAssemblySubtarget * 164 WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const { 165 Attribute CPUAttr = F.getFnAttribute("target-cpu"); 166 Attribute FSAttr = F.getFnAttribute("target-features"); 167 168 std::string CPU = 169 CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU; 170 std::string FS = 171 FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS; 172 173 // This needs to be done before we create a new subtarget since any 174 // creation will depend on the TM and the code generation flags on the 175 // function that reside in TargetOptions. 176 resetTargetOptions(F); 177 178 return getSubtargetImpl(CPU, FS); 179 } 180 181 namespace { 182 183 class CoalesceFeaturesAndStripAtomics final : public ModulePass { 184 // Take the union of all features used in the module and use it for each 185 // function individually, since having multiple feature sets in one module 186 // currently does not make sense for WebAssembly. If atomics are not enabled, 187 // also strip atomic operations and thread local storage. 188 static char ID; 189 WebAssemblyTargetMachine *WasmTM; 190 191 public: 192 CoalesceFeaturesAndStripAtomics(WebAssemblyTargetMachine *WasmTM) 193 : ModulePass(ID), WasmTM(WasmTM) {} 194 195 bool runOnModule(Module &M) override { 196 FeatureBitset Features = coalesceFeatures(M); 197 198 std::string FeatureStr = getFeatureString(Features); 199 WasmTM->setTargetFeatureString(FeatureStr); 200 for (auto &F : M) 201 replaceFeatures(F, FeatureStr); 202 203 bool StrippedAtomics = false; 204 bool StrippedTLS = false; 205 206 if (!Features[WebAssembly::FeatureAtomics]) 207 StrippedAtomics = stripAtomics(M); 208 209 if (!Features[WebAssembly::FeatureBulkMemory]) 210 StrippedTLS = stripThreadLocals(M); 211 212 if (StrippedAtomics && !StrippedTLS) 213 stripThreadLocals(M); 214 else if (StrippedTLS && !StrippedAtomics) 215 stripAtomics(M); 216 217 recordFeatures(M, Features, StrippedAtomics || StrippedTLS); 218 219 // Conservatively assume we have made some change 220 return true; 221 } 222 223 private: 224 FeatureBitset coalesceFeatures(const Module &M) { 225 FeatureBitset Features = 226 WasmTM 227 ->getSubtargetImpl(std::string(WasmTM->getTargetCPU()), 228 std::string(WasmTM->getTargetFeatureString())) 229 ->getFeatureBits(); 230 for (auto &F : M) 231 Features |= WasmTM->getSubtargetImpl(F)->getFeatureBits(); 232 return Features; 233 } 234 235 std::string getFeatureString(const FeatureBitset &Features) { 236 std::string Ret; 237 for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) { 238 if (Features[KV.Value]) 239 Ret += (StringRef("+") + KV.Key + ",").str(); 240 } 241 return Ret; 242 } 243 244 void replaceFeatures(Function &F, const std::string &Features) { 245 F.removeFnAttr("target-features"); 246 F.removeFnAttr("target-cpu"); 247 F.addFnAttr("target-features", Features); 248 } 249 250 bool stripAtomics(Module &M) { 251 // Detect whether any atomics will be lowered, since there is no way to tell 252 // whether the LowerAtomic pass lowers e.g. stores. 253 bool Stripped = false; 254 for (auto &F : M) { 255 for (auto &B : F) { 256 for (auto &I : B) { 257 if (I.isAtomic()) { 258 Stripped = true; 259 goto done; 260 } 261 } 262 } 263 } 264 265 done: 266 if (!Stripped) 267 return false; 268 269 LowerAtomicPass Lowerer; 270 FunctionAnalysisManager FAM; 271 for (auto &F : M) 272 Lowerer.run(F, FAM); 273 274 return true; 275 } 276 277 bool stripThreadLocals(Module &M) { 278 bool Stripped = false; 279 for (auto &GV : M.globals()) { 280 if (GV.isThreadLocal()) { 281 Stripped = true; 282 GV.setThreadLocal(false); 283 } 284 } 285 return Stripped; 286 } 287 288 void recordFeatures(Module &M, const FeatureBitset &Features, bool Stripped) { 289 for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) { 290 if (Features[KV.Value]) { 291 // Mark features as used 292 std::string MDKey = (StringRef("wasm-feature-") + KV.Key).str(); 293 M.addModuleFlag(Module::ModFlagBehavior::Error, MDKey, 294 wasm::WASM_FEATURE_PREFIX_USED); 295 } 296 } 297 // Code compiled without atomics or bulk-memory may have had its atomics or 298 // thread-local data lowered to nonatomic operations or non-thread-local 299 // data. In that case, we mark the pseudo-feature "shared-mem" as disallowed 300 // to tell the linker that it would be unsafe to allow this code ot be used 301 // in a module with shared memory. 302 if (Stripped) { 303 M.addModuleFlag(Module::ModFlagBehavior::Error, "wasm-feature-shared-mem", 304 wasm::WASM_FEATURE_PREFIX_DISALLOWED); 305 } 306 } 307 }; 308 char CoalesceFeaturesAndStripAtomics::ID = 0; 309 310 /// WebAssembly Code Generator Pass Configuration Options. 311 class WebAssemblyPassConfig final : public TargetPassConfig { 312 public: 313 WebAssemblyPassConfig(WebAssemblyTargetMachine &TM, PassManagerBase &PM) 314 : TargetPassConfig(TM, PM) {} 315 316 WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const { 317 return getTM<WebAssemblyTargetMachine>(); 318 } 319 320 FunctionPass *createTargetRegisterAllocator(bool) override; 321 322 void addIRPasses() override; 323 bool addInstSelector() override; 324 void addPostRegAlloc() override; 325 bool addGCPasses() override { return false; } 326 void addPreEmitPass() override; 327 328 // No reg alloc 329 bool addRegAssignAndRewriteFast() override { return false; } 330 331 // No reg alloc 332 bool addRegAssignAndRewriteOptimized() override { return false; } 333 }; 334 } // end anonymous namespace 335 336 TargetTransformInfo 337 WebAssemblyTargetMachine::getTargetTransformInfo(const Function &F) { 338 return TargetTransformInfo(WebAssemblyTTIImpl(this, F)); 339 } 340 341 TargetPassConfig * 342 WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) { 343 return new WebAssemblyPassConfig(*this, PM); 344 } 345 346 FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) { 347 return nullptr; // No reg alloc 348 } 349 350 //===----------------------------------------------------------------------===// 351 // The following functions are called from lib/CodeGen/Passes.cpp to modify 352 // the CodeGen pass sequence. 353 //===----------------------------------------------------------------------===// 354 355 void WebAssemblyPassConfig::addIRPasses() { 356 // Lower atomics and TLS if necessary 357 addPass(new CoalesceFeaturesAndStripAtomics(&getWebAssemblyTargetMachine())); 358 359 // This is a no-op if atomics are not used in the module 360 addPass(createAtomicExpandPass()); 361 362 // Add signatures to prototype-less function declarations 363 addPass(createWebAssemblyAddMissingPrototypes()); 364 365 // Lower .llvm.global_dtors into .llvm_global_ctors with __cxa_atexit calls. 366 addPass(createWebAssemblyLowerGlobalDtors()); 367 368 // Fix function bitcasts, as WebAssembly requires caller and callee signatures 369 // to match. 370 addPass(createWebAssemblyFixFunctionBitcasts()); 371 372 // Optimize "returned" function attributes. 373 if (getOptLevel() != CodeGenOpt::None) 374 addPass(createWebAssemblyOptimizeReturned()); 375 376 // If exception handling is not enabled and setjmp/longjmp handling is 377 // enabled, we lower invokes into calls and delete unreachable landingpad 378 // blocks. Lowering invokes when there is no EH support is done in 379 // TargetPassConfig::addPassesToHandleExceptions, but this runs after this 380 // function and SjLj handling expects all invokes to be lowered before. 381 if (!EnableEmException && 382 TM->Options.ExceptionModel == ExceptionHandling::None) { 383 addPass(createLowerInvokePass()); 384 // The lower invoke pass may create unreachable code. Remove it in order not 385 // to process dead blocks in setjmp/longjmp handling. 386 addPass(createUnreachableBlockEliminationPass()); 387 } 388 389 // Handle exceptions and setjmp/longjmp if enabled. 390 if (EnableEmException || EnableEmSjLj) 391 addPass(createWebAssemblyLowerEmscriptenEHSjLj(EnableEmException, 392 EnableEmSjLj)); 393 394 // Expand indirectbr instructions to switches. 395 addPass(createIndirectBrExpandPass()); 396 397 TargetPassConfig::addIRPasses(); 398 } 399 400 bool WebAssemblyPassConfig::addInstSelector() { 401 (void)TargetPassConfig::addInstSelector(); 402 addPass( 403 createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel())); 404 // Run the argument-move pass immediately after the ScheduleDAG scheduler 405 // so that we can fix up the ARGUMENT instructions before anything else 406 // sees them in the wrong place. 407 addPass(createWebAssemblyArgumentMove()); 408 // Set the p2align operands. This information is present during ISel, however 409 // it's inconvenient to collect. Collect it now, and update the immediate 410 // operands. 411 addPass(createWebAssemblySetP2AlignOperands()); 412 413 // Eliminate range checks and add default targets to br_table instructions. 414 addPass(createWebAssemblyFixBrTableDefaults()); 415 416 return false; 417 } 418 419 void WebAssemblyPassConfig::addPostRegAlloc() { 420 // TODO: The following CodeGen passes don't currently support code containing 421 // virtual registers. Consider removing their restrictions and re-enabling 422 // them. 423 424 // These functions all require the NoVRegs property. 425 disablePass(&MachineCopyPropagationID); 426 disablePass(&PostRAMachineSinkingID); 427 disablePass(&PostRASchedulerID); 428 disablePass(&FuncletLayoutID); 429 disablePass(&StackMapLivenessID); 430 disablePass(&LiveDebugValuesID); 431 disablePass(&PatchableFunctionID); 432 disablePass(&ShrinkWrapID); 433 434 // This pass hurts code size for wasm because it can generate irreducible 435 // control flow. 436 disablePass(&MachineBlockPlacementID); 437 438 TargetPassConfig::addPostRegAlloc(); 439 } 440 441 void WebAssemblyPassConfig::addPreEmitPass() { 442 TargetPassConfig::addPreEmitPass(); 443 444 // Eliminate multiple-entry loops. 445 addPass(createWebAssemblyFixIrreducibleControlFlow()); 446 447 // Do various transformations for exception handling. 448 // Every CFG-changing optimizations should come before this. 449 if (TM->Options.ExceptionModel == ExceptionHandling::Wasm) 450 addPass(createWebAssemblyLateEHPrepare()); 451 452 // Now that we have a prologue and epilogue and all frame indices are 453 // rewritten, eliminate SP and FP. This allows them to be stackified, 454 // colored, and numbered with the rest of the registers. 455 addPass(createWebAssemblyReplacePhysRegs()); 456 457 // Preparations and optimizations related to register stackification. 458 if (getOptLevel() != CodeGenOpt::None) { 459 // LiveIntervals isn't commonly run this late. Re-establish preconditions. 460 addPass(createWebAssemblyPrepareForLiveIntervals()); 461 462 // Depend on LiveIntervals and perform some optimizations on it. 463 addPass(createWebAssemblyOptimizeLiveIntervals()); 464 465 // Prepare memory intrinsic calls for register stackifying. 466 addPass(createWebAssemblyMemIntrinsicResults()); 467 468 // Mark registers as representing wasm's value stack. This is a key 469 // code-compression technique in WebAssembly. We run this pass (and 470 // MemIntrinsicResults above) very late, so that it sees as much code as 471 // possible, including code emitted by PEI and expanded by late tail 472 // duplication. 473 addPass(createWebAssemblyRegStackify()); 474 475 // Run the register coloring pass to reduce the total number of registers. 476 // This runs after stackification so that it doesn't consider registers 477 // that become stackified. 478 addPass(createWebAssemblyRegColoring()); 479 } 480 481 // Sort the blocks of the CFG into topological order, a prerequisite for 482 // BLOCK and LOOP markers. 483 addPass(createWebAssemblyCFGSort()); 484 485 // Insert BLOCK and LOOP markers. 486 addPass(createWebAssemblyCFGStackify()); 487 488 // Insert explicit local.get and local.set operators. 489 if (!WasmDisableExplicitLocals) 490 addPass(createWebAssemblyExplicitLocals()); 491 492 // Lower br_unless into br_if. 493 addPass(createWebAssemblyLowerBrUnless()); 494 495 // Perform the very last peephole optimizations on the code. 496 if (getOptLevel() != CodeGenOpt::None) 497 addPass(createWebAssemblyPeephole()); 498 499 // Create a mapping from LLVM CodeGen virtual registers to wasm registers. 500 addPass(createWebAssemblyRegNumbering()); 501 502 // Fix debug_values whose defs have been stackified. 503 if (!WasmDisableExplicitLocals) 504 addPass(createWebAssemblyDebugFixup()); 505 } 506 507 yaml::MachineFunctionInfo * 508 WebAssemblyTargetMachine::createDefaultFuncInfoYAML() const { 509 return new yaml::WebAssemblyFunctionInfo(); 510 } 511 512 yaml::MachineFunctionInfo *WebAssemblyTargetMachine::convertFuncInfoToYAML( 513 const MachineFunction &MF) const { 514 const auto *MFI = MF.getInfo<WebAssemblyFunctionInfo>(); 515 return new yaml::WebAssemblyFunctionInfo(*MFI); 516 } 517 518 bool WebAssemblyTargetMachine::parseMachineFunctionInfo( 519 const yaml::MachineFunctionInfo &MFI, PerFunctionMIParsingState &PFS, 520 SMDiagnostic &Error, SMRange &SourceRange) const { 521 const auto &YamlMFI = 522 reinterpret_cast<const yaml::WebAssemblyFunctionInfo &>(MFI); 523 MachineFunction &MF = PFS.MF; 524 MF.getInfo<WebAssemblyFunctionInfo>()->initializeBaseYamlFields(YamlMFI); 525 return false; 526 } 527