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
22 changes: 17 additions & 5 deletions tslang/tslang/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
set_Options()

# The backends tslang compiles for, of those LLVM was built with. Not AllTargets*: the prebuilt LLVM
# a release links has RISCV, BPF and NVPTX too, several MB of code no tslang target uses.
# tslang.cpp initializes the same list (TSLANG_TARGET_<NAME>).
set(TSLANG_TARGETS X86 ARM AArch64 WebAssembly CACHE STRING "LLVM backends linked into tslang")
set(TSLANG_TARGET_COMPONENTS)
set(TSLANG_TARGET_DEFINITIONS)
foreach(target ${TSLANG_TARGETS})
if (target IN_LIST LLVM_TARGETS_TO_BUILD)
list(APPEND TSLANG_TARGET_COMPONENTS ${target}AsmParser ${target}CodeGen ${target}Desc ${target}Info)
string(TOUPPER ${target} TARGET_UPPER)
list(APPEND TSLANG_TARGET_DEFINITIONS TSLANG_TARGET_${TARGET_UPPER})
endif()
endforeach()
message(STATUS "tslang backends: ${TSLANG_TARGET_COMPONENTS}")

set(LLVM_LINK_COMPONENTS
# for Obj/ASM
AllTargetsAsmParsers
AllTargetsCodeGens
AllTargetsDescs
AllTargetsInfos
${TSLANG_TARGET_COMPONENTS}
# end - Obj/ASM
Core
Option
Expand Down Expand Up @@ -38,7 +50,6 @@ set(LIBS
MLIROptLib
MLIRParser
MLIRPass
MLIRRegisterAllExtensions
MLIRSideEffectInterfaces
MLIRSupport
MLIRTargetLLVMIRExport
Expand Down Expand Up @@ -92,6 +103,7 @@ elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
endif()
endif()
target_compile_definitions(tslang PUBLIC TSLANG_PACKAGE_VERSION="${TSLANG_PACKAGE_VERSION}")
target_compile_definitions(tslang PRIVATE ${TSLANG_TARGET_DEFINITIONS})

# TSLANG_PACKAGE_VERSION is only set by a release build (CI passes the tag via
# -DTSLANG_PACKAGE_VERSION / the TSLANG_PACKAGE_VERSION env var); a plain dev
Expand Down
51 changes: 43 additions & 8 deletions tslang/tslang/tslang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
#include "TypeScript/TypeScriptDialect.h"
#include "TypeScript/TypeScriptOps.h"

#include "mlir/InitAllDialects.h"
#include "mlir/InitAllExtensions.h"
#include "mlir/InitAllPasses.h"
#include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h"
#include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h"
#include "mlir/Conversion/IndexToLLVM/IndexToLLVM.h"
#include "mlir/Conversion/MathToLLVM/MathToLLVM.h"
#include "mlir/Dialect/Func/Extensions/AllExtensions.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/AsmState.h"
Expand Down Expand Up @@ -345,7 +348,15 @@ static int ReportWarningWithoutBreak(int reportType, char *message, int *returnV
std::unique_ptr<mlir::MLIRContext> createMLIRContext()
{
mlir::DialectRegistry registry;
registerAllExtensions(registry);
// Only the extensions of the dialects the compiler loads (below). registerAllExtensions would
// reference every upstream dialect's extension (GPU, NVVM, ROCDL, linalg, vector, sparse
// tensor, the transform dialect's...) and keep ~13 MB of code nothing runs in the executable.
mlir::arith::registerConvertArithToLLVMInterface(registry);
mlir::cf::registerConvertControlFlowToLLVMInterface(registry);
mlir::func::registerAllExtensions(registry);
mlir::registerConvertFuncToLLVMInterface(registry);
mlir::index::registerConvertIndexToLLVMInterface(registry);
mlir::registerConvertMathToLLVMInterface(registry);
// the llvm dialect promises an inliner interface that only registerAllDialects provides: without
// it a pass that inlines past lowering (the JIT's) failed the promise check in an assertions build
mlir::LLVM::registerInlinerInterface(registry);
Expand All @@ -369,6 +380,33 @@ std::unique_ptr<mlir::MLIRContext> createMLIRContext()
return mlirContext;
}

// The backends tslang links (TSLANG_TARGETS in CMakeLists.txt). Not llvm::InitializeAllTargets():
// it calls into every backend LLVM was built with, which would link them all.
#define TSLANG_INITIALIZE_TARGET(T) \
LLVMInitialize##T##TargetInfo(); \
LLVMInitialize##T##Target(); \
LLVMInitialize##T##TargetMC(); \
LLVMInitialize##T##AsmPrinter(); \
LLVMInitialize##T##AsmParser();

static void initializeTargets()
{
#ifdef TSLANG_TARGET_X86
TSLANG_INITIALIZE_TARGET(X86)
#endif
#ifdef TSLANG_TARGET_ARM
TSLANG_INITIALIZE_TARGET(ARM)
#endif
#ifdef TSLANG_TARGET_AARCH64
TSLANG_INITIALIZE_TARGET(AArch64)
#endif
#ifdef TSLANG_TARGET_WEBASSEMBLY
TSLANG_INITIALIZE_TARGET(WebAssembly)
#endif
}

#undef TSLANG_INITIALIZE_TARGET

int main(int argc, char **argv)
{
#if _MSC_VER && _DEBUG
Expand Down Expand Up @@ -413,10 +451,7 @@ int main(int argc, char **argv)

// Register for Obj/ASM
// Initialize targets first, so that --version shows registered targets.
llvm::InitializeAllTargets();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmPrinters();
llvm::InitializeAllAsmParsers();
initializeTargets();

// Initialize codegen and IR passes used by llc so that the -print-after,
// -print-before, and -stop-after options work.
Expand Down
Loading