diff mbox series

[v2,aarch64] Fix function multiversioning dispatcher link error with LTO

Message ID tencent_3B38856C376AF39D099E680F3AB2E156C40A@qq.com
State New
Headers show
Series [v2,aarch64] Fix function multiversioning dispatcher link error with LTO | expand

Commit Message

Yangyu Chen Oct. 27, 2024, 7:08 a.m. UTC
We forgot to apply DECL_EXTERNAL to __init_cpu_features_resolver decl. When
building with LTO, the linker cannot find the
__init_cpu_features_resolver.lto_priv* symbol, causing the link error.

This patch get this fixed by adding DECL_EXTERNAL to the decl. To avoid used but
never defined warning for this symbol, we also mark TREE_PUBLIC to the decl.

Minimal steps to reproduce the bug:

echo '__attribute__((target_clones("default", "aes"))) void func1() { }' > 1.c
echo '__attribute__((target_clones("default", "aes"))) void func2() { }' > 2.c
echo 'void func1();void func2();int main(){func1();func2();return 0;}' > main.c
gcc -flto -c 1.c 2.c
gcc -flto main.c 1.o 2.o

Fixes: 0cfde688e213 ("[aarch64] Add function multiversioning support")

gcc/ChangeLog:

	* config/aarch64/aarch64.cc (dispatch_function_versions): Adding
	DECL_EXTERNAL and TREE_PUBLIC to __init_cpu_features_resolver decl.
---
 gcc/config/aarch64/aarch64.cc | 2 ++
 1 file changed, 2 insertions(+)

Comments

Richard Sandiford Oct. 29, 2024, 10:18 a.m. UTC | #1
Yangyu Chen <cyy@cyyself.name> writes:
> We forgot to apply DECL_EXTERNAL to __init_cpu_features_resolver decl. When
> building with LTO, the linker cannot find the
> __init_cpu_features_resolver.lto_priv* symbol, causing the link error.
>
> This patch get this fixed by adding DECL_EXTERNAL to the decl. To avoid used but
> never defined warning for this symbol, we also mark TREE_PUBLIC to the decl.
>
> Minimal steps to reproduce the bug:
>
> echo '__attribute__((target_clones("default", "aes"))) void func1() { }' > 1.c
> echo '__attribute__((target_clones("default", "aes"))) void func2() { }' > 2.c
> echo 'void func1();void func2();int main(){func1();func2();return 0;}' > main.c
> gcc -flto -c 1.c 2.c
> gcc -flto main.c 1.o 2.o
>
> Fixes: 0cfde688e213 ("[aarch64] Add function multiversioning support")
>
> gcc/ChangeLog:
>
> 	* config/aarch64/aarch64.cc (dispatch_function_versions): Adding
> 	DECL_EXTERNAL and TREE_PUBLIC to __init_cpu_features_resolver decl.

Thanks for doing this.  I suppose at the same time, we should also
mark __aarch64_cpu_features as TREE_PUBLIC.  We could also mark both
of them as having hidden visibility, via:

  DECL_VISIBILITY (...) = VISIBILITY_HIDDEN;
  DECL_VISIBILITY_SPECIFIED (...) = 1;

Richard

> ---
>  gcc/config/aarch64/aarch64.cc | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
> index 5770491b30c..37123befeaf 100644
> --- a/gcc/config/aarch64/aarch64.cc
> +++ b/gcc/config/aarch64/aarch64.cc
> @@ -20437,6 +20437,8 @@ dispatch_function_versions (tree dispatch_decl,
>    tree init_fn_id = get_identifier ("__init_cpu_features_resolver");
>    tree init_fn_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
>  				  init_fn_id, init_fn_type);
> +  DECL_EXTERNAL (init_fn_decl) = 1;
> +  TREE_PUBLIC (init_fn_decl) = 1;
>    tree arg1 = DECL_ARGUMENTS (dispatch_decl);
>    tree arg2 = TREE_CHAIN (arg1);
>    ifunc_cpu_init_stmt = gimple_build_call (init_fn_decl, 2, arg1, arg2);
diff mbox series

Patch

diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
index 5770491b30c..37123befeaf 100644
--- a/gcc/config/aarch64/aarch64.cc
+++ b/gcc/config/aarch64/aarch64.cc
@@ -20437,6 +20437,8 @@  dispatch_function_versions (tree dispatch_decl,
   tree init_fn_id = get_identifier ("__init_cpu_features_resolver");
   tree init_fn_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
 				  init_fn_id, init_fn_type);
+  DECL_EXTERNAL (init_fn_decl) = 1;
+  TREE_PUBLIC (init_fn_decl) = 1;
   tree arg1 = DECL_ARGUMENTS (dispatch_decl);
   tree arg2 = TREE_CHAIN (arg1);
   ifunc_cpu_init_stmt = gimple_build_call (init_fn_decl, 2, arg1, arg2);