From 84f90c636040f3d664d841dfa22e6f35dcda9e05 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 13 Nov 2026 12:29:00 +0000 Subject: [PATCH 1/2] tslang: register only the MLIR extensions the compiler uses registerAllExtensions referenced every upstream dialect's extension (GPU, NVVM, ROCDL, linalg, vector, sparse tensor, the transform dialect's), keeping ~14 MB of code nothing runs. Register the to-LLVM interfaces of the dialects tslang loads (arith, cf, func, index, math) and the func extensions only. Release tslang.exe: 87.8 MB -> 73.3 MB. ctest Release: 6976 passed. Co-Authored-By: Claude Opus 5.5 --- tslang/tslang/CMakeLists.txt | 1 - tslang/tslang/tslang.cpp | 19 +++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/tslang/tslang/CMakeLists.txt b/tslang/tslang/CMakeLists.txt index cd35f0fea..1f8733fa6 100644 --- a/tslang/tslang/CMakeLists.txt +++ b/tslang/tslang/CMakeLists.txt @@ -38,7 +38,6 @@ set(LIBS MLIROptLib MLIRParser MLIRPass - MLIRRegisterAllExtensions MLIRSideEffectInterfaces MLIRSupport MLIRTargetLLVMIRExport diff --git a/tslang/tslang/tslang.cpp b/tslang/tslang/tslang.cpp index 434afe9a6..7152b33cf 100644 --- a/tslang/tslang/tslang.cpp +++ b/tslang/tslang/tslang.cpp @@ -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" @@ -345,7 +348,15 @@ static int ReportWarningWithoutBreak(int reportType, char *message, int *returnV std::unique_ptr 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); From 8a597e0923cbab55566c5f7427124f1fd9f4bdc3 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 13 Nov 2026 13:25:12 +0000 Subject: [PATCH 2/2] tslang: link only the X86, ARM, AArch64 and WebAssembly backends AllTargets* linked every backend LLVM was built with, and llvm::InitializeAllTargets() referenced them all. The official prebuilt LLVM a release links also has RISCV, BPF and NVPTX, which no tslang target uses. TSLANG_TARGETS (CMake) names the backends, each linked only when LLVM has it, and tslang.cpp initializes the same list. tslang.exe on the official LLVM 22.1.8 package: 83.8 MB -> 71.4 MB (and with the previous commit, ~98 MB -> 71.4 MB). A local LLVM with exactly these four backends is unchanged. ctest Release on the official package: 7064 passed. Co-Authored-By: Claude Opus 5.5 --- tslang/tslang/CMakeLists.txt | 21 +++++++++++++++++---- tslang/tslang/tslang.cpp | 32 ++++++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/tslang/tslang/CMakeLists.txt b/tslang/tslang/CMakeLists.txt index 1f8733fa6..e323dcf70 100644 --- a/tslang/tslang/CMakeLists.txt +++ b/tslang/tslang/CMakeLists.txt @@ -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_). +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 @@ -91,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 diff --git a/tslang/tslang/tslang.cpp b/tslang/tslang/tslang.cpp index 7152b33cf..94b32af28 100644 --- a/tslang/tslang/tslang.cpp +++ b/tslang/tslang/tslang.cpp @@ -380,6 +380,33 @@ std::unique_ptr 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 @@ -424,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.