diff --git a/docs/how/wasm/test/wasm_skips.txt b/docs/how/wasm/test/wasm_skips.txt index c39d4a6f6..70a07e628 100644 --- a/docs/how/wasm/test/wasm_skips.txt +++ b/docs/how/wasm/test/wasm_skips.txt @@ -11,9 +11,6 @@ internals.ts # inline_asm with x86 instructions fetch.ts # (default library) fetch(): the host has no network, so the request throws # compiler gaps on wasm, each to be fixed and taken off this list -00generator3.ts wasm64 # wasm-ld: invalid data relocation for an object literal's generator method `next` -01iterator.ts wasm64 # wasm-ld: invalid data relocation for an object literal's generator method `next` -00owned_generator_locals.ts wasm64 # wasm-ld: invalid data relocation for a generator's `next` (under none) path.ts # a field of Readonly read through Maybe<...> is read wrong: the loop never ends # fails natively under the same model too (not run natively under it by ctest) diff --git a/tslang/tslang/transform.cpp b/tslang/tslang/transform.cpp index 5fc2a8fbc..14d1d527e 100644 --- a/tslang/tslang/transform.cpp +++ b/tslang/tslang/transform.cpp @@ -871,6 +871,49 @@ struct RcPairEliminationPass : llvm::PassInfoMixin } }; +// wasm: a load or a store whose address is a function (at any constant offset) is on a path that never runs. +// SpeculativeDirectCallPass tests a function value against every function of its type, and inlines each +// one under its test; in a test the program never passes - IntIter's loop asking "is it foo3's `next`?" - +// what the inlined body reads through its `this` is, once the optimizer knows the pointer is the function, +// a read of the function's own address. Natively that is a read of code nobody makes; on wasm, where code is +// not in the linear memory, it is a relocation no linker resolves (wasm-ld: "invalid data relocation"). Such +// an access is undefined anyway: a load gives poison, and a store is dropped. +struct WasmFunctionDataAccessPass : llvm::PassInfoMixin +{ + static bool isFunctionAddress(llvm::Value *pointer) + { + return llvm::isa(llvm::getUnderlyingObject(pointer)); + } + + llvm::PreservedAnalyses run(llvm::Function &func, llvm::FunctionAnalysisManager &) + { + llvm::SmallVector accesses; + for (auto &inst : llvm::instructions(func)) + { + if (auto *load = llvm::dyn_cast(&inst); load && isFunctionAddress(load->getPointerOperand())) + { + accesses.push_back(load); + } + else if (auto *store = llvm::dyn_cast(&inst); store && isFunctionAddress(store->getPointerOperand())) + { + accesses.push_back(store); + } + } + + for (auto *access : accesses) + { + if (!access->getType()->isVoidTy()) + { + access->replaceAllUsesWith(llvm::PoisonValue::get(access->getType())); + } + + access->eraseFromParent(); + } + + return accesses.empty() ? llvm::PreservedAnalyses::all() : llvm::PreservedAnalyses::none(); + } +}; + // A call through a function value (an interface's method, a callback) whose type only a few // functions of the module have, among those whose address is taken: the call tests the pointer // against each and calls it directly, and the call through the pointer is left for anything else. @@ -1275,6 +1318,11 @@ std::function makeCustomPassesWithOptimizingTransfo else mpm.addPass(pb.buildPerModuleDefaultPipeline(*ol)); + if (compileOptions.isWasm && *ol != llvm::OptimizationLevel::O0) + { + mpm.addPass(llvm::createModuleToFunctionPassAdaptor(WasmFunctionDataAccessPass())); + } + #ifdef SAVE_VIA_PASS std::unique_ptr FDOut; if (emitAction == Action::DumpLLVMIR)