Skip to content

Commit

Permalink
Support __builtin_frame_address
Browse files Browse the repository at this point in the history
  • Loading branch information
ghehg committed Nov 18, 2024
1 parent c890ce8 commit 74ac0c8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
11 changes: 7 additions & 4 deletions clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1580,18 +1580,21 @@ RValue CIRGenFunction::emitBuiltinExpr(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: {
case Builtin::BI__builtin_return_address:
case Builtin::BI__builtin_frame_address: {
mlir::Location loc = getLoc(E->getExprLoc());
mlir::Attribute levelAttr = ConstantEmitter(*this).emitAbstract(
E->getArg(0), E->getArg(0)->getType());
int64_t level = mlir::cast<cir::IntAttr>(levelAttr).getSInt();
return RValue::get(builder.create<cir::FuncAddrBuiltinOp>(
loc, cir::FuncAddrKind::return_address, builder.getUInt32(level, loc)));
loc,
BuiltinID == Builtin::BI__builtin_return_address
? cir::FuncAddrKind::return_address
: cir::FuncAddrKind::frame_address,
builder.getUInt32(level, loc)));
}
case Builtin::BI_ReturnAddress:
llvm_unreachable("BI_ReturnAddress NYI");
case Builtin::BI__builtin_frame_address:
llvm_unreachable("BI__builtin_frame_address NYI");
case Builtin::BI__builtin_extract_return_addr:
llvm_unreachable("BI__builtin_extract_return_addr NYI");
case Builtin::BI__builtin_frob_return_addr:
Expand Down
11 changes: 11 additions & 0 deletions clang/test/CIR/CodeGen/builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,14 @@ extern "C" void *test_return_address(void) {
// LLVM-LABEL: @test_return_address
// LLVM: {{%.*}} = call ptr @llvm.returnaddress(i32 1)
}

extern "C" void *test_frame_address(void) {
return __builtin_frame_address(1);

// CIR-LABEL: test_frame_address
// CIR: [[ARG:%.*]] = cir.const #cir.int<1> : !u32i
// CIR: {{%.*}} = cir.func_addr_builtin(frame_address, [[ARG]])

// LLVM-LABEL: @test_frame_address
// LLVM: {{%.*}} = call ptr @llvm.frameaddress.p0(i32 1)
}
2 changes: 2 additions & 0 deletions clang/test/CIR/IR/builtins.cir
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ module {
cir.func @test1() {
%0 = cir.const #cir.int<1> : !u32i
%1 = cir.func_addr_builtin(return_address, %0)
%2 = cir.func_addr_builtin(frame_address, %0)
cir.return
}
// CHECK: cir.func @test1()
// CHECK: %0 = cir.const #cir.int<1> : !u32i
// CHECK: %1 = cir.func_addr_builtin(return_address, %0)
// CHECK: %2 = cir.func_addr_builtin(frame_address, %0)
// CHECK: cir.return
}

0 comments on commit 74ac0c8

Please sign in to comment.