Skip to content

Commit

Permalink
[CIR][CIRGen] Support for struct call arguments
Browse files Browse the repository at this point in the history
Essentially emits an LValue for the struct and then passes it as a call
argument.
  • Loading branch information
sitio-couto authored and lanza committed Aug 8, 2023
1 parent c9aa263 commit 45f2128
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
7 changes: 5 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,12 +654,15 @@ void CIRGenFunction::buildCallArg(CallArgList &args, const Expr *E,
// we make it to the call.
if (type->isRecordType() &&
type->castAs<RecordType>()->getDecl()->isParamDestroyedInCallee()) {
llvm_unreachable("NYI");
llvm_unreachable("Microsoft C++ ABI is NYI");
}

if (HasAggregateEvalKind && isa<ImplicitCastExpr>(E) &&
cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) {
assert(0 && "NYI");
LValue L = buildLValue(cast<CastExpr>(E)->getSubExpr());
assert(L.isSimple());
args.addUncopiedAggregate(L, type);
return;
}

args.add(buildAnyExprToTemp(E), type);
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ struct CallArg {
: RV(rv), HasLV(false), IsUsed(false), Ty(ty) {
(void)IsUsed;
}
CallArg(LValue lv, clang::QualType ty)
: LV(lv), HasLV(true), IsUsed(false), Ty(ty) {}

/// \returns an independent RValue. If the CallArg contains an LValue,
/// a temporary copy is returned.
Expand Down Expand Up @@ -242,6 +244,10 @@ class CallArgList : public llvm::SmallVector<CallArg, 8> {
push_back(CallArg(rvalue, type));
}

void addUncopiedAggregate(LValue LV, clang::QualType type) {
push_back(CallArg(LV, type));
}

/// Add all the arguments from another CallArgList to this one. After doing
/// this, the old CallArgList retains its list of arguments, but must not
/// be used to emit a call.
Expand Down
7 changes: 7 additions & 0 deletions clang/test/CIR/CodeGen/struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,10 @@ struct S3 {
int a;
} s3[3] = {{1}, {2}, {3}};
// CHECK-DAG: cir.global external @s3 = #cir.const_array<[#cir.const_struct<{#cir.int<1> : !s32i}> : !ty_22struct2ES322, #cir.const_struct<{#cir.int<2> : !s32i}> : !ty_22struct2ES322, #cir.const_struct<{#cir.int<3> : !s32i}> : !ty_22struct2ES322]> : !cir.array<!ty_22struct2ES322 x 3>

void shouldCopyStructAsCallArg(struct S1 s) {
// CHECK-DAG: cir.func @shouldCopyStructAsCallArg
shouldCopyStructAsCallArg(s);
// CHECK-DAG: %[[#LV:]] = cir.load %{{.+}} : cir.ptr <!ty_22struct2ES122>, !ty_22struct2ES122
// CHECK-DAG: cir.call @shouldCopyStructAsCallArg(%[[#LV]]) : (!ty_22struct2ES122) -> ()
}

0 comments on commit 45f2128

Please sign in to comment.