================
@@ -2361,6 +2361,78 @@ static bool DiagnoseHLSLRegisterAttribute(Sema &S,
SourceLocation &ArgLoc,
return ValidateMultipleRegisterAnnotations(S, D, RegType);
}
+// return false if the slot count exceeds the limit, true otherwise
+static bool AccumulateHLSLResourceSlots(QualType Ty, uint64_t &SlotCount,
+ const uint64_t &Limit, ASTContext &Ctx,
+ uint64_t Multiplier = 1) {
+ Ty = Ty.getCanonicalType();
+ const Type *T = Ty.getTypePtr();
+
+ // Early exit if already overflowed
+ if (SlotCount > Limit)
+ return false;
+
+ // Case 1: array type
+ if (const auto *AT = dyn_cast<ArrayType>(T)) {
+ uint64_t Count = 1;
+
+ if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) {
+ Count = CAT->getSize().getZExtValue();
+ }
+
+ QualType ElemTy = AT->getElementType();
+ return AccumulateHLSLResourceSlots(ElemTy, SlotCount, Limit, Ctx,
+ Multiplier * Count);
+ }
+
+ // Case 2: resource leaf
+ if (T->isHLSLResourceRecord()) {
+
+ SlotCount += Multiplier;
+ return SlotCount <= Limit;
+ }
+
+ // Case 3: struct / record
+ if (const auto *RT = dyn_cast<RecordType>(T)) {
+ const RecordDecl *RD = RT->getDecl();
+ for (const FieldDecl *Field : RD->fields()) {
+ if (!AccumulateHLSLResourceSlots(Field->getType(), SlotCount, Limit, Ctx,
+ Multiplier))
+ return false;
+ }
+ return true;
+ }
+
+ // Case 4: everything else
+ return true;
+}
+
+// return true if there is something invalid, false otherwise
+static bool ValidateRegisterNumber(const StringRef SlotNumStr, Decl *TheDecl,
+ ASTContext &Ctx) {
+ uint64_t SlotNum;
+ if (SlotNumStr.getAsInteger(10, SlotNum))
+ return false;
+
+ uint64_t Limit = UINT32_MAX;
+ VarDecl *VD = dyn_cast<VarDecl>(TheDecl);
+ if (VD) {
----------------
inbelic wrote:
nit:
```suggestion
if (auto *VD = dyn_cast<VarDecl>(TheDecl)) {
```
https://github.com/llvm/llvm-project/pull/174028
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits