Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion tslang/lib/TypeScript/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2380,7 +2380,14 @@ struct FuncOpLowering : public TsLlvmPattern<mlir_ts::FuncOp>
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
Expand Down
6 changes: 4 additions & 2 deletions tslang/tslang/dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ extern cl::opt<std::string> outputFilename;
std::string getDefaultOutputFileName(enum Action);
std::unique_ptr<llvm::ToolOutputFile> getOutputStream(enum Action, std::string);
int registerMLIRDialects(mlir::ModuleOp);
std::function<llvm::Error(llvm::Module *)> getTransformer(bool, int, int, CompileOptions&);
namespace llvm { class TargetMachine; }
std::function<llvm::Error(llvm::Module *)> getTransformer(bool, int, int, CompileOptions &, llvm::TargetMachine *);

int dumpAST()
{
Expand Down Expand Up @@ -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";
Expand Down
30 changes: 26 additions & 4 deletions tslang/tslang/jit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ std::string makeAbsolutePath(std::string);
std::string getExecutablePath(const char *);

int registerMLIRDialects(mlir::ModuleOp);
std::function<llvm::Error(llvm::Module *)> getTransformer(bool, int, int, CompileOptions&);
namespace llvm { class TargetMachine; }
std::function<llvm::Error(llvm::Module *)> getTransformer(bool, int, int, CompileOptions &, llvm::TargetMachine *);

using MlirRunnerInitFn = void (*)(llvm::StringMap<void *> &);
using MlirRunnerDestroyFn = void (*)();
Expand Down Expand Up @@ -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;
Expand All @@ -1540,8 +1539,28 @@ int runJit(int argc, char **argv, mlir::ModuleOp module, CompileOptions &compile
mlir::SmallVector<mlir::StringRef> 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<llvm::TargetMachine> 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;
Expand Down Expand Up @@ -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";
Expand Down
4 changes: 2 additions & 2 deletions tslang/tslang/jitcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ std::unique_ptr<mlir::MLIRContext> createMLIRContext();
int compileTypeScriptFileIntoMLIR(mlir::MLIRContext &, llvm::StringRef, llvm::SourceMgr &, mlir::OwningOpRef<mlir::ModuleOp> &, CompileOptions &);
int runMLIRPasses(mlir::MLIRContext &, llvm::SourceMgr &, mlir::OwningOpRef<mlir::ModuleOp> &, CompileOptions &);
int registerMLIRDialects(mlir::ModuleOp);
std::function<llvm::Error(llvm::Module *)> getTransformer(bool, int, int, CompileOptions &);
std::function<llvm::Error(llvm::Module *)> getTransformer(bool, int, int, CompileOptions &, llvm::TargetMachine *);
llvm::Error addEntryThunk(llvm::Module &, llvm::StringRef);

#define JIT_CACHE_FORMAT "tslang-jit-cache 1"
Expand Down Expand Up @@ -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";
Expand Down
9 changes: 6 additions & 3 deletions tslang/tslang/obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ extern cl::opt<std::string> outputFilename;
std::string getDefaultOutputFileName(enum Action);
std::unique_ptr<llvm::ToolOutputFile> getOutputStream(enum Action, std::string);
int registerMLIRDialects(mlir::ModuleOp);
std::function<llvm::Error(llvm::Module *)> getTransformer(bool, int, int, CompileOptions&);
namespace llvm { class TargetMachine; }
std::function<llvm::Error(llvm::Module *)> getTransformer(bool, int, int, CompileOptions &, llvm::TargetMachine *);

static llvm::codegen::RegisterCodeGenFlags CGF;

Expand Down Expand Up @@ -229,7 +230,9 @@ int setupTargetTriple(llvm::Module *llvmModule, std::unique_ptr<llvm::TargetMach

llvm::CodeGenOptLevel OLvl;
// TODO: finish it, optimization
if (auto Level = llvm::CodeGenOpt::parseLevel(enableOpt ? mapToLevel(optLevel) : '0'))
// optimizing for size generates code as -O2 does, whatever the speed level: clang's -Os and -Oz
auto codeGenLevel = !enableOpt ? '0' : (sizeLevel > 0 && optLevel > 0) ? '2' : mapToLevel(optLevel);
if (auto Level = llvm::CodeGenOpt::parseLevel(codeGenLevel))
{
OLvl = *Level;
}
Expand Down Expand Up @@ -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";
Expand Down
64 changes: 50 additions & 14 deletions tslang/tslang/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -317,13 +319,17 @@ static std::optional<llvm::OptimizationLevel> 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;
Expand All @@ -332,12 +338,37 @@ static std::optional<llvm::OptimizationLevel> 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
Expand Down Expand Up @@ -394,11 +425,15 @@ std::function<llvm::Error(llvm::Module *)> 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<llvm::TargetMachine> ownedTargetMachine;
auto *effectiveTargetMachine = targetMachine;
if (!effectiveTargetMachine)
Expand Down Expand Up @@ -491,20 +526,21 @@ std::function<llvm::Error(llvm::Module *)> makeCustomPassesWithOptimizingTransfo
};
}

std::function<llvm::Error(llvm::Module *)> getTransformer(bool enableOpt, int optLevel, int sizeLevel, CompileOptions &compileOptions)
std::function<llvm::Error(llvm::Module *)> 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;
Expand Down
2 changes: 1 addition & 1 deletion tslang/tslang/tslang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ cl::opt<enum Action> emitAction("emit", cl::desc("Select the kind of output desi
cl::opt<bool> 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<int> optLevel{"opt_level", cl::desc("Optimization level"), cl::ZeroOrMore, cl::value_desc("0-3"), cl::init(3), cl::cat(TypeScriptCompilerCategory)};
cl::opt<int> sizeLevel{"size_level", cl::desc("Optimization size level"), cl::ZeroOrMore, cl::value_desc("value"), cl::init(0), cl::cat(TypeScriptCompilerCategory)};
cl::opt<int> 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<std::string> clSharedLibs{"shared-libs", cl::desc("Libraries to link dynamically. (used in --emit=jit)"), cl::ZeroOrMore, cl::MiscFlags::CommaSeparated,
Expand Down
Loading