'mov <rd>, <imm>' produces an error where imm is 0-255 and -march=armv8m.base is used
Assembly such as mov r0, #0 fails to build with clang but is accepted by GCC under -march=armv8m.base. I'm not entirely sure if my understanding is correct, but it seems like this is might be a bug in LLVM.
Small example (https://godbolt.org/z/3Y1jqWjvs):
void foo(void) {
// Fails to build with clang, accepted by GCC
__asm__ volatile("\n\
.syntax unified\n\
mov r0, #0");
// Clang and GCC are both happy with this
__asm__ volatile("\n\
.syntax unified\n\
mov r0, #256");
}
When building for Arm with -march=armv8m.base, Clang errors on the top __asm__ statement as below (output is abbreviated):
<source>:6:1: error: invalid instruction, any one of the following would fix this:
6 | mov r0, #0");
| ^
<inline asm>:3:23: note: instantiated into assembly here
3 | mov r0, #0
|
...
Looking at section C2.4.119 in the Armv8-M arch manual, my understanding is that:
- T1 isn't feasible because we 1) aren't in an IT block and 2) used
movin the inline asm (rather thanmovs) so we'd look for a flag-preserving variant instead. - T2 isn't feasible because we're looking at Armv8-M baseline (not mainline).
- T3 should be feasible (and, this is what GCC seems to do, IIUC).
I'm again not entirely sure if I'm misunderstanding something above, but it seems like we should be "selecting" T3 here rather than erroring, similar to GCC.
@llvm/issue-subscribers-backend-aarch64
Author: Jonathon Penix (jonathonpenix)
Small example (https://godbolt.org/z/3Y1jqWjvs):
void foo(void) {
// Fails to build with clang, accepted by GCC
__asm__ volatile("\n\
.syntax unified\n\
mov r0, #<!-- -->0");
// Clang and GCC are both happy with this
__asm__ volatile("\n\
.syntax unified\n\
mov r0, #<!-- -->256");
}
When building for Arm with -march=armv8m.base, Clang errors on the top __asm__ statement as below (output is abbreviated):
<source>:6:1: error: invalid instruction, any one of the following would fix this:
6 | mov r0, #<!-- -->0");
| ^
<inline asm>:3:23: note: instantiated into assembly here
3 | mov r0, #<!-- -->0
|
...
Looking at section C2.4.119 in the Armv8-M arch manual, my understanding is that:
- T1 isn't feasible because we 1) aren't in an IT block and 2) used
movin the inline asm (rather thanmovs) so we'd look for a flag-preserving variant instead. - T2 isn't feasible because we're looking at Armv8-M baseline (not mainline).
- T3 should be feasible (and, this is what GCC seems to do, IIUC).
I'm again not entirely sure if I'm misunderstanding something above, but it seems like we should be "selecting" T3 here rather than erroring, similar to GCC.
@llvm/issue-subscribers-backend-arm
Author: Jonathon Penix (jonathonpenix)
Small example (https://godbolt.org/z/3Y1jqWjvs):
void foo(void) {
// Fails to build with clang, accepted by GCC
__asm__ volatile("\n\
.syntax unified\n\
mov r0, #<!-- -->0");
// Clang and GCC are both happy with this
__asm__ volatile("\n\
.syntax unified\n\
mov r0, #<!-- -->256");
}
When building for Arm with -march=armv8m.base, Clang errors on the top __asm__ statement as below (output is abbreviated):
<source>:6:1: error: invalid instruction, any one of the following would fix this:
6 | mov r0, #<!-- -->0");
| ^
<inline asm>:3:23: note: instantiated into assembly here
3 | mov r0, #<!-- -->0
|
...
Looking at section C2.4.119 in the Armv8-M arch manual, my understanding is that:
- T1 isn't feasible because we 1) aren't in an IT block and 2) used
movin the inline asm (rather thanmovs) so we'd look for a flag-preserving variant instead. - T2 isn't feasible because we're looking at Armv8-M baseline (not mainline).
- T3 should be feasible (and, this is what GCC seems to do, IIUC).
I'm again not entirely sure if I'm misunderstanding something above, but it seems like we should be "selecting" T3 here rather than erroring, similar to GCC.
The reference specifies that T2 is the correct encoding here. (The manual says "mov" refers to T3 only if "<imm16> cannot be represented in T1 or T2".) Making the encoding change based on whether you're compiling for armv8m.base or armv8m.main seems dubious. Maybe the practical difference here is small enough we can get away with it, though.
You can write movw r0, #0 to get the T3 encoding on both clang and gcc.
That said, I think the current behavior for mov r0, #256 is a bug: it should produce the same error for the same reasons.
The reference specifies that T2 is the correct encoding here. (The manual says "mov" refers to T3 only if "
<imm16>cannot be represented in T1 or T2".)
Oh, whoops. Thanks for the correction!
Making the encoding change based on whether you're compiling for armv8m.base or armv8m.main seems dubious. Maybe the practical difference here is small enough we can get away with it, though.
You can write movw r0, #0 to get the T3 encoding on both clang and gcc.
The explicit movw should be fine/I'll go that route! Since it sounds like this aligns with the spec (besides the thing you mentioned), I'm happy with this staying as-is.
That said, I think the current behavior for mov r0, #256 is a bug: it should produce the same error for the same reasons.
Hm. I'll close this ticket for now and maybe open one for that if I don't see one.
Thanks again for your help with this!