Skip to content

Commit 8fb2e2a

Browse files
authored
[CIR] Implement UO real on result from real with type promotion (#160951)
Implement UO real on the result from real with type promotion Issue: #141365
1 parent d0a260b commit 8fb2e2a

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,9 +1278,7 @@ mlir::Value ScalarExprEmitter::emitPromoted(const Expr *e,
12781278
"ScalarExprEmitter::emitPromoted unary imag");
12791279
return {};
12801280
case UO_Real:
1281-
cgf.cgm.errorNYI(e->getSourceRange(),
1282-
"ScalarExprEmitter::emitPromoted unary real");
1283-
return {};
1281+
return VisitRealImag(uo, promotionType);
12841282
case UO_Minus:
12851283
return emitUnaryPlusOrMinus(uo, cir::UnaryOpKind::Minus, promotionType);
12861284
case UO_Plus:
@@ -2132,6 +2130,9 @@ mlir::Value ScalarExprEmitter::VisitRealImag(const UnaryOperator *e,
21322130
// this won't work for, e.g. an Obj-C property
21332131
mlir::Value complex = cgf.emitComplexExpr(op);
21342132
if (e->isGLValue() && !promotionTy.isNull()) {
2133+
promotionTy = promotionTy->isAnyComplexType()
2134+
? promotionTy
2135+
: cgf.getContext().getComplexType(promotionTy);
21352136
complex = cgf.emitPromotedValue(complex, promotionTy);
21362137
}
21372138

clang/test/CIR/CodeGen/complex.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,3 +1233,40 @@ void imag_on_const_scalar() {
12331233
// OGCG: %[[A_ADDR:.*]] = alloca float, align 4
12341234
// OGCG: %[[B_ADDR:.*]] = alloca float, align 4
12351235
// OGCG: store float 0.000000e+00, ptr %[[B_ADDR]], align 4
1236+
1237+
void real_on_scalar_from_real_with_type_promotion() {
1238+
_Float16 _Complex a;
1239+
_Float16 b = __real__(__real__ a);
1240+
}
1241+
1242+
// CIR: %[[A_ADDR:.*]] = cir.alloca !cir.complex<!cir.f16>, !cir.ptr<!cir.complex<!cir.f16>>, ["a"]
1243+
// CIR: %[[B_ADDR:.*]] = cir.alloca !cir.f16, !cir.ptr<!cir.f16>, ["b", init]
1244+
// CIR: %[[TMP_A:.*]] = cir.load{{.*}} %[[A_ADDR]] : !cir.ptr<!cir.complex<!cir.f16>>, !cir.complex<!cir.f16>
1245+
// CIR: %[[A_REAL:.*]] = cir.complex.real %[[TMP_A]] : !cir.complex<!cir.f16> -> !cir.f16
1246+
// CIR: %[[A_IMAG:.*]] = cir.complex.imag %[[TMP_A]] : !cir.complex<!cir.f16> -> !cir.f16
1247+
// CIR: %[[A_REAL_F32:.*]] = cir.cast(floating, %[[A_REAL]] : !cir.f16), !cir.float
1248+
// CIR: %[[A_IMAG_F32:.*]] = cir.cast(floating, %[[A_IMAG]] : !cir.f16), !cir.float
1249+
// CIR: %[[A_COMPLEX_F32:.*]] = cir.complex.create %[[A_REAL_F32]], %[[A_IMAG_F32]] : !cir.float -> !cir.complex<!cir.float>
1250+
// CIR: %[[A_REAL_F32:.*]] = cir.complex.real %[[A_COMPLEX_F32]] : !cir.complex<!cir.float> -> !cir.float
1251+
// CIR: %[[A_REAL_F16:.*]] = cir.cast(floating, %[[A_REAL_F32]] : !cir.float), !cir.f16
1252+
// CIR: cir.store{{.*}} %[[A_REAL_F16]], %[[B_ADDR]] : !cir.f16, !cir.ptr<!cir.f16>
1253+
1254+
// LLVM: %[[A_ADDR:.*]] = alloca { half, half }, i64 1, align 2
1255+
// LLVM: %[[B_ADDR]] = alloca half, i64 1, align 2
1256+
// LLVM: %[[TMP_A:.*]] = load { half, half }, ptr %[[A_ADDR]], align 2
1257+
// LLVM: %[[A_REAL:.*]] = extractvalue { half, half } %[[TMP_A]], 0
1258+
// LLVM: %[[A_IMAG:.*]] = extractvalue { half, half } %[[TMP_A]], 1
1259+
// LLVM: %[[A_REAL_F32:.*]] = fpext half %[[A_REAL]] to float
1260+
// LLVM: %[[A_IMAG_F32:.*]] = fpext half %[[A_IMAG]] to float
1261+
// LLVM: %[[TMP_A_COMPLEX_F32:.*]] = insertvalue { float, float } {{.*}}, float %[[A_REAL_F32]], 0
1262+
// LLVM: %[[A_COMPLEX_F32:.*]] = insertvalue { float, float } %[[TMP_A_COMPLEX_F32]], float %[[A_IMAG_F32]], 1
1263+
// LLVM: %[[A_REAL_F16:.*]] = fptrunc float %[[A_REAL_F32]] to half
1264+
// LLVM: store half %[[A_REAL_F16]], ptr %[[B_ADDR]], align 2
1265+
1266+
// OGCG: %[[A_ADDR:.*]] = alloca { half, half }, align 2
1267+
// OGCG: %[[B_ADDR:.*]] = alloca half, align 2
1268+
// OGCG: %[[A_REAL_PTR:.*]] = getelementptr inbounds nuw { half, half }, ptr %[[A_ADDR]], i32 0, i32 0
1269+
// OGCG: %[[A_REAL:.*]] = load half, ptr %[[A_REAL_PTR]], align 2
1270+
// OGCG: %[[A_REAL_F32:.*]] = fpext half %[[A_REAL]] to float
1271+
// OGCG: %[[A_REAL_F16:.*]] = fptrunc float %[[A_REAL_F32]] to half
1272+
// OGCG: store half %[[A_REAL_F16]], ptr %[[B_ADDR]], align 2

0 commit comments

Comments
 (0)