Skip to content

A module with a top-level statement, imported as source, fails with "Global is referenced by parentless instruction" #447

Closed

Description

@ASDAlexander77

A module that has a top-level statement fails to compile when another module imports it as source, i.e. without -shared. This happens under every memory model. The error is invalid LLVM IR:

export_tl.ts:

export function twice(n: number) {
    return n * 2;
}

print("module init");

import_tl.ts:

import "./export_tl";

function main() {
    print(twice(21));
    print("done.");
}
test-runner -mm=gc import_tl.ts export_tl.ts
Global is referenced by parentless instruction!
ptr @puts
; ModuleID = 'LLVMDialectModule'
  call addrspace(0) void @puts(ptr getelementptr inbounds nuw (i8, ptr @s_..., i64 8))
tslang: error: Failed to emit LLVM IR

The result is the same under -mm=rc, and with export const k = 5; in place of the function. Removing the top-level print makes it compile. The same pair with -shared also compiles and runs.

It looks as if the imported module's top-level code is generated with no enclosing function, or into one that is then dropped. The puts call is left with no parent.

Found during -mm=own phase 6 (#445, spec §20.5).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions