Skip to content

Commit 9eb7119

Browse files
authored
[SPIRV] Implements vk::BufferPointer proposal (microsoft#7163)
Implements [vk::BufferPointer proposal](https://github.com/microsoft/hlsl-specs/blob/main/proposals/0010-vk-buffer-ref.md). Closes microsoft#6489.
1 parent 572aef5 commit 9eb7119

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1326
-42
lines changed

include/dxc/HlslIntrinsicOp.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ enum class IntrinsicOp {
231231
IOP_VkReadClock = 223,
232232
IOP_Vkext_execution_mode = 224,
233233
IOP_Vkext_execution_mode_id = 225,
234+
IOP_Vkreinterpret_pointer_cast = 360,
235+
IOP_Vkstatic_pointer_cast = 361,
236+
MOP_GetBufferContents = 362,
234237
MOP_Append = 226,
235238
MOP_RestartStrip = 227,
236239
MOP_CalculateLevelOfDetail = 228,
@@ -366,7 +369,7 @@ enum class IntrinsicOp {
366369
IOP_usign = 355,
367370
MOP_InterlockedUMax = 356,
368371
MOP_InterlockedUMin = 357,
369-
Num_Intrinsics = 360,
372+
Num_Intrinsics = 363,
370373
};
371374
inline bool HasUnsignedIntrinsicOpcode(IntrinsicOp opcode) {
372375
switch (opcode) {

include/dxc/dxcapi.internal.h

Lines changed: 7 additions & 1 deletion
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 {
@@ -128,7 +132,9 @@ enum LEGAL_INTRINSIC_COMPTYPES {
128132

129133
LICOMPTYPE_HIT_OBJECT = 51,
130134

131-
LICOMPTYPE_COUNT = 52
135+
LICOMPTYPE_VK_BUFFER_POINTER = 52,
136+
137+
LICOMPTYPE_COUNT = 53
132138
};
133139

134140
static const BYTE IA_SPECIAL_BASE = 0xf0;

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
@@ -6818,6 +6821,12 @@ IntrinsicLower gLowerTable[] = {
68186821
{IntrinsicOp::IOP_DxMaybeReorderThread, TranslateMaybeReorderThread,
68196822
DXIL::OpCode::NumOpCodes_Dxil_1_8}, // FIXME: Just a placeholder Dxil
68206823
// opcode
6824+
{IntrinsicOp::IOP_Vkstatic_pointer_cast, UnsupportedVulkanIntrinsic,
6825+
DXIL::OpCode::NumOpCodes},
6826+
{IntrinsicOp::IOP_Vkreinterpret_pointer_cast, UnsupportedVulkanIntrinsic,
6827+
DXIL::OpCode::NumOpCodes},
6828+
{IntrinsicOp::MOP_GetBufferContents, UnsupportedVulkanIntrinsic,
6829+
DXIL::OpCode::NumOpCodes},
68216830
};
68226831
} // namespace
68236832
static_assert(

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;
@@ -402,6 +406,10 @@ DeclareNodeOrRecordType(clang::ASTContext &Ctx, DXIL::NodeIOKind Type,
402406
bool IsCompleteType = false);
403407

404408
#ifdef ENABLE_SPIRV_CODEGEN
409+
clang::CXXRecordDecl *
410+
DeclareVkBufferPointerType(clang::ASTContext &context,
411+
clang::DeclContext *declContext);
412+
405413
clang::CXXRecordDecl *DeclareInlineSpirvType(clang::ASTContext &context,
406414
clang::DeclContext *declContext,
407415
llvm::StringRef typeName,
@@ -427,7 +435,7 @@ clang::VarDecl *DeclareBuiltinGlobal(llvm::StringRef name, clang::QualType Ty,
427435
/// method.</summary> <param name="context">AST context in which to
428436
/// work.</param> <param name="recordDecl">Class in which the function template
429437
/// is declared.</param> <param name="functionDecl">Function for which a
430-
/// template is created.</params> <param
438+
/// template is created.</param> <param
431439
/// name="templateParamNamedDecls">Declarations for templates to the
432440
/// function.</param> <param name="templateParamNamedDeclsCount">Count of
433441
/// template declarations.</param> <returns>A new function template declaration
@@ -533,6 +541,29 @@ bool DoesTypeDefineOverloadedOperator(clang::QualType typeWithOperator,
533541
clang::QualType paramType);
534542
bool IsPatchConstantFunctionDecl(const clang::FunctionDecl *FD);
535543

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

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

Lines changed: 5 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
// This file enumerates the different kinds of operations that can be
@@ -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.

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> {
@@ -1447,6 +1450,20 @@ def VKStorageClassExt : InheritableAttr {
14471450
let Documentation = [Undocumented];
14481451
}
14491452

1453+
def VKBufferPointer : InheritableAttr {
1454+
let Spellings = [CXX11<"", "hlsl_vk_buffer_pointer", 2021>];
1455+
let LangOpts = [SPIRV];
1456+
let Documentation = [Undocumented];
1457+
}
1458+
1459+
def VKAliasedPointer : InheritableAttr {
1460+
let Spellings = [CXX11<"vk", "aliased_pointer">];
1461+
let Subjects = SubjectList<[Var, ParmVar], ErrorDiag>;
1462+
let Args = [];
1463+
let LangOpts = [SPIRV];
1464+
let Documentation = [Undocumented];
1465+
}
1466+
14501467
// Global variables that are of struct type
14511468
def StructGlobalVar : SubsetSubject<Var, [{S->hasGlobalStorage() && S->getType()->isStructureType()}]>;
14521469

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
//===----------------------------------------------------------------------===//
@@ -7838,7 +7841,7 @@ def warn_hlsl_intrinsic_in_wrong_shader_model : Warning<
78387841
"intrinsic %0 potentially used by '%1' requires shader model %2 or greater">,
78397842
DefaultError, InGroup<HLSLAvailability>;
78407843
def warn_hlsl_intrinsic_overload_in_wrong_shader_model : Warning<
7841-
"overload of intrinsic %0 requires shader model %1 or greater">,
7844+
"overload of intrinsic %0 requires shader model %1 or greater">,
78427845
DefaultError, InGroup<HLSLAvailability>;
78437846
def err_hlsl_intrinsic_template_arg_unsupported: Error<
78447847
"Explicit template arguments on intrinsic %0 are not supported">;
@@ -8004,6 +8007,10 @@ def err_hlsl_hitobject_unsupported_stage : Error<
80048007

80058008
// SPIRV Change Starts
80068009
def err_hlsl_vulkan_specific_feature: Error<"%0 is a Vulkan specific feature">;
8010+
def err_hlsl_vk_pointer_cast_alignment: Error<
8011+
"Vulkan buffer pointer cannot be cast to greater alignment">;
8012+
def err_hlsl_vk_static_pointer_cast_type: Error<
8013+
"vk::static_pointer_cast() content type must be base class of argument's content type">;
80078014
// SPIRV Change Ends
80088015

80098016
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;

tools/clang/include/clang/SPIRV/SpirvInstruction.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
//
55
// This file is distributed under the University of Illinois Open Source
66
// License. See LICENSE.TXT for details.
7+
//
8+
// Modifications Copyright(C) 2025 Advanced Micro Devices, Inc.
9+
// All rights reserved.
10+
//
711
//===----------------------------------------------------------------------===//
812
#ifndef LLVM_CLANG_SPIRV_SPIRVINSTRUCTION_H
913
#define LLVM_CLANG_SPIRV_SPIRVINSTRUCTION_H
@@ -67,6 +71,10 @@ class SpirvInstruction {
6771
IK_ConstantComposite,
6872
IK_ConstantNull,
6973

74+
// Pointer <-> uint conversions.
75+
IK_ConvertPtrToU,
76+
IK_ConvertUToPtr,
77+
7078
// OpUndef
7179
IK_Undef,
7280

@@ -1306,6 +1314,50 @@ class SpirvConstantNull : public SpirvConstant {
13061314
bool operator==(const SpirvConstantNull &that) const;
13071315
};
13081316

1317+
class SpirvConvertPtrToU : public SpirvInstruction {
1318+
public:
1319+
SpirvConvertPtrToU(SpirvInstruction *ptr, QualType type,
1320+
SourceLocation loc = {}, SourceRange range = {});
1321+
1322+
DEFINE_RELEASE_MEMORY_FOR_CLASS(SpirvConvertPtrToU)
1323+
1324+
// For LLVM-style RTTI
1325+
static bool classof(const SpirvInstruction *inst) {
1326+
return inst->getKind() == IK_ConvertPtrToU;
1327+
}
1328+
1329+
bool operator==(const SpirvConvertPtrToU &that) const;
1330+
1331+
bool invokeVisitor(Visitor *v) override;
1332+
1333+
SpirvInstruction *getPtr() const { return ptr; }
1334+
1335+
private:
1336+
SpirvInstruction *ptr;
1337+
};
1338+
1339+
class SpirvConvertUToPtr : public SpirvInstruction {
1340+
public:
1341+
SpirvConvertUToPtr(SpirvInstruction *intValue, QualType type,
1342+
SourceLocation loc = {}, SourceRange range = {});
1343+
1344+
DEFINE_RELEASE_MEMORY_FOR_CLASS(SpirvConvertUToPtr)
1345+
1346+
// For LLVM-style RTTI
1347+
static bool classof(const SpirvInstruction *inst) {
1348+
return inst->getKind() == IK_ConvertUToPtr;
1349+
}
1350+
1351+
bool operator==(const SpirvConvertUToPtr &that) const;
1352+
1353+
bool invokeVisitor(Visitor *v) override;
1354+
1355+
SpirvInstruction *getVal() const { return val; }
1356+
1357+
private:
1358+
SpirvInstruction *val;
1359+
};
1360+
13091361
class SpirvUndef : public SpirvInstruction {
13101362
public:
13111363
SpirvUndef(QualType type);

0 commit comments

Comments
 (0)