Skip to content
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
14 changes: 8 additions & 6 deletions clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,18 +404,20 @@ ComplexExprEmitter::VisitImaginaryLiteral(const ImaginaryLiteral *il) {
auto imagValue = cast<IntegerLiteral>(il->getSubExpr())->getValue();
realValueAttr = cir::IntAttr::get(elementTy, 0);
imagValueAttr = cir::IntAttr::get(elementTy, imagValue);
} else if (mlir::isa<cir::FPTypeInterface>(elementTy)) {
} else {
assert(mlir::isa<cir::FPTypeInterface>(elementTy) &&
"Expected complex element type to be floating-point");

auto imagValue = cast<FloatingLiteral>(il->getSubExpr())->getValue();
realValueAttr = cir::FPAttr::get(
elementTy, llvm::APFloat::getZero(imagValue.getSemantics()));
imagValueAttr = cir::FPAttr::get(elementTy, imagValue);
} else {
llvm_unreachable("unexpected complex element type");
}

auto realValue = builder.getConstant(loc, realValueAttr);
auto imagValue = builder.getConstant(loc, imagValueAttr);
return builder.createComplexCreate(loc, realValue, imagValue);
auto complexTy = cir::ComplexType::get(realValueAttr.getType());
auto complexAttr =
cir::ComplexAttr::get(complexTy, realValueAttr, imagValueAttr);
return cir::ConstantOp::create(builder, loc, complexAttr);
}

mlir::Value ComplexExprEmitter::VisitCallExpr(const CallExpr *e) {
Expand Down
12 changes: 4 additions & 8 deletions clang/test/CIR/CodeGen/complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,10 @@ void imag_literal() {
ci = 3i;
}

// CHECK-BEFORE: cir.func
// CHECK-BEFORE: %[[#REAL:]] = cir.const #cir.fp<0.000000e+00> : !cir.double
// CHECK-BEFORE-NEXT: %[[#IMAG:]] = cir.const #cir.fp<3.000000e+00> : !cir.double
// CHECK-BEFORE-NEXT: %{{.+}} = cir.complex.create %[[#REAL]], %[[#IMAG]] : !cir.double -> !cir.complex<!cir.double>
// CHECK-BEFORE: %[[#REAL:]] = cir.const #cir.int<0> : !s32i
// CHECK-BEFORE-NEXT: %[[#IMAG:]] = cir.const #cir.int<3> : !s32i
// CHECK-BEFORE-NEXT: %{{.+}} = cir.complex.create %[[#REAL]], %[[#IMAG]] : !s32i -> !cir.complex<!s32i>
// CHECK-BEFORE: }
// CHECK-BEFORE: cir.func
// CHECK-BEFORE: %{{.+}} = cir.const #cir.complex<#cir.fp<0.000000e+00> : !cir.double, #cir.fp<3.000000e+00> : !cir.double> : !cir.complex<!cir.double>
// CHECK-BEFORE: %{{.+}} = cir.const #cir.complex<#cir.int<0> : !s32i, #cir.int<3> : !s32i> : !cir.complex<!s32i>
// CHECK-BEFORE: }

// CHECK-AFTER: cir.func
// CHECK-AFTER: %{{.+}} = cir.const #cir.complex<#cir.fp<0.000000e+00> : !cir.double, #cir.fp<3.000000e+00> : !cir.double> : !cir.complex<!cir.double>
Expand Down
Loading