Skip to content

Commit f0eac35

Browse files
committed
[CIR] Backport CXXScalarValueInitExpr support for ComplexType
1 parent 80d52d7 commit f0eac35

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,13 @@ class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
174174
Scope.ForceCleanup({&V});
175175
return V;
176176
}
177+
177178
mlir::Value VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
178-
llvm_unreachable("NYI");
179+
mlir::Location loc = CGF.getLoc(E->getExprLoc());
180+
mlir::Type complexTy = CGF.convertType(E->getType());
181+
return Builder.getNullValue(complexTy, loc);
179182
}
183+
180184
mlir::Value VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
181185
llvm_unreachable("NYI");
182186
}

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,9 @@ mlir::Value CirAttrToValue::visitCirAttr(cir::GlobalViewAttr globalAttr) {
731731
}
732732
auto resTy = addrOp.getType();
733733
auto eltTy = converter->convertType(sourceType);
734-
addrOp = rewriter.create<mlir::LLVM::GEPOp>(loc, resTy, eltTy, addrOp,
735-
indices, mlir::LLVM::GEPNoWrapFlags::inbounds);
734+
addrOp = rewriter.create<mlir::LLVM::GEPOp>(
735+
loc, resTy, eltTy, addrOp, indices,
736+
mlir::LLVM::GEPNoWrapFlags::inbounds);
736737
}
737738

738739
if (auto intTy = mlir::dyn_cast<cir::IntType>(globalAttr.getType())) {
@@ -1205,8 +1206,9 @@ mlir::LogicalResult CIRToLLVMVTTAddrPointOpLowering::matchAndRewrite(
12051206
offsets.push_back(0);
12061207
offsets.push_back(adaptor.getOffset());
12071208
}
1208-
rewriter.replaceOpWithNewOp<mlir::LLVM::GEPOp>(op, resultType, eltType,
1209-
llvmAddr, offsets, mlir::LLVM::GEPNoWrapFlags::inbounds);
1209+
rewriter.replaceOpWithNewOp<mlir::LLVM::GEPOp>(
1210+
op, resultType, eltType, llvmAddr, offsets,
1211+
mlir::LLVM::GEPNoWrapFlags::inbounds);
12101212
return mlir::success();
12111213
}
12121214

@@ -1903,9 +1905,18 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
19031905
typeConverter->convertType(op.getType()),
19041906
mlir::cast<cir::FPAttr>(op.getValue()).getValue());
19051907
} else if (auto complexTy = mlir::dyn_cast<cir::ComplexType>(op.getType())) {
1908+
mlir::Type complexElemTy = complexTy.getElementType();
1909+
mlir::Type complexElemLLVMTy = typeConverter->convertType(complexElemTy);
1910+
1911+
if (auto zeroInitAttr = mlir::dyn_cast<cir::ZeroAttr>(op.getValue())) {
1912+
mlir::TypedAttr zeroAttr = rewriter.getZeroAttr(complexElemLLVMTy);
1913+
mlir::ArrayAttr array = rewriter.getArrayAttr({zeroAttr, zeroAttr});
1914+
rewriter.replaceOpWithNewOp<mlir::LLVM::ConstantOp>(
1915+
op, getTypeConverter()->convertType(op.getType()), array);
1916+
return mlir::success();
1917+
}
1918+
19061919
auto complexAttr = mlir::cast<cir::ComplexAttr>(op.getValue());
1907-
auto complexElemTy = complexTy.getElementType();
1908-
auto complexElemLLVMTy = typeConverter->convertType(complexElemTy);
19091920

19101921
mlir::Attribute components[2];
19111922
if (mlir::isa<cir::IntType>(complexElemTy)) {
@@ -3881,8 +3892,9 @@ mlir::LogicalResult CIRToLLVMVTableAddrPointOpLowering::matchAndRewrite(
38813892
op.getAddressPointAttr().getOffset()};
38823893

38833894
assert(eltType && "Shouldn't ever be missing an eltType here");
3884-
rewriter.replaceOpWithNewOp<mlir::LLVM::GEPOp>(op, targetType, eltType,
3885-
symAddr, offsets, mlir::LLVM::GEPNoWrapFlags::inbounds);
3895+
rewriter.replaceOpWithNewOp<mlir::LLVM::GEPOp>(
3896+
op, targetType, eltType, symAddr, offsets,
3897+
mlir::LLVM::GEPNoWrapFlags::inbounds);
38863898

38873899
return mlir::success();
38883900
}
@@ -3908,7 +3920,8 @@ mlir::LogicalResult CIRToLLVMVTableGetVirtualFnAddrOpLowering::matchAndRewrite(
39083920
llvm::SmallVector<mlir::LLVM::GEPArg> offsets =
39093921
llvm::SmallVector<mlir::LLVM::GEPArg>{op.getIndex()};
39103922
rewriter.replaceOpWithNewOp<mlir::LLVM::GEPOp>(
3911-
op, targetType, eltType, adaptor.getVptr(), offsets, mlir::LLVM::GEPNoWrapFlags::inbounds);
3923+
op, targetType, eltType, adaptor.getVptr(), offsets,
3924+
mlir::LLVM::GEPNoWrapFlags::inbounds);
39123925
return mlir::success();
39133926
}
39143927

@@ -4000,7 +4013,9 @@ mlir::LogicalResult CIRToLLVMInlineAsmOpLowering::matchAndRewrite(
40004013
op, llResTy, llvmOperands, op.getAsmStringAttr(), op.getConstraintsAttr(),
40014014
op.getSideEffectsAttr(),
40024015
/*is_align_stack*/ mlir::UnitAttr(),
4003-
/*tail_call_kind*/ mlir::LLVM::TailCallKindAttr::get(getContext(), mlir::LLVM::tailcallkind::TailCallKind::None),
4016+
/*tail_call_kind*/
4017+
mlir::LLVM::TailCallKindAttr::get(
4018+
getContext(), mlir::LLVM::tailcallkind::TailCallKind::None),
40044019
mlir::LLVM::AsmDialectAttr::get(getContext(), llDialect),
40054020
rewriter.getArrayAttr(opAttrs));
40064021

clang/test/CIR/CodeGen/complex.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,15 @@ void complex_functional_cast() {
1414

1515
// LLVM: %[[INIT:.*]] = alloca { i32, i32 }, i64 1, align 4
1616
// LLVM: store { i32, i32 } zeroinitializer, ptr %[[INIT]], align 4
17+
18+
void complex_cxx_scalar_value_init_expr() {
19+
using IntComplex = int _Complex;
20+
int _Complex a = IntComplex();
21+
}
22+
23+
// CIR: %[[INIT:.*]] = cir.alloca !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>, ["a", init]
24+
// CIR: %[[COMPLEX:.*]] = cir.const #cir.zero : !cir.complex<!s32i>
25+
// CIR: cir.store align(4) %[[COMPLEX]], %[[INIT]] : !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>
26+
27+
// LLVM: %[[INIT:.*]] = alloca { i32, i32 }, i64 1, align 4
28+
// LLVM: store { i32, i32 } zeroinitializer, ptr %[[INIT]], align 4

0 commit comments

Comments
 (0)