Skip to content

Commit

Permalink
Revert partial changes in the previous commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Lancern committed Mar 24, 2024
1 parent 09b8f2d commit 35f7139
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 92 deletions.
2 changes: 2 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ static void buildDeclInit(CIRGenFunction &CGF, const VarDecl *D,
case TEK_Scalar:
CGF.buildScalarInit(Init, CGF.getLoc(D->getLocation()), lv, false);
return;
case TEK_Complex:
llvm_unreachable("complext evaluation NYI");
}
}

Expand Down
3 changes: 3 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,9 @@ void CIRGenFunction::buildInitializerForField(FieldDecl *Field, LValue LHS,
llvm_unreachable("NYI");
}
break;
case TEK_Complex:
llvm_unreachable("NYI");
break;
case TEK_Aggregate: {
AggValueSlot Slot = AggValueSlot::forLValue(
LHS, AggValueSlot::IsDestructed, AggValueSlot::DoesNotNeedGCBarriers,
Expand Down
5 changes: 4 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,9 +700,12 @@ void CIRGenFunction::buildExprAsInit(const Expr *init, const ValueDecl *D,
return;
}
switch (CIRGenFunction::getEvaluationKind(type)) {
case TEK_Scalar: {
case TEK_Scalar:
buildScalarInit(init, getLoc(D->getSourceRange()), lvalue);
return;
case TEK_Complex: {
assert(0 && "not implemented");
return;
}
case TEK_Aggregate:
assert(!type->isAtomicType() && "NYI");
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,8 @@ LValue CIRGenFunction::buildBinaryOperatorLValue(const BinaryOperator *E) {
return LV;
}

case TEK_Complex:
assert(0 && "not implemented");
case TEK_Aggregate:
assert(0 && "not implemented");
}
Expand Down Expand Up @@ -1063,6 +1065,8 @@ RValue CIRGenFunction::buildAnyExpr(const Expr *E, AggValueSlot aggSlot,
switch (CIRGenFunction::getEvaluationKind(E->getType())) {
case TEK_Scalar:
return RValue::get(buildScalarExpr(E));
case TEK_Complex:
assert(0 && "not implemented");
case TEK_Aggregate: {
if (!ignoreResult && aggSlot.isIgnored())
aggSlot = CreateAggTemp(E->getType(), getLoc(E->getSourceRange()),
Expand Down Expand Up @@ -1850,6 +1854,10 @@ void CIRGenFunction::buildAnyExprToMem(const Expr *E, Address Location,
Qualifiers Quals, bool IsInit) {
// FIXME: This function should take an LValue as an argument.
switch (getEvaluationKind(E->getType())) {
case TEK_Complex:
assert(0 && "NYI");
return;

case TEK_Aggregate: {
buildAggExpr(E, AggValueSlot::forAddr(Location, Quals,
AggValueSlot::IsDestructed_t(IsInit),
Expand Down Expand Up @@ -2298,6 +2306,8 @@ RValue CIRGenFunction::convertTempToRValue(Address addr, clang::QualType type,
clang::SourceLocation loc) {
LValue lvalue = makeAddrLValue(addr, type, AlignmentSource::Decl);
switch (getEvaluationKind(type)) {
case TEK_Complex:
llvm_unreachable("NYI");
case TEK_Aggregate:
llvm_unreachable("NYI");
case TEK_Scalar:
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenExprAgg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,9 @@ void AggExprEmitter::buildInitializationToLValue(Expr *E, LValue LV) {
}

switch (CGF.getEvaluationKind(type)) {
case TEK_Complex:
llvm_unreachable("NYI");
return;
case TEK_Aggregate:
CGF.buildAggExpr(
E, AggValueSlot::forLValue(LV, AggValueSlot::IsDestructed,
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,9 @@ static void StoreAnyExprIntoOneUnit(CIRGenFunction &CGF, const Expr *Init,
CGF.buildScalarInit(Init, CGF.getLoc(Init->getSourceRange()),
CGF.makeAddrLValue(NewPtr, AllocType), false);
return;
case TEK_Complex:
llvm_unreachable("NYI");
return;
case TEK_Aggregate: {
AggValueSlot Slot = AggValueSlot::forAddr(
NewPtr, AllocType.getQualifiers(), AggValueSlot::IsDestructed,
Expand Down
99 changes: 14 additions & 85 deletions clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,25 +171,6 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
CGF.getLoc(E->getExprLoc()), Ty,
Builder.getAttr<mlir::cir::FPAttr>(Ty, E->getValue()));
}
mlir::Value VisitImaginaryLiteral(const ImaginaryLiteral *E) {
auto Loc = CGF.getLoc(E->getExprLoc());
auto Ty = CGF.getCIRType(E->getType()).cast<mlir::cir::ComplexType>();
auto ElementTy = Ty.getElementTy();

mlir::TypedAttr Real = Builder.getZeroInitAttr(ElementTy);
mlir::TypedAttr Imag;
if (ElementTy.isa<mlir::cir::IntType>()) {
auto RealValue = cast<IntegerLiteral>(E->getSubExpr())->getValue();
Imag = mlir::cir::IntAttr::get(ElementTy, RealValue);
} else if (ElementTy.isa<mlir::cir::CIRFPTypeInterface>()) {
auto RealValue = cast<FloatingLiteral>(E->getSubExpr())->getValue();
Imag = mlir::cir::FPAttr::get(ElementTy, RealValue);
} else
llvm_unreachable("unexpected complex element type");

auto Attr = mlir::cir::ComplexAttr::get(Ty, Real, Imag);
return Builder.getConstant(Loc, Attr);
}
mlir::Value VisitCharacterLiteral(const CharacterLiteral *E) {
mlir::Type Ty = CGF.getCIRType(E->getType());
auto loc = CGF.getLoc(E->getExprLoc());
Expand Down Expand Up @@ -746,45 +727,9 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
Result.Loc = E->getSourceRange();
// TODO: Result.FPFeatures
Result.E = E;

buildComplexPromotion(Result);

return Result;
}

// If any of the two operands of a binary operation is of complex type,
// ensure both operands are promoted to complex type.
void buildComplexPromotion(BinOpInfo &Info) {
auto LHSTy = Info.LHS.getType().dyn_cast<mlir::cir::ComplexType>();
auto RHSTy = Info.RHS.getType().dyn_cast<mlir::cir::ComplexType>();
if (!LHSTy && !RHSTy)
return;

if (LHSTy && RHSTy)
return;

// We need to promote *Scalar to ComplexTy.
mlir::Value *Scalar;
mlir::cir::ComplexType ComplexTy;
if (LHSTy) {
Scalar = &Info.RHS;
ComplexTy = LHSTy;
} else {
Scalar = &Info.LHS;
ComplexTy = RHSTy;
}

mlir::cir::CastKind Kind;
if (Scalar->getType().isa<mlir::cir::IntType>())
Kind = mlir::cir::CastKind::int_to_complex;
else if (Scalar->getType().isa<mlir::cir::CIRFPTypeInterface>())
Kind = mlir::cir::CastKind::float_to_complex;
else
llvm_unreachable("unexpected complex promotion candidate");

*Scalar = Builder.createCast(Kind, *Scalar, ComplexTy);
}

mlir::Value buildMul(const BinOpInfo &Ops);
mlir::Value buildDiv(const BinOpInfo &Ops);
mlir::Value buildRem(const BinOpInfo &Ops);
Expand All @@ -808,9 +753,7 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
// codegen.
QualType getPromotionType(QualType Ty) {
if (auto *CT = Ty->getAs<ComplexType>()) {
QualType ElementType = CT->getElementType();
if (ElementType.UseExcessPrecision(CGF.getContext()))
llvm_unreachable("NYI");
llvm_unreachable("NYI");
}
if (Ty.UseExcessPrecision(CGF.getContext()))
llvm_unreachable("NYI");
Expand All @@ -820,8 +763,7 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
// Binary operators and binary compound assignment operators.
#define HANDLEBINOP(OP) \
mlir::Value VisitBin##OP(const BinaryOperator *E) { \
auto binOpInfo = buildBinOps(E); \
return build##OP(binOpInfo); \
return build##OP(buildBinOps(E)); \
} \
mlir::Value VisitBin##OP##Assign(const CompoundAssignOperator *E) { \
return buildCompoundAssign(E, &ScalarExprEmitter::build##OP); \
Expand Down Expand Up @@ -1549,25 +1491,14 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
llvm_unreachable("NYI");
case CK_CopyAndAutoreleaseBlockObject:
llvm_unreachable("NYI");
case CK_IntegralRealToComplex: {
auto DestComplexTy = ConvertType(DestTy).cast<mlir::cir::ComplexType>();
auto Src = Visit(const_cast<Expr *>(E));
return Builder.createCast(mlir::cir::CastKind::int_to_complex, Src,
DestComplexTy);
}
case CK_FloatingRealToComplex: {
auto DestComplexTy = ConvertType(DestTy).cast<mlir::cir::ComplexType>();
auto Src = Visit(const_cast<Expr *>(E));
return Builder.createCast(mlir::cir::CastKind::float_to_complex, Src,
DestComplexTy);
}
case CK_FloatingRealToComplex:
llvm_unreachable("NYI");
case CK_FloatingComplexCast:
llvm_unreachable("NYI");
case CK_IntegralComplexToFloatingComplex:
case CK_FloatingComplexToIntegralComplex: {
auto DestComplexTy = ConvertType(DestTy).cast<mlir::cir::ComplexType>();
auto Src = Visit(const_cast<Expr *>(E));
return Builder.createCast(mlir::cir::CastKind::complex, Src, DestComplexTy);
}
llvm_unreachable("NYI");
case CK_FloatingComplexToIntegralComplex:
llvm_unreachable("NYI");
case CK_ConstructorConversion:
llvm_unreachable("NYI");
case CK_ToUnion:
Expand Down Expand Up @@ -1659,15 +1590,13 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
case CK_MemberPointerToBoolean:
llvm_unreachable("NYI");
case CK_FloatingComplexToReal:
case CK_IntegralComplexToReal: {
auto Src = Visit(const_cast<Expr *>(E));
return Builder.createComplexReal(CGF.getLoc(CE->getSourceRange()), Src);
}
llvm_unreachable("NYI");
case CK_IntegralComplexToReal:
llvm_unreachable("NYI");
case CK_FloatingComplexToBoolean:
case CK_IntegralComplexToBoolean: {
auto Src = Visit(const_cast<Expr *>(E));
return Builder.createComplexIsZero(CGF.getLoc(CE->getSourceRange()), Src);
}
llvm_unreachable("NYI");
case CK_IntegralComplexToBoolean:
llvm_unreachable("NYI");
case CK_ZeroToOCLOpaqueType:
llvm_unreachable("NYI");
case CK_IntToOCLSampler:
Expand Down
5 changes: 4 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ TypeEvaluationKind CIRGenFunction::getEvaluationKind(QualType type) {
case Type::ObjCObjectPointer:
case Type::Pipe:
case Type::BitInt:
case Type::Complex: // In CIR we treat complex types as scalars
return TEK_Scalar;

// Complexes.
case Type::Complex:
return TEK_Complex;

// Arrays, records, and Objective-C objects.
case Type::ConstantArray:
case Type::IncompleteArray:
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/CodeGen/CIRGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace cir {

// FIXME: for now we are reusing this from lib/Clang/CIRGenFunction.h, which
// isn't available in the include dir. Same for getEvaluationKind below.
enum TypeEvaluationKind { TEK_Scalar, TEK_Aggregate };
enum TypeEvaluationKind { TEK_Scalar, TEK_Complex, TEK_Aggregate };
struct CGCoroData;

class CIRGenFunction : public CIRGenTypeCache {
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,9 @@ static void InitCatchParam(CIRGenFunction &CGF, const VarDecl &CatchParam,
LValue srcLV = CGF.MakeNaturalAlignAddrLValue(catchParam, CatchType);
LValue destLV = CGF.makeAddrLValue(ParamAddr, CatchType);
switch (TEK) {
case TEK_Complex:
llvm_unreachable("NYI");
return;
case TEK_Scalar: {
auto exnLoad = CGF.buildLoadOfScalar(srcLV, catchParam.getLoc());
CGF.buildStoreOfScalar(exnLoad, destLV, /*init*/ true);
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,9 @@ mlir::LogicalResult CIRGenFunction::buildReturnStmt(const ReturnStmt &S) {
V = buildScalarExpr(RV);
builder.create<mlir::cir::StoreOp>(loc, V, *FnRetAlloca);
break;
case TEK_Complex:
llvm_unreachable("NYI");
break;
case TEK_Aggregate:
buildAggExpr(
RV, AggValueSlot::forAddr(
Expand Down
22 changes: 18 additions & 4 deletions clang/lib/CIR/CodeGen/CIRGenValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ namespace cir {

/// This trivial value class is used to represent the result of an
/// expression that is evaluated. It can be one of three things: either a
/// simple MLIR SSA value, or the address of an aggregate value in memory.
///
/// Note that in CIR, we treat expressions of complex type as scalars.
/// simple MLIR SSA value, a pair of SSA values for complex numbers, or the
/// address of an aggregate value in memory.
class RValue {
enum Flavor { Scalar, Aggregate };
enum Flavor { Scalar, Complex, Aggregate };

// The shift to make to an aggregate's alignment to make it look
// like a pointer.
Expand All @@ -49,6 +48,7 @@ class RValue {

public:
bool isScalar() const { return V1.getInt() == Scalar; }
bool isComplex() const { return V1.getInt() == Complex; }
bool isAggregate() const { return V1.getInt() == Aggregate; }
bool isIgnored() const { return isScalar() && !getScalarVal(); }

Expand All @@ -60,6 +60,12 @@ class RValue {
return V1.getPointer();
}

/// Return the real/imag components of this complex value.
std::pair<mlir::Value, mlir::Value> getComplexVal() const {
assert(0 && "not implemented");
return {};
}

/// Return the mlir::Value of the address of the aggregate.
Address getAggregateAddress() const {
assert(isAggregate() && "Not an aggregate!");
Expand All @@ -86,6 +92,14 @@ class RValue {
ER.V2.setInt(false);
return ER;
}
static RValue getComplex(mlir::Value V1, mlir::Value V2) {
assert(0 && "not implemented");
return RValue{};
}
static RValue getComplex(const std::pair<mlir::Value, mlir::Value> &C) {
assert(0 && "not implemented");
return RValue{};
}
// FIXME: Aggregate rvalues need to retain information about whether they are
// volatile or not. Remove default to find all places that probably get this
// wrong.
Expand Down

0 comments on commit 35f7139

Please sign in to comment.