Skip to content

Commit 605343a

Browse files
authored
[CIR][CodeGen] Set constant properly for global variables (#904)
Fix #801 (the remaining `constant` part). Actually the missing stage is CIRGen. There are two places where `GV.setConstant` is called: * `buildGlobalVarDefinition` * `getOrCreateCIRGlobal` Therefore, the primary test `global-constant.c` contains a global definition and a global declaration with use, which should be enough to cover the two paths. A test for OpenCL `constant` qualified global is also added. Some existing testcases need tweaking to avoid failure of missing constant.
1 parent 88cdb8d commit 605343a

File tree

6 files changed

+41
-22
lines changed

6 files changed

+41
-22
lines changed

clang/lib/CIR/CodeGen/CIRGenModule.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -946,9 +946,8 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef MangledName, mlir::Type Ty,
946946
// FIXME: This code is overly simple and should be merged with other global
947947
// handling.
948948
GV.setAlignmentAttr(getSize(astCtx.getDeclAlign(D)));
949-
// TODO(cir):
950-
// GV->setConstant(isTypeConstant(D->getType(), false));
951-
// setLinkageForGV(GV, D);
949+
GV.setConstant(isTypeConstant(D->getType(), false, false));
950+
// TODO(cir): setLinkageForGV(GV, D);
952951

953952
if (D->getTLSKind()) {
954953
if (D->getTLSKind() == VarDecl::TLS_Dynamic)
@@ -1277,8 +1276,8 @@ void CIRGenModule::buildGlobalVarDefinition(const clang::VarDecl *D,
12771276
emitter->finalize(GV);
12781277

12791278
// TODO(cir): If it is safe to mark the global 'constant', do so now.
1280-
// GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor &&
1281-
// isTypeConstant(D->getType(), true));
1279+
GV.setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor &&
1280+
isTypeConstant(D->getType(), true, true));
12821281

12831282
// If it is in a read-only section, mark it 'constant'.
12841283
if (const SectionAttr *SA = D->getAttr<SectionAttr>())

clang/test/CIR/CodeGen/OpenCL/global.cl

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ global int b = 15;
1111
// CIR-DAG: cir.global external addrspace(offload_global) @b = #cir.int<15> : !s32i
1212
// LLVM-DAG: @b = addrspace(1) global i32 15
1313

14+
constant int c[2] = {18, 21};
15+
// CIR-DAG: cir.global constant {{.*}}addrspace(offload_constant) {{.*}}@c
16+
// LLVM-DAG: @c = addrspace(2) constant
17+
1418
kernel void test_get_global() {
1519
a = b;
1620
// CIR: %[[#ADDRB:]] = cir.get_global @b : !cir.ptr<!s32i, addrspace(offload_global)>

clang/test/CIR/CodeGen/cxx1z-inline-variables.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ const int &compat_use_after_redecl1 = compat::c;
2626
const int &compat_use_after_redecl2 = compat::d;
2727
const int &compat_use_after_redecl3 = compat::g;
2828

29-
// CIR: cir.global weak_odr comdat @_ZN6compat1bE = #cir.int<2> : !s32i {alignment = 4 : i64}
30-
// CIR: cir.global weak_odr comdat @_ZN6compat1aE = #cir.int<1> : !s32i {alignment = 4 : i64}
31-
// CIR: cir.global weak_odr comdat @_ZN6compat1cE = #cir.int<3> : !s32i {alignment = 4 : i64}
32-
// CIR: cir.global external @_ZN6compat1eE = #cir.int<5> : !s32i {alignment = 4 : i64}
33-
// CIR: cir.global weak_odr comdat @_ZN6compat1fE = #cir.int<6> : !s32i {alignment = 4 : i64}
34-
// CIR: cir.global linkonce_odr comdat @_ZN6compat1dE = #cir.int<4> : !s32i {alignment = 4 : i64}
35-
// CIR: cir.global linkonce_odr comdat @_ZN6compat1gE = #cir.int<7> : !s32i {alignment = 4 : i64}
29+
// CIR: cir.global constant weak_odr comdat @_ZN6compat1bE = #cir.int<2> : !s32i {alignment = 4 : i64}
30+
// CIR: cir.global constant weak_odr comdat @_ZN6compat1aE = #cir.int<1> : !s32i {alignment = 4 : i64}
31+
// CIR: cir.global constant weak_odr comdat @_ZN6compat1cE = #cir.int<3> : !s32i {alignment = 4 : i64}
32+
// CIR: cir.global constant external @_ZN6compat1eE = #cir.int<5> : !s32i {alignment = 4 : i64}
33+
// CIR: cir.global constant weak_odr comdat @_ZN6compat1fE = #cir.int<6> : !s32i {alignment = 4 : i64}
34+
// CIR: cir.global constant linkonce_odr comdat @_ZN6compat1dE = #cir.int<4> : !s32i {alignment = 4 : i64}
35+
// CIR: cir.global constant linkonce_odr comdat @_ZN6compat1gE = #cir.int<7> : !s32i {alignment = 4 : i64}
3636

3737
// LLVM: $_ZN6compat1bE = comdat any
3838
// LLVM: $_ZN6compat1aE = comdat any
@@ -41,10 +41,10 @@ const int &compat_use_after_redecl3 = compat::g;
4141
// LLVM: $_ZN6compat1dE = comdat any
4242
// LLVM: $_ZN6compat1gE = comdat any
4343

44-
// LLVM: @_ZN6compat1bE = weak_odr global i32 2, comdat, align 4
45-
// LLVM: @_ZN6compat1aE = weak_odr global i32 1, comdat, align 4
46-
// LLVM: @_ZN6compat1cE = weak_odr global i32 3, comdat, align 4
47-
// LLVM: @_ZN6compat1eE = global i32 5, align 4
48-
// LLVM: @_ZN6compat1fE = weak_odr global i32 6, comdat, align 4
49-
// LLVM: @_ZN6compat1dE = linkonce_odr global i32 4, comdat, align 4
50-
// LLVM: @_ZN6compat1gE = linkonce_odr global i32 7, comdat, align 4
44+
// LLVM: @_ZN6compat1bE = weak_odr constant i32 2, comdat, align 4
45+
// LLVM: @_ZN6compat1aE = weak_odr constant i32 1, comdat, align 4
46+
// LLVM: @_ZN6compat1cE = weak_odr constant i32 3, comdat, align 4
47+
// LLVM: @_ZN6compat1eE = constant i32 5, align 4
48+
// LLVM: @_ZN6compat1fE = weak_odr constant i32 6, comdat, align 4
49+
// LLVM: @_ZN6compat1dE = linkonce_odr constant i32 4, comdat, align 4
50+
// LLVM: @_ZN6compat1gE = linkonce_odr constant i32 7, comdat, align 4
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
4+
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
5+
6+
const int global_no_use = 12;
7+
// CIR: cir.global constant {{.*}}@global_no_use
8+
// LLVM: @global_no_use = constant
9+
10+
const float global_used = 1.2f;
11+
// CIR: cir.global constant {{.*}}@global_used
12+
// LLVM: @global_used = constant
13+
14+
float const * get_float_ptr() {
15+
return &global_used;
16+
}

clang/test/CIR/CodeGen/globals.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ struct Glob {
9292
} glob;
9393

9494
double *const glob_ptr = &glob.b[1];
95-
// CHECK: cir.global external @glob_ptr = #cir.global_view<@glob, [2 : i32, 1 : i32]> : !cir.ptr<!cir.double>
95+
// CHECK: cir.global constant external @glob_ptr = #cir.global_view<@glob, [2 : i32, 1 : i32]> : !cir.ptr<!cir.double>
9696

9797
// TODO: test tentatives with internal linkage.
9898

clang/test/CIR/CodeGen/temporaries.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ const unsigned int n = 1234;
3232
const int &r = (const int&)n;
3333

3434
// CHECK: cir.global "private" constant internal @_ZGR1r_ = #cir.int<1234> : !s32i
35-
// CHECK-NEXT: cir.global external @r = #cir.global_view<@_ZGR1r_> : !cir.ptr<!s32i> {alignment = 8 : i64}
35+
// CHECK-NEXT: cir.global constant external @r = #cir.global_view<@_ZGR1r_> : !cir.ptr<!s32i> {alignment = 8 : i64}
3636

3737
// LLVM: @_ZGR1r_ = internal constant i32 1234, align 4
38-
// LLVM-NEXT: @r = global ptr @_ZGR1r_, align 8
38+
// LLVM-NEXT: @r = constant ptr @_ZGR1r_, align 8
3939

0 commit comments

Comments
 (0)