Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazily IR-declare global variables #4491

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 5 additions & 2 deletions gen/declarations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,12 @@ class CodegenVisitor : public Visitor {
decl->toPrettyChars());
LOG_SCOPE;

if (decl->ir->isDefined()) {
if (decl->ir->isDefined())
return;

// skip external declarations (IR-declared lazily)
if (decl->storage_class & STCextern)
return;
}

if (decl->type->ty == TY::Terror) {
decl->error("had semantic errors when compiling");
Expand Down
2 changes: 2 additions & 0 deletions tests/codegen/avr.d
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ version (D_SoftFloat) {} else static assert(0);
int definedGlobal = 123;
// CHECK: @_D3avr14declaredGlobali = external global i32
extern int declaredGlobal;

int dummyRef() { return declaredGlobal; } // make sure `declaredGlobal` is IR-declared
2 changes: 0 additions & 2 deletions tests/codegen/wasi.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ version (CRuntime_WASI) {} else static assert(0);

// CHECK: @_D4wasi13definedGlobali = global i32 123
int definedGlobal = 123;
// CHECK: @_D4wasi14declaredGlobali = external global i32
extern int declaredGlobal;


// make sure the ModuleInfo ref is emitted into the __minfo section:
Expand Down
11 changes: 10 additions & 1 deletion tests/fail_compilation/global_var_collision.d
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
// It should compile fine when not referencing the colliding external global:
// RUN: %ldc -c %s -d-version=DontReference

// But fail if referenced:
// RUN: not %ldc -c %s 2>&1 | FileCheck %s

extern(C) extern int myGlobal;

// CHECK: global_var_collision.d(9): Error: Global variable type does not match previous declaration with same mangled name: `myGlobal`
version (DontReference) {} else
{
int dummyRef() { return myGlobal; }
}

// CHECK: global_var_collision.d([[@LINE+4]]): Error: Global variable type does not match previous declaration with same mangled name: `myGlobal`
// CHECK-NEXT: Previous IR type: i32, mutable, thread-local
// CHECK-NEXT: New IR type: i64, const, non-thread-local
pragma(mangle, myGlobal.mangleof)
Expand Down
Loading