[clang] [Clang] Add GCC's __builtin_stack_address() to Clang. (PR #121332)

via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 31 05:38:07 PST 2024


================
@@ -4782,6 +4782,30 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
     Function *F = CGM.getIntrinsic(Intrinsic::frameaddress, AllocaInt8PtrTy);
     return RValue::get(Builder.CreateCall(F, Depth));
   }
+  case Builtin::BI__builtin_stack_address: {
+    IntegerType *SPRegType;
+    StringRef SPRegName;
+    switch (getTarget().getTriple().getArch()) {
+    case Triple::x86:
+      SPRegType = Int32Ty;
+      SPRegName = "esp";
+      break;
+    case Triple::x86_64:
+      SPRegType = Int64Ty;
+      SPRegName = "rsp";
+      break;
+    default:
+      llvm_unreachable("Intrinsic __builtin_stack_address is not supported for "
+                       "the target architecture");
+    }
----------------
aalhwc wrote:

Maybe  `llvm_unreachable` is inappropriate in this case.

The purpose of that `llvm::unreachable` statement is to indicate that  `__builtin_stack_address` is either: (1) supported by the target architecture but not yet implemented in LLVM (I plan to add support for more architectures in future patches) or (2) not supported by the target architecture (e.g. there's no stack pointer register). The `llvm_unreachable` will fire when either (1) or (2) is true.

Would it be more appropriate to `llvm::report_fatal_error` or simply an `assert` in this case?

https://github.com/llvm/llvm-project/pull/121332


More information about the cfe-commits mailing list