Skip to contentSkip to conversation

m68k: Add LTO support

PATCH In review Daniel Palmer 2 participants 3 messages Mar 30 – Mar 31 m68k architecture

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 m68k: Add LTO support

Most m68k retro/homebrew machines will have limited flash/RAM so having LTO working would be nice.

Not many changes are need really. Most of this is copy/paste from the ARM32 version.

The major change is that the direct register usage of d7 for gd needs to be hidden so that when LTO passes over everything it doesn't see multiple instances of d7.

---
 arch/Kconfig                        |  1 +
 arch/m68k/config.mk                 |  8 ++++++--
 arch/m68k/cpu/m680x0/cpu.c          |  2 +-
 arch/m68k/include/asm/global_data.h | 19 +++++++++++++++++++
 arch/m68k/lib/ashldi3.c             |  6 ++++--
 arch/m68k/lib/lshrdi3.c             |  6 ++++--
 arch/m68k/lib/muldi3.c              |  4 +++-
 common/board_r.c                    |  4 ++--
 common/init/board_init.c            |  4 ++--
 9 files changed, 42 insertions(+), 12 deletions(-)
arch/Kconfig +1 -0
index 4af0da2485fb..488de442f557 100644
@@ -112,6 +112,7 @@ config ARM
 
 config M68K
 	bool "M68000 architecture"
+	select ARCH_SUPPORTS_LTO
 	select HAVE_PRIVATE_LIBGCC
 	select USE_PRIVATE_LIBGCC
 	select SYS_BOOT_GET_CMDLINE
arch/m68k/config.mk +6 -2
index 458953f97122..4d8c83b51cdb 100644
@@ -8,9 +8,13 @@ ifneq ($(CONFIG_M680x0),y)
 PLATFORM_CPPFLAGS += -fPIC
 endif
 KBUILD_LDFLAGS    += -n -pie
-PLATFORM_RELFLAGS += -ffunction-sections -fdata-sections
 PLATFORM_RELFLAGS += -ffixed-d7
 ifneq ($(CONFIG_M680x0),y)
 PLATFORM_RELFLAGS += -msep-data
 endif
-LDFLAGS_FINAL     += --gc-sections -pie
+LDFLAGS_FINAL     += -pie
+
+ifneq ($(LTO_ENABLE),y)
+PLATFORM_RELFLAGS += -ffunction-sections -fdata-sections
+LDFLAGS_FINAL     += --gc-sections
+endif
arch/m68k/cpu/m680x0/cpu.c +1 -1
index f60b932c7dd4..3f87076b8c3d 100644
@@ -24,7 +24,7 @@ void m68k_virt_init_reserve(ulong base)
 	for (i = 0; i < sizeof(*gd_ptr); i++)
 		p[i] = 0;
 
-	gd = gd_ptr;
+	arch_setup_gd(gd);
 
 	gd->malloc_base = base + sizeof(*gd_ptr);
 }
arch/m68k/include/asm/global_data.h +19 -0
index aea2ccabe083..9150ed4ab466 100644
@@ -32,6 +32,25 @@ struct arch_global_data {
 
 #include <asm-generic/global_data.h>
 
+#if defined(LTO_ENABLE)
+/* If LTO is enabled we have to hide d7 to avoid multiple symbol declarations */
+#define DECLARE_GLOBAL_DATA_PTR
+#define gd	get_gd()
+
+static inline gd_t *get_gd(void)
+{
+	gd_t *gd_ptr;
+
+	__asm__ volatile("move.l %%d7, %0\n" : "=r" (gd_ptr));
+
+	return gd_ptr;
+}
+#else
 #define DECLARE_GLOBAL_DATA_PTR     register gd_t *gd asm ("d7")
+#endif
+static inline void arch_setup_gd(gd_t *new_gd)
+{
+	__asm__ volatile("move.l %0, %%d7\n" : : "r" (new_gd));
+}
 
 #endif /* __ASM_GBL_DATA_H */
arch/m68k/lib/ashldi3.c +4 -2
index 9a4bc676bf4c..9e84bd0d9a07 100644
@@ -6,6 +6,8 @@
  * Copyright (C) 1989-2015 Free Software Foundation, Inc.
  */
 
+#include <linux/compiler_attributes.h>
+
 #define BITS_PER_UNIT 8
 
 typedef		 int SItype	__attribute__ ((mode (SI)));
@@ -21,7 +23,7 @@ typedef union
   DItype ll;
 } DIunion;
 
-DItype __ashldi3 (DItype u, word_type b)
+__used DItype __ashldi3 (DItype u, word_type b)
 {
 	DIunion w;
 	word_type bm;
@@ -46,4 +48,4 @@ DItype __ashldi3 (DItype u, word_type b)
 	}
 
 	return w.ll;
-}
\ No newline at end of file
+}
arch/m68k/lib/lshrdi3.c +4 -2
index e639e676a269..53163bc5f83f 100644
@@ -6,6 +6,8 @@
  * Copyright (C) 1989-2015 Free Software Foundation, Inc.
  */
 
+#include <linux/compiler_attributes.h>
+
 #define BITS_PER_UNIT 8
 
 typedef		 int SItype	__attribute__ ((mode (SI)));
@@ -21,7 +23,7 @@ typedef union
   DItype ll;
 } DIunion;
 
-DItype __lshrdi3 (DItype u, word_type b)
+__used DItype __lshrdi3 (DItype u, word_type b)
 {
 	DIunion w;
 	word_type bm;
@@ -46,4 +48,4 @@ DItype __lshrdi3 (DItype u, word_type b)
 	}
 
 	return w.ll;
-}
\ No newline at end of file
+}
arch/m68k/lib/muldi3.c +3 -1
index c42ca1d753e5..06af5533fde3 100644
@@ -6,6 +6,8 @@
  * Copyright (C) 1989, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
  */
 
+#include <linux/compiler_attributes.h>
+
 #define SI_TYPE_SIZE 32
 #define __BITS4 (SI_TYPE_SIZE / 4)
 #define __ll_B (1L << (SI_TYPE_SIZE / 2))
@@ -54,7 +56,7 @@ typedef union
 	DItype ll;
 } DIunion;
 
-DItype __muldi3 (DItype u, DItype v)
+__used DItype __muldi3 (DItype u, DItype v)
 {
 	DIunion w;
 	DIunion uu, vv;
common/board_r.c +2 -2
index 76f9fc090fbe..58eb69005468 100644
@@ -800,12 +800,12 @@ void board_init_r(gd_t *new_gd, ulong dest_addr)
 	 * TODO([email protected]): Consider doing this for all archs, or
 	 * dropping the new_gd parameter.
 	 */
-	if (CONFIG_IS_ENABLED(X86_64) && !IS_ENABLED(CONFIG_EFI_APP))
+	if ((CONFIG_IS_ENABLED(X86_64) && !IS_ENABLED(CONFIG_EFI_APP)) || CONFIG_IS_ENABLED(M68K))
 		arch_setup_gd(new_gd);
 
 #if defined(CONFIG_RISCV)
 	set_gd(new_gd);
-#elif !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
+#elif !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_ARM64) && !defined(CONFIG_M68K)
 	gd = new_gd;
 #endif
 	gd->flags &= ~GD_FLG_LOG_READY;
common/init/board_init.c +2 -2
index 2a6f39f51adb..31b499ebad9c 100644
@@ -14,10 +14,10 @@
 DECLARE_GLOBAL_DATA_PTR;
 
 /*
- * Unfortunately x86, ARM and RISC-V can't compile this code as gd is defined
+ * Unfortunately x86, ARM, RISC-V, M68K can't compile this code as gd is defined
  * as macro and cannot be assigned.
  */
-#if !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
+#if !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_RISCV) && !defined(CONFIG_M68K)
 __weak void arch_setup_gd(struct global_data *gd_ptr)
 {
 	gd = gd_ptr;
Signature · 1 line
2.51.0
#1 lore raw

Hi Daniel,

looks quite good to me, will re-test it asap, just have a questions:

Quoted 112 lines · Daniel Palmer

Most m68k retro/homebrew machines will have limited flash/RAM so having LTO working would be nice.

Not many changes are need really. Most of this is copy/paste from the ARM32 version.

The major change is that the direct register usage of d7 for gd needs to be hidden so that when LTO passes over everything it doesn't see multiple instances of d7.

---
 arch/Kconfig                        |  1 +
 arch/m68k/config.mk                 |  8 ++++++--
 arch/m68k/cpu/m680x0/cpu.c          |  2 +-
 arch/m68k/include/asm/global_data.h | 19 +++++++++++++++++++
 arch/m68k/lib/ashldi3.c             |  6 ++++--
 arch/m68k/lib/lshrdi3.c             |  6 ++++--
 arch/m68k/lib/muldi3.c              |  4 +++-
 common/board_r.c                    |  4 ++--
 common/init/board_init.c            |  4 ++--
 9 files changed, 42 insertions(+), 12 deletions(-)
arch/Kconfig +1 -0
index 4af0da2485fb..488de442f557 100644
@@ -112,6 +112,7 @@ config ARM
 
 config M68K
 	bool "M68000 architecture"
+	select ARCH_SUPPORTS_LTO
 	select HAVE_PRIVATE_LIBGCC
 	select USE_PRIVATE_LIBGCC
 	select SYS_BOOT_GET_CMDLINE
arch/m68k/config.mk +6 -2
index 458953f97122..4d8c83b51cdb 100644
@@ -8,9 +8,13 @@ ifneq ($(CONFIG_M680x0),y)
 PLATFORM_CPPFLAGS += -fPIC
 endif
 KBUILD_LDFLAGS    += -n -pie
-PLATFORM_RELFLAGS += -ffunction-sections -fdata-sections
 PLATFORM_RELFLAGS += -ffixed-d7
 ifneq ($(CONFIG_M680x0),y)
 PLATFORM_RELFLAGS += -msep-data
 endif
-LDFLAGS_FINAL     += --gc-sections -pie
+LDFLAGS_FINAL     += -pie
+
+ifneq ($(LTO_ENABLE),y)
+PLATFORM_RELFLAGS += -ffunction-sections -fdata-sections
+LDFLAGS_FINAL     += --gc-sections
+endif
arch/m68k/cpu/m680x0/cpu.c +1 -1
index f60b932c7dd4..3f87076b8c3d 100644
@@ -24,7 +24,7 @@ void m68k_virt_init_reserve(ulong base)
 	for (i = 0; i < sizeof(*gd_ptr); i++)
 		p[i] = 0;
 
-	gd = gd_ptr;
+	arch_setup_gd(gd);
 
 	gd->malloc_base = base + sizeof(*gd_ptr);
 }
arch/m68k/include/asm/global_data.h +19 -0
index aea2ccabe083..9150ed4ab466 100644
@@ -32,6 +32,25 @@ struct arch_global_data {
 
 #include <asm-generic/global_data.h>
 
+#if defined(LTO_ENABLE)
+/* If LTO is enabled we have to hide d7 to avoid multiple symbol declarations */
+#define DECLARE_GLOBAL_DATA_PTR
+#define gd	get_gd()
+
+static inline gd_t *get_gd(void)
+{
+	gd_t *gd_ptr;
+
+	__asm__ volatile("move.l %%d7, %0\n" : "=r" (gd_ptr));
+
+	return gd_ptr;
+}
+#else
 #define DECLARE_GLOBAL_DATA_PTR     register gd_t *gd asm ("d7")
+#endif
+static inline void arch_setup_gd(gd_t *new_gd)
+{
+	__asm__ volatile("move.l %0, %%d7\n" : : "r" (new_gd));
+}
 
 #endif /* __ASM_GBL_DATA_H */
arch/m68k/lib/ashldi3.c +2 -0
index 9a4bc676bf4c..9e84bd0d9a07 100644
@@ -6,6 +6,8 @@
  * Copyright (C) 1989-2015 Free Software Foundation, Inc.
  */
 
+#include <linux/compiler_attributes.h>
+
 #define BITS_PER_UNIT 8
 
 typedef		 int SItype	__attribute__ ((mode (SI)));

arch/m68k/lib/ashldi3.c typedef union · line 26

Quoted 5 lines
arch/m68k/lib/ashldi3.c +1 -1
@@ -21,7 +23,7 @@ typedef union
   DItype ll;
 } DIunion;
 
-DItype __ashldi3 (DItype u, word_type b)
+__used DItype __ashldi3 (DItype u, word_type b)

Why the above __used is needed ? This impacts all the coldfire builds.

Quoted 87 lines
arch/m68k/lib/ashldi3.c +1 -1
 {
 	DIunion w;
 	word_type bm;
@@ -46,4 +48,4 @@ DItype __ashldi3 (DItype u, word_type b)
 	}
 
 	return w.ll;
-}
\ No newline at end of file
+}
arch/m68k/lib/lshrdi3.c +4 -2
index e639e676a269..53163bc5f83f 100644
@@ -6,6 +6,8 @@
  * Copyright (C) 1989-2015 Free Software Foundation, Inc.
  */
 
+#include <linux/compiler_attributes.h>
+
 #define BITS_PER_UNIT 8
 
 typedef		 int SItype	__attribute__ ((mode (SI)));
@@ -21,7 +23,7 @@ typedef union
   DItype ll;
 } DIunion;
 
-DItype __lshrdi3 (DItype u, word_type b)
+__used DItype __lshrdi3 (DItype u, word_type b)
 {
 	DIunion w;
 	word_type bm;
@@ -46,4 +48,4 @@ DItype __lshrdi3 (DItype u, word_type b)
 	}
 
 	return w.ll;
-}
\ No newline at end of file
+}
arch/m68k/lib/muldi3.c +3 -1
index c42ca1d753e5..06af5533fde3 100644
@@ -6,6 +6,8 @@
  * Copyright (C) 1989, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
  */
 
+#include <linux/compiler_attributes.h>
+
 #define SI_TYPE_SIZE 32
 #define __BITS4 (SI_TYPE_SIZE / 4)
 #define __ll_B (1L << (SI_TYPE_SIZE / 2))
@@ -54,7 +56,7 @@ typedef union
 	DItype ll;
 } DIunion;
 
-DItype __muldi3 (DItype u, DItype v)
+__used DItype __muldi3 (DItype u, DItype v)
 {
 	DIunion w;
 	DIunion uu, vv;
common/board_r.c +2 -2
index 76f9fc090fbe..58eb69005468 100644
@@ -800,12 +800,12 @@ void board_init_r(gd_t *new_gd, ulong dest_addr)
 	 * TODO([email protected]): Consider doing this for all archs, or
 	 * dropping the new_gd parameter.
 	 */
-	if (CONFIG_IS_ENABLED(X86_64) && !IS_ENABLED(CONFIG_EFI_APP))
+	if ((CONFIG_IS_ENABLED(X86_64) && !IS_ENABLED(CONFIG_EFI_APP)) || CONFIG_IS_ENABLED(M68K))
 		arch_setup_gd(new_gd);
 
 #if defined(CONFIG_RISCV)
 	set_gd(new_gd);
-#elif !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
+#elif !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_ARM64) && !defined(CONFIG_M68K)
 	gd = new_gd;
 #endif
 	gd->flags &= ~GD_FLG_LOG_READY;
common/init/board_init.c +0 -0
index 2a6f39f51adb..31b499ebad9c 100644
@@ -14,10 +14,10 @@
 DECLARE_GLOBAL_DATA_PTR;
 

common/init/board_init.c line 23

Quoted 10 lines
common/init/board_init.c +2 -2
 /*
- * Unfortunately x86, ARM and RISC-V can't compile this code as gd is defined
+ * Unfortunately x86, ARM, RISC-V, M68K can't compile this code as gd is defined
  * as macro and cannot be assigned.
  */
-#if !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
+#if !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_RISCV) && !defined(CONFIG_M68K)
 __weak void arch_setup_gd(struct global_data *gd_ptr)
 {
 	gd = gd_ptr;

Regards,
-- Angelo

#2 lore raw

Hi Angelo,

Quoted 5 lines · Angelo Dureghello

arch/m68k/lib/ashldi3.c typedef union · line 26

Quoted 2 lines
arch/m68k/lib/ashldi3.c +1 -1
-DItype __ashldi3 (DItype u, word_type b)
+__used DItype __ashldi3 (DItype u, word_type b)

Why the above __used is needed ? This impacts all the coldfire builds.

At least for normal m68k the final link will fail because GCC has output references to the libgcc functions but LTO didn't think they were needed and discarded them already (example below). So this attribute is to tell GCC to keep them so the link completes. On coldfire is this giving you code you didn't have before? I guess that could be the case if coldfire doesn't need to emulate some things with libgcc.

Cheers,

Daniel

Build with the "+__used DItype __ashldi3 (DItype u, word_type b)" hunk reverted:

daniel@kinako:~/coding/cleantrees/u-boot$ make
CROSS_COMPILE=m68k-linux-gnu- -j16
 UPD     include/generated/timestamp_autogenerated.h
 UPD     include/config/uboot.release
 UPD     include/generated/version_autogenerated.h
 LDS     u-boot.lds
 CC      arch/m68k/lib/ashldi3.o
 AR      arch/m68k/lib/lib.a
 CC      cmd/version.o
 CC      common/init/board_init.o
 CC      common/version.o
 AR      cmd/built-in.a
 AR      common/init/built-in.a
 AR      common/built-in.a
 KSL     keep-syms-lto.c
 KSLCC   keep-syms-lto.o
 LTO     u-boot
/usr/bin/m68k-linux-gnu-ld.bfd: /tmp/cciN6Tis.ltrans0.ltrans.o: in
function `board_init_f':
/home/daniel/coding/cleantrees/u-boot/common/board_f.c:213:(.text.board_init_f+0x3fc):
undefined reference to `__ashldi3'
/usr/bin/m68k-linux-gnu-ld.bfd:
/tmp/cciN6Tis.ltrans0.ltrans.o:/home/daniel/coding/cleantrees/u-boot/common/board_f.c:213:(.text.board_init_f+0x404):
undefined reference to `__ashldi3'
/usr/bin/m68k-linux-gnu-ld.bfd: /tmp/cciN6Tis.ltrans1.ltrans.o: in
function `virtio_uclass_child_pre_probe':
/home/daniel/coding/cleantrees/u-boot/include/virtio.h:463:(.text.virtio_uclass_child_pre_probe+0x114):
undefined reference to `__ashldi3'
/usr/bin/m68k-linux-gnu-ld.bfd: /tmp/cciN6Tis.ltrans1.ltrans.o: in
function `virtio_uclass_child_pre_probe':
/home/daniel/coding/cleantrees/u-boot/drivers/virtio/virtio-uclass.c:311:(.text.virtio_uclass_child_pre_probe+0x160):
undefined reference to `__ashldi3'
/usr/bin/m68k-linux-gnu-ld.bfd:
/home/daniel/coding/cleantrees/u-boot/drivers/virtio/virtio-uclass.c:321:(.text.virtio_uclass_child_pre_probe+0x1aa):
undefined reference to `__ashldi3'
/usr/bin/m68k-linux-gnu-ld.bfd:
/tmp/cciN6Tis.ltrans1.ltrans.o:/home/daniel/coding/cleantrees/u-boot/drivers/virtio/virtio_blk.c:82:
more undefined references to `__ashldi3' follow
collect2: error: ld returned 1 exit status
make: *** [Makefile:2096: u-boot] Error 1