Skip to content

Commit 461274b

Browse files
wzssyqatstellar
authored andcommitted
MIPS: Fix asm constraints "f" and "r" for softfloat (#79116)
This include 2 fixes: 1. Disallow 'f' for softfloat. 2. Allow 'r' for softfloat. Currently, 'f' is accpeted by clang, then LLVM meets an internal error. 'r' is rejected by LLVM by: couldn't allocate input reg for constraint 'r'. Fixes: #64241, #63632 --------- Co-authored-by: Fangrui Song <[email protected]> (cherry picked from commit c88beb4)
1 parent e2182a6 commit 461274b

File tree

5 files changed

+78
-4
lines changed

5 files changed

+78
-4
lines changed

clang/lib/Basic/Targets/Mips.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,14 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
237237
case 'r': // CPU registers.
238238
case 'd': // Equivalent to "r" unless generating MIPS16 code.
239239
case 'y': // Equivalent to "r", backward compatibility only.
240-
case 'f': // floating-point registers.
241240
case 'c': // $25 for indirect jumps
242241
case 'l': // lo register
243242
case 'x': // hilo register pair
244243
Info.setAllowsRegister();
245244
return true;
245+
case 'f': // floating-point registers.
246+
Info.setAllowsRegister();
247+
return FloatABI != SoftFloat;
246248
case 'I': // Signed 16-bit constant
247249
case 'J': // Integer 0
248250
case 'K': // Unsigned 16-bit constant
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_cc1 -emit-llvm -triple mips -target-feature +soft-float %s -o - | FileCheck %s --check-prefix=SOFT_FLOAT
2+
3+
// SOFT_FLOAT: call void asm sideeffect "", "r,~{$1}"(float %1)
4+
void read_float(float *p) {
5+
__asm__("" ::"r"(*p));
6+
}
7+
8+
// SOFT_FLOAT: call void asm sideeffect "", "r,~{$1}"(double %1)
9+
void read_double(double *p) {
10+
__asm__("" :: "r"(*p));
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %clang_cc1 -triple mips64 -fsyntax-only -verify %s
2+
// RUN: %clang_cc1 -triple mips64 -target-feature +soft-float -fsyntax-only -verify=softfloat %s
3+
4+
// expected-no-diagnostics
5+
6+
void test_f(float p) {
7+
float result = p;
8+
__asm__("" :: "f"(result)); // softfloat-error{{invalid input constraint 'f' in asm}}
9+
}

llvm/lib/Target/Mips/MipsISelLowering.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -4128,14 +4128,18 @@ MipsTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
41284128
case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
41294129
case 'y': // Same as 'r'. Exists for compatibility.
41304130
case 'r':
4131-
if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8 || VT == MVT::i1) {
4131+
if ((VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8 ||
4132+
VT == MVT::i1) ||
4133+
(VT == MVT::f32 && Subtarget.useSoftFloat())) {
41324134
if (Subtarget.inMips16Mode())
41334135
return std::make_pair(0U, &Mips::CPU16RegsRegClass);
41344136
return std::make_pair(0U, &Mips::GPR32RegClass);
41354137
}
4136-
if (VT == MVT::i64 && !Subtarget.isGP64bit())
4138+
if ((VT == MVT::i64 || (VT == MVT::f64 && Subtarget.useSoftFloat())) &&
4139+
!Subtarget.isGP64bit())
41374140
return std::make_pair(0U, &Mips::GPR32RegClass);
4138-
if (VT == MVT::i64 && Subtarget.isGP64bit())
4141+
if ((VT == MVT::i64 || (VT == MVT::f64 && Subtarget.useSoftFloat())) &&
4142+
Subtarget.isGP64bit())
41394143
return std::make_pair(0U, &Mips::GPR64RegClass);
41404144
// This will generate an error message
41414145
return std::make_pair(0U, nullptr);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
2+
; RUN: llc -march=mips < %s | FileCheck %s --check-prefix=MIPS32
3+
; RUN: llc -march=mips64 < %s | FileCheck %s --check-prefix=MIPS64
4+
5+
define dso_local void @read_double(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
6+
; MIPS32-LABEL: read_double:
7+
; MIPS32: # %bb.0:
8+
; MIPS32-NEXT: lw $2, 4($4)
9+
; MIPS32-NEXT: lw $3, 0($4)
10+
; MIPS32-NEXT: #APP
11+
; MIPS32-NEXT: #NO_APP
12+
; MIPS32-NEXT: jr $ra
13+
; MIPS32-NEXT: nop
14+
;
15+
; MIPS64-LABEL: read_double:
16+
; MIPS64: # %bb.0:
17+
; MIPS64-NEXT: ld $2, 0($4)
18+
; MIPS64-NEXT: #APP
19+
; MIPS64-NEXT: #NO_APP
20+
; MIPS64-NEXT: jr $ra
21+
; MIPS64-NEXT: nop
22+
%2 = load double, ptr %0, align 8
23+
tail call void asm sideeffect "", "r,~{$1}"(double %2)
24+
ret void
25+
}
26+
27+
define dso_local void @read_float(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
28+
; MIPS32-LABEL: read_float:
29+
; MIPS32: # %bb.0:
30+
; MIPS32-NEXT: lw $2, 0($4)
31+
; MIPS32-NEXT: #APP
32+
; MIPS32-NEXT: #NO_APP
33+
; MIPS32-NEXT: jr $ra
34+
; MIPS32-NEXT: nop
35+
;
36+
; MIPS64-LABEL: read_float:
37+
; MIPS64: # %bb.0:
38+
; MIPS64-NEXT: lw $2, 0($4)
39+
; MIPS64-NEXT: #APP
40+
; MIPS64-NEXT: #NO_APP
41+
; MIPS64-NEXT: jr $ra
42+
; MIPS64-NEXT: nop
43+
%2 = load float, ptr %0, align 8
44+
tail call void asm sideeffect "", "r,~{$1}"(float %2)
45+
ret void
46+
}
47+
48+
attributes #0 = { "target-features"="+soft-float" "use-soft-float"="true" }

0 commit comments

Comments
 (0)