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] Fixes function calls with return values and cleanup stage #1214

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
23 changes: 23 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,17 @@ static cir::CIRCallOpInterface emitCallLikeOp(
extraFnAttrs);
}

static RValue getRValueThroughMemory(mlir::Location loc,
CIRGenBuilderTy &builder, mlir::Value val,
Address addr) {
auto ip = builder.saveInsertionPoint();
builder.setInsertionPointAfterValue(val);
builder.createStore(loc, val, addr);
builder.restoreInsertionPoint(ip);
auto load = builder.createLoad(loc, addr);
return RValue::get(load);
}

RValue CIRGenFunction::emitCall(const CIRGenFunctionInfo &CallInfo,
const CIRGenCallee &Callee,
ReturnValueSlot ReturnValue,
Expand Down Expand Up @@ -889,6 +900,18 @@ RValue CIRGenFunction::emitCall(const CIRGenFunctionInfo &CallInfo,
auto Results = theCall->getOpResults();
assert(Results.size() <= 1 && "multiple returns NYI");
assert(Results[0].getType() == RetCIRTy && "Bitcast support NYI");

auto region = builder.getBlock()->getParent();
bcardosolopes marked this conversation as resolved.
Show resolved Hide resolved
if (region != theCall->getParentRegion()) {
Address DestPtr = ReturnValue.getValue();

if (!DestPtr.isValid())
DestPtr = CreateMemTemp(RetTy, callLoc, "tmp");
bcardosolopes marked this conversation as resolved.
Show resolved Hide resolved

return getRValueThroughMemory(callLoc, builder, Results[0],
DestPtr);
}
bcardosolopes marked this conversation as resolved.
Show resolved Hide resolved

return RValue::get(Results[0]);
}
default:
Expand Down
31 changes: 31 additions & 0 deletions clang/test/CIR/CodeGen/try-catch-dtors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,34 @@ void yo2(bool x) {
// CIR: } catch [type #cir.all {
// CIR: cir.catch_param -> !cir.ptr<!void>
// CIR: }]


int foo() { return 42; }

struct A {
~A() {}
};

void bar() {
A a;
int b = foo();
}

// CIR-LABEL: @_Z3barv
// CIR: %[[V0:.*]] = cir.alloca !ty_A, !cir.ptr<!ty_A>, ["a"] {alignment = 1 : i64}
// CIR: %[[V1:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["b", init] {alignment = 4 : i64}
// CIR: %[[V2:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["tmp"] {alignment = 4 : i64}
// CIR: cir.try synthetic cleanup {
// CIR: %[[V4:.*]] = cir.call exception @_Z3foov() : () -> !s32i cleanup {
// CIR: cir.call @_ZN1AD2Ev(%[[V0]]) : (!cir.ptr<!ty_A>) -> () extra(#fn_attr)
// CIR: cir.yield
// CIR: }
// CIR: cir.store %[[V4]], %[[V2]] : !s32i, !cir.ptr<!s32i>
// CIR: cir.yield
// CIR: } catch [#cir.unwind {
// CIR: cir.resume
// CIR: }]
// CIR: %[[V3:.*]] = cir.load %[[V2]] : !cir.ptr<!s32i>, !s32i
// CIR: cir.store %[[V3]], %[[V1]] : !s32i, !cir.ptr<!s32i>
// CIR: cir.call @_ZN1AD2Ev(%[[V0]]) : (!cir.ptr<!ty_A>) -> () extra(#fn_attr)
// CIR: cir.return
Loading