[mono/mono] 2735588c: [runtime] Clean up the usage of the CEE_MONO_TLS opcode in wrappers. Instead of passing it the tls offset, pass one of the TLS_KEY_ constants defined in mono-tls.h, and resolve the offset in the JIT. This allows these wrappers to work with AOT.

"Zoltan Varga ([email protected])" <[email protected]>
Newsgroups gmane.comp.gnome.mono.patches
Message-ID <00000141cc47509e-d1950dce-0e24-433a-94c6-4f932c74aae2-000000@email.amazonses.com>
   Branch: refs/heads/master
     Home: https://github.com/mono/mono
  Compare: https://github.com/mono/mono/compare/dc0bf73e8476...2735588c0c11

   Commit: 2735588c0c112253f3f75478621ef8a9d135278f
   Author: Zoltan Varga <[email protected]> (vargaz)
     Date: 2013-10-18 15:54:20 GMT
      URL: https://github.com/mono/mono/commit/2735588c0c112253f3f75478621ef8a9d135278f

[runtime] Clean up the usage of the CEE_MONO_TLS opcode in wrappers. Instead of passing it the tls offset, pass one of the TLS_KEY_ constants defined in mono-tls.h, and resolve the offset in the JIT. This allows these wrappers to work with AOT.

Changed paths:
  M mono/cil/opcode.def
  M mono/metadata/boehm-gc.c
  M mono/metadata/monitor.c
  M mono/metadata/sgen-alloc.c
  M mono/metadata/sgen-gc.c
  M mono/metadata/sgen-gc.h
  M mono/mini/method-to-ir.c
  M mono/mini/mini.c
  M mono/mini/mini.h
  M mono/utils/Makefile.am
  M mono/utils/mono-tls.h

Modified: mono/cil/opcode.def
===================================================================
@@ -305,6 +305,7 @@ OPDEF(CEE_MONO_SAVE_LMF, "mono_save_lmf", Pop0, Push0, InlineNone, X, 2, 0xF0, 0
 OPDEF(CEE_MONO_RESTORE_LMF, "mono_restore_lmf", Pop0, Push0, InlineNone, X, 2, 0xF0, 0x0A, NEXT)
 OPDEF(CEE_MONO_CLASSCONST, "mono_classconst", Pop0, PushI, InlineI, X, 2, 0xF0, 0x0B, NEXT)
 OPDEF(CEE_MONO_NOT_TAKEN, "mono_not_taken", Pop0, Push0, InlineNone, X, 2, 0xF0, 0x0C, NEXT)
+/* The argument should be one of the TLS_KEY values defined in mono-tls.h */
 OPDEF(CEE_MONO_TLS, "mono_tls", Pop0, PushI, InlineI, X, 2, 0xF0, 0x0D, NEXT)
 OPDEF(CEE_MONO_ICALL_ADDR, "mono_icall_addr", Pop0, PushI, InlineI, X, 2, 0xF0, 0x0E, NEXT)
 OPDEF(CEE_MONO_DYN_CALL, "mono_dyn_call", Pop0, PushI, InlineI, X, 2, 0xF0, 0x0F, NEXT)

Modified: mono/metadata/boehm-gc.c
===================================================================
@@ -680,7 +680,7 @@ enum {
 };
 
 static MonoMethod*
-create_allocator (int atype, int offset)
+create_allocator (int atype, int tls_key)
 {
 	int index_var, bytes_var, my_fl_var, my_entry_var;
 	guint32 no_freelist_branch, not_small_enough_branch = 0;
@@ -758,8 +758,8 @@ enum {
 	my_entry_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
 	/* my_fl = ((GC_thread)tsd) -> ptrfree_freelists + index; */
 	mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
-	mono_mb_emit_byte (mb, 0x0D); /* CEE_MONO_TLS */
-	mono_mb_emit_i4 (mb, offset);
+	mono_mb_emit_byte (mb, CEE_MONO_TLS);
+	mono_mb_emit_i4 (mb, tls_key);
 	if (atype == ATYPE_FREEPTR || atype == ATYPE_FREEPTR_FOR_BOX || atype == ATYPE_STRING)
 		mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, ptrfree_freelists));
 	else if (atype == ATYPE_NORMAL)
@@ -966,10 +966,12 @@ enum {
 	MonoMethod *res;
 	MONO_THREAD_VAR_OFFSET (GC_thread_tls, offset);
 
+	mono_tls_key_set_offset (TLS_KEY_BOEHM_GC_THREAD, offset);
+
 	mono_loader_lock ();
 	res = alloc_method_cache [atype];
 	if (!res)
-		res = alloc_method_cache [atype] = create_allocator (atype, offset);
+		res = alloc_method_cache [atype] = create_allocator (atype, TLS_KEY_BOEHM_GC_THREAD);
 	mono_loader_unlock ();
 	return res;
 }

Modified: mono/metadata/monitor.c
===================================================================
@@ -947,7 +947,7 @@ enum {
 
 	mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
 	mono_mb_emit_byte (mb, CEE_MONO_TLS);
-	mono_mb_emit_i4 (mb, thread_tls_offset);
+	mono_mb_emit_i4 (mb, TLS_KEY_THREAD);
 	mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoInternalThread, tid));
 	mono_mb_emit_byte (mb, CEE_ADD);
 	mono_mb_emit_byte (mb, CEE_LDIND_I);
@@ -1109,7 +1109,7 @@ enum {
 	mono_mb_emit_byte (mb, CEE_LDIND_I);
 	mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
 	mono_mb_emit_byte (mb, CEE_MONO_TLS);
-	mono_mb_emit_i4 (mb, thread_tls_offset);
+	mono_mb_emit_i4 (mb, TLS_KEY_THREAD);
 	mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoInternalThread, tid));
 	mono_mb_emit_byte (mb, CEE_ADD);
 	mono_mb_emit_byte (mb, CEE_LDIND_I);

Modified: mono/metadata/sgen-alloc.c
===================================================================
@@ -851,7 +851,7 @@ enum {
 
 	/* tlab_next_addr (local) = tlab_next_addr (TLS var) */
 	tlab_next_addr_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
-	EMIT_TLS_ACCESS (mb, tlab_next_addr, tlab_next_addr_offset);
+	EMIT_TLS_ACCESS (mb, tlab_next_addr, TLS_KEY_SGEN_TLAB_NEXT);
 	mono_mb_emit_stloc (mb, tlab_next_addr_var);
 
 	/* p = (void**)tlab_next; */
@@ -870,7 +870,7 @@ enum {
 
 	/* if (G_LIKELY (new_next < tlab_temp_end)) */
 	mono_mb_emit_ldloc (mb, new_next_var);
-	EMIT_TLS_ACCESS (mb, tlab_temp_end, tlab_temp_end_offset);
+	EMIT_TLS_ACCESS (mb, tlab_temp_end, TLS_KEY_SGEN_TLAB_TEMP_END);
 	slowpath_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BLT_UN_S);
 
 	/* Slowpath */
@@ -985,6 +985,9 @@ enum {
 
 	if (tlab_next_offset == -1 || tlab_temp_end_offset == -1)
 		return NULL;
+
+	mono_tls_key_set_offset (TLS_KEY_SGEN_TLAB_NEXT, tlab_next_offset);
+	mono_tls_key_set_offset (TLS_KEY_SGEN_TLAB_TEMP_END, tlab_temp_end_offset);
 #endif
 	if (collect_before_allocs)
 		return NULL;

Modified: mono/metadata/sgen-gc.c
===================================================================
@@ -4862,6 +4862,19 @@ void mono_gc_wbarrier_value_copy_bitmap (gpointer _dest, gpointer _src, int size
 
 #ifndef HAVE_KW_THREAD
 	mono_native_tls_alloc (&thread_info_key, NULL);
+#if defined(__APPLE__) || defined (HOST_WIN32)
+	/* 
+	 * CEE_MONO_TLS requires the tls offset, not the key, so the code below only works on darwin,
+	 * where the two are the same.
+	 */
+	mono_tls_key_set_offset (TLS_KEY_SGEN_THREAD_INFO, thread_info_key);
+#endif
+#else
+	{
+		int tls_offset = -1;
+		MONO_THREAD_VAR_OFFSET (sgen_thread_info, tls_offset);
+		mono_tls_key_set_offset (TLS_KEY_SGEN_THREAD_INFO, tls_offset);
+	}
 #endif
 
 	/*

Modified: mono/metadata/sgen-gc.h
===================================================================
@@ -945,28 +945,24 @@ struct _LOSObject {
 #endif
 
 #ifdef HAVE_KW_THREAD
-#define EMIT_TLS_ACCESS(mb,dummy,offset)	do {	\
+#define EMIT_TLS_ACCESS(mb,member,key)	do {	\
 	mono_mb_emit_byte ((mb), MONO_CUSTOM_PREFIX);	\
 	mono_mb_emit_byte ((mb), CEE_MONO_TLS);		\
-	mono_mb_emit_i4 ((mb), (offset));		\
+	mono_mb_emit_i4 ((mb), (key));		\
 	} while (0)
 #else
 
-/* 
- * CEE_MONO_TLS requires the tls offset, not the key, so the code below only works on darwin,
- * where the two are the same.
- */
 #if defined(__APPLE__) || defined (HOST_WIN32)
-#define EMIT_TLS_ACCESS(mb,member,dummy)	do {	\
+#define EMIT_TLS_ACCESS(mb,member,key)	do {	\
 	mono_mb_emit_byte ((mb), MONO_CUSTOM_PREFIX);	\
 	mono_mb_emit_byte ((mb), CEE_MONO_TLS);		\
-	mono_mb_emit_i4 ((mb), thread_info_key);	\
+	mono_mb_emit_i4 ((mb), TLS_KEY_SGEN_THREAD_INFO);	\
 	mono_mb_emit_icon ((mb), G_STRUCT_OFFSET (SgenThreadInfo, member));	\
 	mono_mb_emit_byte ((mb), CEE_ADD);		\
 	mono_mb_emit_byte ((mb), CEE_LDIND_I);		\
 	} while (0)
 #else
-#define EMIT_TLS_ACCESS(mb,member,dummy)	do { g_error ("sgen is not supported when using --with-tls=pthread.\n"); } while (0)
+#define EMIT_TLS_ACCESS(mb,member,key)	do { g_error ("sgen is not supported when using --with-tls=pthread.\n"); } while (0)
 #endif
 
 #endif

Modified: mono/mini/method-to-ir.c
===================================================================
@@ -11123,17 +11123,31 @@ gboolean check_linkdemand (MonoCompile *cfg, MonoMethod *caller, MonoMethod *cal
 				bblock->out_of_line = TRUE;
 				ip += 2;
 				break;
-			case CEE_MONO_TLS:
+			case CEE_MONO_TLS: {
+				int key;
+
 				CHECK_STACK_OVF (1);
 				CHECK_OPSIZE (6);
-				MONO_INST_NEW (cfg, ins, OP_TLS_GET);
-				ins->dreg = alloc_preg (cfg);
-				ins->inst_offset = (gint32)read32 (ip + 2);
+				key = (gint32)read32 (ip + 2);
+				g_assert (key < TLS_KEY_NUM);
+
+				ins = mono_create_tls_get (cfg, key);
+				if (!ins) {
+					if (cfg->compile_aot) {
+						cfg->disable_aot = TRUE;
+						MONO_INST_NEW (cfg, ins, OP_TLS_GET);
+						ins->dreg = alloc_preg (cfg);
+						ins->type = STACK_PTR;
+					} else {
+						g_assert_not_reached ();
+					}
+				}
 				ins->type = STACK_PTR;
 				MONO_ADD_INS (bblock, ins);
 				*sp++ = ins;
 				ip += 6;
 				break;
+			}
 			case CEE_MONO_DYN_CALL: {
 				MonoCallInst *call;
 

Modified: mono/mini/mini.c
===================================================================
@@ -2888,7 +2888,7 @@ void *mono_global_codeman_reserve (int size)
 }
 
 int
-mini_get_tls_offset (MonoJitTlsKey key)
+mini_get_tls_offset (MonoTlsKey key)
 {
 	int offset;
 
@@ -2906,8 +2906,8 @@ void *mono_global_codeman_reserve (int size)
 		offset = mono_get_lmf_tls_offset ();
 		break;
 	default:
-		g_assert_not_reached ();
-		offset = -1;
+		offset = mono_tls_key_get_offset (key);
+		g_assert (offset != -1);
 		break;
 	}
 	return offset;
@@ -2932,21 +2932,25 @@ void *mono_global_codeman_reserve (int size)
 	return ins;
 }
 
-static MonoInst*
-mono_create_tls_get (MonoCompile *cfg, MonoJitTlsKey key)
+MonoInst*
+mono_create_tls_get (MonoCompile *cfg, MonoTlsKey key)
 {
 	/*
 	 * TLS offsets might be different at AOT time, so load them from a GOT slot and
 	 * use a different opcode.
 	 */
-	if (cfg->compile_aot && MONO_ARCH_HAVE_TLS_GET && ARCH_HAVE_TLS_GET_REG) {
-		MonoInst *ins, *c;
-
-		EMIT_NEW_TLS_OFFSETCONST (cfg, c, key);
-		MONO_INST_NEW (cfg, ins, OP_TLS_GET_REG);
-		ins->dreg = mono_alloc_preg (cfg);
-		ins->sreg1 = c->dreg;
-		return ins;
+	if (cfg->compile_aot) {
+		if (MONO_ARCH_HAVE_TLS_GET && ARCH_HAVE_TLS_GET_REG) {
+			MonoInst *ins, *c;
+
+			EMIT_NEW_TLS_OFFSETCONST (cfg, c, key);
+			MONO_INST_NEW (cfg, ins, OP_TLS_GET_REG);
+			ins->dreg = mono_alloc_preg (cfg);
+			ins->sreg1 = c->dreg;
+			return ins;
+		} else {
+			return NULL;
+		}
 	}
 
 	return mono_create_tls_get_offset (cfg, mini_get_tls_offset (key));

Modified: mono/mini/mini.h
===================================================================
@@ -1051,16 +1051,6 @@ struct MonoMethodVar {
 	gboolean orig_ex_ctx_set;
 } MonoJitTlsData;
 
-/* TLS entries used by JITted code */
-typedef enum {
-	/* mono_thread_internal_current () */
-	TLS_KEY_THREAD = 0,
-	TLS_KEY_JIT_TLS = 1,
-	/* mono_domain_get () */
-	TLS_KEY_DOMAIN = 2,
-	TLS_KEY_LMF = 3
-} MonoJitTlsKey;
-
 /*
  * This structure is an extension of MonoLMF and contains extra information.
  */
@@ -1966,7 +1956,8 @@ enum {
 gint32    mono_get_jit_tls_offset           (void) MONO_INTERNAL;
 gint32    mono_get_lmf_tls_offset           (void) MONO_INTERNAL;
 gint32    mono_get_lmf_addr_tls_offset      (void) MONO_INTERNAL;
-int       mini_get_tls_offset               (MonoJitTlsKey key) MONO_INTERNAL;
+int       mini_get_tls_offset               (MonoTlsKey key) MONO_INTERNAL;
+MonoInst* mono_create_tls_get               (MonoCompile *cfg, MonoTlsKey key) MONO_INTERNAL;
 MonoInst* mono_get_jit_tls_intrinsic        (MonoCompile *cfg) MONO_INTERNAL;
 MonoInst* mono_get_domain_intrinsic         (MonoCompile* cfg) MONO_INTERNAL;
 MonoInst* mono_get_thread_intrinsic         (MonoCompile* cfg) MONO_INTERNAL;

Modified: mono/utils/Makefile.am
===================================================================
@@ -100,6 +100,7 @@ monoutils_sources = \
 	mono-threads-windows.c	\
 	mono-threads.h	\
 	mono-tls.h	\
+	mono-tls.c	\
 	linux_magic.h	\
 	mono-memory-model.h	\
 	atomic.h	\

Modified: mono/utils/mono-tls.h
===================================================================
@@ -11,6 +11,22 @@
 #ifndef __MONO_TLS_H__
 #define __MONO_TLS_H__
 
+#include <glib.h>
+
+/* TLS entries used by the runtime */
+typedef enum {
+	/* mono_thread_internal_current () */
+	TLS_KEY_THREAD = 0,
+	TLS_KEY_JIT_TLS = 1,
+	/* mono_domain_get () */
+	TLS_KEY_DOMAIN = 2,
+	TLS_KEY_LMF = 3,
+	TLS_KEY_SGEN_THREAD_INFO = 4,
+	TLS_KEY_SGEN_TLAB_NEXT_ADDR = 5,
+	TLS_KEY_SGEN_TLAB_TEMP_END = 6,
+	TLS_KEY_BOEHM_GC_THREAD = 7,
+	TLS_KEY_NUM = 8
+} MonoTlsKey;
 
 #ifdef HOST_WIN32
 
@@ -49,5 +65,7 @@
 
 #endif /* HOST_WIN32 */
 
+int mono_tls_key_get_offset (MonoTlsKey key);
+void mono_tls_key_set_offset (MonoTlsKey key, int offset);
 
 #endif /* __MONO_TLS_H__ */


_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.