Skip to content

Commit

Permalink
Rename FloatAttr and remove old mlir::FloatType and mlir::FloatAttr
Browse files Browse the repository at this point in the history
  • Loading branch information
Lancern committed Feb 4, 2024
1 parent 49b7712 commit 801f2b9
Show file tree
Hide file tree
Showing 25 changed files with 238 additions and 279 deletions.
10 changes: 5 additions & 5 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,13 @@ def IntAttr : CIR_Attr<"Int", "int", [TypedAttrInterface]> {
}

//===----------------------------------------------------------------------===//
// FloatAttr
// FPAttr
//===----------------------------------------------------------------------===//

def FloatAttr : CIR_Attr<"Float", "float", [TypedAttrInterface]> {
let summary = "An Attribute containing a floating-point value";
def FPAttr : CIR_Attr<"FP", "fp", [TypedAttrInterface]> {
let summary = "An attribute containing a floating-point value";
let description = [{
A float attribute is a literal attribute that represents a floating-point
An fp attribute is a literal attribute that represents a floating-point
value of the specified floating-point type.
}];
let parameters = (ins AttributeSelfTypeParameter<"">:$type, "APFloat":$value);
Expand All @@ -220,7 +220,7 @@ def FloatAttr : CIR_Attr<"Float", "float", [TypedAttrInterface]> {
}]>,
];
let extraClassDeclaration = [{
static FloatAttr getZero(mlir::cir::FloatType type);
static FPAttr getZero(mlir::cir::FloatType type);
}];
let genVerifyDecl = 1;
let hasCustomAssemblyFormat = 1;
Expand Down
3 changes: 1 addition & 2 deletions clang/include/clang/CIR/Dialect/IR/CIRTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ def CIR_Double : CIR_FloatType<"Double", "double"> {

// Constraints

def CIR_AnyFloat: Type<
CPred<"$_self.isa<::mlir::FloatType, ::mlir::cir::FloatType>()">>;
def CIR_AnyFloat: Type<CPred<"$_self.isa<::mlir::cir::FloatType>()">>;

//===----------------------------------------------------------------------===//
// PointerType
Expand Down
28 changes: 10 additions & 18 deletions clang/lib/CIR/CodeGen/CIRGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,8 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy {
mlir::TypedAttr getZeroInitAttr(mlir::Type ty) {
if (ty.isa<mlir::cir::IntType>())
return mlir::cir::IntAttr::get(ty, 0);
if (ty.isa<mlir::FloatType>())
return mlir::FloatAttr::get(ty, 0.0);
if (auto fltType = ty.dyn_cast<mlir::cir::FloatType>())
return mlir::cir::FloatAttr::getZero(fltType);
return mlir::cir::FPAttr::getZero(fltType);
if (auto arrTy = ty.dyn_cast<mlir::cir::ArrayType>())
return getZeroAttr(arrTy);
if (auto ptrTy = ty.dyn_cast<mlir::cir::PointerType>())
Expand All @@ -252,13 +250,8 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy {
if (const auto intVal = attr.dyn_cast<mlir::cir::IntAttr>())
return intVal.isNullValue();

if (attr.isa<mlir::FloatAttr, mlir::cir::FloatAttr>()) {
auto fpVal = [&attr] {
if (auto fpAttr = attr.dyn_cast<mlir::cir::FloatAttr>())
return fpAttr.getValue();
return attr.cast<mlir::FloatAttr>().getValue();
}();

if (auto fpAttr = attr.dyn_cast<mlir::cir::FPAttr>()) {
auto fpVal = fpAttr.getValue();
bool ignored;
llvm::APFloat FV(+0.0);
FV.convert(fpVal.getSemantics(), llvm::APFloat::rmNearestTiesToEven,
Expand Down Expand Up @@ -350,23 +343,23 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy {
}
bool isInt(mlir::Type i) { return i.isa<mlir::cir::IntType>(); }

mlir::FloatType getLongDouble80BitsTy() const {
return typeCache.LongDouble80BitsTy;
mlir::cir::FloatType getLongDouble80BitsTy() const {
llvm_unreachable("NYI");
}

/// Get the proper floating point type for the given semantics.
mlir::FloatType getFloatTyForFormat(const llvm::fltSemantics &format,
bool useNativeHalf) const {
mlir::cir::FloatType getFloatTyForFormat(const llvm::fltSemantics &format,
bool useNativeHalf) const {
if (&format == &llvm::APFloat::IEEEhalf()) {
llvm_unreachable("IEEEhalf float format is NYI");
}

if (&format == &llvm::APFloat::BFloat())
llvm_unreachable("BFloat float format is NYI");
if (&format == &llvm::APFloat::IEEEsingle())
llvm_unreachable("IEEEsingle float format is NYI");
return typeCache.FloatTy;
if (&format == &llvm::APFloat::IEEEdouble())
llvm_unreachable("IEEEdouble float format is NYI");
return typeCache.DoubleTy;
if (&format == &llvm::APFloat::IEEEquad())
llvm_unreachable("IEEEquad float format is NYI");
if (&format == &llvm::APFloat::PPCDoubleDouble())
Expand Down Expand Up @@ -477,8 +470,7 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy {
}

bool isSized(mlir::Type ty) {
if (ty.isIntOrFloat() ||
ty.isa<mlir::cir::PointerType, mlir::cir::StructType,
if (ty.isa<mlir::cir::PointerType, mlir::cir::StructType,
mlir::cir::ArrayType, mlir::cir::BoolType, mlir::cir::IntType,
mlir::cir::FloatType>())
return true;
Expand Down
5 changes: 2 additions & 3 deletions clang/lib/CIR/CodeGen/CIRGenExprConst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1479,9 +1479,8 @@ mlir::Attribute ConstantEmitter::tryEmitPrivate(const APValue &Value,
assert(0 && "not implemented");
else {
mlir::Type ty = CGM.getCIRType(DestType);
if (ty.isa<mlir::cir::FloatType>())
return CGM.getBuilder().getAttr<mlir::cir::FloatAttr>(ty, Init);
return builder.getFloatAttr(ty, Init);
assert(ty.isa<mlir::cir::FloatType>() && "expected floating-point type");
return CGM.getBuilder().getAttr<mlir::cir::FPAttr>(ty, Init);
}
}
case APValue::Array: {
Expand Down
17 changes: 7 additions & 10 deletions clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,10 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
}
mlir::Value VisitFloatingLiteral(const FloatingLiteral *E) {
mlir::Type Ty = CGF.getCIRType(E->getType());
if (Ty.isa<mlir::cir::FloatType>())
return Builder.create<mlir::cir::ConstantOp>(
CGF.getLoc(E->getExprLoc()), Ty,
Builder.getAttr<mlir::cir::FloatAttr>(Ty, E->getValue()));
assert(Ty.isa<mlir::cir::FloatType>() && "expect floating-point type");
return Builder.create<mlir::cir::ConstantOp>(
CGF.getLoc(E->getExprLoc()), Ty,
Builder.getFloatAttr(Ty, E->getValue()));
Builder.getAttr<mlir::cir::FPAttr>(Ty, E->getValue()));
}
mlir::Value VisitCharacterLiteral(const CharacterLiteral *E) {
mlir::Type Ty = CGF.getCIRType(E->getType());
Expand Down Expand Up @@ -1205,7 +1202,7 @@ mlir::Value ScalarExprEmitter::buildSub(const BinOpInfo &Ops) {
llvm_unreachable("NYI");

assert(!UnimplementedFeature::cirVectorType());
if (Ops.LHS.getType().isa<mlir::FloatType, mlir::cir::FloatType>()) {
if (Ops.LHS.getType().isa<mlir::cir::FloatType>()) {
CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(CGF, Ops.FPFeatures);
return Builder.createFSub(Ops.LHS, Ops.RHS);
}
Expand Down Expand Up @@ -1673,20 +1670,20 @@ mlir::Value ScalarExprEmitter::buildScalarCast(
llvm_unreachable("NYI: signed bool");
if (CGF.getBuilder().isInt(DstTy)) {
CastKind = mlir::cir::CastKind::bool_to_int;
} else if (DstTy.isa<mlir::FloatType, mlir::cir::FloatType>()) {
} else if (DstTy.isa<mlir::cir::FloatType>()) {
CastKind = mlir::cir::CastKind::bool_to_float;
} else {
llvm_unreachable("Internal error: Cast to unexpected type");
}
} else if (CGF.getBuilder().isInt(SrcTy)) {
if (CGF.getBuilder().isInt(DstTy)) {
CastKind = mlir::cir::CastKind::integral;
} else if (DstTy.isa<mlir::FloatType, mlir::cir::FloatType>()) {
} else if (DstTy.isa<mlir::cir::FloatType>()) {
CastKind = mlir::cir::CastKind::int_to_float;
} else {
llvm_unreachable("Internal error: Cast to unexpected type");
}
} else if (SrcTy.isa<mlir::FloatType, mlir::cir::FloatType>()) {
} else if (SrcTy.isa<mlir::cir::FloatType>()) {
if (CGF.getBuilder().isInt(DstTy)) {
// If we can't recognize overflow as undefined behavior, assume that
// overflow saturates. This protects against normal optimizations if we
Expand All @@ -1696,7 +1693,7 @@ mlir::Value ScalarExprEmitter::buildScalarCast(
if (Builder.getIsFPConstrained())
llvm_unreachable("NYI");
CastKind = mlir::cir::CastKind::float_to_int;
} else if (DstTy.isa<mlir::FloatType, mlir::cir::FloatType>()) {
} else if (DstTy.isa<mlir::cir::FloatType>()) {
// TODO: split this to createFPExt/createFPTrunc
return Builder.createFloatingCast(Src, DstTy);
} else {
Expand Down
1 change: 0 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &context,
DoubleTy = ::mlir::cir::FloatType::getDouble(builder.getContext());
// TODO(cir): perhaps we should abstract long double variations into a custom
// cir.long_double type. Said type would also hold the semantics for lowering.
LongDouble80BitsTy = ::mlir::FloatType::getF80(builder.getContext());

// TODO: PointerWidthInBits
PointerAlignInBytes =
Expand Down
1 change: 0 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenTypeCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ struct CIRGenTypeCache {
// cir.long_double type. Said type would also hold the semantics for lowering.
mlir::cir::SingleType FloatTy;
mlir::cir::DoubleType DoubleTy;
mlir::FloatType LongDouble80BitsTy;

/// int
mlir::Type UIntTy;
Expand Down
15 changes: 7 additions & 8 deletions clang/lib/CIR/Dialect/IR/CIRAttrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,10 @@ LogicalResult IntAttr::verify(function_ref<InFlightDiagnostic()> emitError,
}

//===----------------------------------------------------------------------===//
// FloatAttr definitions
// FPAttr definitions
//===----------------------------------------------------------------------===//

Attribute cir::FloatAttr::parse(AsmParser &parser, Type odsType) {
Attribute cir::FPAttr::parse(AsmParser &parser, Type odsType) {
double value;

if (!odsType.isa<cir::FloatType>())
Expand All @@ -321,20 +321,19 @@ Attribute cir::FloatAttr::parse(AsmParser &parser, Type odsType) {
convertedValue.convert(ty.getFloatSemantics(), llvm::RoundingMode::TowardZero,
&losesInfo);

return cir::FloatAttr::get(ty, convertedValue);
return cir::FPAttr::get(ty, convertedValue);
}

void cir::FloatAttr::print(AsmPrinter &printer) const {
void cir::FPAttr::print(AsmPrinter &printer) const {
printer << '<' << getValue() << '>';
}

cir::FloatAttr cir::FloatAttr::getZero(mlir::cir::FloatType type) {
cir::FPAttr cir::FPAttr::getZero(mlir::cir::FloatType type) {
return get(type, APFloat::getZero(type.getFloatSemantics()));
}

LogicalResult
cir::FloatAttr::verify(function_ref<InFlightDiagnostic()> emitError, Type type,
APFloat value) {
LogicalResult cir::FPAttr::verify(function_ref<InFlightDiagnostic()> emitError,
Type type, APFloat value) {
auto fltType = type.dyn_cast<cir::FloatType>();
if (!fltType) {
emitError() << "expected floating-point type";
Expand Down
15 changes: 7 additions & 8 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,7 @@ static LogicalResult checkConstantTypes(mlir::Operation *op, mlir::Type opType,
return success();
}

if (attrType
.isa<mlir::cir::IntAttr, mlir::FloatAttr, mlir::cir::FloatAttr>()) {
if (attrType.isa<mlir::cir::IntAttr, mlir::cir::FPAttr>()) {
auto at = attrType.cast<TypedAttr>();
if (at.getType() != opType) {
return op->emitOpError("result type (")
Expand Down Expand Up @@ -424,13 +423,13 @@ LogicalResult CastOp::verify() {
return success();
}
case cir::CastKind::floating: {
if (!srcType.isa<mlir::FloatType, mlir::cir::FloatType>() ||
!resType.isa<mlir::FloatType, mlir::cir::FloatType>())
if (!srcType.isa<mlir::cir::FloatType>() ||
!resType.isa<mlir::cir::FloatType>())
return emitOpError() << "requries floating for source and result";
return success();
}
case cir::CastKind::float_to_int: {
if (!srcType.isa<mlir::FloatType, mlir::cir::FloatType>())
if (!srcType.isa<mlir::cir::FloatType>())
return emitOpError() << "requires floating for source";
if (!resType.dyn_cast<mlir::cir::IntType>())
return emitOpError() << "requires !IntegerType for result";
Expand All @@ -451,7 +450,7 @@ LogicalResult CastOp::verify() {
return success();
}
case cir::CastKind::float_to_bool: {
if (!srcType.isa<mlir::FloatType, mlir::cir::FloatType>())
if (!srcType.isa<mlir::cir::FloatType>())
return emitOpError() << "requires float for source";
if (!resType.isa<mlir::cir::BoolType>())
return emitOpError() << "requires !cir.bool for result";
Expand All @@ -467,14 +466,14 @@ LogicalResult CastOp::verify() {
case cir::CastKind::int_to_float: {
if (!srcType.isa<mlir::cir::IntType>())
return emitOpError() << "requires !cir.int for source";
if (!resType.isa<mlir::FloatType, mlir::cir::FloatType>())
if (!resType.isa<mlir::cir::FloatType>())
return emitOpError() << "requires !cir.float for result";
return success();
}
case cir::CastKind::bool_to_float: {
if (!srcType.isa<mlir::cir::BoolType>())
return emitOpError() << "requires !cir.bool for source";
if (!resType.isa<mlir::FloatType, mlir::cir::FloatType>())
if (!resType.isa<mlir::cir::FloatType>())
return emitOpError() << "requires !cir.float for result";
return success();
}
Expand Down
Loading

0 comments on commit 801f2b9

Please sign in to comment.