[clang] [CIR] Upstream CreateOp for ComplexType with folder (PR #143192)
Henrich Lauko via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 10 04:24:47 PDT 2025
================
@@ -0,0 +1,81 @@
+#include "CIRGenBuilder.h"
+#include "CIRGenFunction.h"
+
+#include "clang/AST/StmtVisitor.h"
+
+using namespace clang;
+using namespace clang::CIRGen;
+
+namespace {
+class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
+ CIRGenFunction &cgf;
+ CIRGenBuilderTy &builder;
+
+public:
+ explicit ComplexExprEmitter(CIRGenFunction &cgf)
+ : cgf(cgf), builder(cgf.getBuilder()) {}
+
+ /// Store the specified real/imag parts into the
+ /// specified value pointer.
+ void emitStoreOfComplex(mlir::Location loc, mlir::Value val, LValue lv,
+ bool isInit);
+
+ mlir::Value VisitInitListExpr(InitListExpr *e);
+};
+
+} // namespace
+
+static const ComplexType *getComplexType(QualType type) {
+ type = type.getCanonicalType();
+ if (const ComplexType *comp = dyn_cast<ComplexType>(type))
+ return comp;
+ return cast<ComplexType>(cast<AtomicType>(type)->getValueType());
+}
+
+void ComplexExprEmitter::emitStoreOfComplex(mlir::Location loc, mlir::Value val,
+ LValue lv, bool isInit) {
+ if (lv.getType()->isAtomicType() ||
+ (!isInit && cgf.isLValueSuitableForInlineAtomic(lv))) {
+ cgf.cgm.errorNYI("StoreOfComplex with Atomic LV");
+ return;
+ }
+
+ const Address destAddr = lv.getAddress();
+ builder.createStore(loc, val, destAddr);
+}
+
+mlir::Value ComplexExprEmitter::VisitInitListExpr(InitListExpr *e) {
+ if (e->getNumInits() == 2) {
+ mlir::Value real = cgf.emitScalarExpr(e->getInit(0));
+ mlir::Value imag = cgf.emitScalarExpr(e->getInit(1));
+ return builder.createComplexCreate(cgf.getLoc(e->getExprLoc()), real, imag);
+ }
+
+ if (e->getNumInits() == 1) {
+ cgf.cgm.errorNYI("Create Complex with InitList with size 1");
+ return {};
+ }
+
+ assert(e->getNumInits() == 0 && "Unexpected number of inits");
+ mlir::Location loc = cgf.getLoc(e->getExprLoc());
+ QualType complexElemTy =
+ e->getType()->castAs<clang::ComplexType>()->getElementType();
+ mlir::Type complexElemLLVMTy = cgf.convertType(complexElemTy);
+ mlir::TypedAttr defaultValue = builder.getZeroInitAttr(complexElemLLVMTy);
+ auto complexTy = cir::ComplexType::get(complexElemLLVMTy);
+ auto complexAttr =
+ cir::ConstComplexAttr::get(complexTy, defaultValue, defaultValue);
----------------
xlauko wrote:
This can be used on multiple other places.
https://github.com/llvm/llvm-project/pull/143192
More information about the cfe-commits
mailing list