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

via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 3 01:37:18 PST 2025


================
@@ -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:

I updated the implementation to produce a compile error when the target architecture is unsupported.

I reused the existing function [`EmitSpecialRegisterBuiltin`](https://github.com/llvm/llvm-project/blob/2fae5bdea7c2016d4086aa7ecf3c5d0592ce95c8/clang/lib/CodeGen/CGBuiltin.cpp#L8929) to generate the `@llvm.read_register` intrinsic. That required moving it earlier in the source file.

I can make this a target-specific builtin if this implementation is not appropriate.

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


More information about the cfe-commits mailing list