Skip to content
Merged
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
5 changes: 3 additions & 2 deletions clang/include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -2773,8 +2773,9 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
/// of some sort, e.g., it is a floating-point type or a vector thereof.
bool hasFloatingRepresentation() const;

/// Determine whether this type has a boolean representation
/// of some sort.
/// Determine whether this type has a boolean representation -- i.e., it is a
/// boolean type, an enum type whose underlying type is a boolean type, or a
/// vector of booleans.
bool hasBooleanRepresentation() const;

// Type Checking Functions: Check to see if this type is structurally the
Expand Down
19 changes: 9 additions & 10 deletions clang/lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2344,16 +2344,15 @@ bool Type::isArithmeticType() const {
}

bool Type::hasBooleanRepresentation() const {
if (isBooleanType())
return true;

if (const EnumType *ET = getAs<EnumType>())
return ET->getDecl()->getIntegerType()->isBooleanType();

if (const AtomicType *AT = getAs<AtomicType>())
return AT->getValueType()->hasBooleanRepresentation();

return false;
if (const auto *VT = dyn_cast<VectorType>(CanonicalType))
return VT->getElementType()->isBooleanType();
if (const auto *ET = dyn_cast<EnumType>(CanonicalType)) {
return ET->getDecl()->isComplete() &&
ET->getDecl()->getIntegerType()->isBooleanType();
}
if (const auto *IT = dyn_cast<BitIntType>(CanonicalType))
return IT->getNumBits() == 1;
return isBooleanType();
}

Type::ScalarTypeKind Type::getScalarTypeKind() const {
Expand Down
20 changes: 13 additions & 7 deletions clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1920,7 +1920,7 @@ static bool getRangeForType(CodeGenFunction &CGF, QualType Ty,
llvm::MDNode *CodeGenFunction::getRangeForLoadFromType(QualType Ty) {
llvm::APInt Min, End;
if (!getRangeForType(*this, Ty, Min, End, CGM.getCodeGenOpts().StrictEnums,
Ty->hasBooleanRepresentation()))
Ty->hasBooleanRepresentation() && !Ty->isVectorType()))
return nullptr;

llvm::MDBuilder MDHelper(getLLVMContext());
Expand Down Expand Up @@ -1948,7 +1948,7 @@ bool CodeGenFunction::EmitScalarRangeCheck(llvm::Value *Value, QualType Ty,
if (!HasBoolCheck && !HasEnumCheck)
return false;

bool IsBool = Ty->hasBooleanRepresentation() ||
bool IsBool = (Ty->hasBooleanRepresentation() && !Ty->isVectorType()) ||
NSAPI(CGM.getContext()).isObjCBOOLType(Ty);
bool NeedsBoolCheck = HasBoolCheck && IsBool;
bool NeedsEnumCheck = HasEnumCheck && Ty->getAs<EnumType>();
Expand Down Expand Up @@ -2068,11 +2068,8 @@ llvm::Value *CodeGenFunction::EmitLoadOfScalar(Address Addr, bool Volatile,
/// by ConvertType) to its load/store type (as returned by
/// convertTypeForLoadStore).
llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) {
if (Ty->hasBooleanRepresentation() || Ty->isBitIntType()) {
llvm::Type *StoreTy = convertTypeForLoadStore(Ty, Value->getType());
bool Signed = Ty->isSignedIntegerOrEnumerationType();
return Builder.CreateIntCast(Value, StoreTy, Signed, "storedv");
}
if (auto *AtomicTy = Ty->getAs<AtomicType>())
Ty = AtomicTy->getValueType();

if (Ty->isExtVectorBoolType()) {
llvm::Type *StoreTy = convertTypeForLoadStore(Ty, Value->getType());
Expand All @@ -2088,13 +2085,22 @@ llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) {
Value = Builder.CreateBitCast(Value, StoreTy);
}

if (Ty->hasBooleanRepresentation() || Ty->isBitIntType()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is isExtVectorBoolType() guaranteed to filter out all the cases where a boolean vector type might get us here? It doesn't seem like the previous code had any sort of handling for vectors of bool other than the isExtVectorBoolType handling, but this seems like a potential change in behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll double check. I know there have been changes recently to support a different ABI in the HLSL case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Only ExtVectorType of booleans are supported, the regular VectorType disallow boolean element types (see Sema::BuildVectorType and Sema::BuildExtVectorType).
In the if (Ty->isExtVectorBoolType()) above both kind of vector bool ABIs are handled, so there is no issue there.

llvm::Type *StoreTy = convertTypeForLoadStore(Ty, Value->getType());
bool Signed = Ty->isSignedIntegerOrEnumerationType();
return Builder.CreateIntCast(Value, StoreTy, Signed, "storedv");
}

return Value;
}

/// Converts a scalar value from its load/store type (as returned
/// by convertTypeForLoadStore) to its primary IR type (as returned
/// by ConvertType).
llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In here it is fine to handle the vector of bool types whenTy->hasBooleanRepresentation() as only the non packed ABI will reach that code, and it is fine to generate a Trunc (just like in EmitToMemory a ZExt is generated)

if (auto *AtomicTy = Ty->getAs<AtomicType>())
Ty = AtomicTy->getValueType();

if (Ty->isPackedVectorBoolType(getContext())) {
const auto *RawIntTy = Value->getType();

Expand Down
Loading