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
194 changes: 194 additions & 0 deletions docs/superpowers/plans/2026-09-22-32-bit-phase-4c-suite.md

Large diffs are not rendered by default.

64 changes: 55 additions & 9 deletions docs/superpowers/specs/2026-09-20-32-bit-compilation-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
Design for compiling TypeScript to 32-bit targets, with `i686-pc-windows-msvc`
as the proof target and width-correctness that generalizes to any 32-bit triple.

Status: approved design. Phases 1-3, 4a and 4b implemented; 4c not.
Status: approved design. Phases 1-4 implemented, except the x86 default library
(deferred; see Open issues).

## Why now

Expand Down Expand Up @@ -289,8 +290,44 @@ only, so phase 4 is three PRs:
cast chains into `sext`/`trunc`. x64 never runs them. All six async corpus
files run at i686 under gc (async needs Boehm's thread API, so under rc and
none it does not link at x64 either); wasm32 had the same allocator bug.
- **4c — the suite at i686:** test-runner arch flag, x86 default library,
reading `__decls` from a DLL file, and the remaining failures.
- **4c — the suite at i686 (done, without the x86 default library).**
`test-runner -x86` builds and runs a test as a 32-bit program (i686 triple,
the x86 library directories, `/machine:x86`). Configure with
`-DTSLANG_TEST_X86=ON`, then run `ctest -C Release -L x86`; the option is off
by default, so the default suite and CI keep their 2769 test names (plus the
two foreign-target import tests below). With it on, every `test-runner`
registration without `-jit` gets a `test-x86-...` twin: 1398 twins. The
1357 `-jit` registrations get none,
because `--emit=jit` runs in the x64 compiler process and refuses a foreign
arch. Only one test file, `02funcs_vararg.ts`, is JIT-only at the file level;
every other file has a compile registration and so a twin. The cmake-script
tests have no twin either. There were 12 of them: `rc-debug-info`,
`gc-shared-auto`, the two default-library collector tests (the default
library has no x86 build) and the 8 ownership-verifier shards. The two
foreign-target import tests below make 14.
1395 twins pass; 3 are registered `DISABLED` in
`tslang/test/tester/x86-exclusions.cmake`: `internals` under gc, rc and
none, because its `inline_asm<i64>` with an `=r` constraint needs a 64-bit
register that i686 does not have. Importing a DLL now reads `__decls` from
the file when the target's arch or OS is not the host's
(`targetInfo.supportsInProcessJit` is false); the host path, which loads the
DLL, is unchanged. Such an import must be a PE DLL for the target's machine,
or it is an error. Two cmake-script tests in the default suite,
`test-compile-foreign-target-import-read` and `-errors`
(`foreign-target-import.cmake`), cover that path without x86 libraries: an
x64 DLL imported by a program compiled for x86_64 Linux, then a non-PE file
and an x64 DLL imported into an x86 program.
The last three failures were not a compiler defect but Windows installer
detection: a 32-bit exe with no `requestedExecutionLevel` manifest, whose
name contains `setup`, `install`, `update` or `patch`, needs elevation and,
unelevated, never starts (`...-abstract-virtual-dispatch`: "dispatch" holds
"patch"). x64 exes are exempt. tslang now links an asInvoker `RT_MANIFEST`
resource object into every Windows x86 `--emit=exe` (not DLLs, which are not
launched, and not x64, whose output stays unchanged); it builds the object
itself because `-manifest:embed` makes MSVC's link.exe run `rc.exe`, which is
on `PATH` only in a developer prompt. The runner passes lld-link
`/manifest:embed /manifestuac:...` for x86 exes. `check-x86-run.sh` builds
hello as `my_setup_patch.exe` and runs it.

Remaining i686-only probe failures after 4b: `internals` (an `inline_asm<i64>` with an `=r` constraint, which no single
i686 register can satisfy); `02funcs_vararg` (unresolved `_printf`; it is
Expand Down Expand Up @@ -318,12 +355,21 @@ confusion:
data layout says `p:32`. Nothing asserts the two agree. Proposed fix: a check
in `lib/TypeScript/MLIRGenModule.cpp` comparing
`createDataLayout().getPointerSizeInBits(0)` with `compileOptions.sizeBits()`.
- **Importing an x86 DLL (phase 4).** An x86 `--emit=dll` builds, but compiling
a program that imports it fails at compile time with
`./lib.dll: Can't open: Unknown error (0xC1)`.
`MLIRGenImpl::mlirGenImportSharedLib` calls `getPermanentLibrary`, which loads
the x86 DLL into the x64 compiler process to read its `__decls`. The fix is to
read `__decls` from the file rather than by loading it.
- **Importing an x86 DLL (phase 4) — closed in 4c.** Compiling a program that
imported an x86 DLL failed with `./lib.dll: Can't open: Unknown error (0xC1)`:
`MLIRGenImpl::mlirGenImportSharedLib` loaded the DLL into the x64 compiler to
read its `__decls`. For a foreign target (arch or OS) it now reads the string from
the file (`Dump::readExportedCString`); the x86 `-shared` twins pass.
- **The x86 default library (deferred from phase 4).** `getDefaultLibSubDir`
in `include/TypeScript/Defines.h` still has no arch segment, and the default
library, built in the separate `TypeScriptCompilerDefaultLib` repository, has
no x86 build. Both are needed before an x86 program can link the default
library; until then x86 programs use `--no-default-lib`, and the collector
test that links the real default library has no x86 twin.
- **Per-test flags miss the entry file of multi-file tests (not 32-bit;
pre-existing).** Found in phase 4c: in `test-runner`, `-mm`, `-fast-math`
and `--gctors-as-method` reach only the non-entry files of a multi-file test,
so the entry file compiles with the defaults.
- **`mlirAsyncRuntimGetNumWorkerThreads` is declared `() -> i32` at 32 bits**
(upstream declares it returning `index`) while the runtime returns `int64_t`.
Harmless at i686 cdecl (the low half is in EAX); at wasm32 it would be a
Expand Down
12 changes: 12 additions & 0 deletions tslang/include/TypeScript/ObjDumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/SmallVector.h"

#include <optional>
#include <string>

using namespace llvm;
using namespace llvm::object;

Expand Down Expand Up @@ -82,6 +85,15 @@ namespace Dump
// library - for a library, that of its first member that names one - or 0 when there is none
// to be read. A query: an unreadable or non-COFF file is 0, not an error.
uint16_t coffMachine(llvm::StringRef);

// The name a user knows a COFF machine by ("x86", "x64", "arm64"), or its number in hex.
std::string coffMachineName(uint16_t);

// The NUL-terminated string that the exported pointer variable `symbol` (a `const char *`)
// points to in a PE DLL, read from the file without loading it - for a DLL built for another
// architecture, which this process cannot load. std::nullopt when the file is not a PE image,
// does not export `symbol`, or the pointer or string lies outside the file's data.
std::optional<std::string> readExportedCString(llvm::StringRef path, llvm::StringRef symbol);
}

std::unique_ptr<Dumper> createCOFFDumper(const COFFObjectFile &);
Expand Down
91 changes: 83 additions & 8 deletions tslang/lib/TypeScript/MLIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -981,20 +981,68 @@ namespace mlirgen

mlir::LogicalResult MLIRGenImpl::mlirGenImportSharedLib(mlir::Location location, StringRef filePath, bool dynamic, const GenContext &genContext)
{
// 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.
auto loadIntoCompiler = compileOptions.targetInfo.supportsInProcessJit;

// TODO: ...
std::string errMsg;
auto dynLib = llvm::sys::DynamicLibrary::getPermanentLibrary(filePath.str().c_str(), &errMsg);
if (!dynLib.isValid())
llvm::sys::DynamicLibrary dynLib;
if (loadIntoCompiler)
{
emitError(location, errMsg);
return mlir::failure();
std::string errMsg;
dynLib = llvm::sys::DynamicLibrary::getPermanentLibrary(filePath.str().c_str(), &errMsg);
if (!dynLib.isValid())
{
emitError(location, errMsg);
return mlir::failure();
}
}
else
{
// Only a PE image's declarations can be read from the file (Dump::readExportedCString).
auto machine = Dump::coffMachine(filePath);
if (machine == 0)
{
emitError(location) << "cannot read declarations from '" << filePath
<< "': for a target other than the host, only PE DLLs can be imported";
return mlir::failure();
}

// A DLL for another machine would link, and then not load in the program.
uint16_t expected = 0;
llvm::Triple targetTriple(compileOptions.moduleTargetTriple);
switch (targetTriple.getArch())
{
case llvm::Triple::x86:
expected = llvm::COFF::IMAGE_FILE_MACHINE_I386;
break;
case llvm::Triple::x86_64:
expected = llvm::COFF::IMAGE_FILE_MACHINE_AMD64;
break;
case llvm::Triple::aarch64:
expected = llvm::COFF::IMAGE_FILE_MACHINE_ARM64;
break;
default:
break;
}

if (machine != expected)
{
emitError(location) << "shared library '" << filePath << "' is built for "
<< Dump::coffMachineName(machine) << ", but this program targets "
<< (expected ? Dump::coffMachineName(expected)
: targetTriple.getArchName().str());
return mlir::failure();
}
}

SmallVector<StringRef> symbols;
StringRef mlirGctors;
// every symbol the library exports
SmallVector<StringRef> symbolsAll;
#ifndef GENERATE_IMPORT_INFO_USING_D_TS_FILE
// loading Binary to get list of symbols
SmallVector<StringRef> symbolsAll;
Dump::getSymbols(filePath, symbolsAll, stringAllocator);

StringRef memoryModelSymbol;
Expand Down Expand Up @@ -1051,6 +1099,11 @@ namespace mlirgen
#else
// only 1 file to load
symbols.push_back(SHARED_LIB_DECLARATIONS_2UNDERSCORE);
if (!loadIntoCompiler)
{
// read from the file below, which tells a missing symbol from an unreadable one
Dump::getSymbols(filePath, symbolsAll, stringAllocator);
}
#endif

if (symbols.empty())
Expand Down Expand Up @@ -1107,11 +1160,33 @@ namespace mlirgen
for (auto declSymbol : symbols)
{
// TODO: for now, we have code in TS to load methods from DLL/Shared libs
if (auto addrOfDeclText = dynLib.getAddressOfSymbol(declSymbol.str().c_str()))
const char *declText = nullptr;
std::optional<std::string> declTextFromFile;
if (loadIntoCompiler)
{
if (auto addrOfDeclText = dynLib.getAddressOfSymbol(declSymbol.str().c_str()))
{
declText = *(const char**)addrOfDeclText;
}
}
else if (llvm::is_contained(symbolsAll, declSymbol))
{
declTextFromFile = Dump::readExportedCString(filePath, declSymbol);
if (!declTextFromFile)
{
emitError(location) << "shared library '" << filePath << "' exports " << declSymbol
<< " but its declarations could not be read from the file";
return mlir::failure();
}

declText = declTextFromFile->c_str();
}

if (declText)
{
std::string result;
// process shared lib declarations
auto dataPtr = *(const char**)addrOfDeclText;
auto dataPtr = declText;
if (dynamic)
{
// TODO: use option variable instead of "this hack"
Expand Down
Loading
Loading