[PATCH] D27547: Allow common linkage global var have non-zero initializer

Yaxun Liu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 7 14:47:15 PST 2016


yaxunl created this revision.
yaxunl added a reviewer: tstellarAMD.
yaxunl added a subscriber: llvm-commits.
Herald added a subscriber: wdng.

In C99 tentative definition of file-scope variable results in common linkage, e.g.

  private char *p;
  private char *p;

which are initialized by default initializer. For pointer type global variable, the default initializer is null pointer, which does not necessarily have zero value. If a structure contains  pointer type member for which null pointer does not have zero value, the strucutre cannot be initialized with zeroinitializer.

Currently IR Verifier asserts when a global variable with common linkage has an initializer which is not zeroinitializer, which it should not. This patch fixes that.


https://reviews.llvm.org/D27547

Files:
  lib/IR/Verifier.cpp
  test/Verifier/common-linkage-initializer.ll


Index: test/Verifier/common-linkage-initializer.ll
===================================================================
--- /dev/null
+++ test/Verifier/common-linkage-initializer.ll
@@ -0,0 +1,21 @@
+; REQUIRES: asserts
+; RUN: llvm-as < %s -o /dev/null 2>&1
+;
+; Test the IR verifier does not assert when a global variable with common linkage
+; has an initializer which is not zeroinitializer.
+;
+; In C99 tentative definition of file-scope variable results in common linkage,
+; e.g.
+;   private char *p;
+;   private char *p;
+; which are initialized by default initializer. For pointer type global variable,
+; the default initializer is null pointer, which does not necessarily have zero
+; value. If a structure contains pointer type member for which null pointer does
+; not have zero value, the strucutre cannot be initialized with zeroinitializer.
+ 
+target datalayout = "e-p:32:32-p1:64:64-p2:64:64-p3:32:32-p4:64:64-p5:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64"
+target triple = "amdgcn"
+
+%struct.ST = type <{ i8*, i8 addrspace(1)*, i8 addrspace(2)*, i8 addrspace(3)*, i8 addrspace(4)* }>
+
+ at st = common addrspace(1) global %struct.ST <{ i8* addrspacecast (i8 addrspace(4)* null to i8*), i8 addrspace(1)* null, i8 addrspace(2)* null, i8 addrspace(3)* addrspacecast (i8 addrspace(4)* null to i8 addrspace(3)*), i8 addrspace(4)* null }>, align 4
Index: lib/IR/Verifier.cpp
===================================================================
--- lib/IR/Verifier.cpp
+++ lib/IR/Verifier.cpp
@@ -571,8 +571,6 @@
     // If the global has common linkage, it must have a zero initializer and
     // cannot be constant.
     if (GV.hasCommonLinkage()) {
-      Assert(GV.getInitializer()->isNullValue(),
-             "'common' global must have a zero initializer!", &GV);
       Assert(!GV.isConstant(), "'common' global may not be marked constant!",
              &GV);
       Assert(!GV.hasComdat(), "'common' global may not be in a Comdat!", &GV);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27547.80668.patch
Type: text/x-patch
Size: 2026 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161207/62d50561/attachment-0001.bin>


More information about the llvm-commits mailing list