Skip to content

Commit 4f235c0

Browse files
committed
Add codegen for existing resource types and make HLSLAttributedResourceType canonical.
1 parent f49344e commit 4f235c0

27 files changed

+290
-83
lines changed

clang/include/clang/AST/Type.h

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,6 +2659,7 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
26592659
#include "clang/Basic/HLSLIntangibleTypes.def"
26602660
bool isHLSLSpecificType() const; // Any HLSL specific type
26612661
bool isHLSLIntangibleType() const; // Any HLSL intangible type
2662+
bool isHLSLAttributedResourceType() const;
26622663

26632664
/// Determines if this type, which must satisfy
26642665
/// isObjCLifetimeType(), is implicitly __unsafe_unretained rather
@@ -6180,6 +6181,14 @@ class HLSLAttributedResourceType : public Type, public llvm::FoldingSetNode {
61806181
: ResourceClass(ResourceClass), IsROV(IsROV), RawBuffer(RawBuffer) {}
61816182

61826183
Attributes() : Attributes(llvm::dxil::ResourceClass::UAV, false, false) {}
6184+
6185+
friend bool operator==(const Attributes &LHS, const Attributes &RHS) {
6186+
return std::tie(LHS.ResourceClass, LHS.IsROV, LHS.RawBuffer) ==
6187+
std::tie(RHS.ResourceClass, RHS.IsROV, RHS.RawBuffer);
6188+
}
6189+
friend bool operator!=(const Attributes &LHS, const Attributes &RHS) {
6190+
return !(LHS == RHS);
6191+
}
61836192
};
61846193

61856194
private:
@@ -6189,18 +6198,19 @@ class HLSLAttributedResourceType : public Type, public llvm::FoldingSetNode {
61896198
QualType ContainedType;
61906199
const Attributes Attrs;
61916200

6192-
HLSLAttributedResourceType(QualType Canon, QualType Wrapped,
6193-
QualType Contained, const Attributes &Attrs)
6194-
: Type(HLSLAttributedResource, Canon, Wrapped->getDependence()),
6201+
HLSLAttributedResourceType(QualType Wrapped, QualType Contained,
6202+
const Attributes &Attrs)
6203+
: Type(HLSLAttributedResource, QualType(), Wrapped->getDependence()),
61956204
WrappedType(Wrapped), ContainedType(Contained), Attrs(Attrs) {}
61966205

61976206
public:
61986207
QualType getWrappedType() const { return WrappedType; }
61996208
QualType getContainedType() const { return ContainedType; }
6209+
bool hasContainedType() const { return !ContainedType.isNull(); }
62006210
const Attributes &getAttrs() const { return Attrs; }
62016211

6202-
bool isSugared() const { return true; }
6203-
QualType desugar() const { return getWrappedType(); }
6212+
bool isSugared() const { return false; }
6213+
QualType desugar() const { return QualType(this, 0); }
62046214

62056215
void Profile(llvm::FoldingSetNodeID &ID) {
62066216
Profile(ID, WrappedType, ContainedType, Attrs);
@@ -8344,17 +8354,19 @@ inline bool Type::isOpenCLSpecificType() const {
83448354
}
83458355
#include "clang/Basic/HLSLIntangibleTypes.def"
83468356

8347-
inline bool Type::isHLSLSpecificType() const {
8357+
inline bool Type::isHLSLIntangibleType() const {
83488358
#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) is##Id##Type() ||
83498359
return
83508360
#include "clang/Basic/HLSLIntangibleTypes.def"
8351-
false; // end boolean or operation
8361+
isHLSLAttributedResourceType();
83528362
}
83538363

8354-
inline bool Type::isHLSLIntangibleType() const {
8355-
// All HLSL specific types are currently intangible type as well, but that
8356-
// might change in the future.
8357-
return isHLSLSpecificType();
8364+
inline bool Type::isHLSLSpecificType() const {
8365+
return isHLSLIntangibleType() || isa<HLSLAttributedResourceType>(this);
8366+
}
8367+
8368+
inline bool Type::isHLSLAttributedResourceType() const {
8369+
return isa<HLSLAttributedResourceType>(this);
83588370
}
83598371

83608372
inline bool Type::isTemplateTypeParmType() const {

clang/include/clang/Basic/TypeNodes.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def EnumType : TypeNode<TagType>, LeafType;
9393
def ElaboratedType : TypeNode<Type>, NeverCanonical;
9494
def AttributedType : TypeNode<Type>, NeverCanonical;
9595
def BTFTagAttributedType : TypeNode<Type>, NeverCanonical;
96-
def HLSLAttributedResourceType : TypeNode<Type>, NeverCanonical;
96+
def HLSLAttributedResourceType : TypeNode<Type>;
9797
def TemplateTypeParmType : TypeNode<Type>, AlwaysDependent, LeafType;
9898
def SubstTemplateTypeParmType : TypeNode<Type>, NeverCanonical;
9999
def SubstTemplateTypeParmPackType : TypeNode<Type>, AlwaysDependent;

clang/lib/AST/ASTContext.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3437,6 +3437,9 @@ static void encodeTypeForFunctionPointerAuth(const ASTContext &Ctx,
34373437
OS << II->getLength() << II->getName();
34383438
return;
34393439
}
3440+
case Type::HLSLAttributedResource:
3441+
llvm_unreachable("not yet implemented");
3442+
break;
34403443
case Type::DeducedTemplateSpecialization:
34413444
case Type::Auto:
34423445
#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
@@ -4108,6 +4111,7 @@ QualType ASTContext::getVariableArrayDecayedType(QualType type) const {
41084111
case Type::BitInt:
41094112
case Type::DependentBitInt:
41104113
case Type::ArrayParameter:
4114+
case Type::HLSLAttributedResource:
41114115
llvm_unreachable("type should never be variably-modified");
41124116

41134117
// These types can be variably-modified but should never need to
@@ -5233,9 +5237,8 @@ QualType ASTContext::getHLSLAttributedResourceType(
52335237
if (Ty)
52345238
return QualType(Ty, 0);
52355239

5236-
QualType Canon = getCanonicalType(Wrapped);
52375240
Ty = new (*this, alignof(HLSLAttributedResourceType))
5238-
HLSLAttributedResourceType(Canon, Wrapped, Contained, Attrs);
5241+
HLSLAttributedResourceType(Wrapped, Contained, Attrs);
52395242

52405243
Types.push_back(Ty);
52415244
HLSLAttributedResourceTypes.InsertNode(Ty, InsertPos);
@@ -9106,6 +9109,9 @@ void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string &S,
91069109
case Type::DeducedTemplateSpecialization:
91079110
return;
91089111

9112+
case Type::HLSLAttributedResource:
9113+
llvm_unreachable("unexpected type");
9114+
91099115
case Type::ArrayParameter:
91109116
case Type::Pipe:
91119117
#define ABSTRACT_TYPE(KIND, BASE)
@@ -11533,6 +11539,18 @@ QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, bool OfBlockPointer,
1153311539
return {};
1153411540
return LHS;
1153511541
}
11542+
case Type::HLSLAttributedResource: {
11543+
const HLSLAttributedResourceType *LHSTy =
11544+
LHS->castAs<HLSLAttributedResourceType>();
11545+
const HLSLAttributedResourceType *RHSTy =
11546+
RHS->castAs<HLSLAttributedResourceType>();
11547+
11548+
if (LHSTy->getWrappedType() == RHSTy->getWrappedType() &&
11549+
LHSTy->getContainedType() == RHSTy->getContainedType() &&
11550+
LHSTy->getAttrs() == RHSTy->getAttrs())
11551+
return LHS;
11552+
return {};
11553+
}
1153611554
}
1153711555

1153811556
llvm_unreachable("Invalid Type::Class!");
@@ -13672,6 +13690,9 @@ static QualType getCommonNonSugarTypeNode(ASTContext &Ctx, const Type *X,
1367213690
TX->getDepth(), TX->getIndex(), TX->isParameterPack(),
1367313691
getCommonDecl(TX->getDecl(), TY->getDecl()));
1367413692
}
13693+
case Type::HLSLAttributedResource: {
13694+
llvm_unreachable("not yet implemented");
13695+
}
1367513696
}
1367613697
llvm_unreachable("Unknown Type Class");
1367713698
}

clang/lib/AST/ASTStructuralEquivalence.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -802,16 +802,6 @@ static bool IsEquivalentExceptionSpec(StructuralEquivalenceContext &Context,
802802
return true;
803803
}
804804

805-
// Determine structural equivalence of two instances of
806-
// HLSLAttributedResourceType::Attributes
807-
static bool
808-
IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
809-
const HLSLAttributedResourceType::Attributes &Attrs1,
810-
const HLSLAttributedResourceType::Attributes &Attrs2) {
811-
return std::tie(Attrs1.ResourceClass, Attrs1.IsROV, Attrs1.RawBuffer) ==
812-
std::tie(Attrs2.ResourceClass, Attrs2.IsROV, Attrs2.RawBuffer);
813-
}
814-
815805
/// Determine structural equivalence of two types.
816806
static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
817807
QualType T1, QualType T2) {
@@ -1115,9 +1105,8 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
11151105
Context, cast<HLSLAttributedResourceType>(T1)->getContainedType(),
11161106
cast<HLSLAttributedResourceType>(T2)->getContainedType()))
11171107
return false;
1118-
if (!IsStructurallyEquivalent(
1119-
Context, cast<HLSLAttributedResourceType>(T1)->getAttrs(),
1120-
cast<HLSLAttributedResourceType>(T2)->getAttrs()))
1108+
if (cast<HLSLAttributedResourceType>(T1)->getAttrs() !=
1109+
cast<HLSLAttributedResourceType>(T2)->getAttrs())
11211110
return false;
11221111
break;
11231112

clang/lib/AST/DeclCXX.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,10 +1411,10 @@ void CXXRecordDecl::addedMember(Decl *D) {
14111411
Ty = Ty->getArrayElementTypeNoTypeQual();
14121412

14131413
Ty = Ty->getUnqualifiedDesugaredType();
1414-
if (Ty->isBuiltinType())
1415-
data().IsHLSLIntangible |= Ty->isHLSLIntangibleType();
1416-
else if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1414+
if (const RecordType *RT = dyn_cast<RecordType>(Ty))
14171415
data().IsHLSLIntangible |= RT->getAsCXXRecordDecl()->isHLSLIntangible();
1416+
else
1417+
data().IsHLSLIntangible |= Ty->isHLSLIntangibleType();
14181418
}
14191419
}
14201420

clang/lib/AST/ExprConstant.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12163,6 +12163,7 @@ GCCTypeClass EvaluateBuiltinClassifyType(QualType T,
1216312163
case Type::ObjCInterface:
1216412164
case Type::ObjCObjectPointer:
1216512165
case Type::Pipe:
12166+
case Type::HLSLAttributedResource:
1216612167
// Classify all other types that don't fit into the regular
1216712168
// classification the same way.
1216812169
return GCCTypeClass::None;

clang/lib/AST/ItaniumMangle.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4488,6 +4488,30 @@ void CXXNameMangler::mangleType(const ArrayParameterType *T) {
44884488
mangleType(cast<ConstantArrayType>(T));
44894489
}
44904490

4491+
void CXXNameMangler::mangleType(const HLSLAttributedResourceType *T) {
4492+
mangleType(T->getWrappedType());
4493+
const HLSLAttributedResourceType::Attributes &Attrs = T->getAttrs();
4494+
switch (Attrs.ResourceClass) {
4495+
case llvm::dxil::ResourceClass::UAV:
4496+
Out << 'U';
4497+
break;
4498+
case llvm::dxil::ResourceClass::SRV:
4499+
Out << 'T';
4500+
break;
4501+
case llvm::dxil::ResourceClass::CBuffer:
4502+
Out << 'C';
4503+
break;
4504+
case llvm::dxil::ResourceClass::Sampler:
4505+
Out << 'S';
4506+
break;
4507+
}
4508+
mangleNumber(Attrs.IsROV);
4509+
mangleNumber(Attrs.RawBuffer);
4510+
4511+
if (!T->hasContainedType())
4512+
mangleType(T->getContainedType());
4513+
}
4514+
44914515
void CXXNameMangler::mangleIntegerLiteral(QualType T,
44924516
const llvm::APSInt &Value) {
44934517
// <expr-primary> ::= L <type> <value number> E # integer literal

clang/lib/AST/MicrosoftMangle.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "llvm/ADT/SmallVector.h"
3333
#include "llvm/ADT/StringExtras.h"
3434
#include "llvm/Support/CRC.h"
35+
#include "llvm/Support/DXILABI.h"
3536
#include "llvm/Support/MD5.h"
3637
#include "llvm/Support/MathExtras.h"
3738
#include "llvm/Support/StringSaver.h"
@@ -3753,6 +3754,31 @@ void MicrosoftCXXNameMangler::mangleType(const DependentBitIntType *T,
37533754
Error(Range.getBegin(), "DependentBitInt type") << Range;
37543755
}
37553756

3757+
void MicrosoftCXXNameMangler::mangleType(const HLSLAttributedResourceType *T,
3758+
Qualifiers, SourceRange Range) {
3759+
mangleType(T->getWrappedType(), SourceRange(), QMM_Escape);
3760+
const HLSLAttributedResourceType::Attributes &Attrs = T->getAttrs();
3761+
switch (Attrs.ResourceClass) {
3762+
case llvm::dxil::ResourceClass::UAV:
3763+
Out << 'U';
3764+
break;
3765+
case llvm::dxil::ResourceClass::SRV:
3766+
Out << 'T';
3767+
break;
3768+
case llvm::dxil::ResourceClass::CBuffer:
3769+
Out << 'C';
3770+
break;
3771+
case llvm::dxil::ResourceClass::Sampler:
3772+
Out << 'S';
3773+
break;
3774+
}
3775+
mangleNumber(Attrs.IsROV);
3776+
mangleNumber(Attrs.RawBuffer);
3777+
3778+
if (T->hasContainedType())
3779+
mangleType(T->getContainedType(), SourceRange(), QMM_Escape);
3780+
}
3781+
37563782
// <this-adjustment> ::= <no-adjustment> | <static-adjustment> |
37573783
// <virtual-adjustment>
37583784
// <no-adjustment> ::= A # private near

clang/lib/AST/Type.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4575,6 +4575,8 @@ static CachedProperties computeCachedProperties(const Type *T) {
45754575
return Cache::get(cast<AtomicType>(T)->getValueType());
45764576
case Type::Pipe:
45774577
return Cache::get(cast<PipeType>(T)->getElementType());
4578+
case Type::HLSLAttributedResource:
4579+
return Cache::get(cast<HLSLAttributedResourceType>(T)->getWrappedType());
45784580
}
45794581

45804582
llvm_unreachable("unhandled type class");
@@ -4664,6 +4666,8 @@ LinkageInfo LinkageComputer::computeTypeLinkageInfo(const Type *T) {
46644666
return computeTypeLinkageInfo(cast<AtomicType>(T)->getValueType());
46654667
case Type::Pipe:
46664668
return computeTypeLinkageInfo(cast<PipeType>(T)->getElementType());
4669+
case Type::HLSLAttributedResource:
4670+
llvm_unreachable("not yet implemented");
46674671
}
46684672

46694673
llvm_unreachable("unhandled type class");
@@ -4846,6 +4850,7 @@ bool Type::canHaveNullability(bool ResultIfUnknown) const {
48464850
case Type::BitInt:
48474851
case Type::DependentBitInt:
48484852
case Type::ArrayParameter:
4853+
case Type::HLSLAttributedResource:
48494854
return false;
48504855
}
48514856
llvm_unreachable("bad type kind!");

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ TypeEvaluationKind CodeGenFunction::getEvaluationKind(QualType type) {
296296
case Type::ObjCObject:
297297
case Type::ObjCInterface:
298298
case Type::ArrayParameter:
299+
case Type::HLSLAttributedResource:
299300
return TEK_Aggregate;
300301

301302
// We operate on atomic values according to their underlying type.

0 commit comments

Comments
 (0)