Skip to content

Commit

Permalink
Implement __builtin_return_address
Browse files Browse the repository at this point in the history
  • Loading branch information
ghehg committed Nov 1, 2024
1 parent 3d16a0f commit c5b0391
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
15 changes: 13 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1462,8 +1462,19 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
llvm_unreachable("BI__builtin_wmemcmp NYI");
case Builtin::BI__builtin_dwarf_cfa:
llvm_unreachable("BI__builtin_dwarf_cfa NYI");
case Builtin::BI__builtin_return_address:
llvm_unreachable("BI__builtin_return_address NYI");
case Builtin::BI__builtin_return_address: {
mlir::Location loc = getLoc(E->getExprLoc());
mlir::Attribute frameAttr = ConstantEmitter(*this).emitAbstract(
E->getArg(0), E->getArg(0)->getType());
int64_t frame = mlir::cast<mlir::cir::IntAttr>(frameAttr).getSInt();
// The intrinsic call is expecting unsigned int arg type, void* return type
return RValue::get(builder
.create<mlir::cir::IntrinsicCallOp>(
loc, builder.getStringAttr("llvm.returnaddress"),
VoidPtrTy,
builder.getUInt32(frame, loc).getRes())
.getResult());
}
case Builtin::BI_ReturnAddress:
llvm_unreachable("BI_ReturnAddress NYI");
case Builtin::BI__builtin_frame_address:
Expand Down
15 changes: 15 additions & 0 deletions clang/test/CIR/CodeGen/builtins-memory.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck %s --check-prefix=CIR --input-file=%t.cir
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
// RUN: FileCheck %s --check-prefix=LLVM --input-file=%t.ll

void *test_return_address(void) {
return __builtin_return_address(1);

// CIR-LABEL: test_return_address
// [[ARG:%.*]] = cir.const #cir.int<1> : !u32i
// {{%.*}} = cir.llvm.intrinsic "llvm.returnaddress" [[ARG]] : (!u32i) -> !cir.ptr<!void>

// LLVM-LABEL: @test_return_address
// LLVM: {{%.*}} = call ptr @llvm.returnaddress(i32 1)
}

0 comments on commit c5b0391

Please sign in to comment.