Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIR][CIRGen][Builtin] Support __builtin_return_address #1046

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -4125,6 +4125,37 @@ def MemChrOp : CIR_Op<"libc.memchr"> {
let hasVerifier = 0;
}

//===----------------------------------------------------------------------===//
// ReturnAddrOp
//===----------------------------------------------------------------------===//

def ReturnAddrOp : CIR_Op<"return_address"> {
let arguments = (ins UInt32:$level);
let summary = "The return address of the current function, or of one of its callers";
let results = (outs Res<VoidPtr, "">:$result);

let description = [{
Represents call to builtin function ` __builtin_return_address` in CIR.
This builtin function returns the return address of the current function,
or of one of its callers.
The `level` argument is number of frames to scan up the call stack.
For instance, value of 0 yields the return address of the current function,
value of 1 yields the return address of the caller of the current function,
and so forth.

Examples:

```mlir
%p = return_address(%level) -> !cir.ptr<!void>
```
}];

let assemblyFormat = [{
`(` $level `)` attr-dict
}];
let hasVerifier = 0;
}

//===----------------------------------------------------------------------===//
// StdFindOp
//===----------------------------------------------------------------------===//
Expand Down
10 changes: 8 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1510,8 +1510,14 @@ 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());
bcardosolopes marked this conversation as resolved.
Show resolved Hide resolved
mlir::Attribute levelAttr = ConstantEmitter(*this).emitAbstract(
E->getArg(0), E->getArg(0)->getType());
int64_t level = mlir::cast<mlir::cir::IntAttr>(levelAttr).getSInt();
return RValue::get(builder.create<mlir::cir::ReturnAddrOp>(
loc, builder.getUInt32(level, loc)));
}
case Builtin::BI_ReturnAddress:
llvm_unreachable("BI_ReturnAddress NYI");
case Builtin::BI__builtin_frame_address:
Expand Down
26 changes: 24 additions & 2 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4115,6 +4115,21 @@ class CIRCmpThreeWayOpLowering
}
};

class CIRReturnAddrOpLowering
: public mlir::OpConversionPattern<mlir::cir::ReturnAddrOp> {
public:
using OpConversionPattern<mlir::cir::ReturnAddrOp>::OpConversionPattern;

mlir::LogicalResult
matchAndRewrite(mlir::cir::ReturnAddrOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const override {
auto llvmPtrTy = mlir::LLVM::LLVMPointerType::get(rewriter.getContext());
replaceOpWithCallLLVMIntrinsicOp(rewriter, op, "llvm.returnaddress",
llvmPtrTy, adaptor.getOperands());
return mlir::success();
}
};

class CIRClearCacheOpLowering
: public mlir::OpConversionPattern<mlir::cir::ClearCacheOp> {
public:
Expand Down Expand Up @@ -4370,9 +4385,16 @@ void populateCIRToLLVMConversionPatterns(
CIRVectorShuffleVecLowering, CIRStackSaveLowering, CIRUnreachableLowering,
CIRTrapLowering, CIRInlineAsmOpLowering, CIRSetBitfieldLowering,
CIRGetBitfieldLowering, CIRPrefetchLowering, CIRObjSizeOpLowering,
CIRIsConstantOpLowering, CIRCmpThreeWayOpLowering, CIRMemCpyOpLowering,
CIRFAbsOpLowering, CIRExpectOpLowering, CIRVTableAddrPointOpLowering,
CIRVectorCreateLowering, CIRVectorCmpOpLowering, CIRVectorSplatLowering,
CIRVectorTernaryLowering, CIRVectorShuffleIntsLowering,
CIRVectorShuffleVecLowering, CIRStackSaveLowering, CIRUnreachableLowering,
CIRTrapLowering, CIRInlineAsmOpLowering, CIRSetBitfieldLowering,
CIRGetBitfieldLowering, CIRPrefetchLowering, CIRObjSizeOpLowering,
CIRIsConstantOpLowering, CIRCmpThreeWayOpLowering,
CIRClearCacheOpLowering, CIREhTypeIdOpLowering, CIRCatchParamOpLowering,
CIRResumeOpLowering, CIRAllocExceptionOpLowering,
CIRReturnAddrOpLowering, CIRClearCacheOpLowering, CIREhTypeIdOpLowering,
CIRCatchParamOpLowering, CIRResumeOpLowering, CIRAllocExceptionOpLowering,
CIRFreeExceptionOpLowering, CIRThrowOpLowering, CIRIntrinsicCallLowering,
CIRAssumeLowering, CIRAssumeAlignedLowering, CIRAssumeSepStorageLowering,
CIRBaseClassAddrOpLowering, CIRDerivedClassAddrOpLowering,
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 @@ -75,3 +75,14 @@ extern "C" char* test_memchr(const char arg[32]) {
// LLVM: [[RET:%.*]] = load ptr, ptr [[RET_P]], align 8
// LLVM: ret ptr [[RET]]
}

extern "C" void *test_return_address(void) {
return __builtin_return_address(1);

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

// LLVM-LABEL: @test_return_address
// LLVM: {{%.*}} = call ptr @llvm.returnaddress(i32 1)
}
14 changes: 14 additions & 0 deletions clang/test/CIR/IR/builtins.cir
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: cir-opt %s | cir-opt | FileCheck %s
!u32i = !cir.int<u, 32>

module {
cir.func @test1() {
%0 = cir.const #cir.int<1> : !u32i
%1 = cir.return_address(%0)
cir.return
}
// CHECK: cir.func @test1()
// CHECK: %0 = cir.const #cir.int<1> : !u32i
// CHECK: %1 = cir.return_address(%0)
// CHECK: cir.return
}