Skip to content

Commit 8c565de

Browse files
authored
[LoongArch] Support llvm.lround intrinsics with i32 return type. (llvm#114733)
This is needed by flang, similar to RISCV-64 in https://reviews.llvm.org/D147195.
1 parent ed9dab6 commit 8c565de

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp

+13
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.
@@ -3107,6 +3108,18 @@ void LoongArchTargetLowering::ReplaceNodeResults(
31073108
replaceINTRINSIC_WO_CHAINResults(N, Results, DAG, Subtarget);
31083109
break;
31093110
}
3111+
case ISD::LROUND: {
3112+
SDValue Op0 = N->getOperand(0);
3113+
EVT OpVT = Op0.getValueType();
3114+
RTLIB::Libcall LC =
3115+
OpVT == MVT::f64 ? RTLIB::LROUND_F64 : RTLIB::LROUND_F32;
3116+
MakeLibCallOptions CallOptions;
3117+
CallOptions.setTypeListBeforeSoften(OpVT, MVT::i64, true);
3118+
SDValue Result = makeLibCall(DAG, LC, MVT::i64, Op0, CallOptions, DL).first;
3119+
Result = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Result);
3120+
Results.push_back(Result);
3121+
break;
3122+
}
31103123
}
31113124
}
31123125

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

0 commit comments

Comments
 (0)