Skip to content

Commit cf10cf9

Browse files
committed
[LoongArch] Support llvm.lround intrinsics with i32 return type.
This is needed by flang, similar to RISCV-64 in D147195.
1 parent 4006b28 commit cf10cf9

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ LoongArchTargetLowering::LoongArchTargetLowering(const TargetMachine &TM,
143143
setOperationAction(ISD::BITREVERSE, MVT::i32, Custom);
144144
setOperationAction(ISD::BSWAP, MVT::i32, Custom);
145145
setOperationAction({ISD::UDIV, ISD::UREM}, MVT::i32, Custom);
146+
setOperationAction(ISD::LROUND, MVT::i32, Custom);
146147
}
147148

148149
// Set operations for LA32 only.
@@ -3103,6 +3104,18 @@ void LoongArchTargetLowering::ReplaceNodeResults(
31033104
replaceINTRINSIC_WO_CHAINResults(N, Results, DAG, Subtarget);
31043105
break;
31053106
}
3107+
case ISD::LROUND: {
3108+
SDValue Op0 = N->getOperand(0);
3109+
RTLIB::Libcall LC =
3110+
Op0.getValueType() == MVT::f64 ? RTLIB::LROUND_F64 : RTLIB::LROUND_F32;
3111+
MakeLibCallOptions CallOptions;
3112+
EVT OpVT = Op0.getValueType();
3113+
CallOptions.setTypeListBeforeSoften(OpVT, MVT::i64, true);
3114+
SDValue Result = makeLibCall(DAG, LC, MVT::i64, Op0, CallOptions, DL).first;
3115+
Result = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Result);
3116+
Results.push_back(Result);
3117+
break;
3118+
}
31063119
}
31073120
}
31083121

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc --mtriple=loongarch64 --mattr=-f,-d %s -o - \
3+
; RUN: | FileCheck -check-prefix=LA64S %s
4+
5+
declare i32 @llvm.lround.i32.f64(double)
6+
7+
;; We support lround with i32 as return type on LoongArch64. This is needed by flang.
8+
define i32 @lround_i32_f64(double %a) nounwind {
9+
; LA64S-LABEL: lround_i32_f64:
10+
; LA64S: # %bb.0:
11+
; LA64S-NEXT: addi.d $sp, $sp, -16
12+
; LA64S-NEXT: st.d $ra, $sp, 8 # 8-byte Folded Spill
13+
; LA64S-NEXT: bl %plt(lround)
14+
; LA64S-NEXT: ld.d $ra, $sp, 8 # 8-byte Folded Reload
15+
; LA64S-NEXT: addi.d $sp, $sp, 16
16+
; LA64S-NEXT: ret
17+
18+
%1 = call i32 @llvm.lround.i32.f64(double %a)
19+
ret i32 %1
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc --mtriple=loongarch64 --mattr=-f,-d %s -o - \
3+
; RUN: | FileCheck -check-prefix=LA64S %s
4+
5+
declare i32 @llvm.lround.i32.f32(float)
6+
7+
; We support lround with i32 as return type on LoongArch64. This is needed by flang.
8+
define i32 @lround_i32_f32(float %a) nounwind {
9+
; LA64S-LABEL: lround_i32_f32:
10+
; LA64S: # %bb.0:
11+
; LA64S-NEXT: addi.d $sp, $sp, -16
12+
; LA64S-NEXT: st.d $ra, $sp, 8 # 8-byte Folded Spill
13+
; LA64S-NEXT: bl %plt(lroundf)
14+
; LA64S-NEXT: ld.d $ra, $sp, 8 # 8-byte Folded Reload
15+
; LA64S-NEXT: addi.d $sp, $sp, 16
16+
; LA64S-NEXT: ret
17+
18+
%1 = call i32 @llvm.lround.i32.f32(float %a)
19+
ret i32 %1
20+
}

0 commit comments

Comments
 (0)