diff --git a/tslang/lib/TypeScript/LowerToLLVM.cpp b/tslang/lib/TypeScript/LowerToLLVM.cpp index 826f1c547..d3f1b9278 100644 --- a/tslang/lib/TypeScript/LowerToLLVM.cpp +++ b/tslang/lib/TypeScript/LowerToLLVM.cpp @@ -2380,7 +2380,14 @@ struct FuncOpLowering : public TsLlvmPattern LLVMRTTIHelperVC rttih(funcOp, rewriter, typeConverter, tsLlvmContext->compileOptions); rttih.setPersonality(newFuncOp); - funcAttrs.push_back(ATTR("noinline")); + // Only where Win32ExceptionPass rewrites the function's landing pads into funclets: + // that rewrite is per function and is not made to survive the function's body being + // copied into another. Itanium landing pads are what LLVM's inliner is written for, + // so elsewhere a function is not kept out of line for having a `try` or a `using`. + if (tsLlvmContext->compileOptions.isWindows) + { + funcAttrs.push_back(ATTR("noinline")); + } } // copy attributes over diff --git a/tslang/tslang/dump.cpp b/tslang/tslang/dump.cpp index 6f051fefd..4a0d68a0b 100644 --- a/tslang/tslang/dump.cpp +++ b/tslang/tslang/dump.cpp @@ -32,7 +32,8 @@ extern cl::opt outputFilename; std::string getDefaultOutputFileName(enum Action); std::unique_ptr getOutputStream(enum Action, std::string); int registerMLIRDialects(mlir::ModuleOp); -std::function getTransformer(bool, int, int, CompileOptions&); +namespace llvm { class TargetMachine; } +std::function getTransformer(bool, int, int, CompileOptions &, llvm::TargetMachine *); int dumpAST() { @@ -76,7 +77,8 @@ int dumpLLVMIR(enum Action emitAction, std::string outputFile, mlir::ModuleOp mo llvm::InitializeNativeTarget(); llvm::InitializeNativeTargetAsmPrinter(); - auto optPipeline = getTransformer(enableOpt, optLevel, sizeLevel, compileOptions); + auto optPipeline = getTransformer(enableOpt, optLevel, sizeLevel, compileOptions, + /*targetMachine=*/nullptr); if (auto err = optPipeline(llvmModule.get())) { llvm::WithColor::error(llvm::errs(), "tslang") << "Failed to optimize LLVM IR " << err << "\n"; diff --git a/tslang/tslang/jit.cpp b/tslang/tslang/jit.cpp index 3acdabafa..0af116707 100644 --- a/tslang/tslang/jit.cpp +++ b/tslang/tslang/jit.cpp @@ -89,7 +89,8 @@ std::string makeAbsolutePath(std::string); std::string getExecutablePath(const char *); int registerMLIRDialects(mlir::ModuleOp); -std::function getTransformer(bool, int, int, CompileOptions&); +namespace llvm { class TargetMachine; } +std::function getTransformer(bool, int, int, CompileOptions &, llvm::TargetMachine *); using MlirRunnerInitFn = void (*)(llvm::StringMap &); using MlirRunnerDestroyFn = void (*)(); @@ -1525,8 +1526,6 @@ int runJit(int argc, char **argv, mlir::ModuleOp module, CompileOptions &compile llvm::InitializeNativeTarget(); llvm::InitializeNativeTargetAsmPrinter(); - auto optPipeline = getTransformer(enableOpt, optLevel, sizeLevel, compileOptions); - if (auto result = prepareJitProcess(compileOptions, /*nativeImage=*/false)) { return result; @@ -1540,8 +1539,28 @@ int runJit(int argc, char **argv, mlir::ModuleOp module, CompileOptions &compile mlir::SmallVector sharedLibPaths; sharedLibPaths.append(begin(clSharedLibs), end(clSharedLibs)); + // The engine makes its own machine for the host; the optimizer is given one like it, so + // its cost model is the host's too. With none it builds a machine from -mcpu/-mattr. + std::unique_ptr dumpTargetMachine; + if (auto tmBuilderOrError = getJitTargetMachineBuilder()) + { + if (auto tmOrError = tmBuilderOrError->createTargetMachine()) + { + dumpTargetMachine = std::move(*tmOrError); + } + else + { + llvm::consumeError(tmOrError.takeError()); + } + } + else + { + llvm::consumeError(tmBuilderOrError.takeError()); + } + mlir::ExecutionEngineOptions engineOptions; - engineOptions.transformer = optPipeline; + engineOptions.transformer = + getTransformer(enableOpt, optLevel, sizeLevel, compileOptions, dumpTargetMachine.get()); engineOptions.enableObjectDump = true; engineOptions.enableGDBNotificationListener = !enableOpt; engineOptions.sharedLibPaths = sharedLibPaths; @@ -1620,6 +1639,9 @@ int runJit(int argc, char **argv, mlir::ModuleOp module, CompileOptions &compile llvmModule->setDataLayout((*tmOrError)->createDataLayout()); llvmModule->setTargetTriple((*tmOrError)->getTargetTriple()); + // the host's machine, which is what the JIT compiles with: the optimizer's cost model has to + // be for the CPU the code is generated for + auto optPipeline = getTransformer(enableOpt, optLevel, sizeLevel, compileOptions, tmOrError->get()); if (auto err = optPipeline(llvmModule.get())) { llvm::WithColor::error(llvm::errs(), "tslang") << "failed to optimize LLVM IR, error: " << err << "\n"; diff --git a/tslang/tslang/jitcache.cpp b/tslang/tslang/jitcache.cpp index 68c675b8e..d8c95c8b9 100644 --- a/tslang/tslang/jitcache.cpp +++ b/tslang/tslang/jitcache.cpp @@ -68,7 +68,7 @@ std::unique_ptr createMLIRContext(); int compileTypeScriptFileIntoMLIR(mlir::MLIRContext &, llvm::StringRef, llvm::SourceMgr &, mlir::OwningOpRef &, CompileOptions &); int runMLIRPasses(mlir::MLIRContext &, llvm::SourceMgr &, mlir::OwningOpRef &, CompileOptions &); int registerMLIRDialects(mlir::ModuleOp); -std::function getTransformer(bool, int, int, CompileOptions &); +std::function getTransformer(bool, int, int, CompileOptions &, llvm::TargetMachine *); llvm::Error addEntryThunk(llvm::Module &, llvm::StringRef); #define JIT_CACHE_FORMAT "tslang-jit-cache 1" @@ -600,7 +600,7 @@ class JitUnitBuilder llvmModule->setDataLayout(tm->createDataLayout()); llvmModule->setTargetTriple(tm->getTargetTriple()); - auto optPipeline = getTransformer(enableOpt, optLevel, sizeLevel, options); + auto optPipeline = getTransformer(enableOpt, optLevel, sizeLevel, options, tm.get()); if (auto err = optPipeline(llvmModule.get())) { llvm::WithColor::error(llvm::errs(), "tslang") << "failed to optimize LLVM IR, error: " << err << "\n"; diff --git a/tslang/tslang/obj.cpp b/tslang/tslang/obj.cpp index 43351e7be..083dd139a 100644 --- a/tslang/tslang/obj.cpp +++ b/tslang/tslang/obj.cpp @@ -38,7 +38,8 @@ extern cl::opt outputFilename; std::string getDefaultOutputFileName(enum Action); std::unique_ptr getOutputStream(enum Action, std::string); int registerMLIRDialects(mlir::ModuleOp); -std::function getTransformer(bool, int, int, CompileOptions&); +namespace llvm { class TargetMachine; } +std::function getTransformer(bool, int, int, CompileOptions &, llvm::TargetMachine *); static llvm::codegen::RegisterCodeGenFlags CGF; @@ -229,7 +230,9 @@ int setupTargetTriple(llvm::Module *llvmModule, std::unique_ptr 0 && optLevel > 0) ? '2' : mapToLevel(optLevel); + if (auto Level = llvm::CodeGenOpt::parseLevel(codeGenLevel)) { OLvl = *Level; } @@ -287,7 +290,7 @@ int dumpObjOrAssembly(int argc, char **argv, enum Action emitAction, std::string return retCode; } - auto optPipeline = getTransformer(enableOpt, optLevel, sizeLevel, compileOptions); + auto optPipeline = getTransformer(enableOpt, optLevel, sizeLevel, compileOptions, Target.get()); if (auto err = optPipeline(llvmModule.get())) { llvm::WithColor::error(llvm::errs(), "tslang") << "Failed to optimize LLVM IR " << err << "\n"; diff --git a/tslang/tslang/transform.cpp b/tslang/tslang/transform.cpp index 4e06e62d0..7adc22a72 100644 --- a/tslang/tslang/transform.cpp +++ b/tslang/tslang/transform.cpp @@ -205,7 +205,9 @@ int runMLIRPasses(mlir::MLIRContext &context, llvm::SourceMgr &sourceMgr, mlir:: { optPM.addPass(mlir::createCSEPass()); } - pm.addPass(mlir::createLoopInvariantCodeMotionPass()); + // No MLIR loop-invariant code motion: it moves code out of ops with + // LoopLikeOpInterface, and the lowering above has already turned every While, + // DoWhile and For into branches. LLVM's LICM does the work. // Not under --di: stripping left every function a subprogram and every call no // location, which the LLVM dialect's verifier rejects for a call it could inline if (!compileOptions.generateDebugInfo) @@ -317,13 +319,17 @@ static std::optional mapToLevel(unsigned optLevel, unsi return llvm::OptimizationLevel::O0; case 1: - return llvm::OptimizationLevel::O1; - case 2: + case 3: + // A size level is a pipeline of its own, as clang's -Os and -Oz are: it replaces the + // speed level rather than being read only beside --opt_level=2. The default level is 3, + // so `--opt --size_level=2` used to be a plain -O3. switch (sizeLevel) { case 0: - return llvm::OptimizationLevel::O2; + return optLevel == 1 ? llvm::OptimizationLevel::O1 + : optLevel == 2 ? llvm::OptimizationLevel::O2 + : llvm::OptimizationLevel::O3; case 1: return llvm::OptimizationLevel::Os; @@ -332,12 +338,37 @@ static std::optional mapToLevel(unsigned optLevel, unsi return llvm::OptimizationLevel::Oz; } break; - case 3: - return llvm::OptimizationLevel::O3; } return std::nullopt; } +// --size_level: the pipeline's level alone decides little - the inliner's thresholds, the +// vectorizer, the unroller and the backend all ask the function whether it is optimized for +// size. clang marks every function `optsize` under -Os and `optsize minsize` under -Oz. +static void applySizeAttributes(llvm::Module &m, llvm::OptimizationLevel level) +{ + auto minSize = level == llvm::OptimizationLevel::Oz; + if (!minSize && level != llvm::OptimizationLevel::Os) + { + return; + } + + for (auto &func : m) + { + // `optnone` and the size attributes exclude each other (the verifier rejects the pair) + if (func.isDeclaration() || func.hasFnAttribute(llvm::Attribute::OptimizeNone)) + { + continue; + } + + func.addFnAttr(llvm::Attribute::OptimizeForSize); + if (minSize) + { + func.addFnAttr(llvm::Attribute::MinSize); + } + } +} + // --fast-math: relax IEEE-754 strictness the same way clang's // -funsafe-math-optimizations does, but deliberately WITHOUT nnan/ninf // (finite-math-only). In TypeScript NaN and Infinity are first-class values @@ -394,11 +425,15 @@ std::function makeCustomPassesWithOptimizingTransfo applyFastMathFlags(*m); } - // Every caller hands in a null TargetMachine, which leaves the -O2/-O3 - // cost model without target information: no vector registers are known, - // so LoopVectorize/SLP never consider vectorization profitable. Build a - // TargetMachine from the module triple (honoring -mcpu/-mattr) so the - // optimization pipeline sees the real target. + applySizeAttributes(*m, *ol); + + // Without a TargetMachine the -O2/-O3 cost model has no target information: no vector + // registers are known, so LoopVectorize/SLP never consider vectorization profitable. + // A caller that generates the code hands in the machine it generates it with, so the + // cost model and the code generator agree on the CPU - the JIT's is the host's, which + // -mcpu/-mattr (empty unless given) do not name. Only a caller with no machine of its + // own (--emit=llvm, --emit=bc) gets one built here from the module triple and + // -mcpu/-mattr. std::unique_ptr ownedTargetMachine; auto *effectiveTargetMachine = targetMachine; if (!effectiveTargetMachine) @@ -491,20 +526,21 @@ std::function makeCustomPassesWithOptimizingTransfo }; } -std::function getTransformer(bool enableOpt, int optLevel, int sizeLevel, CompileOptions &compileOptions) +std::function getTransformer(bool enableOpt, int optLevel, int sizeLevel, CompileOptions &compileOptions, + llvm::TargetMachine *targetMachine) { #ifdef ENABLE_CUSTOM_PASSES auto optPipeline = makeCustomPassesWithOptimizingTransformer( /*optLevel=*/enableOpt ? optLevel : 0, /*sizeLevel=*/enableOpt ? sizeLevel : 0, - /*targetMachine=*/nullptr, + targetMachine, compileOptions); #else // An optimization pipeline to use within the execution engine. auto optPipeline = mlir::makeOptimizingTransformer( /*optLevel=*/enableOpt ? optLevel : 0, /*sizeLevel=*/enableOpt ? sizeLevel : 0, - /*targetMachine=*/nullptr); + targetMachine); #endif return optPipeline; diff --git a/tslang/tslang/tslang.cpp b/tslang/tslang/tslang.cpp index 7293a5d3a..d8543bd33 100644 --- a/tslang/tslang/tslang.cpp +++ b/tslang/tslang/tslang.cpp @@ -116,7 +116,7 @@ cl::opt emitAction("emit", cl::desc("Select the kind of output desi cl::opt enableOpt{"opt", cl::desc("Enable optimizations (on by default with --emit=jit, unless --opt_level=0 or --di; --opt=false turns it off)"), cl::init(false), cl::cat(TypeScriptCompilerCategory), cl::cat(TypeScriptCompilerCategory)}; cl::opt optLevel{"opt_level", cl::desc("Optimization level"), cl::ZeroOrMore, cl::value_desc("0-3"), cl::init(3), cl::cat(TypeScriptCompilerCategory)}; -cl::opt sizeLevel{"size_level", cl::desc("Optimization size level"), cl::ZeroOrMore, cl::value_desc("value"), cl::init(0), cl::cat(TypeScriptCompilerCategory)}; +cl::opt sizeLevel{"size_level", cl::desc("Optimization size level, with --opt: 0 - optimize for speed at --opt_level, 1 - for size (-Os), 2 - for minimal size (-Oz)"), cl::ZeroOrMore, cl::value_desc("0-2"), cl::init(0), cl::cat(TypeScriptCompilerCategory)}; // dump obj cl::list clSharedLibs{"shared-libs", cl::desc("Libraries to link dynamically. (used in --emit=jit)"), cl::ZeroOrMore, cl::MiscFlags::CommaSeparated,