Skip to content

Commit be8a2b9

Browse files
committed
[LoongArch] Replace assertion by error message while lowering RETURNADDR and FRAMEADDR
If `__builtin_frame_address` or `__builtin_return_address` is invoked with non-zero argument, show an error message instead of a crash. Reference: https://reviews.llvm.org/rG83b88441ad951fe99c30402930ef3cd661f2fd2b Differential Revision: https://reviews.llvm.org/D136917
1 parent cd0174a commit be8a2b9

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,11 @@ SDValue LoongArchTargetLowering::lowerFRAMEADDR(SDValue Op,
239239
}
240240

241241
// Currently only support lowering frame address for current frame.
242-
unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
243-
assert((Depth == 0) &&
244-
"Frame address can only be determined for current frame.");
245-
if (Depth != 0)
242+
if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() != 0) {
243+
DAG.getContext()->emitError(
244+
"frame address can only be determined for the current frame");
246245
return SDValue();
246+
}
247247

248248
MachineFunction &MF = DAG.getMachineFunction();
249249
MF.getFrameInfo().setFrameAddressIsTaken(true);
@@ -259,11 +259,11 @@ SDValue LoongArchTargetLowering::lowerRETURNADDR(SDValue Op,
259259
return SDValue();
260260

261261
// Currently only support lowering return address for current frame.
262-
unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
263-
assert((Depth == 0) &&
264-
"Return address can only be determined for current frame.");
265-
if (Depth != 0)
262+
if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() != 0) {
263+
DAG.getContext()->emitError(
264+
"return address can only be determined for the current frame");
266265
return SDValue();
266+
}
267267

268268
MachineFunction &MF = DAG.getMachineFunction();
269269
MF.getFrameInfo().setReturnAddressIsTaken(true);

llvm/test/CodeGen/LoongArch/frameaddr-returnaddr-error.ll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,16 @@ define ptr @non_const_depth_returnaddress(i32 %x) nounwind {
1616
ret ptr %1
1717
}
1818

19+
define ptr @non_zero_frameaddress() nounwind {
20+
; CHECK: frame address can only be determined for the current frame
21+
%1 = call ptr @llvm.frameaddress(i32 1)
22+
ret ptr %1
23+
}
24+
25+
26+
define ptr @non_zero_returnaddress() nounwind {
27+
; CHECK: return address can only be determined for the current frame
28+
%1 = call ptr @llvm.returnaddress(i32 1)
29+
ret ptr %1
30+
}
31+

0 commit comments

Comments
 (0)