Skip to content

Commit 7e92f20

Browse files
authored
[CIR] Backport generate ConstComplexAttr for ImaginaryLiteral (#1975)
Backport generate ConstComplexAttr for ImaginaryLiteral, not ComplexCreateOp
1 parent 610ec53 commit 7e92f20

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -404,18 +404,20 @@ ComplexExprEmitter::VisitImaginaryLiteral(const ImaginaryLiteral *il) {
404404
auto imagValue = cast<IntegerLiteral>(il->getSubExpr())->getValue();
405405
realValueAttr = cir::IntAttr::get(elementTy, 0);
406406
imagValueAttr = cir::IntAttr::get(elementTy, imagValue);
407-
} else if (mlir::isa<cir::FPTypeInterface>(elementTy)) {
407+
} else {
408+
assert(mlir::isa<cir::FPTypeInterface>(elementTy) &&
409+
"Expected complex element type to be floating-point");
410+
408411
auto imagValue = cast<FloatingLiteral>(il->getSubExpr())->getValue();
409412
realValueAttr = cir::FPAttr::get(
410413
elementTy, llvm::APFloat::getZero(imagValue.getSemantics()));
411414
imagValueAttr = cir::FPAttr::get(elementTy, imagValue);
412-
} else {
413-
llvm_unreachable("unexpected complex element type");
414415
}
415416

416-
auto realValue = builder.getConstant(loc, realValueAttr);
417-
auto imagValue = builder.getConstant(loc, imagValueAttr);
418-
return builder.createComplexCreate(loc, realValue, imagValue);
417+
auto complexTy = cir::ComplexType::get(realValueAttr.getType());
418+
auto complexAttr =
419+
cir::ComplexAttr::get(complexTy, realValueAttr, imagValueAttr);
420+
return cir::ConstantOp::create(builder, loc, complexAttr);
419421
}
420422

421423
mlir::Value ComplexExprEmitter::VisitCallExpr(const CallExpr *e) {

clang/test/CIR/CodeGen/complex.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,10 @@ void imag_literal() {
8181
ci = 3i;
8282
}
8383

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

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

0 commit comments

Comments
 (0)