|
| 1 | +#include "CIRGenBuilder.h" |
| 2 | +#include "CIRGenFunction.h" |
| 3 | + |
| 4 | +#include "clang/AST/StmtVisitor.h" |
| 5 | + |
| 6 | +using namespace clang; |
| 7 | +using namespace clang::CIRGen; |
| 8 | + |
| 9 | +namespace { |
| 10 | +class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> { |
| 11 | + CIRGenFunction &cgf; |
| 12 | + CIRGenBuilderTy &builder; |
| 13 | + |
| 14 | +public: |
| 15 | + explicit ComplexExprEmitter(CIRGenFunction &cgf) |
| 16 | + : cgf(cgf), builder(cgf.getBuilder()) {} |
| 17 | + |
| 18 | + /// Store the specified real/imag parts into the |
| 19 | + /// specified value pointer. |
| 20 | + void emitStoreOfComplex(mlir::Location loc, mlir::Value val, LValue lv, |
| 21 | + bool isInit); |
| 22 | + |
| 23 | + mlir::Value VisitInitListExpr(InitListExpr *e); |
| 24 | +}; |
| 25 | + |
| 26 | +} // namespace |
| 27 | + |
| 28 | +static const ComplexType *getComplexType(QualType type) { |
| 29 | + type = type.getCanonicalType(); |
| 30 | + if (const ComplexType *comp = dyn_cast<ComplexType>(type)) |
| 31 | + return comp; |
| 32 | + return cast<ComplexType>(cast<AtomicType>(type)->getValueType()); |
| 33 | +} |
| 34 | + |
| 35 | +void ComplexExprEmitter::emitStoreOfComplex(mlir::Location loc, mlir::Value val, |
| 36 | + LValue lv, bool isInit) { |
| 37 | + if (lv.getType()->isAtomicType() || |
| 38 | + (!isInit && cgf.isLValueSuitableForInlineAtomic(lv))) { |
| 39 | + cgf.cgm.errorNYI("StoreOfComplex with Atomic LV"); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + const Address destAddr = lv.getAddress(); |
| 44 | + builder.createStore(loc, val, destAddr); |
| 45 | +} |
| 46 | + |
| 47 | +mlir::Value ComplexExprEmitter::VisitInitListExpr(InitListExpr *e) { |
| 48 | + mlir::Location loc = cgf.getLoc(e->getExprLoc()); |
| 49 | + if (e->getNumInits() == 2) { |
| 50 | + mlir::Value real = cgf.emitScalarExpr(e->getInit(0)); |
| 51 | + mlir::Value imag = cgf.emitScalarExpr(e->getInit(1)); |
| 52 | + return builder.createComplexCreate(loc, real, imag); |
| 53 | + } |
| 54 | + |
| 55 | + if (e->getNumInits() == 1) { |
| 56 | + cgf.cgm.errorNYI("Create Complex with InitList with size 1"); |
| 57 | + return {}; |
| 58 | + } |
| 59 | + |
| 60 | + assert(e->getNumInits() == 0 && "Unexpected number of inits"); |
| 61 | + QualType complexElemTy = |
| 62 | + e->getType()->castAs<clang::ComplexType>()->getElementType(); |
| 63 | + mlir::Type complexElemLLVMTy = cgf.convertType(complexElemTy); |
| 64 | + mlir::TypedAttr defaultValue = builder.getZeroInitAttr(complexElemLLVMTy); |
| 65 | + auto complexAttr = cir::ConstComplexAttr::get(defaultValue, defaultValue); |
| 66 | + return builder.create<cir::ConstantOp>(loc, complexAttr); |
| 67 | +} |
| 68 | + |
| 69 | +mlir::Value CIRGenFunction::emitComplexExpr(const Expr *e) { |
| 70 | + assert(e && getComplexType(e->getType()) && |
| 71 | + "Invalid complex expression to emit"); |
| 72 | + |
| 73 | + return ComplexExprEmitter(*this).Visit(const_cast<Expr *>(e)); |
| 74 | +} |
| 75 | + |
| 76 | +void CIRGenFunction::emitStoreOfComplex(mlir::Location loc, mlir::Value v, |
| 77 | + LValue dest, bool isInit) { |
| 78 | + ComplexExprEmitter(*this).emitStoreOfComplex(loc, v, dest, isInit); |
| 79 | +} |
0 commit comments