llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[RISCV] +zcmp triggers assertion error with inline assembly clobber declarations

Open Kuree opened this issue 8 months ago • 6 comments

See godbolt with llc (assertions trunk): https://godbolt.org/z/ra37G4MeT

source_filename = "test"
target datalayout = "e-m:e-p:32:32-i64:64-n32-S128"
target triple = "riscv32-rv32imac-unknown"

define dso_local void @test() local_unnamed_addr #23 align 64 {
entry:
  %0 = tail call i32 asm sideeffect "csrr $0, 0x42", "=r"() #25 
  %1 = tail call i32 asm sideeffect "csrr $0, 0x43", "=r"() #25
  tail call void asm sideeffect "", "~{x17},~{x10},~{x11},~{x12},~{x13}"() #25
  %cmp = icmp eq i32 %0, 11
  br i1 %cmp, label %if.else, label %if.else

if.else:                                          ; preds = %entry
  %call = tail call i32 @foo(i32 noundef %0, i32 noundef %1)
  br label %if.end

if.end:                                           ; preds = %if.else, %if.then
  ret void
}

declare i32 @foo(i32 noundef %arg0, i32 noundef %arg1);

attributes #23 = { nounwind "stack-protector-buffer-size"="8" "target-cpu"="generic-rv32" "target-features"="+32bit,+m,+zcmp" }
attributes #25 = { nounwind }

Kuree avatar Mar 13 '26 22:03 Kuree

@llvm/issue-subscribers-backend-risc-v

Author: Keyi Zhang (Kuree)

See godbolt with llc (assertions trunk): https://godbolt.org/z/ra37G4MeT
source_filename = "test"
target datalayout = "e-m:e-p:32:32-i64:64-n32-S128"
target triple = "riscv32-rv32imac-unknown"

define dso_local void @<!-- -->test() local_unnamed_addr #<!-- -->23 align 64 {
entry:
  %0 = tail call i32 asm sideeffect "csrr $0, 0x42", "=r"() #<!-- -->25 
  %1 = tail call i32 asm sideeffect "csrr $0, 0x43", "=r"() #<!-- -->25
  tail call void asm sideeffect "", "~{x17},~{x10},~{x11},~{x12},~{x13}"() #<!-- -->25
  %cmp = icmp eq i32 %0, 11
  br i1 %cmp, label %if.else, label %if.else

if.else:                                          ; preds = %entry
  %call = tail call i32 @<!-- -->foo(i32 noundef %0, i32 noundef %1)
  br label %if.end

if.end:                                           ; preds = %if.else, %if.then
  ret void
}

declare i32 @<!-- -->foo(i32 noundef %arg0, i32 noundef %arg1);

attributes #<!-- -->23 = { nounwind "stack-protector-buffer-size"="8" "target-cpu"="generic-rv32" "target-features"="+32bit,+m,+zcmp" }
attributes #<!-- -->25 = { nounwind }

llvmbot avatar Mar 13 '26 22:03 llvmbot

Thanks. Do you know which versions this affects? Does it affect 22.x?

lenary avatar Mar 13 '26 22:03 lenary

Hey lenary, is it alright if I try solving this? I believe it's because the mergeGPRPairInsns function is being called despite the target not supporting zdinx or P.

KavinTheG avatar Mar 13 '26 23:03 KavinTheG

@lenary I don't have a released version with assertion on, but I believe it works with commit 45a0f9cc1b1547430c9329b95a0090d5722e907e (with assertions on).

Kuree avatar Mar 13 '26 23:03 Kuree

Hey lenary, is it alright if I try solving this? I believe it's because the mergeGPRPairInsns function is being called despite the target not supporting zdinx or P.

Sure, if you reduce and add the provided testcase. "Reduce" in this case means:

  • Remove source_filename, target triple, target datalayout
  • Remove dso_local, local_unnamed_address, #23 and align 64 from @test, but add nounwind into the declaration.
  • Remove the attributes lines at the bottom
  • Add a RUN line for rv32, with -mattr=+m,+zcmp (this bit is important to get the right target features back).

You can add other run lines, if you think it's justified based on the fix, e.g. xqccmp (which should have the same crash), and maybe add equivalents for when a target has both zcmp and P (zcmp and dinx is less likely)

lenary avatar Mar 13 '26 23:03 lenary

Appreciate the guidance! Will have a fix asap.

KavinTheG avatar Mar 14 '26 00:03 KavinTheG