Skip to content

[Clang][LoongArch] Add inline asm support for the q constraint #141037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clang/lib/Basic/Targets/LoongArch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ bool LoongArchTargetInfo::validateAsmConstraint(
// A signed 16-bit constant.
Info.setRequiresImmediate(-32768, 32767);
return true;
case 'q':
// A general-purpose register except for $r0 and $r1 (for the csrxchg
// instruction)
Info.setAllowsRegister();
return true;
case 'I':
// A signed 12-bit constant (for arithmetic instructions).
Info.setRequiresImmediate(-2048, 2047);
Expand Down
6 changes: 6 additions & 0 deletions clang/test/CodeGen/LoongArch/inline-asm-constraints.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ void test_m(int *p) {
asm volatile("" :: "m"(*(p+4)));
}

void test_q(void) {
// CHECK-LABEL: define{{.*}} void @test_q()
// CHECK: call void asm sideeffect "", "q"(i32 0)
asm volatile ("" :: "q"(0));
}

void test_I(void) {
// CHECK-LABEL: define{{.*}} void @test_I()
// CHECK: call void asm sideeffect "", "I"(i32 2047)
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7276,6 +7276,8 @@ LoongArchTargetLowering::getConstraintType(StringRef Constraint) const {
// 'm': A memory operand whose address is formed by a base register and
// offset that is suitable for use in instructions with the same
// addressing mode as st.w and ld.w.
// 'q': A general-purpose register except for $r0 and $r1 (for the csrxchg
// instruction)
// 'I': A signed 12-bit constant (for arithmetic instructions).
// 'J': Integer zero.
// 'K': An unsigned 12-bit constant (for logic instructions).
Expand All @@ -7289,6 +7291,7 @@ LoongArchTargetLowering::getConstraintType(StringRef Constraint) const {
default:
break;
case 'f':
case 'q':
return C_RegisterClass;
case 'l':
case 'I':
Expand Down Expand Up @@ -7328,6 +7331,8 @@ LoongArchTargetLowering::getRegForInlineAsmConstraint(
if (VT.isVector())
break;
return std::make_pair(0U, &LoongArch::GPRRegClass);
case 'q':
return std::make_pair(0U, &LoongArch::GPRNoR0R1RegClass);
case 'f':
if (Subtarget.hasBasicF() && VT == MVT::f32)
return std::make_pair(0U, &LoongArch::FPR32RegClass);
Expand Down
21 changes: 21 additions & 0 deletions llvm/test/CodeGen/LoongArch/inline-asm-constraint-q.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
; RUN: llc --mtriple=loongarch32 --mattr=+f --verify-machineinstrs < %s | FileCheck %s
; RUN: llc --mtriple=loongarch64 --mattr=+f --verify-machineinstrs < %s | FileCheck %s

;; Check that the "q" operand is not R0.
define i32 @constraint_q_not_r0() {
; CHECK-NOT: csrxchg ${{[a-z]*}}, $r0, 0
; CHECK-NOT: csrxchg ${{[a-z]*}}, $zero, 0
entry:
%2 = tail call i32 asm "csrxchg $0, $1, 0", "=r,q,0"(i32 0, i32 0)
ret i32 %2
}

;; Check that the "q" operand is not R1.
define i32 @constraint_q_not_r1(i32 %0) {
; CHECK-NOT: csrxchg ${{[a-z]*}}, $r1, 0
; CHECK-NOT: csrxchg ${{[a-z]*}}, $ra, 0
entry:
%2 = tail call i32 asm "", "={$r1},{$r1}"(i32 0)
%3 = tail call i32 asm "csrxchg $0, $1, 0", "=r,q,0"(i32 %2, i32 %0)
ret i32 %3
}