Skip to content

Commit 8f0012d

Browse files
authored
[NVPTX] Use .common linkage for common globals (#84416)
Switch from `.weak` to `.common` linkage for common global variables where possible. The `.common` linkage is described in [PTX ISA 11.6.4. Linking Directives: .common](https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#linking-directives-common) > Declares identifier to be globally visible but “common”. > >Common symbols are similar to globally visible symbols. However multiple object files may declare the same common symbol and they may have different types and sizes and references to a symbol get resolved against a common symbol with the largest size. > >Only one object file can initialize a common symbol and that must have the largest size among all other definitions of that common symbol from different object files. > >.common linking directive can be used only on variables with .global storage. It cannot be used on function symbols or on symbols with opaque type.
1 parent 00ba2a6 commit 8f0012d

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,14 +1019,16 @@ void NVPTXAsmPrinter::printModuleLevelGV(const GlobalVariable *GVar,
10191019
const DataLayout &DL = getDataLayout();
10201020

10211021
// GlobalVariables are always constant pointers themselves.
1022-
PointerType *PTy = GVar->getType();
10231022
Type *ETy = GVar->getValueType();
10241023

10251024
if (GVar->hasExternalLinkage()) {
10261025
if (GVar->hasInitializer())
10271026
O << ".visible ";
10281027
else
10291028
O << ".extern ";
1029+
} else if (GVar->hasCommonLinkage() &&
1030+
GVar->getAddressSpace() == ADDRESS_SPACE_GLOBAL) {
1031+
O << ".common ";
10301032
} else if (GVar->hasLinkOnceLinkage() || GVar->hasWeakLinkage() ||
10311033
GVar->hasAvailableExternallyLinkage() ||
10321034
GVar->hasCommonLinkage()) {
@@ -1138,7 +1140,7 @@ void NVPTXAsmPrinter::printModuleLevelGV(const GlobalVariable *GVar,
11381140
}
11391141

11401142
O << ".";
1141-
emitPTXAddressSpace(PTy->getAddressSpace(), O);
1143+
emitPTXAddressSpace(GVar->getAddressSpace(), O);
11421144

11431145
if (isManaged(*GVar)) {
11441146
if (STI.getPTXVersion() < 40 || STI.getSmVersion() < 30) {
@@ -1167,8 +1169,8 @@ void NVPTXAsmPrinter::printModuleLevelGV(const GlobalVariable *GVar,
11671169
// Ptx allows variable initilization only for constant and global state
11681170
// spaces.
11691171
if (GVar->hasInitializer()) {
1170-
if ((PTy->getAddressSpace() == ADDRESS_SPACE_GLOBAL) ||
1171-
(PTy->getAddressSpace() == ADDRESS_SPACE_CONST)) {
1172+
if ((GVar->getAddressSpace() == ADDRESS_SPACE_GLOBAL) ||
1173+
(GVar->getAddressSpace() == ADDRESS_SPACE_CONST)) {
11721174
const Constant *Initializer = GVar->getInitializer();
11731175
// 'undef' is treated as there is no value specified.
11741176
if (!Initializer->isNullValue() && !isa<UndefValue>(Initializer)) {
@@ -1183,7 +1185,7 @@ void NVPTXAsmPrinter::printModuleLevelGV(const GlobalVariable *GVar,
11831185
!isa<UndefValue>(GVar->getInitializer())) {
11841186
report_fatal_error("initial value of '" + GVar->getName() +
11851187
"' is not allowed in addrspace(" +
1186-
Twine(PTy->getAddressSpace()) + ")");
1188+
Twine(GVar->getAddressSpace()) + ")");
11871189
}
11881190
}
11891191
}
@@ -1202,8 +1204,8 @@ void NVPTXAsmPrinter::printModuleLevelGV(const GlobalVariable *GVar,
12021204
ElementSize = DL.getTypeStoreSize(ETy);
12031205
// Ptx allows variable initilization only for constant and
12041206
// global state spaces.
1205-
if (((PTy->getAddressSpace() == ADDRESS_SPACE_GLOBAL) ||
1206-
(PTy->getAddressSpace() == ADDRESS_SPACE_CONST)) &&
1207+
if (((GVar->getAddressSpace() == ADDRESS_SPACE_GLOBAL) ||
1208+
(GVar->getAddressSpace() == ADDRESS_SPACE_CONST)) &&
12071209
GVar->hasInitializer()) {
12081210
const Constant *Initializer = GVar->getInitializer();
12091211
if (!isa<UndefValue>(Initializer) && !Initializer->isNullValue()) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
; RUN: llc < %s -march=nvptx -mcpu=sm_20 | FileCheck %s
2+
; RUN: %if ptxas %{ llc < %s -march=nvptx64 -mcpu=sm_20 | %ptxas-verify %}
3+
4+
; CHECK: .common .global .align 4 .u32 g
5+
@g = common addrspace(1) global i32 0, align 4
6+
7+
; CHECK: .weak .const .align 4 .u32 c
8+
@c = common addrspace(4) global i32 0, align 4
9+
10+
; CHECK: .weak .shared .align 4 .u32 s
11+
@s = common addrspace(3) global i32 0, align 4
12+
13+
define i32 @f1() {
14+
%1 = load i32, ptr addrspace(1) @g
15+
ret i32 %1
16+
}
17+
18+
define i32 @f4() {
19+
%1 = load i32, ptr addrspace(4) @c
20+
ret i32 %1
21+
}
22+
23+
define i32 @f3() {
24+
%1 = load i32, ptr addrspace(3) @s
25+
ret i32 %1
26+
}

llvm/test/CodeGen/NVPTX/weak-global.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; RUN: llc < %s -march=nvptx64 -mcpu=sm_20 | FileCheck %s
22
; RUN: %if ptxas %{ llc < %s -march=nvptx64 -mcpu=sm_20 | %ptxas-verify %}
33

4-
; CHECK: .weak .global .align 4 .u32 g
4+
; CHECK: .common .global .align 4 .u32 g
55
@g = common addrspace(1) global i32 zeroinitializer
66

77
define i32 @func0() {

0 commit comments

Comments
 (0)