Skip to content

[CIR] Upstream CompoundLiteralExpr for Scalar #148943

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

Merged
Merged
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
61 changes: 61 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,67 @@ LValue CIRGenFunction::emitMemberExpr(const MemberExpr *e) {
llvm_unreachable("Unhandled member declaration!");
}

/// Evaluate an expression into a given memory location.
void CIRGenFunction::emitAnyExprToMem(const Expr *e, Address location,
Qualifiers quals, bool isInit) {
// FIXME: This function should take an LValue as an argument.
switch (getEvaluationKind(e->getType())) {
case cir::TEK_Complex: {
LValue lv = makeAddrLValue(location, e->getType());
emitComplexExprIntoLValue(e, lv, isInit);
return;
}

case cir::TEK_Aggregate: {
emitAggExpr(e, AggValueSlot::forAddr(location, quals,
AggValueSlot::IsDestructed_t(isInit),
AggValueSlot::IsAliased_t(!isInit),
AggValueSlot::MayOverlap));
return;
}

case cir::TEK_Scalar: {
RValue rv = RValue::get(emitScalarExpr(e));
LValue lv = makeAddrLValue(location, e->getType());
emitStoreThroughLValue(rv, lv);
return;
}
}

llvm_unreachable("bad evaluation kind");
}

LValue CIRGenFunction::emitCompoundLiteralLValue(const CompoundLiteralExpr *e) {
if (e->isFileScope()) {
cgm.errorNYI(e->getSourceRange(), "emitCompoundLiteralLValue: FileScope");
return {};
}

if (e->getType()->isVariablyModifiedType()) {
cgm.errorNYI(e->getSourceRange(),
"emitCompoundLiteralLValue: VariablyModifiedType");
return {};
}

Address declPtr = createMemTemp(e->getType(), getLoc(e->getSourceRange()),
".compoundliteral");
const Expr *initExpr = e->getInitializer();
LValue result = makeAddrLValue(declPtr, e->getType(), AlignmentSource::Decl);

emitAnyExprToMem(initExpr, declPtr, e->getType().getQualifiers(),
/*Init*/ true);

// Block-scope compound literals are destroyed at the end of the enclosing
// scope in C.
if (!getLangOpts().CPlusPlus && e->getType().isDestructedType()) {
cgm.errorNYI(e->getSourceRange(),
"emitCompoundLiteralLValue: non C++ DestructedType");
return {};
}

return result;
}

LValue CIRGenFunction::emitCallExprLValue(const CallExpr *e) {
RValue rv = emitCallExpr(e);

Expand Down
14 changes: 14 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
mlir::Value VisitGenericSelectionExpr(GenericSelectionExpr *e);
mlir::Value VisitImplicitCastExpr(ImplicitCastExpr *e);
mlir::Value VisitInitListExpr(const InitListExpr *e);

mlir::Value VisitCompoundLiteralExpr(CompoundLiteralExpr *e) {
return emitLoadOfLValue(e);
}

mlir::Value VisitImaginaryLiteral(const ImaginaryLiteral *il);
mlir::Value VisitParenExpr(ParenExpr *e);
mlir::Value
Expand Down Expand Up @@ -467,6 +472,15 @@ mlir::Value CIRGenFunction::emitComplexPrePostIncDec(const UnaryOperator *e,
return isPre ? incVal : inVal;
}

void CIRGenFunction::emitComplexExprIntoLValue(const Expr *e, LValue dest,
bool isInit) {
assert(e && getComplexType(e->getType()) &&
"Invalid complex expression to emit");
ComplexExprEmitter emitter(*this);
mlir::Value value = emitter.Visit(const_cast<Expr *>(e));
emitter.emitStoreOfComplex(getLoc(e->getExprLoc()), value, dest, isInit);
}

mlir::Value CIRGenFunction::emitLoadOfComplex(LValue src, SourceLocation loc) {
return ComplexExprEmitter(*this).emitLoadOfLValue(src, loc);
}
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {

mlir::Value VisitMemberExpr(MemberExpr *e);

mlir::Value VisitCompoundLiteralExpr(CompoundLiteralExpr *e) {
return emitLoadOfLValue(e);
}

mlir::Value VisitInitListExpr(InitListExpr *e);

mlir::Value VisitExplicitCastExpr(ExplicitCastExpr *e) {
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,8 @@ LValue CIRGenFunction::emitLValue(const Expr *e) {
return emitStringLiteralLValue(cast<StringLiteral>(e));
case Expr::MemberExprClass:
return emitMemberExpr(cast<MemberExpr>(e));
case Expr::CompoundLiteralExprClass:
return emitCompoundLiteralLValue(cast<CompoundLiteralExpr>(e));
case Expr::BinaryOperatorClass:
return emitBinaryOperatorLValue(cast<BinaryOperator>(e));
case Expr::CompoundAssignOperatorClass: {
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,11 @@ class CIRGenFunction : public CIRGenTypeCache {
RValue emitAnyExpr(const clang::Expr *e,
AggValueSlot aggSlot = AggValueSlot::ignored());

/// Emits the code necessary to evaluate an arbitrary expression into the
/// given memory location.
void emitAnyExprToMem(const Expr *e, Address location, Qualifiers quals,
bool isInitializer);

/// Similarly to emitAnyExpr(), however, the result will always be accessible
/// even if no aggregate location is provided.
RValue emitAnyExprToTemp(const clang::Expr *e);
Expand Down Expand Up @@ -828,6 +833,7 @@ class CIRGenFunction : public CIRGenTypeCache {
mlir::Value emitCheckedArgForAssume(const Expr *e);

LValue emitCompoundAssignmentLValue(const clang::CompoundAssignOperator *e);
LValue emitCompoundLiteralLValue(const CompoundLiteralExpr *e);

void emitConstructorBody(FunctionArgList &args);
void emitDestructorBody(FunctionArgList &args);
Expand Down Expand Up @@ -930,6 +936,8 @@ class CIRGenFunction : public CIRGenTypeCache {
/// returning the result.
mlir::Value emitComplexExpr(const Expr *e);

void emitComplexExprIntoLValue(const Expr *e, LValue dest, bool isInit);

mlir::Value emitComplexPrePostIncDec(const UnaryOperator *e, LValue lv,
bool isInc, bool isPre);

Expand Down
99 changes: 99 additions & 0 deletions clang/test/CIR/CodeGen/compound_literal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll
// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -emit-llvm %s -o %t.ll
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG

int foo() {
int e = (int){1};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add test cases for complex and aggregate?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Complex type requires to handle it too in CIRGenExprComplex, i will do that in this PR and update tests

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used Vector for aggregate test because visitCXXParenListOrInitListExpr is NYI for structure type

return e;
}

// CIR: %[[RET:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"]
// CIR: %[[INIT:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["e", init]
// CIR: %[[COMPOUND:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, [".compoundliteral", init]
// CIR: %[[VALUE:.*]] = cir.const #cir.int<1> : !s32i
// CIR: cir.store{{.*}} %[[VALUE]], %[[COMPOUND]] : !s32i, !cir.ptr<!s32i>
// CIR: %[[TMP:.*]] = cir.load{{.*}} %[[COMPOUND]] : !cir.ptr<!s32i>, !s32i
// CIR: cir.store{{.*}} %[[TMP]], %[[INIT]] : !s32i, !cir.ptr<!s32i>
// CIR: %[[TMP_2:.*]] = cir.load{{.*}} %[[INIT]] : !cir.ptr<!s32i>, !s32i
// CIR: cir.store %[[TMP_2]], %[[RET]] : !s32i, !cir.ptr<!s32i>
// CIR: %[[TMP_3:.*]] = cir.load %[[RET]] : !cir.ptr<!s32i>, !s32i
// CIR: cir.return %[[TMP_3]] : !s32i

// LLVM: %[[RET:.*]] = alloca i32, i64 1, align 4
// LLVM: %[[INIT:.*]] = alloca i32, i64 1, align 4
// LLVM: %[[COMPOUND:.*]] = alloca i32, i64 1, align 4
// LLVM: store i32 1, ptr %[[COMPOUND]], align 4
// LLVM: %[[TMP:.*]] = load i32, ptr %[[COMPOUND]], align 4
// LLVM: store i32 %[[TMP]], ptr %[[INIT]], align 4
// LLVM: %[[TMP_2:.*]] = load i32, ptr %[[INIT]], align 4
// LLVM: store i32 %[[TMP_2]], ptr %[[RET]], align 4
// LLVM: %[[TMP_3:.*]] = load i32, ptr %[[RET]], align 4
// LLVM: ret i32 %[[TMP_3]]

// OGCG: %[[INIT:.*]] = alloca i32, align 4
// OGCG: %[[COMPOUND:.*]] = alloca i32, align 4
// OGCG: store i32 1, ptr %[[COMPOUND]], align 4
// OGCG: %[[TMP:.*]] = load i32, ptr %[[COMPOUND]], align 4
// OGCG: store i32 %[[TMP]], ptr %[[INIT]], align 4
// OGCG: %[[TMP_2:.*]] = load i32, ptr %[[INIT]], align 4
// OGCG: ret i32 %[[TMP_2]]

void foo2() {
int _Complex a = (int _Complex) { 1, 2};
}

// CIR: %[[A_ADDR:.*]] = cir.alloca !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>, ["a", init]
// CIR: %[[CL_ADDR:.*]] = cir.alloca !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>, [".compoundliteral"]
// CIR: %[[COMPLEX:.*]] = cir.const #cir.const_complex<#cir.int<1> : !s32i, #cir.int<2> : !s32i> : !cir.complex<!s32i>
// CIR: cir.store{{.*}} %[[COMPLEX]], %[[CL_ADDR]] : !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>
// CIR: %[[TMP:.*]] = cir.load{{.*}} %[[CL_ADDR]] : !cir.ptr<!cir.complex<!s32i>>, !cir.complex<!s32i>
// CIR: cir.store{{.*}} %[[TMP]], %[[A_ADDR]] : !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>

// LLVM: %[[A_ADDR:.*]] = alloca { i32, i32 }, i64 1, align 4
// LLVM: %[[CL_ADDR:.*]] = alloca { i32, i32 }, i64 1, align 4
// LLVM: store { i32, i32 } { i32 1, i32 2 }, ptr %[[CL_ADDR]], align 4
// LLVM: %[[TMP:.*]] = load { i32, i32 }, ptr %[[CL_ADDR]], align 4
// LLVM: store { i32, i32 } %[[TMP]], ptr %[[A_ADDR]], align 4

// OGCG: %[[A_ADDR:.*]] = alloca { i32, i32 }, align 4
// OGCG: %[[CL_ADDR:.*]] = alloca { i32, i32 }, align 4
// OGCG: %[[CL_REAL_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[CL_ADDR]], i32 0, i32 0
// OGCG: %[[CL_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[CL_ADDR]], i32 0, i32 1
// OGCG: store i32 1, ptr %[[CL_REAL_PTR]], align 4
// OGCG: store i32 2, ptr %[[CL_IMAG_PTR]], align 4
// OGCG: %[[CL_REAL_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[CL_ADDR]], i32 0, i32 0
// OGCG: %[[CL_REAL:.*]] = load i32, ptr %[[CL_REAL_PTR]], align 4
// OGCG: %[[CL_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[CL_ADDR]], i32 0, i32 1
// OGCG: %[[CL_IMAG:.*]] = load i32, ptr %[[CL_IMAG_PTR]], align 4
// OGCG: %[[A_REAL_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[A_ADDR]], i32 0, i32 0
// OGCG: %[[A_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[A_ADDR]], i32 0, i32 1
// OGCG: store i32 %[[CL_REAL]], ptr %[[A_REAL_PTR]], align 4
// OGCG: store i32 %[[CL_IMAG]], ptr %[[A_IMAG_PTR]], align 4

void foo3() {
typedef int vi4 __attribute__((vector_size(16)));
auto a = (vi4){10, 20, 30, 40};
}

// CIR: %[[A_ADDR:.*]] = cir.alloca !cir.vector<4 x !s32i>, !cir.ptr<!cir.vector<4 x !s32i>>, ["a", init]
// CIR: %[[CL_ADDR:.*]] = cir.alloca !cir.vector<4 x !s32i>, !cir.ptr<!cir.vector<4 x !s32i>>, [".compoundliteral", init]
// CIR: %[[VEC:.*]] = cir.const #cir.const_vector<[#cir.int<10> : !s32i, #cir.int<20> : !s32i, #cir.int<30> : !s32i, #cir.int<40> : !s32i]> : !cir.vector<4 x !s32i>
// CIR: cir.store{{.*}} %[[VEC]], %[[CL_ADDR]] : !cir.vector<4 x !s32i>, !cir.ptr<!cir.vector<4 x !s32i>>
// CIR: %[[TMP:.*]] = cir.load{{.*}} %[[CL_ADDR]] : !cir.ptr<!cir.vector<4 x !s32i>>, !cir.vector<4 x !s32i>
// CIR: cir.store{{.*}} %[[TMP]], %[[A_ADDR]] : !cir.vector<4 x !s32i>, !cir.ptr<!cir.vector<4 x !s32i>>

// LLVM: %[[A_ADDR:.*]] = alloca <4 x i32>, i64 1, align 16
// LLVM: %[[CL_ADDR:.*]] = alloca <4 x i32>, i64 1, align 16
// LLVM: store <4 x i32> <i32 10, i32 20, i32 30, i32 40>, ptr %[[CL_ADDR]], align 16
// LLVM: %[[TMP:.*]] = load <4 x i32>, ptr %[[CL_ADDR]], align 16
// LLVM: store <4 x i32> %[[TMP]], ptr %[[A_ADDR]], align 16

// OGCG: %[[A_ADDR:.*]] = alloca <4 x i32>, align 16
// OGCG: %[[CL_ADDR:.*]] = alloca <4 x i32>, align 16
// OGCG: store <4 x i32> <i32 10, i32 20, i32 30, i32 40>, ptr %[[CL_ADDR]], align 16
// OGCG: %[[TMP:.*]] = load <4 x i32>, ptr %[[CL_ADDR]], align 16
// OGCG: store <4 x i32> %[[TMP]], ptr %[[A_ADDR]], align 16