[clang] [clang][Interp] Support zero init for complex types (PR #79728)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Sat Jan 27 23:04:40 PST 2024
Timm =?utf-8?q?Bäder?= <tbaeder at redhat.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/79728 at github.com>
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/79728
>From 0b6925d01645fbe3af9a1ac51ee6e396cbe67a03 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sun, 28 Jan 2024 07:21:06 +0100
Subject: [PATCH 1/2] [clang][Interp] Support zero init for complex types
Initialize both elements to 0.
---
clang/lib/AST/Interp/ByteCodeExprGen.cpp | 24 +++++++++++++++++++++++-
clang/test/AST/Interp/complex.cpp | 13 +++++++++++++
2 files changed, 36 insertions(+), 1 deletion(-)
diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index d4501cefb2131da..b549e35f208bdfd 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1830,7 +1830,29 @@ bool ByteCodeExprGen<Emitter>::VisitCXXScalarValueInitExpr(
if (Ty->isVoidType())
return true;
- return this->visitZeroInitializer(classifyPrim(Ty), Ty, E);
+ if (std::optional<PrimType> T = classify(Ty))
+ return this->visitZeroInitializer(*T, Ty, E);
+
+ assert(Ty->isAnyComplexType());
+ if (!Initializing) {
+ std::optional<unsigned> LocalIndex = allocateLocal(E, /*IsExtended=*/false);
+ if (!LocalIndex)
+ return false;
+ if (!this->emitGetPtrLocal(*LocalIndex, E))
+ return false;
+ }
+
+ // Initialize both fields to 0.
+ QualType ElemQT = Ty->getAs<ComplexType>()->getElementType();
+ PrimType ElemT = classifyPrim(ElemQT);
+
+ for (unsigned I = 0; I != 2; ++I) {
+ if (!this->visitZeroInitializer(ElemT, ElemQT, E))
+ return false;
+ if (!this->emitInitElem(ElemT, I, E))
+ return false;
+ }
+ return true;
}
template <class Emitter>
diff --git a/clang/test/AST/Interp/complex.cpp b/clang/test/AST/Interp/complex.cpp
index 836ea552adac86d..4e730a117de4bd8 100644
--- a/clang/test/AST/Interp/complex.cpp
+++ b/clang/test/AST/Interp/complex.cpp
@@ -146,3 +146,16 @@ namespace Sub {
}
}
+
+namespace ZeroInit {
+ typedef _Complex float fcomplex;
+ typedef _Complex unsigned icomplex;
+
+ constexpr fcomplex test7 = fcomplex();
+ static_assert(__real(test7) == 0.0f, "");
+ static_assert(__imag(test7) == 0.0f, "");
+
+ constexpr icomplex test8 = icomplex();
+ static_assert(__real(test8) == 0, "");
+ static_assert(__imag(test8) == 0, "");
+}
>From 604bbb985f6a7e2bfc9bc518c92e835068f8a9e2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sun, 28 Jan 2024 08:04:18 +0100
Subject: [PATCH 2/2] Fix ignoring CXXScalarValueInitExprs
---
clang/lib/AST/Interp/ByteCodeExprGen.cpp | 2 +-
clang/test/AST/Interp/complex.cpp | 2 ++
2 files changed, 3 insertions(+), 1 deletion(-)
diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index b549e35f208bdfd..4c7e60e702c8923 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1827,7 +1827,7 @@ bool ByteCodeExprGen<Emitter>::VisitCXXScalarValueInitExpr(
const CXXScalarValueInitExpr *E) {
QualType Ty = E->getType();
- if (Ty->isVoidType())
+ if (DiscardResult || Ty->isVoidType())
return true;
if (std::optional<PrimType> T = classify(Ty))
diff --git a/clang/test/AST/Interp/complex.cpp b/clang/test/AST/Interp/complex.cpp
index 4e730a117de4bd8..a0e5c7446aa8c16 100644
--- a/clang/test/AST/Interp/complex.cpp
+++ b/clang/test/AST/Interp/complex.cpp
@@ -158,4 +158,6 @@ namespace ZeroInit {
constexpr icomplex test8 = icomplex();
static_assert(__real(test8) == 0, "");
static_assert(__imag(test8) == 0, "");
+
+ constexpr int ignored = (fcomplex(), 0);
}
More information about the cfe-commits
mailing list