Skip to content

Commit 87ac5f2

Browse files
authored
[CIR] Implement part of ConstantExpr support for ComplexType (#173102)
Implement part of the ConstantExpr support for the ComplexType
1 parent 37545b8 commit 87ac5f2

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "CIRGenBuilder.h"
2+
#include "CIRGenConstantEmitter.h"
23
#include "CIRGenFunction.h"
34

45
#include "clang/AST/StmtVisitor.h"
@@ -66,7 +67,12 @@ class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
6667

6768
mlir::Value VisitExpr(Expr *e);
6869
mlir::Value VisitConstantExpr(ConstantExpr *e) {
69-
cgf.cgm.errorNYI(e->getExprLoc(), "ComplexExprEmitter VisitConstantExpr");
70+
if (mlir::Attribute result = ConstantEmitter(cgf).tryEmitConstantExpr(e))
71+
return builder.getConstant(cgf.getLoc(e->getSourceRange()),
72+
mlir::cast<mlir::TypedAttr>(result));
73+
74+
cgf.cgm.errorNYI(e->getExprLoc(),
75+
"ComplexExprEmitter VisitConstantExpr non constantexpr");
7076
return {};
7177
}
7278

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
3+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll
4+
// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
5+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -emit-llvm %s -o %t.ll
6+
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
7+
8+
struct StructWithConstEval {
9+
consteval int _Complex consteval_ret_complex() { return {1, 2}; }
10+
};
11+
12+
void calling_consteval_methods() {
13+
StructWithConstEval a;
14+
int _Complex c = a.consteval_ret_complex();
15+
}
16+
17+
// CIR: %[[A_ADDR:.*]] = cir.alloca !rec_StructWithConstEval, !cir.ptr<!rec_StructWithConstEval>, ["a"]
18+
// CIR: %[[C_ADDR:.*]] = cir.alloca !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>, ["c", init]
19+
// CIR: %[[CONST_COMPLEX:.*]] = cir.const #cir.const_complex<#cir.int<1> : !s32i, #cir.int<2> : !s32i> : !cir.complex<!s32i>
20+
// CIR: cir.store {{.*}} %[[CONST_COMPLEX]], %[[C_ADDR]] : !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>
21+
22+
// LLVM: %[[A_ADDR:.*]] = alloca %struct.StructWithConstEval, i64 1, align 1
23+
// LLVM: %[[C_ADDR:.*]] = alloca { i32, i32 }, i64 1, align 4
24+
// LLVM: store { i32, i32 } { i32 1, i32 2 }, ptr %[[C_ADDR]], align 4
25+
26+
// OGCG: %[[A_ADDR:.*]] = alloca %struct.StructWithConstEval, align 1
27+
// OGCG: %[[C_ADDR:.*]] = alloca { i32, i32 }, align 4
28+
// OGCG: %[[C_REAL_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[C_ADDR]], i32 0, i32 0
29+
// OGCG: %[[C_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[C_ADDR]], i32 0, i32 1
30+
// OGCG: store i32 1, ptr %[[C_REAL_PTR]], align 4
31+
// OGCG: store i32 2, ptr %[[C_IMAG_PTR]], align 4

0 commit comments

Comments
 (0)