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
74 changes: 51 additions & 23 deletions tslang/lib/TypeScript/MLIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<StringRef> declaredSymbols;
SmallVector<LibraryDeclarations> pendingDeclarations;
for (auto declSymbol : symbols)
{
if (emittedFiles.contains(declSymbol) || declSymbolsInProgress.contains(declSymbol))
Expand Down Expand Up @@ -1989,36 +2003,50 @@ 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
{
emitWarning(location, "missing information about shared library. (reference " SHARED_LIB_DECLARATIONS " is missing)");
}
}

while (!pendingDeclarations.empty())
{
SmallVector<LibraryDeclarations> 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)
Expand Down
5 changes: 5 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions tslang/test/tester/import-chain/order_a_saves.ts
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 10 additions & 0 deletions tslang/test/tester/import-chain/order_m_state.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
29 changes: 29 additions & 0 deletions tslang/test/tester/import-chain/order_program.ts
Original file line number Diff line number Diff line change
@@ -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_<module>, 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.");
10 changes: 10 additions & 0 deletions tslang/test/tester/import-chain/order_z_profiler.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading