[llvm] be8a2b9 - [LoongArch] Replace assertion by error message while lowering RETURNADDR and FRAMEADDR

via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 30 18:43:52 PDT 2022


Author: gonglingqin
Date: 2022-10-31T09:19:00+08:00
New Revision: be8a2b98da582d4cfd96be3b2cf561679a72bbb0

URL: https://github.com/llvm/llvm-project/commit/be8a2b98da582d4cfd96be3b2cf561679a72bbb0
DIFF: https://github.com/llvm/llvm-project/commit/be8a2b98da582d4cfd96be3b2cf561679a72bbb0.diff

LOG: [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

Added: 
    

Modified: 
    llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
    llvm/test/CodeGen/LoongArch/frameaddr-returnaddr-error.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
index 1c58fa0d1a09..94f02861b584 100644
--- a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
@@ -239,11 +239,11 @@ SDValue LoongArchTargetLowering::lowerFRAMEADDR(SDValue Op,
   }
 
   // Currently only support lowering frame address for current frame.
-  unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
-  assert((Depth == 0) &&
-         "Frame address can only be determined for current frame.");
-  if (Depth != 0)
+  if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() != 0) {
+    DAG.getContext()->emitError(
+        "frame address can only be determined for the current frame");
     return SDValue();
+  }
 
   MachineFunction &MF = DAG.getMachineFunction();
   MF.getFrameInfo().setFrameAddressIsTaken(true);
@@ -259,11 +259,11 @@ SDValue LoongArchTargetLowering::lowerRETURNADDR(SDValue Op,
     return SDValue();
 
   // Currently only support lowering return address for current frame.
-  unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
-  assert((Depth == 0) &&
-         "Return address can only be determined for current frame.");
-  if (Depth != 0)
+  if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() != 0) {
+    DAG.getContext()->emitError(
+        "return address can only be determined for the current frame");
     return SDValue();
+  }
 
   MachineFunction &MF = DAG.getMachineFunction();
   MF.getFrameInfo().setReturnAddressIsTaken(true);

diff  --git a/llvm/test/CodeGen/LoongArch/frameaddr-returnaddr-error.ll b/llvm/test/CodeGen/LoongArch/frameaddr-returnaddr-error.ll
index 6adb2e5b7775..2ac837c1b4e5 100644
--- a/llvm/test/CodeGen/LoongArch/frameaddr-returnaddr-error.ll
+++ b/llvm/test/CodeGen/LoongArch/frameaddr-returnaddr-error.ll
@@ -16,3 +16,16 @@ define ptr @non_const_depth_returnaddress(i32 %x) nounwind {
   ret ptr %1
 }
 
+define ptr @non_zero_frameaddress() nounwind {
+; CHECK: frame address can only be determined for the current frame
+  %1 = call ptr @llvm.frameaddress(i32 1)
+  ret ptr %1
+}
+
+
+define ptr @non_zero_returnaddress() nounwind {
+; CHECK: return address can only be determined for the current frame
+  %1 = call ptr @llvm.returnaddress(i32 1)
+  ret ptr %1
+}
+


        


More information about the llvm-commits mailing list