Skip to content

Commit 04e3256

Browse files
ghehgxlauko
authored andcommitted
[CIR][CIRGen][Builtin] Support __builtin_return_address (llvm#1046)
test case is from [Traditional Clang CodeGen test file](https://github.com/llvm/clangir/blob/723e78afb5ae4fbd000269a057410913ade3ef44/clang/test/CodeGen/2004-02-13-BuiltinFrameReturnAddress.c#L5)
1 parent 474f460 commit 04e3256

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

+31
Original file line numberDiff line numberDiff line change
@@ -4125,6 +4125,37 @@ def MemChrOp : CIR_Op<"libc.memchr"> {
41254125
let hasVerifier = 0;
41264126
}
41274127

4128+
//===----------------------------------------------------------------------===//
4129+
// ReturnAddrOp
4130+
//===----------------------------------------------------------------------===//
4131+
4132+
def ReturnAddrOp : CIR_Op<"return_address"> {
4133+
let arguments = (ins UInt32:$level);
4134+
let summary = "The return address of the current function, or of one of its callers";
4135+
let results = (outs Res<VoidPtr, "">:$result);
4136+
4137+
let description = [{
4138+
Represents call to builtin function ` __builtin_return_address` in CIR.
4139+
This builtin function returns the return address of the current function,
4140+
or of one of its callers.
4141+
The `level` argument is number of frames to scan up the call stack.
4142+
For instance, value of 0 yields the return address of the current function,
4143+
value of 1 yields the return address of the caller of the current function,
4144+
and so forth.
4145+
4146+
Examples:
4147+
4148+
```mlir
4149+
%p = return_address(%level) -> !cir.ptr<!void>
4150+
```
4151+
}];
4152+
4153+
let assemblyFormat = [{
4154+
`(` $level `)` attr-dict
4155+
}];
4156+
let hasVerifier = 0;
4157+
}
4158+
41284159
//===----------------------------------------------------------------------===//
41294160
// StdFindOp
41304161
//===----------------------------------------------------------------------===//

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -3403,8 +3403,14 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
34033403
llvm_unreachable("BI__builtin_wmemcmp NYI");
34043404
case Builtin::BI__builtin_dwarf_cfa:
34053405
llvm_unreachable("BI__builtin_dwarf_cfa NYI");
3406-
case Builtin::BI__builtin_return_address:
3407-
llvm_unreachable("BI__builtin_return_address NYI");
3406+
case Builtin::BI__builtin_return_address: {
3407+
mlir::Location loc = getLoc(E->getExprLoc());
3408+
mlir::Attribute levelAttr = ConstantEmitter(*this).emitAbstract(
3409+
E->getArg(0), E->getArg(0)->getType());
3410+
int64_t level = mlir::cast<mlir::cir::IntAttr>(levelAttr).getSInt();
3411+
return RValue::get(builder.create<mlir::cir::ReturnAddrOp>(
3412+
loc, builder.getUInt32(level, loc)));
3413+
}
34083414
case Builtin::BI_ReturnAddress:
34093415
llvm_unreachable("BI_ReturnAddress NYI");
34103416
case Builtin::BI__builtin_frame_address:

0 commit comments

Comments
 (0)