Skip to content

Commit b81854e

Browse files
committed
Lazily IR-declare global variables
Analogous to ldc-developers#4420.
1 parent 16b3c7e commit b81854e

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

gen/declarations.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,12 @@ class CodegenVisitor : public Visitor {
236236
decl->toPrettyChars());
237237
LOG_SCOPE;
238238

239-
if (decl->ir->isDefined()) {
239+
if (decl->ir->isDefined())
240+
return;
241+
242+
// skip external declarations (IR-declared lazily)
243+
if (decl->storage_class & STCextern)
240244
return;
241-
}
242245

243246
if (decl->type->ty == TY::Terror) {
244247
decl->error("had semantic errors when compiling");

tests/codegen/avr.d

+2
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ version (D_SoftFloat) {} else static assert(0);
1111
int definedGlobal = 123;
1212
// CHECK: @_D3avr14declaredGlobali = external global i32
1313
extern int declaredGlobal;
14+
15+
int dummyRef() { return declaredGlobal; } // make sure `declaredGlobal` is IR-declared

tests/codegen/wasi.d

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ version (CRuntime_WASI) {} else static assert(0);
1515

1616
// CHECK: @_D4wasi13definedGlobali = global i32 123
1717
int definedGlobal = 123;
18-
// CHECK: @_D4wasi14declaredGlobali = external global i32
19-
extern int declaredGlobal;
2018

2119

2220
// make sure the ModuleInfo ref is emitted into the __minfo section:

tests/fail_compilation/global_var_collision.d

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
// It should compile fine when not referencing the colliding external global:
2+
// RUN: %ldc -c %s -d-version=DontReference
3+
4+
// But fail if referenced:
15
// RUN: not %ldc -c %s 2>&1 | FileCheck %s
26

37
extern(C) extern int myGlobal;
48

5-
// CHECK: global_var_collision.d(9): Error: Global variable type does not match previous declaration with same mangled name: `myGlobal`
9+
version (DontReference) {} else
10+
{
11+
int dummyRef() { return myGlobal; }
12+
}
13+
14+
// CHECK: global_var_collision.d([[@LINE+4]]): Error: Global variable type does not match previous declaration with same mangled name: `myGlobal`
615
// CHECK-NEXT: Previous IR type: i32, mutable, thread-local
716
// CHECK-NEXT: New IR type: i64, const, non-thread-local
817
pragma(mangle, myGlobal.mangleof)

0 commit comments

Comments
 (0)