Skip to content

Commit 70e5b37

Browse files
committed
[SPIR-V] Support sizeof(vk::BufferPointer)
This commit enables sizeof operator support for vk::BufferPointer types. It updates SemaExpr to allow vk::BufferPointer in sizeof expressions, and modifies ASTContext to correctly report the size and alignment of vk::BufferPointer as equivalent to uint64_t. Fixes #7998
1 parent 5f29bff commit 70e5b37

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

tools/clang/lib/AST/ASTContext.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "clang/AST/Expr.h"
2525
#include "clang/AST/ExprCXX.h"
2626
#include "clang/AST/ExternalASTSource.h"
27+
#include "clang/AST/HlslTypes.h" // HLSL Change
2728
#include "clang/AST/Mangle.h"
2829
#include "clang/AST/MangleNumberingContext.h"
2930
#include "clang/AST/RecordLayout.h"
@@ -38,8 +39,8 @@
3839
#include "llvm/ADT/Triple.h"
3940
#include "llvm/Support/Capacity.h"
4041
#include "llvm/Support/MathExtras.h"
41-
#include "llvm/Support/raw_ostream.h"
4242
#include "llvm/Support/MathExtras.h" // HLSL Change
43+
#include "llvm/Support/raw_ostream.h"
4344
#include <map>
4445

4546
using namespace clang;
@@ -1804,6 +1805,17 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
18041805

18051806
const RecordType *RT = cast<RecordType>(TT);
18061807
const RecordDecl *RD = RT->getDecl();
1808+
// HLSL Change Begins
1809+
#ifdef ENABLE_SPIRV_CODEGEN
1810+
if (hlsl::IsVKBufferPointerType(QualType(T, 0))) {
1811+
TypeInfo Info = getTypeInfo(UnsignedLongLongTy);
1812+
Width = Info.Width;
1813+
Align = Info.Align;
1814+
AlignIsRequired = Info.AlignIsRequired;
1815+
break;
1816+
}
1817+
#endif
1818+
// HLSL Change Ends
18071819
const ASTRecordLayout &Layout = getASTRecordLayout(RD);
18081820
Width = toBits(Layout.getSize());
18091821
Align = toBits(Layout.getAlignment());

tools/clang/lib/Sema/SemaExpr.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#include "clang/AST/OperationKinds.h"
15-
#include "clang/Sema/SemaInternal.h"
1614
#include "TreeTransform.h"
1715
#include "clang/AST/ASTConsumer.h"
1816
#include "clang/AST/ASTContext.h"
@@ -25,6 +23,8 @@
2523
#include "clang/AST/Expr.h"
2624
#include "clang/AST/ExprCXX.h"
2725
#include "clang/AST/ExprObjC.h"
26+
#include "clang/AST/HlslTypes.h" // HLSL Change
27+
#include "clang/AST/OperationKinds.h"
2828
#include "clang/AST/RecursiveASTVisitor.h"
2929
#include "clang/AST/TypeLoc.h"
3030
#include "clang/Basic/PartialDiagnostic.h"
@@ -42,9 +42,10 @@
4242
#include "clang/Sema/Scope.h"
4343
#include "clang/Sema/ScopeInfo.h"
4444
#include "clang/Sema/SemaFixItUtils.h"
45+
#include "clang/Sema/SemaHLSL.h" // HLSL Change
46+
#include "clang/Sema/SemaInternal.h"
4547
#include "clang/Sema/Template.h"
4648
#include "llvm/Support/ConvertUTF.h"
47-
#include "clang/Sema/SemaHLSL.h" // HLSL Change
4849
using namespace clang;
4950
using namespace sema;
5051

@@ -3820,6 +3821,13 @@ bool Sema::CheckHLSLUnaryExprOrTypeTraitOperand(QualType ExprType,
38203821
return true;
38213822
}
38223823

3824+
// vk::BufferPointer is allowed in sizeof
3825+
#ifdef ENABLE_SPIRV_CODEGEN
3826+
if (hlsl::IsVKBufferPointerType(ExprType)) {
3827+
return false;
3828+
}
3829+
#endif
3830+
38233831
if (!hlsl::IsHLSLNumericOrAggregateOfNumericType(ExprType)) {
38243832
Diag(Loc, diag::err_hlsl_sizeof_nonnumeric) << ExprType;
38253833
return true;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %dxc -T cs_6_0 -E main -fcgl %s -spirv | FileCheck %s
2+
3+
struct S {
4+
float a;
5+
};
6+
7+
[numthreads(1, 1, 1)]
8+
void main() {
9+
vk::BufferPointer<S> p = vk::BufferPointer<S>(0);
10+
// CHECK: %uint_8 = OpConstant %uint 8
11+
// CHECK: OpStore %size %uint_8
12+
uint size = sizeof(p);
13+
}

0 commit comments

Comments
 (0)