Skip to contentSkip to conversation

RISC-V: fix compile error from decuplicated __ALTERNATIVE_CFG_2

PATCH Superseded Heiko Stuebner 3 participants 9 messages Jan 3, 2023 – Jan 5, 2023 RISC-V

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 RISC-V: fix compile error from decuplicated __ALTERNATIVE_CFG_2

From: Heiko Stuebner <[email protected]>

On the non-assembler-side wrapping alternative-macros inside other macros to prevent duplication of code works, as the end result will just be a string that gets fed to the asm instruction.

In real assembler code, wrapping .macro blocks inside other .macro blocks brings more restrictions on usage and the optimization done by
commit 2ba8c7dc71c0 ("riscv: Don't duplicate __ALTERNATIVE_CFG in __ALTERNATIVE_CFG_2") results in a compile error like:

../arch/riscv/lib/strcmp.S: Assembler messages:
../arch/riscv/lib/strcmp.S:15: Error: too many positional arguments ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "886:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "887:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "886:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "887:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "886:" ../arch/riscv/lib/strcmp.S:15: Error: attempt to move .org backwards

Going back to the original code for the non-assembler-part makes that code work again. So this reverts the #ifdef ASSEMBLY part of that commit to the previous variant with duplicated base.

Fixes 2ba8c7dc71c0 ("riscv: Don't duplicate __ALTERNATIVE_CFG in __ALTERNATIVE_CFG_2")

---
I was of two minds about either to revert the full patch, or doing just this partial one for the ASSEMBLY part. I did go with this variant, as I still like the idea of deduplicating as much as possible :-)

 arch/riscv/include/asm/alternative-macros.h | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
arch/riscv/include/asm/alternative-macros.h +12 -1
index 7226e2462584..e7bdb2a510a4 100644
@@ -44,9 +44,20 @@
 	ALT_NEW_CONTENT \vendor_id, \errata_id, \enable, \new_c
 .endm
 
+/*
+ * Using ALTERNATIVE_CFG inside ALTERNATIVE_CFG_2 results in compile errors.
+ * So the common code needs to stay duplicated.
+ */
 .macro ALTERNATIVE_CFG_2 old_c, new_c_1, vendor_id_1, errata_id_1, enable_1,	\
 				new_c_2, vendor_id_2, errata_id_2, enable_2
-	ALTERNATIVE_CFG \old_c, \new_c_1, \vendor_id_1, \errata_id_1, \enable_1
+886 :
+	.option push
+	.option norvc
+	.option norelax
+	\old_c
+	.option pop
+887 :
+	ALT_NEW_CONTENT \vendor_id_1, \errata_id_1, \enable_1, \new_c_1
 	ALT_NEW_CONTENT \vendor_id_2, \errata_id_2, \enable_2, \new_c_2
 .endm
 
Signature · 7 lines
2.35.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#1 lore raw
Quoted 3 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

RISC-V: fix compile error from decuplicated __ALTERNATIVE_CFG_2

Hey Heiko/Palmer,

Was a little hard to tell from Drew's mail if he was objecting to this variant of the patch, but FWIW s/decup/dedup/ if this gets applied as-is.

Thanks,
Conor.

Quoted 66 lines

On the non-assembler-side wrapping alternative-macros inside other macros to prevent duplication of code works, as the end result will just be a string that gets fed to the asm instruction.

In real assembler code, wrapping .macro blocks inside other .macro blocks brings more restrictions on usage and the optimization done by
commit 2ba8c7dc71c0 ("riscv: Don't duplicate __ALTERNATIVE_CFG in __ALTERNATIVE_CFG_2") results in a compile error like:

../arch/riscv/lib/strcmp.S: Assembler messages:
../arch/riscv/lib/strcmp.S:15: Error: too many positional arguments ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "886:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "887:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "886:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "887:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "886:" ../arch/riscv/lib/strcmp.S:15: Error: attempt to move .org backwards

Going back to the original code for the non-assembler-part makes that code work again. So this reverts the #ifdef ASSEMBLY part of that commit to the previous variant with duplicated base.

Fixes 2ba8c7dc71c0 ("riscv: Don't duplicate __ALTERNATIVE_CFG in __ALTERNATIVE_CFG_2")

---
I was of two minds about either to revert the full patch, or doing just this partial one for the ASSEMBLY part. I did go with this variant, as I still like the idea of deduplicating as much as possible :-)

 arch/riscv/include/asm/alternative-macros.h | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
arch/riscv/include/asm/alternative-macros.h +12 -1
index 7226e2462584..e7bdb2a510a4 100644
@@ -44,9 +44,20 @@
 	ALT_NEW_CONTENT \vendor_id, \errata_id, \enable, \new_c
 .endm
 
+/*
+ * Using ALTERNATIVE_CFG inside ALTERNATIVE_CFG_2 results in compile errors.
+ * So the common code needs to stay duplicated.
+ */
 .macro ALTERNATIVE_CFG_2 old_c, new_c_1, vendor_id_1, errata_id_1, enable_1,	\
 				new_c_2, vendor_id_2, errata_id_2, enable_2
-	ALTERNATIVE_CFG \old_c, \new_c_1, \vendor_id_1, \errata_id_1, \enable_1
+886 :
+	.option push
+	.option norvc
+	.option norelax
+	\old_c
+	.option pop
+887 :
+	ALT_NEW_CONTENT \vendor_id_1, \errata_id_1, \enable_1, \new_c_1
 	ALT_NEW_CONTENT \vendor_id_2, \errata_id_2, \enable_2, \new_c_2
 .endm
 
Signature · 7 lines
2.35.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#2 lore raw
Quoted 10 lines · Conor Dooley
Quoted 3 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

RISC-V: fix compile error from decuplicated __ALTERNATIVE_CFG_2

Hey Heiko/Palmer,

Was a little hard to tell from Drew's mail if he was objecting to this variant of the patch, but FWIW s/decup/dedup/ if this gets applied as-is.

I wasn't completely objecting, but rather suggesting we still try to factor out what we can. However, after reading a bit more about macros I tried this

 .macro ALTERNATIVE_CFG_2 old_c, new_c_1, vendor_id_1, errata_id_1, enable_1,   \
                                new_c_2, vendor_id_2, errata_id_2, enable_2
-       ALTERNATIVE_CFG \old_c, \new_c_1, \vendor_id_1, \errata_id_1, \enable_1
+       ALTERNATIVE_CFG "\old_c", "\new_c_1", \vendor_id_1, \errata_id_1, \enable_1
        ALT_NEW_CONTENT \vendor_id_2, \errata_id_2, \enable_2, \new_c_2

.endm

which appears to work for my simple test. Does that work for you too, Heiko? If so, then I think I'd prefer we do that. Also, we may want to add quotes to all macro arguments which may contain spaces, even if things seem to work now, e.g. the \new_c_2 argument passed to ALT_NEW_CONTENT.

Thanks,
drew

Quoted 62 lines

Thanks,
Conor.

Quoted 58 lines

On the non-assembler-side wrapping alternative-macros inside other macros to prevent duplication of code works, as the end result will just be a string that gets fed to the asm instruction.

In real assembler code, wrapping .macro blocks inside other .macro blocks brings more restrictions on usage and the optimization done by
commit 2ba8c7dc71c0 ("riscv: Don't duplicate __ALTERNATIVE_CFG in __ALTERNATIVE_CFG_2") results in a compile error like:

../arch/riscv/lib/strcmp.S: Assembler messages:
../arch/riscv/lib/strcmp.S:15: Error: too many positional arguments ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "886:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "887:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "886:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "887:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "886:" ../arch/riscv/lib/strcmp.S:15: Error: attempt to move .org backwards

Going back to the original code for the non-assembler-part makes that code work again. So this reverts the #ifdef ASSEMBLY part of that commit to the previous variant with duplicated base.

Fixes 2ba8c7dc71c0 ("riscv: Don't duplicate __ALTERNATIVE_CFG in __ALTERNATIVE_CFG_2")

---
I was of two minds about either to revert the full patch, or doing just this partial one for the ASSEMBLY part. I did go with this variant, as I still like the idea of deduplicating as much as possible :-)

 arch/riscv/include/asm/alternative-macros.h | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
arch/riscv/include/asm/alternative-macros.h +12 -1
index 7226e2462584..e7bdb2a510a4 100644
@@ -44,9 +44,20 @@
 	ALT_NEW_CONTENT \vendor_id, \errata_id, \enable, \new_c
 .endm
 
+/*
+ * Using ALTERNATIVE_CFG inside ALTERNATIVE_CFG_2 results in compile errors.
+ * So the common code needs to stay duplicated.
+ */
 .macro ALTERNATIVE_CFG_2 old_c, new_c_1, vendor_id_1, errata_id_1, enable_1,	\
 				new_c_2, vendor_id_2, errata_id_2, enable_2
-	ALTERNATIVE_CFG \old_c, \new_c_1, \vendor_id_1, \errata_id_1, \enable_1
+886 :
+	.option push
+	.option norvc
+	.option norelax
+	\old_c
+	.option pop
+887 :
+	ALT_NEW_CONTENT \vendor_id_1, \errata_id_1, \enable_1, \new_c_1
 	ALT_NEW_CONTENT \vendor_id_2, \errata_id_2, \enable_2, \new_c_2
 .endm
 
Quoted 8 lines
Quoted 8 lines
Signature · 7 lines
2.35.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#3 lore raw

Hi again,

Am Mittwoch, 4. Januar 2023, 15:08:33 CET schrieb Andrew Jones:

Quoted 23 lines · Conor Dooley
Quoted 10 lines · Heiko Stuebner
Quoted 3 lines

From: Heiko Stuebner <[email protected]>

RISC-V: fix compile error from decuplicated __ALTERNATIVE_CFG_2

Hey Heiko/Palmer,

Was a little hard to tell from Drew's mail if he was objecting to this variant of the patch, but FWIW s/decup/dedup/ if this gets applied as-is.

I wasn't completely objecting, but rather suggesting we still try to factor out what we can. However, after reading a bit more about macros I tried this

 .macro ALTERNATIVE_CFG_2 old_c, new_c_1, vendor_id_1, errata_id_1, enable_1,   \
                                new_c_2, vendor_id_2, errata_id_2, enable_2
arch/riscv/include/asm/alternative-macros.h +1 -1
-       ALTERNATIVE_CFG \old_c, \new_c_1, \vendor_id_1, \errata_id_1, \enable_1
+       ALTERNATIVE_CFG "\old_c", "\new_c_1", \vendor_id_1, \errata_id_1, \enable_1
        ALT_NEW_CONTENT \vendor_id_2, \errata_id_2, \enable_2, \new_c_2
 .endm
Quoted 4 lines

which appears to work for my simple test. Does that work for you too, Heiko? If so, then I think I'd prefer we do that. Also, we may want to add quotes to all macro arguments which may contain spaces, even if things seem to work now, e.g. the \new_c_2 argument passed to ALT_NEW_CONTENT.

Yay ... adding quotes really seems to work - at least a GNU toolchain

I guess that may have been the reason to move new_c at the end and making it vararg in ALT_NEW_CONTENT originally.

And in fact with added quotes the varargs argument wouldn't be necessary anymore.

Just removing the varargs argument alone results in
Error: too many positional arguments
erros, but wrapping the "\new_c*" in quotes lets it compile again.

Heiko

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#4 lore raw
Quoted 26 lines · Heiko Stübner

Hi again,

Am Mittwoch, 4. Januar 2023, 15:08:33 CET schrieb Andrew Jones:

Quoted 23 lines · Conor Dooley
Quoted 10 lines · Heiko Stuebner
Quoted 3 lines

From: Heiko Stuebner <[email protected]>

RISC-V: fix compile error from decuplicated __ALTERNATIVE_CFG_2

Hey Heiko/Palmer,

Was a little hard to tell from Drew's mail if he was objecting to this variant of the patch, but FWIW s/decup/dedup/ if this gets applied as-is.

I wasn't completely objecting, but rather suggesting we still try to factor out what we can. However, after reading a bit more about macros I tried this

 .macro ALTERNATIVE_CFG_2 old_c, new_c_1, vendor_id_1, errata_id_1, enable_1,   \
                                new_c_2, vendor_id_2, errata_id_2, enable_2
arch/riscv/include/asm/alternative-macros.h +1 -1
-       ALTERNATIVE_CFG \old_c, \new_c_1, \vendor_id_1, \errata_id_1, \enable_1
+       ALTERNATIVE_CFG "\old_c", "\new_c_1", \vendor_id_1, \errata_id_1, \enable_1
        ALT_NEW_CONTENT \vendor_id_2, \errata_id_2, \enable_2, \new_c_2
 .endm
Quoted 6 lines
Quoted 4 lines

which appears to work for my simple test. Does that work for you too, Heiko? If so, then I think I'd prefer we do that. Also, we may want to add quotes to all macro arguments which may contain spaces, even if things seem to work now, e.g. the \new_c_2 argument passed to ALT_NEW_CONTENT.

Yay ... adding quotes really seems to work - at least a GNU toolchain

clang isn't complaining for my test either.

Quoted 10 lines

I guess that may have been the reason to move new_c at the end and making it vararg in ALT_NEW_CONTENT originally.

And in fact with added quotes the varargs argument wouldn't be necessary anymore.

Just removing the varargs argument alone results in
Error: too many positional arguments
erros, but wrapping the "\new_c*" in quotes lets it compile again.

Excellent news. Do you plan to post the patch? Or should I post with your Reported-by tag?

Thanks,
drew

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#5 lore raw

Am Donnerstag, 5. Januar 2023, 16:00:45 CET schrieb Andrew Jones:

Quoted 27 lines · Heiko Stübner
Quoted 26 lines

Hi again,

Am Mittwoch, 4. Januar 2023, 15:08:33 CET schrieb Andrew Jones:

Quoted 23 lines · Conor Dooley
Quoted 10 lines · Heiko Stuebner
Quoted 3 lines

From: Heiko Stuebner <[email protected]>

RISC-V: fix compile error from decuplicated __ALTERNATIVE_CFG_2

Hey Heiko/Palmer,

Was a little hard to tell from Drew's mail if he was objecting to this variant of the patch, but FWIW s/decup/dedup/ if this gets applied as-is.

I wasn't completely objecting, but rather suggesting we still try to factor out what we can. However, after reading a bit more about macros I tried this

 .macro ALTERNATIVE_CFG_2 old_c, new_c_1, vendor_id_1, errata_id_1, enable_1,   \
                                new_c_2, vendor_id_2, errata_id_2, enable_2
arch/riscv/include/asm/alternative-macros.h +1 -1
-       ALTERNATIVE_CFG \old_c, \new_c_1, \vendor_id_1, \errata_id_1, \enable_1
+       ALTERNATIVE_CFG "\old_c", "\new_c_1", \vendor_id_1, \errata_id_1, \enable_1
        ALT_NEW_CONTENT \vendor_id_2, \errata_id_2, \enable_2, \new_c_2
 .endm
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

which appears to work for my simple test. Does that work for you too, Heiko? If so, then I think I'd prefer we do that. Also, we may want to add quotes to all macro arguments which may contain spaces, even if things seem to work now, e.g. the \new_c_2 argument passed to ALT_NEW_CONTENT.

Yay ... adding quotes really seems to work - at least a GNU toolchain

clang isn't complaining for my test either.

Nice

Quoted 8 lines
Quoted 8 lines

I guess that may have been the reason to move new_c at the end and making it vararg in ALT_NEW_CONTENT originally.

And in fact with added quotes the varargs argument wouldn't be necessary anymore.

Quoted 6 lines
Quoted 3 lines

Just removing the varargs argument alone results in
Error: too many positional arguments
erros, but wrapping the "\new_c*" in quotes lets it compile again.

Excellent news. Do you plan to post the patch? Or should I post with your Reported-by tag?

I was planning on posting this today. Did the v2 an hour ago :-)

Heiko

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#6 lore raw
In reply to Heiko Stuebner
Quoted 11 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

On the non-assembler-side wrapping alternative-macros inside other macros to prevent duplication of code works, as the end result will just be a string that gets fed to the asm instruction.

In real assembler code, wrapping .macro blocks inside other .macro blocks brings more restrictions on usage and the optimization done by
commit 2ba8c7dc71c0 ("riscv: Don't duplicate __ALTERNATIVE_CFG in __ALTERNATIVE_CFG_2") results in a compile error like:

Quoted 8 lines

../arch/riscv/lib/strcmp.S: Assembler messages:
../arch/riscv/lib/strcmp.S:15: Error: too many positional arguments ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "886:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "887:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "886:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "887:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "886:" ../arch/riscv/lib/strcmp.S:15: Error: attempt to move .org backwards

Ouch. I thought I had tested that, but looking now at my test code I see I only bothered to test ALTERNATIVE(), not ALTERNATIVE_2(). Adding the ALTERNATIVE_2() test, I can reproduce this.

It appears the issue is that as macro arguments may be separated by commas or spaces, the old and new instruction macro arguments, which have spaces between their instructions and operands, get interpreted as extra macro arguments. There's probably no way to convince the macro otherwise, unfortunately.

Quoted 27 lines

Going back to the original code for the non-assembler-part makes that code work again. So this reverts the #ifdef ASSEMBLY part of that commit to the previous variant with duplicated base.

Fixes 2ba8c7dc71c0 ("riscv: Don't duplicate __ALTERNATIVE_CFG in __ALTERNATIVE_CFG_2")

---
I was of two minds about either to revert the full patch, or doing just this partial one for the ASSEMBLY part. I did go with this variant, as I still like the idea of deduplicating as much as possible :-)

 arch/riscv/include/asm/alternative-macros.h | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
arch/riscv/include/asm/alternative-macros.h +4 -0
index 7226e2462584..e7bdb2a510a4 100644
@@ -44,9 +44,20 @@
 	ALT_NEW_CONTENT \vendor_id, \errata_id, \enable, \new_c
 .endm
 
+/*
+ * Using ALTERNATIVE_CFG inside ALTERNATIVE_CFG_2 results in compile errors.
+ * So the common code needs to stay duplicated.
+ */

arch/riscv/include/asm/alternative-macros.h line 59

Quoted 10 lines
arch/riscv/include/asm/alternative-macros.h +7 -1
 .macro ALTERNATIVE_CFG_2 old_c, new_c_1, vendor_id_1, errata_id_1, enable_1,	\
 				new_c_2, vendor_id_2, errata_id_2, enable_2
-	ALTERNATIVE_CFG \old_c, \new_c_1, \vendor_id_1, \errata_id_1, \enable_1
+886 :
+	.option push
+	.option norvc
+	.option norelax
+	\old_c
+	.option pop
+887 :

We could still share this by creating another macro which only takes old_c, and then invoke that from both ALTERNATIVE_CFG and ALTERNATIVE_CFG_2, I think.

Quoted 7 lines
arch/riscv/include/asm/alternative-macros.h +1 -0
+	ALT_NEW_CONTENT \vendor_id_1, \errata_id_1, \enable_1, \new_c_1
 	ALT_NEW_CONTENT \vendor_id_2, \errata_id_2, \enable_2, \new_c_2
 .endm
 
Signature · 1 line
2.35.1

Thanks,
drew

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#7 lore raw

Hi Andrew,

Am Mittwoch, 4. Januar 2023, 11:06:29 CET schrieb Andrew Jones:

Quoted 64 lines · Heiko Stuebner
Quoted 11 lines

From: Heiko Stuebner <[email protected]>

On the non-assembler-side wrapping alternative-macros inside other macros to prevent duplication of code works, as the end result will just be a string that gets fed to the asm instruction.

In real assembler code, wrapping .macro blocks inside other .macro blocks brings more restrictions on usage and the optimization done by
commit 2ba8c7dc71c0 ("riscv: Don't duplicate __ALTERNATIVE_CFG in __ALTERNATIVE_CFG_2") results in a compile error like:

Quoted 8 lines

../arch/riscv/lib/strcmp.S: Assembler messages:
../arch/riscv/lib/strcmp.S:15: Error: too many positional arguments ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "886:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "887:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "886:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "887:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "886:" ../arch/riscv/lib/strcmp.S:15: Error: attempt to move .org backwards

Ouch. I thought I had tested that, but looking now at my test code I see I only bothered to test ALTERNATIVE(), not ALTERNATIVE_2(). Adding the ALTERNATIVE_2() test, I can reproduce this.

It appears the issue is that as macro arguments may be separated by commas or spaces, the old and new instruction macro arguments, which have spaces between their instructions and operands, get interpreted as extra macro arguments. There's probably no way to convince the macro otherwise, unfortunately.

Quoted 33 lines

Going back to the original code for the non-assembler-part makes that code work again. So this reverts the #ifdef ASSEMBLY part of that commit to the previous variant with duplicated base.

Fixes 2ba8c7dc71c0 ("riscv: Don't duplicate __ALTERNATIVE_CFG in __ALTERNATIVE_CFG_2")

---
I was of two minds about either to revert the full patch, or doing just this partial one for the ASSEMBLY part. I did go with this variant, as I still like the idea of deduplicating as much as possible :-)

 arch/riscv/include/asm/alternative-macros.h | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
arch/riscv/include/asm/alternative-macros.h +7 -1
index 7226e2462584..e7bdb2a510a4 100644
@@ -44,9 +44,20 @@
 	ALT_NEW_CONTENT \vendor_id, \errata_id, \enable, \new_c
 .endm
 
+/*
+ * Using ALTERNATIVE_CFG inside ALTERNATIVE_CFG_2 results in compile errors.
+ * So the common code needs to stay duplicated.
+ */
 .macro ALTERNATIVE_CFG_2 old_c, new_c_1, vendor_id_1, errata_id_1, enable_1,	\
 				new_c_2, vendor_id_2, errata_id_2, enable_2
-	ALTERNATIVE_CFG \old_c, \new_c_1, \vendor_id_1, \errata_id_1, \enable_1
+886 :
+	.option push
+	.option norvc
Quoted 8 lines

arch/riscv/include/asm/alternative-macros.h line 59

Quoted 4 lines
arch/riscv/include/asm/alternative-macros.h +4 -0
+	.option norelax
+	\old_c
+	.option pop
+887 :

We could still share this by creating another macro which only takes old_c, and then invoke that from both ALTERNATIVE_CFG and ALTERNATIVE_CFG_2, I think.

hmm, so I was trying the change below, but then even ALTERNATIVE_CFG fails. I was looking for quite a while for some sort of typo I may have in that change, but so far haven't found any.

Did you have a different solution in mind that may work?

Heiko

------------- 8< -------------

arch/riscv/include/asm/alternative-macros.h +7 -7
index e7bdb2a510a4..49c67fd94c57 100644
@@ -33,13 +33,17 @@
        .endif
 .endm
 
-.macro ALTERNATIVE_CFG old_c, new_c, vendor_id, errata_id, enable
-886 :
+.macro ALT_OLD_CONTENT old_c
        .option push
        .option norvc
        .option norelax
        \old_c
        .option pop
+.endm
+
+.macro ALTERNATIVE_CFG old_c, new_c, vendor_id, errata_id, enable
+886 :
+       ALT_OLD_CONTENT \old_c
 887 :
        ALT_NEW_CONTENT \vendor_id, \errata_id, \enable, \new_c
 .endm
@@ -51,11 +55,7 @@
 .macro ALTERNATIVE_CFG_2 old_c, new_c_1, vendor_id_1, errata_id_1, enable_1,   \
                                new_c_2, vendor_id_2, errata_id_2, enable_2
 886 :
-       .option push
-       .option norvc
-       .option norelax
-       \old_c
-       .option pop
+       ALT_OLD_CONTENT \old_c
 887 :
        ALT_NEW_CONTENT \vendor_id_1, \errata_id_1, \enable_1, \new_c_1
        ALT_NEW_CONTENT \vendor_id_2, \errata_id_2, \enable_2, \new_c_2

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#8 lore raw

Hi again,

scratch this mail, I just saw the other one from 15:08 :-)

Heiko

Am Mittwoch, 4. Januar 2023, 15:11:38 CET schrieb Heiko Stübner:

Quoted 115 lines

Hi Andrew,

Am Mittwoch, 4. Januar 2023, 11:06:29 CET schrieb Andrew Jones:

Quoted 64 lines · Heiko Stuebner
Quoted 11 lines

From: Heiko Stuebner <[email protected]>

On the non-assembler-side wrapping alternative-macros inside other macros to prevent duplication of code works, as the end result will just be a string that gets fed to the asm instruction.

In real assembler code, wrapping .macro blocks inside other .macro blocks brings more restrictions on usage and the optimization done by
commit 2ba8c7dc71c0 ("riscv: Don't duplicate __ALTERNATIVE_CFG in __ALTERNATIVE_CFG_2") results in a compile error like:

Quoted 8 lines

../arch/riscv/lib/strcmp.S: Assembler messages:
../arch/riscv/lib/strcmp.S:15: Error: too many positional arguments ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "886:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "887:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "886:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "887:" ../arch/riscv/lib/strcmp.S:15: Error: backward ref to unknown label "886:" ../arch/riscv/lib/strcmp.S:15: Error: attempt to move .org backwards

Ouch. I thought I had tested that, but looking now at my test code I see I only bothered to test ALTERNATIVE(), not ALTERNATIVE_2(). Adding the ALTERNATIVE_2() test, I can reproduce this.

It appears the issue is that as macro arguments may be separated by commas or spaces, the old and new instruction macro arguments, which have spaces between their instructions and operands, get interpreted as extra macro arguments. There's probably no way to convince the macro otherwise, unfortunately.

Quoted 33 lines

Going back to the original code for the non-assembler-part makes that code work again. So this reverts the #ifdef ASSEMBLY part of that commit to the previous variant with duplicated base.

Fixes 2ba8c7dc71c0 ("riscv: Don't duplicate __ALTERNATIVE_CFG in __ALTERNATIVE_CFG_2")

---
I was of two minds about either to revert the full patch, or doing just this partial one for the ASSEMBLY part. I did go with this variant, as I still like the idea of deduplicating as much as possible :-)

 arch/riscv/include/asm/alternative-macros.h | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
arch/riscv/include/asm/alternative-macros.h +7 -1
index 7226e2462584..e7bdb2a510a4 100644
@@ -44,9 +44,20 @@
 	ALT_NEW_CONTENT \vendor_id, \errata_id, \enable, \new_c
 .endm
 
+/*
+ * Using ALTERNATIVE_CFG inside ALTERNATIVE_CFG_2 results in compile errors.
+ * So the common code needs to stay duplicated.
+ */
 .macro ALTERNATIVE_CFG_2 old_c, new_c_1, vendor_id_1, errata_id_1, enable_1,	\
 				new_c_2, vendor_id_2, errata_id_2, enable_2
-	ALTERNATIVE_CFG \old_c, \new_c_1, \vendor_id_1, \errata_id_1, \enable_1
+886 :
+	.option push
+	.option norvc
Quoted 8 lines

arch/riscv/include/asm/alternative-macros.h line 59

Quoted 4 lines
arch/riscv/include/asm/alternative-macros.h +4 -0
+	.option norelax
+	\old_c
+	.option pop
+887 :

We could still share this by creating another macro which only takes old_c, and then invoke that from both ALTERNATIVE_CFG and ALTERNATIVE_CFG_2, I think.

hmm, so I was trying the change below, but then even ALTERNATIVE_CFG fails. I was looking for quite a while for some sort of typo I may have in that change, but so far haven't found any.

Did you have a different solution in mind that may work?

Heiko

------------- 8< -------------

arch/riscv/include/asm/alternative-macros.h +6 -2
index e7bdb2a510a4..49c67fd94c57 100644
@@ -33,13 +33,17 @@
        .endif
 .endm
 
-.macro ALTERNATIVE_CFG old_c, new_c, vendor_id, errata_id, enable
-886 :
+.macro ALT_OLD_CONTENT old_c
        .option push
        .option norvc
        .option norelax
        \old_c
        .option pop
+.endm
+
+.macro ALTERNATIVE_CFG old_c, new_c, vendor_id, errata_id, enable
+886 :
+       ALT_OLD_CONTENT \old_c
 887 :
        ALT_NEW_CONTENT \vendor_id, \errata_id, \enable, \new_c
 .endm
@@ -51,11 +55,7 @@
 .macro ALTERNATIVE_CFG_2 old_c, new_c_1, vendor_id_1, errata_id_1, enable_1,   \
                                new_c_2, vendor_id_2, errata_id_2, enable_2

arch/riscv/include/asm/alternative-macros.h line 61

Quoted 10 lines
arch/riscv/include/asm/alternative-macros.h +1 -5
 886 :
-       .option push
-       .option norvc
-       .option norelax
-       \old_c
-       .option pop
+       ALT_OLD_CONTENT \old_c
 887 :
        ALT_NEW_CONTENT \vendor_id_1, \errata_id_1, \enable_1, \new_c_1
        ALT_NEW_CONTENT \vendor_id_2, \errata_id_2, \enable_2, \new_c_2

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv