Skip to content

Commit 37a9232

Browse files
committed
[spirv] Implements vk::BufferPointer proposal
(https://github.com/microsoft/hlsl-specs/blob/main/proposals/0010-vk-buffer-ref.md) Addresses issue #6489.
1 parent 92fcae2 commit 37a9232

36 files changed

Lines changed: 1152 additions & 189 deletions

include/dxc/HlslIntrinsicOp.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,13 @@ enum class IntrinsicOp {
241241
#ifdef ENABLE_SPIRV_CODEGEN
242242
IOP_Vkext_execution_mode_id,
243243
#endif // ENABLE_SPIRV_CODEGEN
244+
#ifdef ENABLE_SPIRV_CODEGEN
245+
IOP_Vkreinterpret_pointer_cast,
246+
#endif // ENABLE_SPIRV_CODEGEN
247+
#ifdef ENABLE_SPIRV_CODEGEN
248+
IOP_Vkstatic_pointer_cast,
249+
#endif // ENABLE_SPIRV_CODEGEN
250+
MOP_GetBufferContents,
244251
MOP_Append,
245252
MOP_RestartStrip,
246253
MOP_CalculateLevelOfDetail,

include/dxc/dxcapi.internal.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
// //
88
// Provides non-public declarations for the DirectX Compiler component. //
99
// //
10+
// Modifications Copyright(C) 2025 Advanced Micro Devices, Inc. //
11+
// All rights reserved. //
12+
// //
1013
///////////////////////////////////////////////////////////////////////////////
1114

1215
#ifndef __DXC_API_INTERNAL__
@@ -35,6 +38,7 @@ typedef struct ID3D10Blob ID3D10Blob;
3538
static const BYTE INTRIN_TEMPLATE_FROM_TYPE = 0xff;
3639
static const BYTE INTRIN_TEMPLATE_VARARGS = 0xfe;
3740
static const BYTE INTRIN_TEMPLATE_FROM_FUNCTION = 0xfd;
41+
static const BYTE INTRIN_TEMPLATE_FROM_FUNCTION_2 = 0xfc;
3842

3943
// Use this enumeration to describe allowed templates (layouts) in intrinsics.
4044
enum LEGAL_INTRINSIC_TEMPLATES {
@@ -125,8 +129,9 @@ enum LEGAL_INTRINSIC_COMPTYPES {
125129
LICOMPTYPE_ANY_NODE_OUTPUT_RECORD = 48,
126130
LICOMPTYPE_GROUP_NODE_OUTPUT_RECORDS = 49,
127131
LICOMPTYPE_THREAD_NODE_OUTPUT_RECORDS = 50,
132+
LICOMPTYPE_VK_BUFFER_POINTER = 51,
128133

129-
LICOMPTYPE_COUNT = 51
134+
LICOMPTYPE_COUNT = 52
130135
};
131136

132137
static const BYTE IA_SPECIAL_BASE = 0xf0;
@@ -138,7 +143,7 @@ static const BYTE IA_SPECIAL_SLOTS = 4;
138143

139144
struct HLSL_INTRINSIC_ARGUMENT {
140145
LPCSTR
141-
pName; // Name of the argument; the first argument has the function name.
146+
pName; // Name of the argument; the first argument has the function name.
142147
UINT64 qwUsage; // A combination of
143148
// AR_QUAL_IN|AR_QUAL_OUT|AR_QUAL_COLMAJOR|AR_QUAL_ROWMAJOR in
144149
// parameter tables; other values possible elsewhere.

lib/HLSL/HLOperationLower.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
// //
88
// Lower functions to lower HL operations to DXIL operations. //
99
// //
10+
// Modifications Copyright(C) 2025 Advanced Micro Devices, Inc. //
11+
// All rights reserved. //
12+
// //
1013
///////////////////////////////////////////////////////////////////////////////
1114

1215
#define _USE_MATH_DEFINES
@@ -6532,6 +6535,12 @@ IntrinsicLower gLowerTable[] = {
65326535
DXIL::OpCode::NumOpCodes},
65336536
{IntrinsicOp::IOP_Vkext_execution_mode_id, UnsupportedVulkanIntrinsic,
65346537
DXIL::OpCode::NumOpCodes},
6538+
{IntrinsicOp::IOP_Vkstatic_pointer_cast, UnsupportedVulkanIntrinsic,
6539+
DXIL::OpCode::NumOpCodes},
6540+
{IntrinsicOp::IOP_Vkreinterpret_pointer_cast, UnsupportedVulkanIntrinsic,
6541+
DXIL::OpCode::NumOpCodes},
6542+
{IntrinsicOp::MOP_GetBufferContents, UnsupportedVulkanIntrinsic,
6543+
DXIL::OpCode::NumOpCodes},
65356544
#endif // ENABLE_SPIRV_CODEGEN
65366545
{IntrinsicOp::MOP_Append, StreamOutputLower, DXIL::OpCode::EmitStream},
65376546
{IntrinsicOp::MOP_RestartStrip, StreamOutputLower, DXIL::OpCode::CutStream},

tools/clang/include/clang/AST/HlslTypes.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
// This file is distributed under the University of Illinois Open Source //
77
// License. See LICENSE.TXT for details. //
88
// //
9+
// Modifications Copyright(C) 2025 Advanced Micro Devices, Inc. //
10+
// All rights reserved. //
11+
// //
912
///
1013
/// \file //
1114
/// \brief Defines the HLSL type system interface. //
@@ -31,6 +34,7 @@
3134
namespace clang {
3235
class ASTContext;
3336
class AttributeList;
37+
class CXXConstructorDecl;
3438
class CXXMethodDecl;
3539
class CXXRecordDecl;
3640
class ClassTemplateDecl;
@@ -400,6 +404,10 @@ DeclareNodeOrRecordType(clang::ASTContext &Ctx, DXIL::NodeIOKind Type,
400404
bool IsCompleteType = false);
401405

402406
#ifdef ENABLE_SPIRV_CODEGEN
407+
clang::CXXRecordDecl *
408+
DeclareVkBufferPointerType(clang::ASTContext &context,
409+
clang::DeclContext *declContext);
410+
403411
clang::CXXRecordDecl *DeclareInlineSpirvType(clang::ASTContext &context,
404412
clang::DeclContext *declContext,
405413
llvm::StringRef typeName,
@@ -425,7 +433,7 @@ clang::VarDecl *DeclareBuiltinGlobal(llvm::StringRef name, clang::QualType Ty,
425433
/// method.</summary> <param name="context">AST context in which to
426434
/// work.</param> <param name="recordDecl">Class in which the function template
427435
/// is declared.</param> <param name="functionDecl">Function for which a
428-
/// template is created.</params> <param
436+
/// template is created.</param> <param
429437
/// name="templateParamNamedDecls">Declarations for templates to the
430438
/// function.</param> <param name="templateParamNamedDeclsCount">Count of
431439
/// template declarations.</param> <returns>A new function template declaration
@@ -530,6 +538,29 @@ bool DoesTypeDefineOverloadedOperator(clang::QualType typeWithOperator,
530538
clang::QualType paramType);
531539
bool IsPatchConstantFunctionDecl(const clang::FunctionDecl *FD);
532540

541+
#ifdef ENABLE_SPIRV_CODEGEN
542+
bool IsVKBufferPointerType(clang::QualType type);
543+
clang::QualType GetVKBufferPointerBufferType(clang::QualType type);
544+
unsigned GetVKBufferPointerAlignment(clang::QualType type);
545+
#endif
546+
547+
/// <summary>Adds a constructor declaration to the specified class
548+
/// record.</summary> <param name="context">ASTContext that owns
549+
/// declarations.</param> <param name="recordDecl">Record declaration in which
550+
/// to add constructor.</param> <param name="resultType">Result type for
551+
/// constructor.</param> <param name="paramTypes">Types for constructor
552+
/// parameters.</param> <param name="paramNames">Names for constructor
553+
/// parameters.</param> <param name="declarationName">Name for
554+
/// constructor.</param> <param name="isConst">Whether the constructor is a
555+
/// const function.</param> <returns>The method declaration for the
556+
/// constructor.</returns>
557+
clang::CXXConstructorDecl *CreateConstructorDeclarationWithParams(
558+
clang::ASTContext &context, clang::CXXRecordDecl *recordDecl,
559+
clang::QualType resultType, llvm::ArrayRef<clang::QualType> paramTypes,
560+
llvm::ArrayRef<clang::StringRef> paramNames,
561+
clang::DeclarationName declarationName, bool isConst,
562+
bool isTemplateFunction = false);
563+
533564
/// <summary>Adds a function declaration to the specified class
534565
/// record.</summary> <param name="context">ASTContext that owns
535566
/// declarations.</param> <param name="recordDecl">Record declaration in which

tools/clang/include/clang/AST/OperationKinds.h

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
// This file is distributed under the University of Illinois Open Source
66
// License. See LICENSE.TXT for details.
77
//
8+
// Modifications Copyright(C) 2025 Advanced Micro Devices, Inc.
9+
// All rights reserved.
10+
//
811
//===----------------------------------------------------------------------===//
912
//
1013
// This file enumerates the different kinds of operations that can be
@@ -18,7 +21,7 @@
1821
#include <limits> // HLSL Change
1922

2023
namespace clang {
21-
24+
2225
/// CastKind - The kind of operation required for a conversion.
2326
enum CastKind {
2427
/// CK_Dependent - A conversion which cannot yet be analyzed because
@@ -321,6 +324,8 @@ enum CastKind {
321324
CK_HLSLCC_FloatingToIntegral,
322325
CK_HLSLCC_FloatingToBoolean,
323326
CK_HLSLCC_FloatingCast,
327+
CK_VK_BufferPointerToIntegral,
328+
CK_VK_IntegralToBufferPointer,
324329

325330
// HLSL Change - Made CK_Invalid an enum case because otherwise it is UB to
326331
// assign it to a value of CastKind.
@@ -335,35 +340,55 @@ static_assert(
335340
enum BinaryOperatorKind {
336341
// Operators listed in order of precedence.
337342
// Note that additions to this should also update the StmtVisitor class.
338-
BO_PtrMemD, BO_PtrMemI, // [C++ 5.5] Pointer-to-member operators.
339-
BO_Mul, BO_Div, BO_Rem, // [C99 6.5.5] Multiplicative operators.
340-
BO_Add, BO_Sub, // [C99 6.5.6] Additive operators.
341-
BO_Shl, BO_Shr, // [C99 6.5.7] Bitwise shift operators.
342-
BO_LT, BO_GT, BO_LE, BO_GE, // [C99 6.5.8] Relational operators.
343-
BO_EQ, BO_NE, // [C99 6.5.9] Equality operators.
344-
BO_And, // [C99 6.5.10] Bitwise AND operator.
345-
BO_Xor, // [C99 6.5.11] Bitwise XOR operator.
346-
BO_Or, // [C99 6.5.12] Bitwise OR operator.
347-
BO_LAnd, // [C99 6.5.13] Logical AND operator.
348-
BO_LOr, // [C99 6.5.14] Logical OR operator.
349-
BO_Assign, BO_MulAssign, // [C99 6.5.16] Assignment operators.
350-
BO_DivAssign, BO_RemAssign,
351-
BO_AddAssign, BO_SubAssign,
352-
BO_ShlAssign, BO_ShrAssign,
353-
BO_AndAssign, BO_XorAssign,
343+
BO_PtrMemD,
344+
BO_PtrMemI, // [C++ 5.5] Pointer-to-member operators.
345+
BO_Mul,
346+
BO_Div,
347+
BO_Rem, // [C99 6.5.5] Multiplicative operators.
348+
BO_Add,
349+
BO_Sub, // [C99 6.5.6] Additive operators.
350+
BO_Shl,
351+
BO_Shr, // [C99 6.5.7] Bitwise shift operators.
352+
BO_LT,
353+
BO_GT,
354+
BO_LE,
355+
BO_GE, // [C99 6.5.8] Relational operators.
356+
BO_EQ,
357+
BO_NE, // [C99 6.5.9] Equality operators.
358+
BO_And, // [C99 6.5.10] Bitwise AND operator.
359+
BO_Xor, // [C99 6.5.11] Bitwise XOR operator.
360+
BO_Or, // [C99 6.5.12] Bitwise OR operator.
361+
BO_LAnd, // [C99 6.5.13] Logical AND operator.
362+
BO_LOr, // [C99 6.5.14] Logical OR operator.
363+
BO_Assign,
364+
BO_MulAssign, // [C99 6.5.16] Assignment operators.
365+
BO_DivAssign,
366+
BO_RemAssign,
367+
BO_AddAssign,
368+
BO_SubAssign,
369+
BO_ShlAssign,
370+
BO_ShrAssign,
371+
BO_AndAssign,
372+
BO_XorAssign,
354373
BO_OrAssign,
355-
BO_Comma // [C99 6.5.17] Comma operator.
374+
BO_Comma // [C99 6.5.17] Comma operator.
356375
};
357376

358377
enum UnaryOperatorKind {
359378
// Note that additions to this should also update the StmtVisitor class.
360-
UO_PostInc, UO_PostDec, // [C99 6.5.2.4] Postfix increment and decrement
361-
UO_PreInc, UO_PreDec, // [C99 6.5.3.1] Prefix increment and decrement
362-
UO_AddrOf, UO_Deref, // [C99 6.5.3.2] Address and indirection
363-
UO_Plus, UO_Minus, // [C99 6.5.3.3] Unary arithmetic
364-
UO_Not, UO_LNot, // [C99 6.5.3.3] Unary arithmetic
365-
UO_Real, UO_Imag, // "__real expr"/"__imag expr" Extension.
366-
UO_Extension // __extension__ marker.
379+
UO_PostInc,
380+
UO_PostDec, // [C99 6.5.2.4] Postfix increment and decrement
381+
UO_PreInc,
382+
UO_PreDec, // [C99 6.5.3.1] Prefix increment and decrement
383+
UO_AddrOf,
384+
UO_Deref, // [C99 6.5.3.2] Address and indirection
385+
UO_Plus,
386+
UO_Minus, // [C99 6.5.3.3] Unary arithmetic
387+
UO_Not,
388+
UO_LNot, // [C99 6.5.3.3] Unary arithmetic
389+
UO_Real,
390+
UO_Imag, // "__real expr"/"__imag expr" Extension.
391+
UO_Extension // __extension__ marker.
367392
};
368393

369394
/// \brief The kind of bridging performed by the Objective-C bridge cast.
@@ -379,6 +404,6 @@ enum ObjCBridgeCastKind {
379404
OBC_BridgeRetained
380405
};
381406

382-
}
407+
} // namespace clang
383408

384409
#endif

tools/clang/include/clang/Basic/Attr.td

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
// This file is distributed under the University of Illinois Open Source
66
// License. See LICENSE.TXT for details.
77
//
8+
// Modifications Copyright(C) 2025 Advanced Micro Devices, Inc.
9+
// All rights reserved.
10+
//
811
//===----------------------------------------------------------------------===//
912

1013
class DocumentationCategory<string name> {
@@ -1386,6 +1389,20 @@ def VKStorageClassExt : InheritableAttr {
13861389
let Documentation = [Undocumented];
13871390
}
13881391

1392+
def VKBufferPointer : InheritableAttr {
1393+
let Spellings = [CXX11<"", "hlsl_vk_buffer_pointer", 2021>];
1394+
let LangOpts = [SPIRV];
1395+
let Documentation = [Undocumented];
1396+
}
1397+
1398+
def VKAliasedPointer : InheritableAttr {
1399+
let Spellings = [CXX11<"vk", "aliased_pointer">];
1400+
let Subjects = SubjectList<[Var, ParmVar], ErrorDiag>;
1401+
let Args = [];
1402+
let LangOpts = [SPIRV];
1403+
let Documentation = [Undocumented];
1404+
}
1405+
13891406
// Global variables that are of struct type
13901407
def StructGlobalVar : SubsetSubject<Var, [{S->hasGlobalStorage() && S->getType()->isStructureType()}]>;
13911408

tools/clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
// This file is distributed under the University of Illinois Open Source
66
// License. See LICENSE.TXT for details.
77
//
8+
// Modifications Copyright(C) 2025 Advanced Micro Devices, Inc.
9+
// All rights reserved.
10+
//
811
//===----------------------------------------------------------------------===//
912

1013
//===----------------------------------------------------------------------===//
@@ -7820,7 +7823,7 @@ def warn_hlsl_intrinsic_in_wrong_shader_model : Warning<
78207823
"intrinsic %0 potentially used by '%1' requires shader model %2 or greater">,
78217824
DefaultError, InGroup<HLSLAvailability>;
78227825
def warn_hlsl_intrinsic_overload_in_wrong_shader_model : Warning<
7823-
"overload of intrinsic %0 requires shader model %1 or greater">,
7826+
"overload of intrinsic %0 requires shader model %1 or greater">,
78247827
DefaultError, InGroup<HLSLAvailability>;
78257828
def err_hlsl_intrinsic_template_arg_unsupported: Error<
78267829
"Explicit template arguments on intrinsic %0 are not supported">;
@@ -7972,6 +7975,10 @@ def err_hlsl_unsupported_semantic_index: Error<
79727975

79737976
// SPIRV Change Starts
79747977
def err_hlsl_vulkan_specific_feature: Error<"%0 is a Vulkan specific feature">;
7978+
def err_hlsl_vk_pointer_cast_alignment: Error<
7979+
"Vulkan buffer pointer cannot be cast to greater alignment">;
7980+
def err_hlsl_vk_static_pointer_cast_type: Error<
7981+
"vk::static_pointer_cast() content type must be base class of argument's content type">;
79757982
// SPIRV Change Ends
79767983

79777984
let CategoryName = "OpenMP Issue" in {

tools/clang/include/clang/SPIRV/SpirvBuilder.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
// This file is distributed under the University of Illinois Open Source
66
// License. See LICENSE.TXT for details.
77
//
8+
// Modifications Copyright(C) 2025 Advanced Micro Devices, Inc.
9+
// All rights reserved.
10+
//
811
//===----------------------------------------------------------------------===//
912
#ifndef LLVM_CLANG_SPIRV_SPIRVBUILDER_H
1013
#define LLVM_CLANG_SPIRV_SPIRVBUILDER_H
@@ -273,6 +276,14 @@ class SpirvBuilder {
273276
SpirvInstruction *sample,
274277
SourceLocation);
275278

279+
/// \brief Creates an OpConverPtrToU SPIR-V instruction with the given
280+
/// parameters.
281+
SpirvConvertPtrToU *createConvertPtrToU(SpirvInstruction *ptr, QualType type);
282+
283+
/// \brief Creates an OpConverUToPtr SPIR-V instruction with the given
284+
/// parameters.
285+
SpirvConvertUToPtr *createConvertUToPtr(SpirvInstruction *val, QualType type);
286+
276287
/// \brief Creates SPIR-V instructions for sampling the given image.
277288
///
278289
/// If compareVal is given a non-zero value, *Dref* variants of OpImageSample*

tools/clang/include/clang/SPIRV/SpirvContext.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
// This file is distributed under the University of Illinois Open Source
66
// License. See LICENSE.TXT for details.
77
//
8+
// Modifications Copyright(C) 2025 Advanced Micro Devices, Inc.
9+
// All rights reserved.
10+
//
811
//===----------------------------------------------------------------------===//
912
#ifndef LLVM_CLANG_SPIRV_SPIRVCONTEXT_H
1013
#define LLVM_CLANG_SPIRV_SPIRVCONTEXT_H
@@ -317,6 +320,13 @@ class SpirvContext {
317320

318321
const HybridPointerType *getPointerType(QualType pointee, spv::StorageClass);
319322

323+
const ForwardPointerType *getForwardPointerType(QualType pointee);
324+
325+
const SpirvPointerType *getForwardReference(QualType type);
326+
327+
void registerForwardReference(QualType type,
328+
const SpirvPointerType *pointerType);
329+
320330
/// Generates (or reuses an existing) OpString for the given string literal.
321331
SpirvString *getSpirvString(llvm::StringRef str);
322332

@@ -478,6 +488,8 @@ class SpirvContext {
478488
llvm::SmallVector<const HybridStructType *, 8> hybridStructTypes;
479489
llvm::DenseMap<const SpirvType *, SCToPtrTyMap> pointerTypes;
480490
llvm::SmallVector<const HybridPointerType *, 8> hybridPointerTypes;
491+
llvm::MapVector<QualType, const ForwardPointerType *> forwardPointerTypes;
492+
llvm::MapVector<QualType, const SpirvPointerType *> forwardReferences;
481493
llvm::DenseSet<FunctionType *, FunctionTypeMapInfo> functionTypes;
482494
llvm::DenseMap<unsigned, SpirvIntrinsicType *> spirvIntrinsicTypesById;
483495
llvm::SmallVector<const SpirvIntrinsicType *, 8> spirvIntrinsicTypes;

0 commit comments

Comments
 (0)