diff --git a/tslang/lib/TypeScript/MLIRGenModule.cpp b/tslang/lib/TypeScript/MLIRGenModule.cpp index 6f745ae90..fa5ce0f40 100644 --- a/tslang/lib/TypeScript/MLIRGenModule.cpp +++ b/tslang/lib/TypeScript/MLIRGenModule.cpp @@ -1942,7 +1942,21 @@ namespace mlirgen // 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. + // + // The modules' declarations name each other's classes (one module's State holds the + // Profiler another declares), and nothing orders them: they come in the order the library's + // symbol table lists them - by name in a DLL's export table, as the linker laid them out in + // an ELF one. A module's declarations that name what is not declared yet fail, and are read + // again once others have been; only a pass that declares none of them is a failure. + struct LibraryDeclarations + { + StringRef symbol; + std::string text; + bool isGenericClassDecl; + }; + SmallVector declaredSymbols; + SmallVector pendingDeclarations; for (auto declSymbol : symbols) { if (emittedFiles.contains(declSymbol) || declSymbolsInProgress.contains(declSymbol)) @@ -1989,29 +2003,18 @@ namespace mlirgen LLVM_DEBUG(llvm::dbgs() << "\n!! Shared lib import: \n" << dataPtr << "\n";); - { - MLIRLocationGuard vgLoc(overwriteLoc); - overwriteLoc = location; - - // a generic class's declaration (see - // createGenericClassDeclarationExportGlobalVar) must NOT be parsed with - // the ".d.ts" filename convention used below for every other kind of - // declaration: that convention makes the parser mark everything ambient/ - // external regardless of any per-declaration @dllimport marker (see the - // comment on parsePartialStatements's file_d_ts parameter), which would - // make the generic's instantiated specializations wrongly look like - // external stubs with no compilable body - they need to be treated as - // ordinary, fully-compilable local source instead. - auto isGenericClassDecl = - declSymbol.starts_with(std::string(SHARED_LIB_DECLARATIONS_2UNDERSCORE) + "_generic_"); - - auto importData = convertUTF8toWide(dataPtr); - if (mlir::failed(parsePartialStatements(importData, genContext, false, !isGenericClassDecl, true))) - { - //assert(false); - return mlir::failure(); - } - } + // a generic class's declaration (see + // createGenericClassDeclarationExportGlobalVar) must NOT be parsed with + // the ".d.ts" filename convention used below for every other kind of + // declaration: that convention makes the parser mark everything ambient/ + // external regardless of any per-declaration @dllimport marker (see the + // comment on parsePartialStatements's file_d_ts parameter), which would + // make the generic's instantiated specializations wrongly look like + // external stubs with no compilable body - they need to be treated as + // ordinary, fully-compilable local source instead. + auto isGenericClassDecl = + declSymbol.starts_with(std::string(SHARED_LIB_DECLARATIONS_2UNDERSCORE) + "_generic_"); + pendingDeclarations.push_back({declSymbol, std::string(dataPtr), isGenericClassDecl}); } else { @@ -2019,6 +2022,31 @@ namespace mlirgen } } + while (!pendingDeclarations.empty()) + { + SmallVector notDeclaredYet; + for (auto &declarations : pendingDeclarations) + { + MLIRLocationGuard vgLoc(overwriteLoc); + overwriteLoc = location; + + auto importData = convertUTF8toWide(declarations.text); + if (mlir::failed( + parsePartialStatements(importData, genContext, false, !declarations.isGenericClassDecl, true))) + { + notDeclaredYet.push_back(declarations); + } + } + + // a pass that declared none of them: what they name is nowhere (its errors are the reason) + if (notDeclaredYet.size() == pendingDeclarations.size()) + { + return mlir::failure(); + } + + pendingDeclarations = std::move(notDeclaredYet); + } + // What a library built under own says about its functions (SHARED_LIB_OWN_FACTS). Kept in // every model, so MLIRGen emits the same under own as under rc; only own reads it. for (auto factsSymbol : ownFactsSymbols) diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 1312a2338..a4be444f0 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -5443,6 +5443,11 @@ tslang_add_import_tests(import-chain-rtti-skipped "${PROJECT_SOURCE_DIR}/test/te rtti_bottom rtti_middle rtti_top) tslang_add_test(NAME test-jit-import-chain-rtti-skipped COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/import-chain/rtti_skipped_import.ts") +# The same with the modules named the other way round: a library's modules are declared in the order +# its symbol table lists them (by name in a DLL), and one naming a class not declared yet is read again +# after the others. The library is named after order_z_profiler, which order_m_state imports. +tslang_add_import_tests(import-chain-decl-order "${PROJECT_SOURCE_DIR}/test/tester/import-chain" order_program + order_z_profiler order_m_state order_a_saves) 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. diff --git a/tslang/test/tester/import-chain/order_a_saves.ts b/tslang/test/tester/import-chain/order_a_saves.ts new file mode 100644 index 000000000..b0f1134c5 --- /dev/null +++ b/tslang/test/tester/import-chain/order_a_saves.ts @@ -0,0 +1,6 @@ +// One more link between the program and the Profiler; its module sorts first. See order_program.ts. +import { State } from './order_m_state' + +export class Saves extends State { + saved = 0; +} diff --git a/tslang/test/tester/import-chain/order_m_state.ts b/tslang/test/tester/import-chain/order_m_state.ts new file mode 100644 index 000000000..186634d32 --- /dev/null +++ b/tslang/test/tester/import-chain/order_m_state.ts @@ -0,0 +1,10 @@ +// Holds a Profiler | null; see order_program.ts. +import { Profiler } from './order_z_profiler' + +export class State { + protected profiler: Profiler | null = null; + + start() { + this.profiler = new Profiler(); + } +} diff --git a/tslang/test/tester/import-chain/order_program.ts b/tslang/test/tester/import-chain/order_program.ts new file mode 100644 index 000000000..0aba7b307 --- /dev/null +++ b/tslang/test/tester/import-chain/order_program.ts @@ -0,0 +1,29 @@ +// rtti_skipped_import.ts with its modules named the other way round. Under -shared the three are +// one library, which declares each module from its own __decls_, and they name each other's +// classes: State holds a Profiler. They were declared in the order the library's symbol table lists +// them - by name in a DLL (order_a_saves, order_m_state, order_z_profiler: State before Profiler), as +// the linker laid them out in a shared object - and the first that named what was not declared yet +// failed the import: "can't find type by name: Profiler". Now one that fails is read again after +// the others. +import { Saves } from './order_a_saves' + +class Simulation extends Saves { + reports = 0; + + readProfile() { + const profiler = this.profiler; + if (profiler && profiler.read()) { + this.reports++; + } + } +} + +const sim = new Simulation(); +sim.readProfile(); +assert(sim.reports == 0); +sim.start(); +sim.readProfile(); +sim.readProfile(); +assert(sim.reports == 1); + +print("done."); diff --git a/tslang/test/tester/import-chain/order_z_profiler.ts b/tslang/test/tester/import-chain/order_z_profiler.ts new file mode 100644 index 000000000..4cd19a542 --- /dev/null +++ b/tslang/test/tester/import-chain/order_z_profiler.ts @@ -0,0 +1,10 @@ +// The class the others name; its module sorts last by name. See order_program.ts. + +export class Profiler { + reads = 0; + + read(): boolean { + this.reads++; + return this.reads > 1; + } +}