[llvm-commits] [PATCH] Enable MCJIT on cygming

NAKAMURA Takumi geek4civic at gmail.com
Wed Oct 3 22:28:43 PDT 2012


LLIMCJITMemoryManager does not suppress __main.
On cygwin-1.7, multiple invocation of __main would call global ctors
also from JIT context.
(It was similar to http://llvm.org/bugs/show_bug.cgi?id=3897 )

I have a couple of options.

[0001] Like legacy MemoryManager, allocate noop to __main.

[0002] Suppress emitting __main with triple=i686-cygming-elf.
  It could be supposed that Win32-ELF would not be linked to crt.


[0003] Enable MCJIT tests also on cygwin.
  Note, MCJIT has been available apparently on mingw. __main is
reentrant on mingw crt. (not on cygwin-1.7)


Which option can I take?

...Takumi
-------------- next part --------------
From 111a5cd3911672a429a5ea6507f1c3f7f20c34a5 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Thu, 4 Oct 2012 13:54:17 +0900
Subject: [PATCH 1/3] lli: [MCJIT] Suppress "__main" for cygming in LLIMCJITMemoryManager::getPointerToNamedFunction(), like legacy JITMemoryManager's.
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="------------1.7.4.1"

This is a multi-part message in MIME format.
--------------1.7.4.1
Content-Type: text/plain; charset=UTF-8; format=fixed
Content-Transfer-Encoding: 8bit


CRT's __main (aka premain) invokes global ctors on cygming.
---
 llvm/tools/lli/lli.cpp |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)


--------------1.7.4.1
Content-Type: text/x-patch; name="0001-lli-MCJIT-Suppress-__main-for-cygming-in-LLIMCJI.patch.txt"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="0001-lli-MCJIT-Suppress-__main-for-cygming-in-LLIMCJI.patch.txt"

diff --git a/llvm/tools/lli/lli.cpp b/llvm/tools/lli/lli.cpp
index c5645ec..57a31f2 100644
--- a/llvm/tools/lli/lli.cpp
+++ b/llvm/tools/lli/lli.cpp
@@ -338,6 +338,10 @@ void LLIMCJITMemoryManager::invalidateInstructionCache() {
                                             AllocatedCodeMem[i].size());
 }
 
+static int jit_noop() {
+  return 0;
+}
+
 void *LLIMCJITMemoryManager::getPointerToNamedFunction(const std::string &Name,
                                                        bool AbortOnFailure) {
 #if defined(__linux__)
@@ -360,6 +364,14 @@ void *LLIMCJITMemoryManager::getPointerToNamedFunction(const std::string &Name,
   if (Name == "mknod") return (void*)(intptr_t)&mknod;
 #endif // __linux__
 
+  // We should not invoke parent's ctors/dtors from generated main()!
+  // On Mingw and Cygwin, the symbol __main is resolved to
+  // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
+  // (and register wrong callee's dtors with atexit(3)).
+  // We expect ExecutionEngine::runStaticConstructorsDestructors()
+  // is called before ExecutionEngine::runFunctionAsMain() is called.
+  if (Name == "__main") return (void*)(intptr_t)&jit_noop;
+
   const char *NameStr = Name.c_str();
   void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
   if (Ptr) return Ptr;

--------------1.7.4.1--


-------------- next part --------------
From bc6ff0f19aa678700b7c3d8d6af5f6adafd5ca27 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Thu, 4 Oct 2012 13:54:58 +0900
Subject: [PATCH 2/3] X86ISelDAGToDAG.cpp: Suppress emitting __main in cygming-elf. Win32 ELF emitter may be unaware of CRT's functionality.
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="------------1.7.4.1"

This is a multi-part message in MIME format.
--------------1.7.4.1
Content-Type: text/plain; charset=UTF-8; format=fixed
Content-Transfer-Encoding: 8bit

---
 llvm/lib/Target/X86/X86ISelDAGToDAG.cpp |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)


--------------1.7.4.1
Content-Type: text/x-patch; name="0002-X86ISelDAGToDAG.cpp-Suppress-emitting-__main-in-.patch.txt"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="0002-X86ISelDAGToDAG.cpp-Suppress-emitting-__main-in-.patch.txt"

diff --git a/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp b/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
index 98778c3..b931105 100644
--- a/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
+++ b/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
@@ -552,7 +552,7 @@ void X86DAGToDAGISel::PreprocessISelDAG() {
 void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
                                              MachineFrameInfo *MFI) {
   const TargetInstrInfo *TII = TM.getInstrInfo();
-  if (Subtarget->isTargetCygMing()) {
+  if (!Subtarget->isTargetELF() && Subtarget->isTargetCygMing()) {
     unsigned CallOp =
       Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32;
     BuildMI(BB, DebugLoc(),

--------------1.7.4.1--


-------------- next part --------------
From f489f2d838de5e9e31fc055743c6d755a7c6f404 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Thu, 4 Oct 2012 13:53:42 +0900
Subject: [PATCH 3/3] Enable llvm/test/ExecutionEngine/MCJIT also for cygwin.
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="------------1.7.4.1"

This is a multi-part message in MIME format.
--------------1.7.4.1
Content-Type: text/plain; charset=UTF-8; format=fixed
Content-Transfer-Encoding: 8bit

---
 llvm/test/ExecutionEngine/MCJIT/lit.local.cfg |    2 +-
 llvm/test/lit.cfg                             |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)


--------------1.7.4.1
Content-Type: text/x-patch; name="0003-Enable-llvm-test-ExecutionEngine-MCJIT-also-for-.patch.txt"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="0003-Enable-llvm-test-ExecutionEngine-MCJIT-also-for-.patch.txt"

diff --git a/llvm/test/ExecutionEngine/MCJIT/lit.local.cfg b/llvm/test/ExecutionEngine/MCJIT/lit.local.cfg
index 420d955..af3d13f 100644
--- a/llvm/test/ExecutionEngine/MCJIT/lit.local.cfg
+++ b/llvm/test/ExecutionEngine/MCJIT/lit.local.cfg
@@ -16,5 +16,5 @@ else:
 if root.host_arch not in ['x86', 'x86_64', 'ARM', 'Mips']:
     config.unsupported = True
 
-if root.host_os in ['Cygwin', 'Darwin']:
+if root.host_os in ['Darwin']:
     config.unsupported = True
diff --git a/llvm/test/lit.cfg b/llvm/test/lit.cfg
index d815633..dc37317 100644
--- a/llvm/test/lit.cfg
+++ b/llvm/test/lit.cfg
@@ -142,7 +142,7 @@ if config.test_exec_root is None:
 # Provide a target triple for mcjit tests
 mcjit_triple = config.target_triple
 # Force ELF format on Windows
-if re.search(r'mingw32|win32', mcjit_triple):
+if re.search(r'cygwin|mingw32|win32', mcjit_triple):
   mcjit_triple += "-elf"
 config.substitutions.append( ('%mcjit_triple', mcjit_triple) )
 

--------------1.7.4.1--




More information about the llvm-commits mailing list