From 8fc5e37ec237bd49cf1ffe29cee0f4922fe33811 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 5 Nov 2026 19:03:24 +0000 Subject: [PATCH 1/2] A module imported as source that a library it reaches holds is imported from that library Under test-runner -shared every module but the program is linked into one library, named after the first module. With main -> m2 -> m1 the program imported m2 as source, met the library m1 only inside m2, and the library declared m2 again while m2's source was declaring it: "redefinition of symbol named 'f2'". Skipping it there was not enough - linked with the library alone, the program has no other definition of what m2's source declares. mlirGenInclude now follows the module's imports (and theirs), read once per file, and when a library they reach holds the module itself, imports the library in its place. A library never declares a module whose source is being generated around it (declSymbolsInProgress), the program's own included. And a library is read the same inside a declaration-mode source import: its function globals were made external, their symbol lookups left at the module's top ("Global is referenced by parentless instruction"). Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/MLIRGenImpl.h | 34 ++++ tslang/lib/TypeScript/MLIRGenModule.cpp | 169 ++++++++++++++++-- tslang/test/tester/CMakeLists.txt | 3 + .../tester/reference-path/nested_bottom.ts | 2 + .../tester/reference-path/nested_import.ts | 9 + .../tester/reference-path/nested_middle.ts | 4 + 6 files changed, 202 insertions(+), 19 deletions(-) create mode 100644 tslang/test/tester/reference-path/nested_bottom.ts create mode 100644 tslang/test/tester/reference-path/nested_import.ts create mode 100644 tslang/test/tester/reference-path/nested_middle.ts diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index a4e8e17c7..5f910f4c0 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -289,6 +289,23 @@ class MLIRGenImpl mlir::LogicalResult mlirGenImportSharedLib(mlir::Location location, StringRef filePath, bool dynamic, const GenContext &genContext); + // A library an `import` names: its file, and whether it is loaded at run time (no @static). + struct ImportedLibrary + { + std::string path; + bool dynamic; + }; + + // The library file an `import` of `specifier` points to when that file exists, named as the + // target names libraries. + std::string importLibraryPath(StringRef specifier); + + // A library that the imports of the source module `fullPath` reach - its own, or those of the + // source modules it imports, and so on - and that holds the module itself (exports one of + // `declSymbols`). + std::optional libraryHoldingModule(mlir::Location location, StringRef fullPath, + ArrayRef declSymbols); + void addImportedOwnNoDrops(StringRef factsText); void addImportedOwnObjectFacts(StringRef factsText); @@ -16814,6 +16831,23 @@ class MLIRGenImpl // imports a) stops instead of recursing until the stack overflows. llvm::StringSet<> filesInProgress; + // The declaration symbols (__decls_, and the _generic_ one) of the files + // in filesInProgress. A library imported while one of them is generated must not declare that + // one: its source is declaring it right now, and the two redefine each other. + llvm::StringSet<> declSymbolsInProgress; + + // What a source module's top-level imports name, read from its text once + // (libraryHoldingModule): the source modules, by canonicalFilePath, and the libraries. + struct ModuleImports + { + std::vector sources; + std::vector libraries; + }; + llvm::StringMap moduleImportsCache; + + // The declaration symbols each library libraryHoldingModule looked into exports. + llvm::StringMap> libraryDeclSymbolsCache; + // The default library's files - lib.d.ts and every file it references - by file key. Its // ambient data is defined in its DLL when the program links that (see // isImportedFromSharedDefaultLib). diff --git a/tslang/lib/TypeScript/MLIRGenModule.cpp b/tslang/lib/TypeScript/MLIRGenModule.cpp index 6a7cf9b15..d095caf49 100644 --- a/tslang/lib/TypeScript/MLIRGenModule.cpp +++ b/tslang/lib/TypeScript/MLIRGenModule.cpp @@ -326,6 +326,10 @@ namespace mlirgen // an imported module importing the program back does not generate the program again filesInProgress.insert(fileKey(module)); + // nor does a library holding the program declare the program again + auto programSuffix = moduleSymbolSuffix(fileKey(module)); + declSymbolsInProgress.insert(std::string(SHARED_LIB_DECLARATIONS_2UNDERSCORE "_") + programSuffix); + declSymbolsInProgress.insert(std::string(SHARED_LIB_DECLARATIONS_2UNDERSCORE "_generic_") + programSuffix); stage = Stages::Discovering; auto storeDebugInfo = compileOptions.generateDebugInfo; @@ -1367,8 +1371,25 @@ namespace mlirgen return mlir::success(); } + // Held by a library that its own imports reach: test-runner -shared links every module + // but the program into one, so with main -> m2 -> m1 the library m1 holds m2 as well. The + // program finds the module there - linked with the library alone, it has no other + // definition of what the module's source declares - so the library is imported in its + // place. Found before the source is read: once generated, even by discovery, the source's + // declarations would stand beside the library's. + if (auto library = libraryHoldingModule(location, fullPath, {declSymbol, genericDeclSymbol})) + { + return mlirGenImportSharedLib(location, library->path, library->dynamic, genContext); + } + filesInProgress.insert(canonicalPath); - auto inProgress = llvm::make_scope_exit([&]() { filesInProgress.erase(canonicalPath); }); + declSymbolsInProgress.insert(declSymbol); + declSymbolsInProgress.insert(genericDeclSymbol); + auto inProgress = llvm::make_scope_exit([&]() { + filesInProgress.erase(canonicalPath); + declSymbolsInProgress.erase(declSymbol); + declSymbolsInProgress.erase(genericDeclSymbol); + }); // An import that points to a .ts file: compiled, it is a declaration - the module is // compiled into an object of its own, and the objects are linked into one program. The @@ -1467,6 +1488,13 @@ namespace mlirgen return mlir::success(); } + // Read the same wherever the import is: inside a module imported as source, which is + // generated in declaration mode (mlirGenInclude), the globals through which this module + // calls the library's functions became external, and their initializers - the symbol + // lookups - were left at the module's top, outside any function. + MLIRValueGuard declarationModeGuard(declarationMode); + declarationMode = false; + // A library built for another architecture or OS (an i686 DLL for this x64 compiler) // cannot be loaded into this process. Its declarations are then read from the file; the // program still loads it at run time, through the global constructor below. @@ -1659,11 +1687,13 @@ namespace mlirgen // A library can hold several modules (test-runner -shared links them all into one), each // with its own __decls_. A module already in this module - imported as source, - // or through another library - is not declared again; see mlirGenInclude. + // or through another library - is not declared again; see mlirGenInclude. Nor is one + // whose source is being generated around this import (declSymbolsInProgress): the + // program, say, built into a library it imports. Its source declares it. SmallVector declaredSymbols; for (auto declSymbol : symbols) { - if (emittedFiles.contains(declSymbol)) + if (emittedFiles.contains(declSymbol) || declSymbolsInProgress.contains(declSymbol)) { continue; } @@ -1780,31 +1810,19 @@ namespace mlirgen return mlir::success(); } - mlir::LogicalResult MLIRGenImpl::mlirGen(ImportDeclaration importDeclarationAST, const GenContext &genContext) + std::string MLIRGenImpl::importLibraryPath(StringRef specifier) { - auto location = loc(importDeclarationAST); - - auto result = mlirGen(importDeclarationAST->moduleSpecifier, genContext); - EXIT_IF_FAILED_OR_NO_VALUE(result) - auto modulePath = V(result); - - auto constantOp = modulePath.getDefiningOp(); - assert(constantOp); - auto valueAttr = mlir::cast(constantOp.getValueAttr()); - - auto stringVal = valueAttr.getValue(); - // The library is named as the target names it, as --emit=dll does (utils.cpp): the path is // also what the program loads at run time, so a host's naming would not be found there. auto targetIsWindows = llvm::Triple(compileOptions.moduleTargetTriple).getOS() == llvm::Triple::Win32; std::string fullPath; - fullPath += stringVal; + fullPath += specifier; if (!targetIsWindows) { // rebuild file path - auto fileName = sys::path::filename(stringVal); - auto path = stringVal.substr(0, stringVal.size() - fileName.size()); + auto fileName = sys::path::filename(specifier); + auto path = specifier.substr(0, specifier.size() - fileName.size()); fullPath = path; fullPath += "lib"; fullPath += fileName; @@ -1815,6 +1833,119 @@ namespace mlirgen fullPath += targetIsWindows ? ".dll" : ".so"; } + return fullPath; + } + + std::optional MLIRGenImpl::libraryHoldingModule(mlir::Location location, StringRef fullPath, + ArrayRef declSymbols) + { +#ifdef GENERATE_IMPORT_INFO_USING_D_TS_FILE + // a library's declarations are not told apart by module there + return std::nullopt; +#else + auto libraryHolds = [&](StringRef libraryPath) { + auto cached = libraryDeclSymbolsCache.find(libraryPath); + if (cached == libraryDeclSymbolsCache.end()) + { + SmallVector symbolsAll; + Dump::getSymbols(libraryPath, symbolsAll, stringAllocator); + llvm::StringSet<> declSymbolsOfLibrary; + for (auto symbol : symbolsAll) + { + if (symbol.starts_with(SHARED_LIB_DECLARATIONS_2UNDERSCORE)) + { + declSymbolsOfLibrary.insert(symbol); + } + } + + cached = libraryDeclSymbolsCache.insert({libraryPath, std::move(declSymbolsOfLibrary)}).first; + } + + return llvm::any_of(declSymbols, [&](const std::string &symbol) { return cached->second.contains(symbol); }); + }; + + // every source module reached through imports, each read once (moduleImportsCache) + llvm::StringSet<> visited; + SmallVector pending{canonicalFilePath(fullPath)}; + while (!pending.empty()) + { + auto file = pending.pop_back_val(); + if (!visited.insert(file).second) + { + continue; + } + + auto cached = moduleImportsCache.find(file); + if (cached == moduleImportsCache.end()) + { + ModuleImports imports; + // a file that does not open is the import's to report + if (sys::fs::exists(file) && !StringRef(file).ends_with(".d.ts")) + { + auto [source, includeFiles] = loadIncludeFile(location, file); + if (source) + { + for (auto statement : source->statements) + { + if (statement != SyntaxKind::ImportDeclaration) + { + continue; + } + + auto importDeclaration = statement.as(); + if (importDeclaration->moduleSpecifier != SyntaxKind::StringLiteral) + { + continue; + } + + auto specifier = convertWideToUTF8(importDeclaration->moduleSpecifier.as()->text); + auto libraryPath = importLibraryPath(specifier); + if (sys::fs::exists(libraryPath)) + { + imports.libraries.push_back({libraryPath, !MLIRHelper::hasDecorator(importDeclaration, "static")}); + } + else + { + imports.sources.push_back(canonicalFilePath(includeFilePath(specifier))); + } + } + } + } + + cached = moduleImportsCache.insert({file, std::move(imports)}).first; + } + + auto &imports = cached->second; + for (auto &library : imports.libraries) + { + if (libraryHolds(library.path)) + { + return library; + } + } + + pending.append(imports.sources.begin(), imports.sources.end()); + } + + return std::nullopt; +#endif + } + + mlir::LogicalResult MLIRGenImpl::mlirGen(ImportDeclaration importDeclarationAST, const GenContext &genContext) + { + auto location = loc(importDeclarationAST); + + auto result = mlirGen(importDeclarationAST->moduleSpecifier, genContext); + EXIT_IF_FAILED_OR_NO_VALUE(result) + auto modulePath = V(result); + + auto constantOp = modulePath.getDefiningOp(); + assert(constantOp); + auto valueAttr = mlir::cast(constantOp.getValueAttr()); + + auto stringVal = valueAttr.getValue(); + + auto fullPath = importLibraryPath(stringVal); if (sys::fs::exists(fullPath)) { //auto dynamic = MLIRHelper::hasDecorator(importDeclarationAST, "dynamic"); diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 3b35cca7c..b6b312fd9 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -5140,6 +5140,9 @@ set(reference_path_dir "${PROJECT_SOURCE_DIR}/test/tester/reference-path") tslang_add_import_tests(reference-path-import-and-reference "${reference_path_dir}" import_and_reference module_left) tslang_add_import_tests(reference-path-two-imports "${reference_path_dir}" two_imports module_left module_right) tslang_add_import_tests(reference-path-import-diamond "${reference_path_dir}" import_diamond plain_module via_module) +# The library is named after nested_bottom, the first module, and holds nested_middle as well: the +# program imports nested_middle as source and meets the library only inside it. +tslang_add_import_tests(reference-path-import-nested "${reference_path_dir}" nested_import nested_bottom nested_middle) tslang_add_import_tests(reference-path-import-cycle "${reference_path_dir}" import_cycle cycle_module_a cycle_module_b) # Through a -shared library of these two modules the importer read Node twice ("redefinition of # symbol named 'Node..new'"), and then called its methods through the wrong vtable slots - see diff --git a/tslang/test/tester/reference-path/nested_bottom.ts b/tslang/test/tester/reference-path/nested_bottom.ts new file mode 100644 index 000000000..3a620a9af --- /dev/null +++ b/tslang/test/tester/reference-path/nested_bottom.ts @@ -0,0 +1,2 @@ +// The bottom of nested_import.ts's chain; see there. +export function bottom_fn() { return 1; } diff --git a/tslang/test/tester/reference-path/nested_import.ts b/tslang/test/tester/reference-path/nested_import.ts new file mode 100644 index 000000000..21dfb4330 --- /dev/null +++ b/tslang/test/tester/reference-path/nested_import.ts @@ -0,0 +1,9 @@ +// The program imports nested_middle, which imports nested_bottom. Under test-runner -shared the +// library is named after nested_bottom and holds nested_middle too, which the program only learns +// inside nested_middle: it was declared both from its source and from the library ("redefinition +// of symbol named 'nested_fn'"). The library is imported in its place. +import './nested_middle' + +assert(nested_fn() == 3); + +print("done."); diff --git a/tslang/test/tester/reference-path/nested_middle.ts b/tslang/test/tester/reference-path/nested_middle.ts new file mode 100644 index 000000000..9dba5b89b --- /dev/null +++ b/tslang/test/tester/reference-path/nested_middle.ts @@ -0,0 +1,4 @@ +// Imported by nested_import.ts; imports nested_bottom.ts. +import './nested_bottom' + +export function nested_fn() { return bottom_fn() + 2; } From a9e20a3961b44a656e4829077b57322d45ad3700 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 5 Nov 2026 19:08:45 +0000 Subject: [PATCH 2/2] deep_chain.ts runs under -shared and -jit -shared too Left out while a module imported as source was declared twice when the library it reached held it as well; with that fixed the library chain_01 holds every link and chain_20 is imported from it. Co-Authored-By: Claude Opus 5.5 --- tslang/test/tester/CMakeLists.txt | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 1bf1b649b..8ec5f967d 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -5154,13 +5154,18 @@ foreach(import_chain_index RANGE 1 20) endif() list(APPEND import_chain_modules "${PROJECT_SOURCE_DIR}/test/tester/import-chain/chain_${import_chain_index}.ts") endforeach() -# Not -shared: a module that imports another is declared twice in the one library, chain or not. -# The JIT compiles each import into its cache by itself. +# The JIT compiles each import into its cache by itself. Under -shared the library is chain_01, +# reached only at the bottom of the chain, and holds every link: chain_20 is imported from it. tslang_add_test(NAME test-compile-import-chain-deep COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/import-chain/deep_chain.ts" ${import_chain_modules}) tslang_add_test(NAME test-jit-import-chain-deep COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/import-chain/deep_chain.ts") -set_tests_properties(test-compile-import-chain-deep test-jit-import-chain-deep PROPERTIES TIMEOUT 120) +tslang_add_test(NAME test-compile-shared-import-chain-deep + COMMAND test-runner -shared "${PROJECT_SOURCE_DIR}/test/tester/import-chain/deep_chain.ts" ${import_chain_modules}) +tslang_add_test(NAME test-jit-shared-import-chain-deep + COMMAND test-runner -jit -shared "${PROJECT_SOURCE_DIR}/test/tester/import-chain/deep_chain.ts" ${import_chain_modules}) +set_tests_properties(test-compile-import-chain-deep test-jit-import-chain-deep test-compile-shared-import-chain-deep + test-jit-shared-import-chain-deep PROPERTIES TIMEOUT 120) tslang_add_import_tests(reference-path-import-diamond "${reference_path_dir}" import_diamond plain_module via_module) # The library is named after nested_bottom, the first module, and holds nested_middle as well: the # program imports nested_middle as source and meets the library only inside it.