Skip to content

Commit

Permalink
Lazily IR-declare global variables
Browse files Browse the repository at this point in the history
Analogous to ldc-developers#4420.
  • Loading branch information
kinke committed Sep 7, 2023
1 parent 16b3c7e commit b81854e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
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

0 comments on commit b81854e

Please sign in to comment.