[AArch64] Miscompilation when stack tagging is enabled in AArch64
Here is the reduced test : https://godbolt.org/ There is a br instruction (line 16) at end of the entry block which is suppose to read the NZCV flag edited by previous cmp instruction (line 14) and branch accordingly. But the stg loop gets in the way and edits the nzcv flag which is not handled properly.
This results in mis compiled assembly
cmp w0, #10
LBB0_1:
st2g x9, [x9], #32
subs x8, x8, #32
b.ne .LBB0_1
b.ge .LBB0_4
Here b.ge jumps based on NZCV edited by subs instruction rather than from cmp instruction.
Run Command
llc -mtriple=aarch64 -mattr=+mte -aarch64-order-frame-objects=0 settag.ll
settag.ll
declare void @llvm.aarch64.settag(ptr %p, i64 %a)
; Function Attrs: nounwind
declare i32 @printf(ptr, ...) #0
@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
define i32 @stg_func(i32 %in) {
entry:
%a = alloca i8, i32 16, align 16
%b = alloca i8, i32 512, align 16
%c = alloca i8, i32 16, align 16
call void @llvm.aarch64.settag(ptr %a, i64 16)
call void @llvm.aarch64.settag(ptr %b, i64 512)
%cmp = icmp slt i32 %in, 10
call void @llvm.aarch64.settag(ptr %c, i64 16)
br i1 %cmp, label %return0, label %return1
return0: ; preds = %entry
%call = call i32 (ptr, ...) @printf(ptr @.str, i32 10) #1
ret i32 0
return1:
ret i32 1
}
This is observed after the patch : 5e612bc As this patch expands stg loops to b.ne instead of Bcc used earlier
@llvm/issue-subscribers-backend-aarch64
This results in mis compiled assembly
cmp w0, #<!-- -->10
LBB0_1:
st2g x9, [x9], #<!-- -->32
subs x8, x8, #<!-- -->32
b.ne .LBB0_1
b.ge .LBB0_4
Here b.ge jumps based on NZCV edited by subs instruction rather than from cmp instruction.
Run Command
llc -mtriple=aarch64 -mattr=+mte -aarch64-order-frame-objects=0 settag.ll
settag.ll
declare void @<!-- -->llvm.aarch64.settag(ptr %p, i64 %a)
; Function Attrs: nounwind
declare i32 @<!-- -->printf(ptr, ...) #<!-- -->0
@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
define i32 @<!-- -->stg_func(i32 %in) {
entry:
%a = alloca i8, i32 16, align 16
%b = alloca i8, i32 512, align 16
%c = alloca i8, i32 16, align 16
call void @<!-- -->llvm.aarch64.settag(ptr %a, i64 16)
call void @<!-- -->llvm.aarch64.settag(ptr %b, i64 512)
%cmp = icmp slt i32 %in, 10
call void @<!-- -->llvm.aarch64.settag(ptr %c, i64 16)
br i1 %cmp, label %return0, label %return1
return0: ; preds = %entry
%call = call i32 (ptr, ...) @<!-- -->printf(ptr @.str, i32 10) #<!-- -->1
ret i32 0
return1:
ret i32 1
}
This is observed after the patch : 5e612bc As this patch expands stg loops to b.ne instead of Bcc used earlier
@kbeyls for the mentioned commit (though it's not clear yet if that's the commit that causes it).
I think this issue is a duplicate of #64309, and was already fixed in commit b09c575975b691e988a0f2e31d632c5f1038ab1d
From our internal testing, there were actually two issues: one, the scheduling issue fixed by b09c575975b691e988a0f2e31d632c5f1038ab1d, and two, an issue where the tryMergeAdjacentSTG transform clobbers the flags register.
@kartcq has a patch for tryMergeAdjacentSTG; we'll try to get it posted soon.
Created a Pull Request to fix the issue : https://github.com/llvm/llvm-project/pull/68873 @DavidSpickett @kbeyls @efriedma-quic @eugenis
can we close this issue now that the patch is in main branch?
Yes we can, as the patch is merged.
Patch resolving the issue : https://github.com/llvm/llvm-project/commit/6726c99f8893c4a7bce00059416cb462237c64a0