Re: [PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-09 Thread Sami Tolvanen
On Thu, Nov 09, 2017 at 07:48:06AM +0300, Maxim Kuvyrkov wrote:
> Regarding CONFIG_* options, I would expect most of the configuration
> changes to be equally valid for both GCC's and Clang's LTO support.
> Sami, I don't think it's fair to ask you to support both Clang and GCC in
> your patchset, but, where changes are obviously toolchain-agnostic, could
> you use CONFIG_LTO?  And use CONFIG_LTO_CLANG for Clang-specific parts?

Sure, using CONFIG_LTO for common code and CONFIG_LTO_CLANG for clang-
specific parts sounds good.

Sami


Re: [PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-09 Thread Sami Tolvanen
On Thu, Nov 09, 2017 at 07:48:06AM +0300, Maxim Kuvyrkov wrote:
> Regarding CONFIG_* options, I would expect most of the configuration
> changes to be equally valid for both GCC's and Clang's LTO support.
> Sami, I don't think it's fair to ask you to support both Clang and GCC in
> your patchset, but, where changes are obviously toolchain-agnostic, could
> you use CONFIG_LTO?  And use CONFIG_LTO_CLANG for Clang-specific parts?

Sure, using CONFIG_LTO for common code and CONFIG_LTO_CLANG for clang-
specific parts sounds good.

Sami


Re: [PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-09 Thread Sami Tolvanen
On Wed, Nov 08, 2017 at 04:02:22PM -0800, Andi Kleen wrote:
> There's also older patches to enable single-pass-linking for kallsyms,
> which is extremly useful for LTO build performance.

Excellent, can you point me to the patch in question?

I worked around the build performance problem by reusing vmlinux.o for
kallsyms instead of linking all bitcode again in each step. I'm not sure
if this is feasible with gcc.

Sami


Re: [PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-09 Thread Sami Tolvanen
On Wed, Nov 08, 2017 at 04:02:22PM -0800, Andi Kleen wrote:
> There's also older patches to enable single-pass-linking for kallsyms,
> which is extremly useful for LTO build performance.

Excellent, can you point me to the patch in question?

I worked around the build performance problem by reusing vmlinux.o for
kallsyms instead of linking all bitcode again in each step. I'm not sure
if this is feasible with gcc.

Sami


Re: [PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-09 Thread Sami Tolvanen
On Wed, Nov 08, 2017 at 12:25:55PM +0300, Yury Norov wrote:
> The patch below uses trick with undefining mrs_s/msr_s immediately
> after use to solve the problem. It works for both gcc and clang.

Great, looks good to me! I tested the patch with LTO and clang's integrated
assembler seems to be happy with it.

> It has solved many problems you also try to solve, and some patches
> are looking very similar.

I haven't had a closer look at the gcc LTO patches yet, but I am definitely
all for using common code where possible.

Sami 


Re: [PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-09 Thread Sami Tolvanen
On Wed, Nov 08, 2017 at 12:25:55PM +0300, Yury Norov wrote:
> The patch below uses trick with undefining mrs_s/msr_s immediately
> after use to solve the problem. It works for both gcc and clang.

Great, looks good to me! I tested the patch with LTO and clang's integrated
assembler seems to be happy with it.

> It has solved many problems you also try to solve, and some patches
> are looking very similar.

I haven't had a closer look at the gcc LTO patches yet, but I am definitely
all for using common code where possible.

Sami 


Re: [PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-08 Thread Maxim Kuvyrkov
> On Nov 9, 2017, at 3:02 AM, Andi Kleen  wrote:
> 
>> There's the series from Andi Kleen that enables LTO for Linux on x86:
>> https://lwn.net/Articles/512548/
>> https://github.com/andikleen/linux-misc/tree/lto-411-1
>> 
>> It has solved many problems you also try to solve, and some patches
>> are looking very similar. 
>> 
>> At now we have different patchsets for gcc and clang, and it would be
>> better to have them together. One thing I'm worried is that you introduce
>> CONFIG_CLANG_LTO and use it for all cases, including that where more
>> generic CONFIG_LTO should be used.
> 
> Yes would be good to merge the two. I've been looking at updating
> my old one.
> 
> I don't cover any ARM code, but lots of generic code. My patches
> also worked on MIPS at least.
> 
> There's also older patches to enable single-pass-linking for kallsyms,
> which is extremly useful for LTO build performance.

[Yury, thanks for the CC:]

Chiming in from the toolchain side, Linaro's Toolchain team will try to help 
with any GCC or Clang issues that are exposed by building kernel with LTO on 
arm64 / arm.

Regarding CONFIG_* options, I would expect most of the configuration changes to 
be equally valid for both GCC's and Clang's LTO support.  Sami, I don't think 
it's fair to ask you to support both Clang and GCC in your patchset, but, where 
changes are obviously toolchain-agnostic, could you use CONFIG_LTO?  And use 
CONFIG_LTO_CLANG for Clang-specific parts?

This way we will be able to avoid most of the refactoring when adding support 
for GCC's LTO.

Thank you,

--
Maxim Kuvyrkov
www.linaro.org



Re: [PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-08 Thread Maxim Kuvyrkov
> On Nov 9, 2017, at 3:02 AM, Andi Kleen  wrote:
> 
>> There's the series from Andi Kleen that enables LTO for Linux on x86:
>> https://lwn.net/Articles/512548/
>> https://github.com/andikleen/linux-misc/tree/lto-411-1
>> 
>> It has solved many problems you also try to solve, and some patches
>> are looking very similar. 
>> 
>> At now we have different patchsets for gcc and clang, and it would be
>> better to have them together. One thing I'm worried is that you introduce
>> CONFIG_CLANG_LTO and use it for all cases, including that where more
>> generic CONFIG_LTO should be used.
> 
> Yes would be good to merge the two. I've been looking at updating
> my old one.
> 
> I don't cover any ARM code, but lots of generic code. My patches
> also worked on MIPS at least.
> 
> There's also older patches to enable single-pass-linking for kallsyms,
> which is extremly useful for LTO build performance.

[Yury, thanks for the CC:]

Chiming in from the toolchain side, Linaro's Toolchain team will try to help 
with any GCC or Clang issues that are exposed by building kernel with LTO on 
arm64 / arm.

Regarding CONFIG_* options, I would expect most of the configuration changes to 
be equally valid for both GCC's and Clang's LTO support.  Sami, I don't think 
it's fair to ask you to support both Clang and GCC in your patchset, but, where 
changes are obviously toolchain-agnostic, could you use CONFIG_LTO?  And use 
CONFIG_LTO_CLANG for Clang-specific parts?

This way we will be able to avoid most of the refactoring when adding support 
for GCC's LTO.

Thank you,

--
Maxim Kuvyrkov
www.linaro.org



Re: [PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-08 Thread Andi Kleen
> There's the series from Andi Kleen that enables LTO for Linux on x86:
> https://lwn.net/Articles/512548/
> https://github.com/andikleen/linux-misc/tree/lto-411-1
> 
> It has solved many problems you also try to solve, and some patches
> are looking very similar. 
> 
> At now we have different patchsets for gcc and clang, and it would be
> better to have them together. One thing I'm worried is that you introduce
> CONFIG_CLANG_LTO and use it for all cases, including that where more
> generic CONFIG_LTO should be used.

Yes would be good to merge the two. I've been looking at updating
my old one.

I don't cover any ARM code, but lots of generic code. My patches
also worked on MIPS at least.

There's also older patches to enable single-pass-linking for kallsyms,
which is extremly useful for LTO build performance.

-Andi


Re: [PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-08 Thread Andi Kleen
> There's the series from Andi Kleen that enables LTO for Linux on x86:
> https://lwn.net/Articles/512548/
> https://github.com/andikleen/linux-misc/tree/lto-411-1
> 
> It has solved many problems you also try to solve, and some patches
> are looking very similar. 
> 
> At now we have different patchsets for gcc and clang, and it would be
> better to have them together. One thing I'm worried is that you introduce
> CONFIG_CLANG_LTO and use it for all cases, including that where more
> generic CONFIG_LTO should be used.

Yes would be good to merge the two. I've been looking at updating
my old one.

I don't cover any ARM code, but lots of generic code. My patches
also worked on MIPS at least.

There's also older patches to enable single-pass-linking for kallsyms,
which is extremly useful for LTO build performance.

-Andi


Re: [PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-08 Thread Yury Norov
Hi Sami,

(CC Andi Kleen, Alex Matveev, Maxim Kuvyrkov)

On Fri, Nov 03, 2017 at 10:12:01AM -0700, Sami Tolvanen wrote:
> Clang's integrated assembler does not allow assembly macros defined
> in one inline asm block using the .macro directive to be used across
> separate asm blocks. LLVM developers consider this a feature and not a
> bug, recommending code refactoring:
> 
>   https://bugs.llvm.org/show_bug.cgi?id=19749
> 
> As binutils doesn't allow macros to be redefined, this change adds C
> preprocessor macros that define the assembly macros globally for binutils
> and locally for clang's integrated assembler.

This macros are also the problem for gcc and ld.bfd if we try to
enable LTO.

The patch below uses trick with undefining mrs_s/msr_s immediately
after use to solve the problem. It works for both gcc and clang.

There's the series from Andi Kleen that enables LTO for Linux on x86:
https://lwn.net/Articles/512548/
https://github.com/andikleen/linux-misc/tree/lto-411-1

It has solved many problems you also try to solve, and some patches
are looking very similar. 

At now we have different patchsets for gcc and clang, and it would be
better to have them together. One thing I'm worried is that you introduce
CONFIG_CLANG_LTO and use it for all cases, including that where more
generic CONFIG_LTO should be used.

I think we'd also discuss it here.


>From c939344c726e8ae6b28012ad2807c63b54175e40 Mon Sep 17 00:00:00 2001
From: Alex Matveev 
Date: Tue, 7 Nov 2017 01:06:33 +
Subject: [PATCH] arm64: make mrs_s and msr_s macros work with LTO

Use UNDEFINE_MRS_S and UNDEFINE_MSR_S to define corresponding macros
in-place and workaround gcc and clang limitations on redefining macros
across different assembler blocks.

Signed-off-by: Alex Matveev 
Signed-off-by: Yury Norov 
---
 arch/arm64/include/asm/kvm_hyp.h |  8 --
 arch/arm64/include/asm/sysreg.h  | 55 +++-
 2 files changed, 43 insertions(+), 20 deletions(-)

diff --git a/arch/arm64/include/asm/kvm_hyp.h b/arch/arm64/include/asm/kvm_hyp.h
index 4572a9b560fa..20bfb8e676e0 100644
--- a/arch/arm64/include/asm/kvm_hyp.h
+++ b/arch/arm64/include/asm/kvm_hyp.h
@@ -29,7 +29,9 @@
({  \
u64 reg;\
asm volatile(ALTERNATIVE("mrs %0, " __stringify(r##nvh),\
-"mrs_s %0, " __stringify(r##vh),\
+DEFINE_MRS_S   \
+"mrs_s %0, " __stringify(r##vh) "\n"\
+UNDEFINE_MRS_S,\
 ARM64_HAS_VIRT_HOST_EXTN)  \
 : "=r" (reg)); \
reg;\
@@ -39,7 +41,9 @@
do {\
u64 __val = (u64)(v);   \
asm volatile(ALTERNATIVE("msr " __stringify(r##nvh) ", %x0",\
-"msr_s " __stringify(r##vh) ", %x0",\
+DEFINE_MSR_S   \
+"msr_s " __stringify(r##vh) ", %x0\n"\
+UNDEFINE_MSR_S,\
 ARM64_HAS_VIRT_HOST_EXTN)  \
 : : "rZ" (__val)); \
} while (0)
diff --git a/arch/arm64/include/asm/sysreg.h b/arch/arm64/include/asm/sysreg.h
index f707fed5886f..a69b0ca9a3b4 100644
--- a/arch/arm64/include/asm/sysreg.h
+++ b/arch/arm64/include/asm/sysreg.h
@@ -463,20 +463,39 @@
 
 #include 
 
-asm(
-"  .irp
num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n"
-"  .equ.L__reg_num_x\\num, \\num\n"
-"  .endr\n"
+#define __DEFINE_MRS_MSR_S_REGNUM  \
+"  .irp
num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n"
 \
+"  .equ.L__reg_num_x\\num, \\num\n"\
+"  .endr\n"\
 "  .equ.L__reg_num_xzr, 31\n"
-"\n"
-"  .macro  mrs_s, rt, sreg\n"
-   __emit_inst(0xd520|(\\sreg)|(.L__reg_num_\\rt))
+
+#define DEFINE_MRS_S   \
+   __DEFINE_MRS_MSR_S_REGNUM   \
+"  .macro  mrs_s, rt, sreg\n"  \
+   __emit_inst(0xd520|(\\sreg)|(.L__reg_num_\\rt)) \
 "  .endm\n"
-"\n"
-"  .macro  msr_s, sreg, rt\n"
-   __emit_inst(0xd500|(\\sreg)|(.L__reg_num_\\rt))
+
+#define 

Re: [PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-08 Thread Yury Norov
Hi Sami,

(CC Andi Kleen, Alex Matveev, Maxim Kuvyrkov)

On Fri, Nov 03, 2017 at 10:12:01AM -0700, Sami Tolvanen wrote:
> Clang's integrated assembler does not allow assembly macros defined
> in one inline asm block using the .macro directive to be used across
> separate asm blocks. LLVM developers consider this a feature and not a
> bug, recommending code refactoring:
> 
>   https://bugs.llvm.org/show_bug.cgi?id=19749
> 
> As binutils doesn't allow macros to be redefined, this change adds C
> preprocessor macros that define the assembly macros globally for binutils
> and locally for clang's integrated assembler.

This macros are also the problem for gcc and ld.bfd if we try to
enable LTO.

The patch below uses trick with undefining mrs_s/msr_s immediately
after use to solve the problem. It works for both gcc and clang.

There's the series from Andi Kleen that enables LTO for Linux on x86:
https://lwn.net/Articles/512548/
https://github.com/andikleen/linux-misc/tree/lto-411-1

It has solved many problems you also try to solve, and some patches
are looking very similar. 

At now we have different patchsets for gcc and clang, and it would be
better to have them together. One thing I'm worried is that you introduce
CONFIG_CLANG_LTO and use it for all cases, including that where more
generic CONFIG_LTO should be used.

I think we'd also discuss it here.


>From c939344c726e8ae6b28012ad2807c63b54175e40 Mon Sep 17 00:00:00 2001
From: Alex Matveev 
Date: Tue, 7 Nov 2017 01:06:33 +
Subject: [PATCH] arm64: make mrs_s and msr_s macros work with LTO

Use UNDEFINE_MRS_S and UNDEFINE_MSR_S to define corresponding macros
in-place and workaround gcc and clang limitations on redefining macros
across different assembler blocks.

Signed-off-by: Alex Matveev 
Signed-off-by: Yury Norov 
---
 arch/arm64/include/asm/kvm_hyp.h |  8 --
 arch/arm64/include/asm/sysreg.h  | 55 +++-
 2 files changed, 43 insertions(+), 20 deletions(-)

diff --git a/arch/arm64/include/asm/kvm_hyp.h b/arch/arm64/include/asm/kvm_hyp.h
index 4572a9b560fa..20bfb8e676e0 100644
--- a/arch/arm64/include/asm/kvm_hyp.h
+++ b/arch/arm64/include/asm/kvm_hyp.h
@@ -29,7 +29,9 @@
({  \
u64 reg;\
asm volatile(ALTERNATIVE("mrs %0, " __stringify(r##nvh),\
-"mrs_s %0, " __stringify(r##vh),\
+DEFINE_MRS_S   \
+"mrs_s %0, " __stringify(r##vh) "\n"\
+UNDEFINE_MRS_S,\
 ARM64_HAS_VIRT_HOST_EXTN)  \
 : "=r" (reg)); \
reg;\
@@ -39,7 +41,9 @@
do {\
u64 __val = (u64)(v);   \
asm volatile(ALTERNATIVE("msr " __stringify(r##nvh) ", %x0",\
-"msr_s " __stringify(r##vh) ", %x0",\
+DEFINE_MSR_S   \
+"msr_s " __stringify(r##vh) ", %x0\n"\
+UNDEFINE_MSR_S,\
 ARM64_HAS_VIRT_HOST_EXTN)  \
 : : "rZ" (__val)); \
} while (0)
diff --git a/arch/arm64/include/asm/sysreg.h b/arch/arm64/include/asm/sysreg.h
index f707fed5886f..a69b0ca9a3b4 100644
--- a/arch/arm64/include/asm/sysreg.h
+++ b/arch/arm64/include/asm/sysreg.h
@@ -463,20 +463,39 @@
 
 #include 
 
-asm(
-"  .irp
num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n"
-"  .equ.L__reg_num_x\\num, \\num\n"
-"  .endr\n"
+#define __DEFINE_MRS_MSR_S_REGNUM  \
+"  .irp
num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n"
 \
+"  .equ.L__reg_num_x\\num, \\num\n"\
+"  .endr\n"\
 "  .equ.L__reg_num_xzr, 31\n"
-"\n"
-"  .macro  mrs_s, rt, sreg\n"
-   __emit_inst(0xd520|(\\sreg)|(.L__reg_num_\\rt))
+
+#define DEFINE_MRS_S   \
+   __DEFINE_MRS_MSR_S_REGNUM   \
+"  .macro  mrs_s, rt, sreg\n"  \
+   __emit_inst(0xd520|(\\sreg)|(.L__reg_num_\\rt)) \
 "  .endm\n"
-"\n"
-"  .macro  msr_s, sreg, rt\n"
-   __emit_inst(0xd500|(\\sreg)|(.L__reg_num_\\rt))
+
+#define DEFINE_MSR_S   \
+   

Re: [PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-03 Thread Mark Rutland
On Fri, Nov 03, 2017 at 10:12:01AM -0700, Sami Tolvanen wrote:
> Clang's integrated assembler does not allow assembly macros defined
> in one inline asm block using the .macro directive to be used across
> separate asm blocks. LLVM developers consider this a feature and not a
> bug, recommending code refactoring:
> 
>   https://bugs.llvm.org/show_bug.cgi?id=19749

I agree that this a construct to be avoided if possible.

[...]

> +/*
> + * llvm-as doesn't allow macros defined in an asm block to be used in other
> + * asm blocks, which means we cannot define them globally.
> + */
> +#if !defined(CONFIG_CLANG_LTO)

Can the integrated assembler not be used in the absence of LTO?

> +asm(
> + ___MRS_MSR_S_REGNUM
> + ___MRS_S
> + ___MSR_S
>  );
>  
> +#undef  ___MRS_MSR_S_REGNUM
> +#define ___MRS_MSR_S_REGNUM
> +#undef  ___MRS_S
> +#define ___MRS_S
> +#undef  ___MSR_S
> +#define ___MSR_S
> +#endif

It would be much nicer if we could use one code path regardless here.

Can we fold the asm macros into the CPP macros somehow instead?

IIRC the only reason we use asm macros for the inline assembly case was
because we couldn't figure out how to token-paste with %N in CPP, but
there might be another way around that.

Thanks,
Mark.


Re: [PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-03 Thread Mark Rutland
On Fri, Nov 03, 2017 at 10:12:01AM -0700, Sami Tolvanen wrote:
> Clang's integrated assembler does not allow assembly macros defined
> in one inline asm block using the .macro directive to be used across
> separate asm blocks. LLVM developers consider this a feature and not a
> bug, recommending code refactoring:
> 
>   https://bugs.llvm.org/show_bug.cgi?id=19749

I agree that this a construct to be avoided if possible.

[...]

> +/*
> + * llvm-as doesn't allow macros defined in an asm block to be used in other
> + * asm blocks, which means we cannot define them globally.
> + */
> +#if !defined(CONFIG_CLANG_LTO)

Can the integrated assembler not be used in the absence of LTO?

> +asm(
> + ___MRS_MSR_S_REGNUM
> + ___MRS_S
> + ___MSR_S
>  );
>  
> +#undef  ___MRS_MSR_S_REGNUM
> +#define ___MRS_MSR_S_REGNUM
> +#undef  ___MRS_S
> +#define ___MRS_S
> +#undef  ___MSR_S
> +#define ___MSR_S
> +#endif

It would be much nicer if we could use one code path regardless here.

Can we fold the asm macros into the CPP macros somehow instead?

IIRC the only reason we use asm macros for the inline assembly case was
because we couldn't figure out how to token-paste with %N in CPP, but
there might be another way around that.

Thanks,
Mark.


Re: [PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-03 Thread Nick Desaulniers
On Fri, Nov 3, 2017 at 11:06 AM, Mark Rutland  wrote:
> On Fri, Nov 03, 2017 at 10:53:08AM -0700, Nick Desaulniers wrote:
>> These mrs_s and msr_s macros in particular were preventing us from
>> linking arm64 with Clang's integrated assembler, regardless of LTO.
>> Those macros ran into: https://bugs.llvm.org/show_bug.cgi?id=19749.
>> So while I appreciate how clever they are, they prevent us from
>> assembling with Clang so I would like to see them go.
>
> They're necessary to work with some currently-supported toolchains
> (which don't support the s*_*_c*_c*_* syntax), so they're not going to
> go completely.
>
> If you could suggest something that clang might prefer, which doesn't
> adversely affect GCC, I'm all ears.

I wasn't clear in my point; I recognize what they do and agree that
they are needed.

More specifically, my problem is the use of .macro assembly
directives, since Clang "considers it a feature" not to let you invoke
a .macro defined in one inline asm block from another asm inline
block.

So I would like to see *the use of .macro in separate inline asm
blocks within mrs_s and msr_s c-preprocessor macros go.*


Re: [PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-03 Thread Nick Desaulniers
On Fri, Nov 3, 2017 at 11:06 AM, Mark Rutland  wrote:
> On Fri, Nov 03, 2017 at 10:53:08AM -0700, Nick Desaulniers wrote:
>> These mrs_s and msr_s macros in particular were preventing us from
>> linking arm64 with Clang's integrated assembler, regardless of LTO.
>> Those macros ran into: https://bugs.llvm.org/show_bug.cgi?id=19749.
>> So while I appreciate how clever they are, they prevent us from
>> assembling with Clang so I would like to see them go.
>
> They're necessary to work with some currently-supported toolchains
> (which don't support the s*_*_c*_c*_* syntax), so they're not going to
> go completely.
>
> If you could suggest something that clang might prefer, which doesn't
> adversely affect GCC, I'm all ears.

I wasn't clear in my point; I recognize what they do and agree that
they are needed.

More specifically, my problem is the use of .macro assembly
directives, since Clang "considers it a feature" not to let you invoke
a .macro defined in one inline asm block from another asm inline
block.

So I would like to see *the use of .macro in separate inline asm
blocks within mrs_s and msr_s c-preprocessor macros go.*


Re: [PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-03 Thread Mark Rutland
On Fri, Nov 03, 2017 at 10:53:08AM -0700, Nick Desaulniers wrote:
> These mrs_s and msr_s macros in particular were preventing us from
> linking arm64 with Clang's integrated assembler, regardless of LTO.
> Those macros ran into: https://bugs.llvm.org/show_bug.cgi?id=19749.
> So while I appreciate how clever they are, they prevent us from
> assembling with Clang so I would like to see them go.

They're necessary to work with some currently-supported toolchains
(which don't support the s*_*_c*_c*_* syntax), so they're not going to
go completely.

If you could suggest something that clang might prefer, which doesn't
adversely affect GCC, I'm all ears.

Thanks,
Mark.

> 
> On Fri, Nov 3, 2017 at 10:12 AM, Sami Tolvanen  
> wrote:
> > Clang's integrated assembler does not allow assembly macros defined
> > in one inline asm block using the .macro directive to be used across
> > separate asm blocks. LLVM developers consider this a feature and not a
> > bug, recommending code refactoring:
> >
> >   https://bugs.llvm.org/show_bug.cgi?id=19749
> >
> > As binutils doesn't allow macros to be redefined, this change adds C
> > preprocessor macros that define the assembly macros globally for binutils
> > and locally for clang's integrated assembler.
> >
> > Suggested-by: Greg Hackmann 
> > Suggested-by: Nick Desaulniers 
> > Signed-off-by: Sami Tolvanen 
> > ---
> >  arch/arm64/include/asm/kvm_hyp.h |  2 ++
> >  arch/arm64/include/asm/sysreg.h  | 71 
> > ++--
> >  2 files changed, 56 insertions(+), 17 deletions(-)
> >
> > diff --git a/arch/arm64/include/asm/kvm_hyp.h 
> > b/arch/arm64/include/asm/kvm_hyp.h
> > index 4572a9b560fa..6840704ea894 100644
> > --- a/arch/arm64/include/asm/kvm_hyp.h
> > +++ b/arch/arm64/include/asm/kvm_hyp.h
> > @@ -29,6 +29,7 @@
> > ({  \
> > u64 reg;\
> > asm volatile(ALTERNATIVE("mrs %0, " __stringify(r##nvh),\
> > +DEFINE_MRS_S   \
> >  "mrs_s %0, " __stringify(r##vh),\
> >  ARM64_HAS_VIRT_HOST_EXTN)  \
> >  : "=r" (reg)); \
> > @@ -39,6 +40,7 @@
> > do {\
> > u64 __val = (u64)(v);   \
> > asm volatile(ALTERNATIVE("msr " __stringify(r##nvh) ", 
> > %x0",\
> > +DEFINE_MSR_S   \
> >  "msr_s " __stringify(r##vh) ", 
> > %x0",\
> >  ARM64_HAS_VIRT_HOST_EXTN)  \
> >  : : "rZ" (__val)); \
> > diff --git a/arch/arm64/include/asm/sysreg.h 
> > b/arch/arm64/include/asm/sysreg.h
> > index f707fed5886f..1588ac3f3690 100644
> > --- a/arch/arm64/include/asm/sysreg.h
> > +++ b/arch/arm64/include/asm/sysreg.h
> > @@ -463,21 +463,58 @@
> >
> >  #include 
> >
> > -asm(
> > -"  .irp
> > num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n"
> > -"  .equ.L__reg_num_x\\num, \\num\n"
> > -"  .endr\n"
> > +#define ___MRS_MSR_S_REGNUM\
> > +"  .irp
> > num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n"
> >  \
> > +"  .equ.L__reg_num_x\\num, \\num\n"\
> > +"  .endr\n"\
> >  "  .equ.L__reg_num_xzr, 31\n"
> > -"\n"
> > -"  .macro  mrs_s, rt, sreg\n"
> > -   __emit_inst(0xd520|(\\sreg)|(.L__reg_num_\\rt))
> > +
> > +#define ___MRS_S   \
> > +"  .macro  mrs_s, rt, sreg\n"  \
> > +   __emit_inst(0xd520|(\\sreg)|(.L__reg_num_\\rt)) \
> >  "  .endm\n"
> > -"\n"
> > -"  .macro  msr_s, sreg, rt\n"
> > -   __emit_inst(0xd500|(\\sreg)|(.L__reg_num_\\rt))
> > +
> > +#define ___MSR_S   \
> > +"  .macro  msr_s, sreg, rt\n"  \
> > +   __emit_inst(0xd500|(\\sreg)|(.L__reg_num_\\rt)) \
> >  "  .endm\n"
> > +
> > +/*
> > + * llvm-as doesn't allow macros defined in an asm block to be used in other
> > + * asm blocks, which means we cannot define them globally.
> > + */
> > +#if !defined(CONFIG_CLANG_LTO)
> > +asm(
> > +   ___MRS_MSR_S_REGNUM
> > +   ___MRS_S
> > +   ___MSR_S
> >  );
> >
> > +#undef  ___MRS_MSR_S_REGNUM
> > +#define ___MRS_MSR_S_REGNUM
> > +#undef  ___MRS_S
> > +#define ___MRS_S
> > +#undef  

Re: [PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-03 Thread Mark Rutland
On Fri, Nov 03, 2017 at 10:53:08AM -0700, Nick Desaulniers wrote:
> These mrs_s and msr_s macros in particular were preventing us from
> linking arm64 with Clang's integrated assembler, regardless of LTO.
> Those macros ran into: https://bugs.llvm.org/show_bug.cgi?id=19749.
> So while I appreciate how clever they are, they prevent us from
> assembling with Clang so I would like to see them go.

They're necessary to work with some currently-supported toolchains
(which don't support the s*_*_c*_c*_* syntax), so they're not going to
go completely.

If you could suggest something that clang might prefer, which doesn't
adversely affect GCC, I'm all ears.

Thanks,
Mark.

> 
> On Fri, Nov 3, 2017 at 10:12 AM, Sami Tolvanen  
> wrote:
> > Clang's integrated assembler does not allow assembly macros defined
> > in one inline asm block using the .macro directive to be used across
> > separate asm blocks. LLVM developers consider this a feature and not a
> > bug, recommending code refactoring:
> >
> >   https://bugs.llvm.org/show_bug.cgi?id=19749
> >
> > As binutils doesn't allow macros to be redefined, this change adds C
> > preprocessor macros that define the assembly macros globally for binutils
> > and locally for clang's integrated assembler.
> >
> > Suggested-by: Greg Hackmann 
> > Suggested-by: Nick Desaulniers 
> > Signed-off-by: Sami Tolvanen 
> > ---
> >  arch/arm64/include/asm/kvm_hyp.h |  2 ++
> >  arch/arm64/include/asm/sysreg.h  | 71 
> > ++--
> >  2 files changed, 56 insertions(+), 17 deletions(-)
> >
> > diff --git a/arch/arm64/include/asm/kvm_hyp.h 
> > b/arch/arm64/include/asm/kvm_hyp.h
> > index 4572a9b560fa..6840704ea894 100644
> > --- a/arch/arm64/include/asm/kvm_hyp.h
> > +++ b/arch/arm64/include/asm/kvm_hyp.h
> > @@ -29,6 +29,7 @@
> > ({  \
> > u64 reg;\
> > asm volatile(ALTERNATIVE("mrs %0, " __stringify(r##nvh),\
> > +DEFINE_MRS_S   \
> >  "mrs_s %0, " __stringify(r##vh),\
> >  ARM64_HAS_VIRT_HOST_EXTN)  \
> >  : "=r" (reg)); \
> > @@ -39,6 +40,7 @@
> > do {\
> > u64 __val = (u64)(v);   \
> > asm volatile(ALTERNATIVE("msr " __stringify(r##nvh) ", 
> > %x0",\
> > +DEFINE_MSR_S   \
> >  "msr_s " __stringify(r##vh) ", 
> > %x0",\
> >  ARM64_HAS_VIRT_HOST_EXTN)  \
> >  : : "rZ" (__val)); \
> > diff --git a/arch/arm64/include/asm/sysreg.h 
> > b/arch/arm64/include/asm/sysreg.h
> > index f707fed5886f..1588ac3f3690 100644
> > --- a/arch/arm64/include/asm/sysreg.h
> > +++ b/arch/arm64/include/asm/sysreg.h
> > @@ -463,21 +463,58 @@
> >
> >  #include 
> >
> > -asm(
> > -"  .irp
> > num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n"
> > -"  .equ.L__reg_num_x\\num, \\num\n"
> > -"  .endr\n"
> > +#define ___MRS_MSR_S_REGNUM\
> > +"  .irp
> > num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n"
> >  \
> > +"  .equ.L__reg_num_x\\num, \\num\n"\
> > +"  .endr\n"\
> >  "  .equ.L__reg_num_xzr, 31\n"
> > -"\n"
> > -"  .macro  mrs_s, rt, sreg\n"
> > -   __emit_inst(0xd520|(\\sreg)|(.L__reg_num_\\rt))
> > +
> > +#define ___MRS_S   \
> > +"  .macro  mrs_s, rt, sreg\n"  \
> > +   __emit_inst(0xd520|(\\sreg)|(.L__reg_num_\\rt)) \
> >  "  .endm\n"
> > -"\n"
> > -"  .macro  msr_s, sreg, rt\n"
> > -   __emit_inst(0xd500|(\\sreg)|(.L__reg_num_\\rt))
> > +
> > +#define ___MSR_S   \
> > +"  .macro  msr_s, sreg, rt\n"  \
> > +   __emit_inst(0xd500|(\\sreg)|(.L__reg_num_\\rt)) \
> >  "  .endm\n"
> > +
> > +/*
> > + * llvm-as doesn't allow macros defined in an asm block to be used in other
> > + * asm blocks, which means we cannot define them globally.
> > + */
> > +#if !defined(CONFIG_CLANG_LTO)
> > +asm(
> > +   ___MRS_MSR_S_REGNUM
> > +   ___MRS_S
> > +   ___MSR_S
> >  );
> >
> > +#undef  ___MRS_MSR_S_REGNUM
> > +#define ___MRS_MSR_S_REGNUM
> > +#undef  ___MRS_S
> > +#define ___MRS_S
> > +#undef  ___MSR_S
> > +#define ___MSR_S
> > +#endif
> > +
> > +#define DEFINE_MRS_S   \
> > +   

Re: [PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-03 Thread Nick Desaulniers
These mrs_s and msr_s macros in particular were preventing us from
linking arm64 with Clang's integrated assembler, regardless of LTO.
Those macros ran into: https://bugs.llvm.org/show_bug.cgi?id=19749.
So while I appreciate how clever they are, they prevent us from
assembling with Clang so I would like to see them go.

On Fri, Nov 3, 2017 at 10:12 AM, Sami Tolvanen  wrote:
> Clang's integrated assembler does not allow assembly macros defined
> in one inline asm block using the .macro directive to be used across
> separate asm blocks. LLVM developers consider this a feature and not a
> bug, recommending code refactoring:
>
>   https://bugs.llvm.org/show_bug.cgi?id=19749
>
> As binutils doesn't allow macros to be redefined, this change adds C
> preprocessor macros that define the assembly macros globally for binutils
> and locally for clang's integrated assembler.
>
> Suggested-by: Greg Hackmann 
> Suggested-by: Nick Desaulniers 
> Signed-off-by: Sami Tolvanen 
> ---
>  arch/arm64/include/asm/kvm_hyp.h |  2 ++
>  arch/arm64/include/asm/sysreg.h  | 71 
> ++--
>  2 files changed, 56 insertions(+), 17 deletions(-)
>
> diff --git a/arch/arm64/include/asm/kvm_hyp.h 
> b/arch/arm64/include/asm/kvm_hyp.h
> index 4572a9b560fa..6840704ea894 100644
> --- a/arch/arm64/include/asm/kvm_hyp.h
> +++ b/arch/arm64/include/asm/kvm_hyp.h
> @@ -29,6 +29,7 @@
> ({  \
> u64 reg;\
> asm volatile(ALTERNATIVE("mrs %0, " __stringify(r##nvh),\
> +DEFINE_MRS_S   \
>  "mrs_s %0, " __stringify(r##vh),\
>  ARM64_HAS_VIRT_HOST_EXTN)  \
>  : "=r" (reg)); \
> @@ -39,6 +40,7 @@
> do {\
> u64 __val = (u64)(v);   \
> asm volatile(ALTERNATIVE("msr " __stringify(r##nvh) ", %x0",\
> +DEFINE_MSR_S   \
>  "msr_s " __stringify(r##vh) ", %x0",\
>  ARM64_HAS_VIRT_HOST_EXTN)  \
>  : : "rZ" (__val)); \
> diff --git a/arch/arm64/include/asm/sysreg.h b/arch/arm64/include/asm/sysreg.h
> index f707fed5886f..1588ac3f3690 100644
> --- a/arch/arm64/include/asm/sysreg.h
> +++ b/arch/arm64/include/asm/sysreg.h
> @@ -463,21 +463,58 @@
>
>  #include 
>
> -asm(
> -"  .irp
> num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n"
> -"  .equ.L__reg_num_x\\num, \\num\n"
> -"  .endr\n"
> +#define ___MRS_MSR_S_REGNUM\
> +"  .irp
> num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n"
>  \
> +"  .equ.L__reg_num_x\\num, \\num\n"\
> +"  .endr\n"\
>  "  .equ.L__reg_num_xzr, 31\n"
> -"\n"
> -"  .macro  mrs_s, rt, sreg\n"
> -   __emit_inst(0xd520|(\\sreg)|(.L__reg_num_\\rt))
> +
> +#define ___MRS_S   \
> +"  .macro  mrs_s, rt, sreg\n"  \
> +   __emit_inst(0xd520|(\\sreg)|(.L__reg_num_\\rt)) \
>  "  .endm\n"
> -"\n"
> -"  .macro  msr_s, sreg, rt\n"
> -   __emit_inst(0xd500|(\\sreg)|(.L__reg_num_\\rt))
> +
> +#define ___MSR_S   \
> +"  .macro  msr_s, sreg, rt\n"  \
> +   __emit_inst(0xd500|(\\sreg)|(.L__reg_num_\\rt)) \
>  "  .endm\n"
> +
> +/*
> + * llvm-as doesn't allow macros defined in an asm block to be used in other
> + * asm blocks, which means we cannot define them globally.
> + */
> +#if !defined(CONFIG_CLANG_LTO)
> +asm(
> +   ___MRS_MSR_S_REGNUM
> +   ___MRS_S
> +   ___MSR_S
>  );
>
> +#undef  ___MRS_MSR_S_REGNUM
> +#define ___MRS_MSR_S_REGNUM
> +#undef  ___MRS_S
> +#define ___MRS_S
> +#undef  ___MSR_S
> +#define ___MSR_S
> +#endif
> +
> +#define DEFINE_MRS_S   \
> +   ___MRS_MSR_S_REGNUM \
> +   ___MRS_S
> +
> +#define DEFINE_MSR_S   \
> +   ___MRS_MSR_S_REGNUM \
> +   ___MSR_S
> +
> +
> +#define __mrs_s(r, v)  \
> +   DEFINE_MRS_S\
> +"  mrs_s %0, " __stringify(r) : "=r" (v)
> +
> +#define __msr_s(r, v)  \
> +   DEFINE_MSR_S\
> 

Re: [PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-03 Thread Nick Desaulniers
These mrs_s and msr_s macros in particular were preventing us from
linking arm64 with Clang's integrated assembler, regardless of LTO.
Those macros ran into: https://bugs.llvm.org/show_bug.cgi?id=19749.
So while I appreciate how clever they are, they prevent us from
assembling with Clang so I would like to see them go.

On Fri, Nov 3, 2017 at 10:12 AM, Sami Tolvanen  wrote:
> Clang's integrated assembler does not allow assembly macros defined
> in one inline asm block using the .macro directive to be used across
> separate asm blocks. LLVM developers consider this a feature and not a
> bug, recommending code refactoring:
>
>   https://bugs.llvm.org/show_bug.cgi?id=19749
>
> As binutils doesn't allow macros to be redefined, this change adds C
> preprocessor macros that define the assembly macros globally for binutils
> and locally for clang's integrated assembler.
>
> Suggested-by: Greg Hackmann 
> Suggested-by: Nick Desaulniers 
> Signed-off-by: Sami Tolvanen 
> ---
>  arch/arm64/include/asm/kvm_hyp.h |  2 ++
>  arch/arm64/include/asm/sysreg.h  | 71 
> ++--
>  2 files changed, 56 insertions(+), 17 deletions(-)
>
> diff --git a/arch/arm64/include/asm/kvm_hyp.h 
> b/arch/arm64/include/asm/kvm_hyp.h
> index 4572a9b560fa..6840704ea894 100644
> --- a/arch/arm64/include/asm/kvm_hyp.h
> +++ b/arch/arm64/include/asm/kvm_hyp.h
> @@ -29,6 +29,7 @@
> ({  \
> u64 reg;\
> asm volatile(ALTERNATIVE("mrs %0, " __stringify(r##nvh),\
> +DEFINE_MRS_S   \
>  "mrs_s %0, " __stringify(r##vh),\
>  ARM64_HAS_VIRT_HOST_EXTN)  \
>  : "=r" (reg)); \
> @@ -39,6 +40,7 @@
> do {\
> u64 __val = (u64)(v);   \
> asm volatile(ALTERNATIVE("msr " __stringify(r##nvh) ", %x0",\
> +DEFINE_MSR_S   \
>  "msr_s " __stringify(r##vh) ", %x0",\
>  ARM64_HAS_VIRT_HOST_EXTN)  \
>  : : "rZ" (__val)); \
> diff --git a/arch/arm64/include/asm/sysreg.h b/arch/arm64/include/asm/sysreg.h
> index f707fed5886f..1588ac3f3690 100644
> --- a/arch/arm64/include/asm/sysreg.h
> +++ b/arch/arm64/include/asm/sysreg.h
> @@ -463,21 +463,58 @@
>
>  #include 
>
> -asm(
> -"  .irp
> num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n"
> -"  .equ.L__reg_num_x\\num, \\num\n"
> -"  .endr\n"
> +#define ___MRS_MSR_S_REGNUM\
> +"  .irp
> num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n"
>  \
> +"  .equ.L__reg_num_x\\num, \\num\n"\
> +"  .endr\n"\
>  "  .equ.L__reg_num_xzr, 31\n"
> -"\n"
> -"  .macro  mrs_s, rt, sreg\n"
> -   __emit_inst(0xd520|(\\sreg)|(.L__reg_num_\\rt))
> +
> +#define ___MRS_S   \
> +"  .macro  mrs_s, rt, sreg\n"  \
> +   __emit_inst(0xd520|(\\sreg)|(.L__reg_num_\\rt)) \
>  "  .endm\n"
> -"\n"
> -"  .macro  msr_s, sreg, rt\n"
> -   __emit_inst(0xd500|(\\sreg)|(.L__reg_num_\\rt))
> +
> +#define ___MSR_S   \
> +"  .macro  msr_s, sreg, rt\n"  \
> +   __emit_inst(0xd500|(\\sreg)|(.L__reg_num_\\rt)) \
>  "  .endm\n"
> +
> +/*
> + * llvm-as doesn't allow macros defined in an asm block to be used in other
> + * asm blocks, which means we cannot define them globally.
> + */
> +#if !defined(CONFIG_CLANG_LTO)
> +asm(
> +   ___MRS_MSR_S_REGNUM
> +   ___MRS_S
> +   ___MSR_S
>  );
>
> +#undef  ___MRS_MSR_S_REGNUM
> +#define ___MRS_MSR_S_REGNUM
> +#undef  ___MRS_S
> +#define ___MRS_S
> +#undef  ___MSR_S
> +#define ___MSR_S
> +#endif
> +
> +#define DEFINE_MRS_S   \
> +   ___MRS_MSR_S_REGNUM \
> +   ___MRS_S
> +
> +#define DEFINE_MSR_S   \
> +   ___MRS_MSR_S_REGNUM \
> +   ___MSR_S
> +
> +
> +#define __mrs_s(r, v)  \
> +   DEFINE_MRS_S\
> +"  mrs_s %0, " __stringify(r) : "=r" (v)
> +
> +#define __msr_s(r, v)  \
> +   DEFINE_MSR_S\
> +"  msr_s " __stringify(r) ", %0" : : "r" (v)
> +
>  /*
>   * Unlike read_cpuid, calls to 

[PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-03 Thread Sami Tolvanen
Clang's integrated assembler does not allow assembly macros defined
in one inline asm block using the .macro directive to be used across
separate asm blocks. LLVM developers consider this a feature and not a
bug, recommending code refactoring:

  https://bugs.llvm.org/show_bug.cgi?id=19749

As binutils doesn't allow macros to be redefined, this change adds C
preprocessor macros that define the assembly macros globally for binutils
and locally for clang's integrated assembler.

Suggested-by: Greg Hackmann 
Suggested-by: Nick Desaulniers 
Signed-off-by: Sami Tolvanen 
---
 arch/arm64/include/asm/kvm_hyp.h |  2 ++
 arch/arm64/include/asm/sysreg.h  | 71 ++--
 2 files changed, 56 insertions(+), 17 deletions(-)

diff --git a/arch/arm64/include/asm/kvm_hyp.h b/arch/arm64/include/asm/kvm_hyp.h
index 4572a9b560fa..6840704ea894 100644
--- a/arch/arm64/include/asm/kvm_hyp.h
+++ b/arch/arm64/include/asm/kvm_hyp.h
@@ -29,6 +29,7 @@
({  \
u64 reg;\
asm volatile(ALTERNATIVE("mrs %0, " __stringify(r##nvh),\
+DEFINE_MRS_S   \
 "mrs_s %0, " __stringify(r##vh),\
 ARM64_HAS_VIRT_HOST_EXTN)  \
 : "=r" (reg)); \
@@ -39,6 +40,7 @@
do {\
u64 __val = (u64)(v);   \
asm volatile(ALTERNATIVE("msr " __stringify(r##nvh) ", %x0",\
+DEFINE_MSR_S   \
 "msr_s " __stringify(r##vh) ", %x0",\
 ARM64_HAS_VIRT_HOST_EXTN)  \
 : : "rZ" (__val)); \
diff --git a/arch/arm64/include/asm/sysreg.h b/arch/arm64/include/asm/sysreg.h
index f707fed5886f..1588ac3f3690 100644
--- a/arch/arm64/include/asm/sysreg.h
+++ b/arch/arm64/include/asm/sysreg.h
@@ -463,21 +463,58 @@
 
 #include 
 
-asm(
-"  .irp
num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n"
-"  .equ.L__reg_num_x\\num, \\num\n"
-"  .endr\n"
+#define ___MRS_MSR_S_REGNUM\
+"  .irp
num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n"
 \
+"  .equ.L__reg_num_x\\num, \\num\n"\
+"  .endr\n"\
 "  .equ.L__reg_num_xzr, 31\n"
-"\n"
-"  .macro  mrs_s, rt, sreg\n"
-   __emit_inst(0xd520|(\\sreg)|(.L__reg_num_\\rt))
+
+#define ___MRS_S   \
+"  .macro  mrs_s, rt, sreg\n"  \
+   __emit_inst(0xd520|(\\sreg)|(.L__reg_num_\\rt)) \
 "  .endm\n"
-"\n"
-"  .macro  msr_s, sreg, rt\n"
-   __emit_inst(0xd500|(\\sreg)|(.L__reg_num_\\rt))
+
+#define ___MSR_S   \
+"  .macro  msr_s, sreg, rt\n"  \
+   __emit_inst(0xd500|(\\sreg)|(.L__reg_num_\\rt)) \
 "  .endm\n"
+
+/*
+ * llvm-as doesn't allow macros defined in an asm block to be used in other
+ * asm blocks, which means we cannot define them globally.
+ */
+#if !defined(CONFIG_CLANG_LTO)
+asm(
+   ___MRS_MSR_S_REGNUM
+   ___MRS_S
+   ___MSR_S
 );
 
+#undef  ___MRS_MSR_S_REGNUM
+#define ___MRS_MSR_S_REGNUM
+#undef  ___MRS_S
+#define ___MRS_S
+#undef  ___MSR_S
+#define ___MSR_S
+#endif
+
+#define DEFINE_MRS_S   \
+   ___MRS_MSR_S_REGNUM \
+   ___MRS_S
+
+#define DEFINE_MSR_S   \
+   ___MRS_MSR_S_REGNUM \
+   ___MSR_S
+
+
+#define __mrs_s(r, v)  \
+   DEFINE_MRS_S\
+"  mrs_s %0, " __stringify(r) : "=r" (v)
+
+#define __msr_s(r, v)  \
+   DEFINE_MSR_S\
+"  msr_s " __stringify(r) ", %0" : : "r" (v)
+
 /*
  * Unlike read_cpuid, calls to read_sysreg are never expected to be
  * optimized away or replaced with synthetic values.
@@ -502,15 +539,15 @@ asm(
  * For registers without architectural names, or simply unsupported by
  * GAS.
  */
-#define read_sysreg_s(r) ({\
-   u64 __val;  \
-   asm volatile("mrs_s %0, " __stringify(r) : "=r" (__val));   \
-   __val;  \
+#define read_sysreg_s(r) ({  

[PATCH 13/15] arm64: fix mrs_s/msr_s macros for clang LTO

2017-11-03 Thread Sami Tolvanen
Clang's integrated assembler does not allow assembly macros defined
in one inline asm block using the .macro directive to be used across
separate asm blocks. LLVM developers consider this a feature and not a
bug, recommending code refactoring:

  https://bugs.llvm.org/show_bug.cgi?id=19749

As binutils doesn't allow macros to be redefined, this change adds C
preprocessor macros that define the assembly macros globally for binutils
and locally for clang's integrated assembler.

Suggested-by: Greg Hackmann 
Suggested-by: Nick Desaulniers 
Signed-off-by: Sami Tolvanen 
---
 arch/arm64/include/asm/kvm_hyp.h |  2 ++
 arch/arm64/include/asm/sysreg.h  | 71 ++--
 2 files changed, 56 insertions(+), 17 deletions(-)

diff --git a/arch/arm64/include/asm/kvm_hyp.h b/arch/arm64/include/asm/kvm_hyp.h
index 4572a9b560fa..6840704ea894 100644
--- a/arch/arm64/include/asm/kvm_hyp.h
+++ b/arch/arm64/include/asm/kvm_hyp.h
@@ -29,6 +29,7 @@
({  \
u64 reg;\
asm volatile(ALTERNATIVE("mrs %0, " __stringify(r##nvh),\
+DEFINE_MRS_S   \
 "mrs_s %0, " __stringify(r##vh),\
 ARM64_HAS_VIRT_HOST_EXTN)  \
 : "=r" (reg)); \
@@ -39,6 +40,7 @@
do {\
u64 __val = (u64)(v);   \
asm volatile(ALTERNATIVE("msr " __stringify(r##nvh) ", %x0",\
+DEFINE_MSR_S   \
 "msr_s " __stringify(r##vh) ", %x0",\
 ARM64_HAS_VIRT_HOST_EXTN)  \
 : : "rZ" (__val)); \
diff --git a/arch/arm64/include/asm/sysreg.h b/arch/arm64/include/asm/sysreg.h
index f707fed5886f..1588ac3f3690 100644
--- a/arch/arm64/include/asm/sysreg.h
+++ b/arch/arm64/include/asm/sysreg.h
@@ -463,21 +463,58 @@
 
 #include 
 
-asm(
-"  .irp
num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n"
-"  .equ.L__reg_num_x\\num, \\num\n"
-"  .endr\n"
+#define ___MRS_MSR_S_REGNUM\
+"  .irp
num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n"
 \
+"  .equ.L__reg_num_x\\num, \\num\n"\
+"  .endr\n"\
 "  .equ.L__reg_num_xzr, 31\n"
-"\n"
-"  .macro  mrs_s, rt, sreg\n"
-   __emit_inst(0xd520|(\\sreg)|(.L__reg_num_\\rt))
+
+#define ___MRS_S   \
+"  .macro  mrs_s, rt, sreg\n"  \
+   __emit_inst(0xd520|(\\sreg)|(.L__reg_num_\\rt)) \
 "  .endm\n"
-"\n"
-"  .macro  msr_s, sreg, rt\n"
-   __emit_inst(0xd500|(\\sreg)|(.L__reg_num_\\rt))
+
+#define ___MSR_S   \
+"  .macro  msr_s, sreg, rt\n"  \
+   __emit_inst(0xd500|(\\sreg)|(.L__reg_num_\\rt)) \
 "  .endm\n"
+
+/*
+ * llvm-as doesn't allow macros defined in an asm block to be used in other
+ * asm blocks, which means we cannot define them globally.
+ */
+#if !defined(CONFIG_CLANG_LTO)
+asm(
+   ___MRS_MSR_S_REGNUM
+   ___MRS_S
+   ___MSR_S
 );
 
+#undef  ___MRS_MSR_S_REGNUM
+#define ___MRS_MSR_S_REGNUM
+#undef  ___MRS_S
+#define ___MRS_S
+#undef  ___MSR_S
+#define ___MSR_S
+#endif
+
+#define DEFINE_MRS_S   \
+   ___MRS_MSR_S_REGNUM \
+   ___MRS_S
+
+#define DEFINE_MSR_S   \
+   ___MRS_MSR_S_REGNUM \
+   ___MSR_S
+
+
+#define __mrs_s(r, v)  \
+   DEFINE_MRS_S\
+"  mrs_s %0, " __stringify(r) : "=r" (v)
+
+#define __msr_s(r, v)  \
+   DEFINE_MSR_S\
+"  msr_s " __stringify(r) ", %0" : : "r" (v)
+
 /*
  * Unlike read_cpuid, calls to read_sysreg are never expected to be
  * optimized away or replaced with synthetic values.
@@ -502,15 +539,15 @@ asm(
  * For registers without architectural names, or simply unsupported by
  * GAS.
  */
-#define read_sysreg_s(r) ({\
-   u64 __val;  \
-   asm volatile("mrs_s %0, " __stringify(r) : "=r" (__val));   \
-   __val;  \
+#define read_sysreg_s(r) ({\
+   u64 __val;