Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DFP] Initial changes to supprt DFP under APFloat and parse declarations #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/include/clang/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,9 @@ class ASTContext : public RefCountedBase<ASTContext> {
CanQualType HalfTy; // [OpenCL 6.1.1.1], ARM NEON
CanQualType BFloat16Ty;
CanQualType Float16Ty; // C11 extension ISO/IEC TS 18661-3
// ISO/IEC TS 18661-2:2015 c23 conditionally supported
CanQualType DecimalFloat32Ty, DecimalFloat64Ty, DecimalFloat128Ty;
CanQualType DecimalFloatDPD32Ty, DecimalFloatDPD64Ty, DecimalFloatDPD128Ty;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency shouldn't we have DecimalFloatBID32Ty, DecimalFloatBID64Ty and DecimalFloatBID64Ty?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should only have DecimalFloat32/64/128Ty with no prefix and differentiate the decoding via the fltSematics. WDYT?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @zahiraam; there should be just one set of types with behavior differentiated based on semantics.

These types are already present in the dfp branch.

CanQualType VoidPtrTy, NullPtrTy;
CanQualType DependentTy, OverloadTy, BoundMemberTy, UnresolvedTemplateTy,
UnknownAnyTy;
Expand Down
13 changes: 13 additions & 0 deletions clang/include/clang/AST/BuiltinTypes.def
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,19 @@ FLOATING_TYPE(Float128, Float128Ty)
// '__ibm128'
FLOATING_TYPE(Ibm128, Ibm128Ty)

// '_Decimal32'
FLOATING_TYPE(DecimalFloatBID32, DecimalFloat32Ty)
FLOATING_TYPE(DecimalFloatDPD32, DecimalFloatDPD32Ty)

// '_Decimal64'
FLOATING_TYPE(DecimalFloatBID64, DecimalFloat64Ty)
FLOATING_TYPE(DecimalFloatDPD64, DecimalFloatDPD64Ty)

// '_Decimal128'
FLOATING_TYPE(DecimalFloatBID128, DecimalFloat128Ty)
FLOATING_TYPE(DecimalFloatDPD128, DecimalFloatDPD128Ty)


//===- Language-specific types --------------------------------------------===//

// This is the type of C++0x 'nullptr'.
Expand Down
6 changes: 3 additions & 3 deletions clang/include/clang/AST/Stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,10 @@ class alignas(void *) Stmt {
unsigned : NumExprBits;

static_assert(
llvm::APFloat::S_MaxSemantics < 16,
"Too many Semantics enum values to fit in bitfield of size 4");
llvm::APFloat::S_MaxSemantics < 32,
"Too many Semantics enum values to fit in bitfield of size 5");
LLVM_PREFERRED_TYPE(llvm::APFloat::Semantics)
unsigned Semantics : 4; // Provides semantics for APFloat construction
unsigned Semantics : 5; // Provides semantics for APFloat construction
LLVM_PREFERRED_TYPE(bool)
unsigned IsExact : 1;
};
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -2479,6 +2479,7 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
bool isBFloat16Type() const;
bool isFloat128Type() const;
bool isIbm128Type() const;
bool isDecimalFloatType() const;
bool isRealType() const; // C99 6.2.5p17 (real floating + integer)
bool isArithmeticType() const; // C99 6.2.5p18 (integer + floating)
bool isVoidType() const; // C99 6.2.5p19
Expand Down
35 changes: 34 additions & 1 deletion clang/include/clang/Basic/TargetInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ struct TransferrableTargetInfo {
unsigned char FloatWidth, FloatAlign;
unsigned char DoubleWidth, DoubleAlign;
unsigned char LongDoubleWidth, LongDoubleAlign, Float128Align, Ibm128Align;
unsigned char DecimalFloat32Width, DecimalFloat32Align;
unsigned char DecimalFloat64Width, DecimalFloat64Align;
unsigned char DecimalFloat128Width, DecimalFloat128Align;
unsigned char LargeArrayMinWidth, LargeArrayAlign;
unsigned char LongWidth, LongAlign;
unsigned char LongLongWidth, LongLongAlign;
Expand Down Expand Up @@ -136,7 +139,9 @@ struct TransferrableTargetInfo {
unsigned MaxTLSAlign;

const llvm::fltSemantics *HalfFormat, *BFloat16Format, *FloatFormat,
*DoubleFormat, *LongDoubleFormat, *Float128Format, *Ibm128Format;
*DoubleFormat, *LongDoubleFormat, *Float128Format, *Ibm128Format,
*DecimalFloatBID32Format, *DecimalFloatBID64Format, *DecimalFloatBID128Format,
*DecimalFloatDPD32Format, *DecimalFloatDPD64Format, *DecimalFloatDPD128Format;

///===---- Target Data Type Query Methods -------------------------------===//
enum IntType {
Expand Down Expand Up @@ -794,6 +799,34 @@ class TargetInfo : public TransferrableTargetInfo,
return *Float128Format;
}

unsigned getDecimalFloat32Width() const { return 32; }
unsigned getDecimalFloat32Align() const { return DecimalFloat32Align; }
const llvm::fltSemantics &getDecimalFloatBID32Format() const {
return *DecimalFloatBID32Format;
}
const llvm::fltSemantics &getDecimalFloatDPD32Format() const {
return *DecimalFloatDPD32Format;
}

unsigned getDecimalFloat64Width() const { return 64; }
unsigned getDecimalFloat64Align() const { return DecimalFloat64Align; }
const llvm::fltSemantics &getDecimalFloatBID64Format() const {
return *DecimalFloatBID64Format;
}
const llvm::fltSemantics &getDecimalFloatDPD64Format() const {
return *DecimalFloatDPD64Format;
}

unsigned getDecimalFloat128Width() const { return 128; }
unsigned getDecimalFloat128Align() const { return DecimalFloat128Align; }
const llvm::fltSemantics &getDecimalFloatBID128Format() const {
return *DecimalFloatBID128Format;
}
const llvm::fltSemantics &getDecimalFloatDPD128Format() const {
return *DecimalFloatDPD128Format;
}


/// getIbm128Width/Align/Format - Return the size/align/format of
/// '__ibm128'.
unsigned getIbm128Width() const { return 128; }
Expand Down
41 changes: 41 additions & 0 deletions clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,14 @@ void ASTContext::InitBuiltinTypes(const TargetInfo &Target,
InitBuiltinType(SatUnsignedFractTy, BuiltinType::SatUFract);
InitBuiltinType(SatUnsignedLongFractTy, BuiltinType::SatULongFract);

InitBuiltinType(DecimalFloat32Ty, BuiltinType::DecimalFloatBID32);
InitBuiltinType(DecimalFloat64Ty, BuiltinType::DecimalFloatBID64);
InitBuiltinType(DecimalFloat128Ty, BuiltinType::DecimalFloatBID128);

InitBuiltinType(DecimalFloatDPD32Ty, BuiltinType::DecimalFloatDPD32);
InitBuiltinType(DecimalFloatDPD64Ty, BuiltinType::DecimalFloatDPD64);
InitBuiltinType(DecimalFloatDPD128Ty, BuiltinType::DecimalFloatDPD128);

// GNU extension, 128-bit integers.
InitBuiltinType(Int128Ty, BuiltinType::Int128);
InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
Expand Down Expand Up @@ -1628,6 +1636,18 @@ const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
if (getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice)
return AuxTarget->getFloat128Format();
return Target->getFloat128Format();
case BuiltinType::DecimalFloatBID32:
return Target->getDecimalFloatBID32Format();
case BuiltinType::DecimalFloatDPD32:
return Target->getDecimalFloatDPD32Format();
case BuiltinType::DecimalFloatBID64:
return Target->getDecimalFloatBID64Format();
case BuiltinType::DecimalFloatDPD64:
return Target->getDecimalFloatDPD64Format();
case BuiltinType::DecimalFloatBID128:
return Target->getDecimalFloatBID128Format();
case BuiltinType::DecimalFloatDPD128:
return Target->getDecimalFloatDPD128Format();
}
}

Expand Down Expand Up @@ -2102,6 +2122,21 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
Width = Target->getIbm128Width();
Align = Target->getIbm128Align();
break;
case BuiltinType::DecimalFloatBID32:
case BuiltinType::DecimalFloatDPD32:
Width = Target->getDecimalFloat32Width();
Align = Target->getDecimalFloat32Align();
break;
case BuiltinType::DecimalFloatBID64:
case BuiltinType::DecimalFloatDPD64:
Width = Target->getDecimalFloat64Width();
Align = Target->getDecimalFloat64Align();
break;
case BuiltinType::DecimalFloatBID128:
case BuiltinType::DecimalFloatDPD128:
Width = Target->getDecimalFloat128Width();
Align = Target->getDecimalFloat128Align();
break;
case BuiltinType::LongDouble:
if (getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice &&
(Target->getLongDoubleWidth() != AuxTarget->getLongDoubleWidth() ||
Expand Down Expand Up @@ -8140,6 +8175,12 @@ static char getObjCEncodingForPrimitiveType(const ASTContext *C,
case BuiltinType::SatUShortFract:
case BuiltinType::SatUFract:
case BuiltinType::SatULongFract:
case BuiltinType::DecimalFloatBID32:
case BuiltinType::DecimalFloatDPD32:
case BuiltinType::DecimalFloatBID64:
case BuiltinType::DecimalFloatDPD64:
case BuiltinType::DecimalFloatBID128:
case BuiltinType::DecimalFloatDPD128:
// FIXME: potentially need @encodes for these!
return ' ';

Expand Down
20 changes: 18 additions & 2 deletions clang/lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2238,12 +2238,19 @@ bool Type::hasUnsignedIntegerRepresentation() const {
bool Type::isFloatingType() const {
if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
return BT->getKind() >= BuiltinType::Half &&
BT->getKind() <= BuiltinType::Ibm128;
BT->getKind() <= BuiltinType::DecimalFloatDPD128;
if (const auto *CT = dyn_cast<ComplexType>(CanonicalType))
return CT->getElementType()->isFloatingType();
return false;
}

bool Type::isDecimalFloatType() const {
if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we call this function isDecimalFloatingType instead?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function already exists in the dfp branch (with the name @zahiraam requested).

return BT->getKind() >= BuiltinType::DecimalFloatBID32 &&
BT->getKind() <= BuiltinType::DecimalFloatDPD128;
return false;
}

bool Type::hasFloatingRepresentation() const {
if (const auto *VT = dyn_cast<VectorType>(CanonicalType))
return VT->getElementType()->isFloatingType();
Expand All @@ -2270,7 +2277,7 @@ bool Type::isRealType() const {
bool Type::isArithmeticType() const {
if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
return BT->getKind() >= BuiltinType::Bool &&
BT->getKind() <= BuiltinType::Ibm128;
BT->getKind() <= BuiltinType::DecimalFloatDPD128;
if (const auto *ET = dyn_cast<EnumType>(CanonicalType))
// GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
// If a body isn't seen by the time we get here, return false.
Expand Down Expand Up @@ -3379,6 +3386,15 @@ StringRef BuiltinType::getName(const PrintingPolicy &Policy) const {
return "__float128";
case Ibm128:
return "__ibm128";
case DecimalFloatBID32:
case DecimalFloatDPD32:
return "_Decimal32";
case DecimalFloatBID64:
case DecimalFloatDPD64:
return "_Decimal64";
case DecimalFloatBID128:
case DecimalFloatDPD128:
return "_Decimal128";
case WChar_S:
case WChar_U:
return Policy.MSWChar ? "__wchar_t" : "wchar_t";
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Basic/TargetInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ TargetInfo::TargetInfo(const llvm::Triple &T) : Triple(T) {
LongDoubleFormat = &llvm::APFloat::IEEEdouble();
Float128Format = &llvm::APFloat::IEEEquad();
Ibm128Format = &llvm::APFloat::PPCDoubleDouble();
DecimalFloatBID32Format = &llvm::APFloat::DFP32BID();
DecimalFloatBID64Format = &llvm::APFloat::DFP64BID();
DecimalFloatBID128Format = &llvm::APFloat::DFP128BID();
DecimalFloatDPD32Format = &llvm::APFloat::DFP32DPD();
DecimalFloatDPD64Format = &llvm::APFloat::DFP64DPD();
DecimalFloatDPD128Format = &llvm::APFloat::DFP128DPD();
MCountName = "mcount";
UserLabelPrefix = "_";
RegParmMax = 0;
Expand Down
8 changes: 5 additions & 3 deletions clang/lib/Sema/SemaType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1170,11 +1170,13 @@ static QualType ConvertDeclSpecToType(TypeProcessingState &state) {
Result = Context.BoolTy; // _Bool or bool
break;
case DeclSpec::TST_decimal32: // _Decimal32
Result = Context.DecimalFloat32Ty;
break;
case DeclSpec::TST_decimal64: // _Decimal64
Result = Context.DecimalFloat64Ty;
break;
case DeclSpec::TST_decimal128: // _Decimal128
S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
Result = Context.IntTy;
declarator.setInvalidType(true);
Result = Context.DecimalFloat128Ty;
break;
case DeclSpec::TST_class:
case DeclSpec::TST_enum:
Expand Down
9 changes: 9 additions & 0 deletions clang/test/AST/dfp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %clang_cc1 -x c -ast-dump %s | FileCheck %s --strict-whitespace

_Decimal32 x;
_Decimal64 y;
_Decimal128 z;

//CHECK: |-VarDecl {{.*}} x '_Decimal32'
//CHECK-NEXT: |-VarDecl {{.*}} y '_Decimal64'
//CHECK-NEXT: `-VarDecl {{.*}} z '_Decimal128'
4 changes: 3 additions & 1 deletion clang/test/Sema/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ enum e { e_1 };
extern int j[sizeof(enum e)]; // expected-note {{previous declaration}}
int j[42]; // expected-error {{redefinition of 'j' with a different type: 'int[42]' vs 'int[4]'}}

_Decimal32 x; // expected-error {{GNU decimal type extension not supported}}
_Decimal32 x;
_Decimal64 y;
_Decimal128 z;

int __attribute__ ((vector_size (8), vector_size (8))) v; // expected-error {{invalid vector element type}}

Expand Down
Loading