[PATCH] D16739: [CUDA] Die if we ask the NVPTX backend to emit a global ctor/dtor.

Justin Lebar via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 29 15:30:07 PST 2016


jlebar created this revision.
jlebar added a reviewer: tra.
jlebar added subscribers: echristo, jhen, llvm-commits.
Herald added a subscriber: jholewinski.

Previously we'd just silently skip these.

http://reviews.llvm.org/D16739

Files:
  lib/Target/NVPTX/NVPTXAsmPrinter.cpp
  test/CodeGen/NVPTX/global-ctor-empty.ll
  test/CodeGen/NVPTX/global-ctor.ll
  test/CodeGen/NVPTX/global-dtor.ll

Index: test/CodeGen/NVPTX/global-dtor.ll
===================================================================
--- /dev/null
+++ test/CodeGen/NVPTX/global-dtor.ll
@@ -0,0 +1,9 @@
+; RUN: not llc < %s -march=nvptx -mcpu=sm_20 2>&1 | FileCheck %s
+
+; Check that llc dies when given a nonempty global dtor.
+ at llvm.global_dtors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @foo, i8* null }]
+
+; CHECK: ERROR: Module has a nontrivial global dtor
+define internal void @foo() {
+  ret void
+}
Index: test/CodeGen/NVPTX/global-ctor.ll
===================================================================
--- /dev/null
+++ test/CodeGen/NVPTX/global-ctor.ll
@@ -0,0 +1,9 @@
+; RUN: not llc < %s -march=nvptx -mcpu=sm_20 2>&1 | FileCheck %s
+
+; Check that llc dies when given a nonempty global ctor.
+ at llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @foo, i8* null }]
+
+; CHECK: ERROR: Module has a nontrivial global ctor
+define internal void @foo() {
+  ret void
+}
Index: test/CodeGen/NVPTX/global-ctor-empty.ll
===================================================================
--- /dev/null
+++ test/CodeGen/NVPTX/global-ctor-empty.ll
@@ -0,0 +1,5 @@
+; RUN: llc < %s -march=nvptx -mcpu=sm_20 2>&1
+
+; Check that llc doesn't die when given an empty global ctor / dtor.
+ at llvm.global_ctors = appending global [0 x { i32, void ()*, i8* }] []
+ at llvm.global_dtors = appending global [0 x { i32, void ()*, i8* }] []
Index: lib/Target/NVPTX/NVPTXAsmPrinter.cpp
===================================================================
--- lib/Target/NVPTX/NVPTXAsmPrinter.cpp
+++ lib/Target/NVPTX/NVPTXAsmPrinter.cpp
@@ -802,6 +802,13 @@
   }
 }
 
+static bool isEmptyXXStructor(GlobalVariable *GV) {
+  if (!GV) return true;
+  const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
+  if (!InitList) return true;  // Not an array; we don't know how to parse.
+  return InitList->getNumOperands() == 0;
+}
+
 bool NVPTXAsmPrinter::doInitialization(Module &M) {
   // Construct a default subtarget off of the TargetMachine defaults. The
   // rest of NVPTX isn't friendly to change subtargets per function and
@@ -816,6 +823,16 @@
     report_fatal_error("Module has aliases, which NVPTX does not support.");
     return true; // error
   }
+  if (!isEmptyXXStructor(M.getNamedGlobal("llvm.global_ctors"))) {
+    report_fatal_error(
+        "Module has a nontrivial global ctor, which NVPTX does not support.");
+    return true;  // error
+  }
+  if (!isEmptyXXStructor(M.getNamedGlobal("llvm.global_dtors"))) {
+    report_fatal_error(
+        "Module has a nontrivial global dtor, which NVPTX does not support.");
+    return true;  // error
+  }
 
   SmallString<128> Str1;
   raw_svector_ostream OS1(Str1);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D16739.46442.patch
Type: text/x-patch
Size: 2838 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160129/dc8def0e/attachment.bin>


More information about the llvm-commits mailing list