From 227a6c09f1d54ddea5e273106b9612baebfa62b5 Mon Sep 17 00:00:00 2001 From: Tex Riddell Date: Mon, 11 May 2026 14:43:29 -0700 Subject: [PATCH 1/2] Fix type annotation serialization for resources in HL modules This broke for -fcgl when adding resource type annotations in DXIL 1.8. It broke because the dx.types.ResourceProperties type was created by the DxilModule DxilOperations class (hlsl::OP), and the code tried to get this class from the DxilModule, which was nullptr because it was serializing an HLModule, not a DxilModule. The fix moves dx.types.ResourceProperties type creation to hlsl::resource_helper, which is where it seems to fit best. The local member and accessor in hlsl::OP is left as this will cache the type, as it did before, instead of looking it up by name frequently when for each dxil op. Only the initialization of the hlsl::OP member is changed to use the resource_helper function instead. Fixes #8440 --- include/dxc/DXIL/DxilResourceProperties.h | 2 ++ lib/DXIL/DxilMetadataHelper.cpp | 3 +-- lib/DXIL/DxilOperations.cpp | 5 ++--- lib/DXIL/DxilResourceProperties.cpp | 12 +++++++++++ .../hl-annotation-resource-param.hlsl | 21 +++++++++++++++++++ 5 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 tools/clang/test/CodeGenDXIL/hlsl/functions/hl-annotation-resource-param.hlsl diff --git a/include/dxc/DXIL/DxilResourceProperties.h b/include/dxc/DXIL/DxilResourceProperties.h index 2f4ff58969..7136f90d7e 100644 --- a/include/dxc/DXIL/DxilResourceProperties.h +++ b/include/dxc/DXIL/DxilResourceProperties.h @@ -16,6 +16,7 @@ namespace llvm { class Constant; class Type; +class Module; } // namespace llvm namespace hlsl { @@ -89,6 +90,7 @@ struct DxilInst_AnnotateHandle; namespace resource_helper { llvm::Constant *getAsConstant(const DxilResourceProperties &, llvm::Type *Ty, const ShaderModel &); +llvm::Type *GetResourcePropertiesType(llvm::Module &M); DxilResourceProperties loadPropsFromConstant(const llvm::Constant &C); DxilResourceProperties loadPropsFromAnnotateHandle(DxilInst_AnnotateHandle &annotateHandle, diff --git a/lib/DXIL/DxilMetadataHelper.cpp b/lib/DXIL/DxilMetadataHelper.cpp index 9598fa3da4..60e7a04183 100644 --- a/lib/DXIL/DxilMetadataHelper.cpp +++ b/lib/DXIL/DxilMetadataHelper.cpp @@ -1345,8 +1345,7 @@ Metadata *DxilMDHelper::EmitDxilFieldAnnotation(const DxilFieldAnnotation &FA) { MDVals.emplace_back(Uint32ToConstMD(kDxilFieldAnnotationResPropTag)); MDVals.emplace_back(ValueAsMetadata::get(resource_helper::getAsConstant( FA.GetResourceProperties(), - m_pModule->GetDxilModule().GetOP()->GetResourcePropertiesType(), - *m_pSM))); + resource_helper::GetResourcePropertiesType(*m_pModule), *m_pSM))); } if (DXIL::CompareVersions(m_MinValMajor, m_MinValMinor, 1, 7) >= 0) { if (FA.HasBitFields()) { diff --git a/lib/DXIL/DxilOperations.cpp b/lib/DXIL/DxilOperations.cpp index b786fee9fc..101d98e2e0 100644 --- a/lib/DXIL/DxilOperations.cpp +++ b/lib/DXIL/DxilOperations.cpp @@ -4118,9 +4118,8 @@ OP::OP(LLVMContext &Ctx, Module *pModule) "dx.types.NodeHandle", pModule); m_pNodeRecordHandleType = GetOrCreateStructType( m_Ctx, Type::getInt8PtrTy(m_Ctx), "dx.types.NodeRecordHandle", pModule); - m_pResourcePropertiesType = GetOrCreateStructType( - m_Ctx, {Type::getInt32Ty(m_Ctx), Type::getInt32Ty(m_Ctx)}, - "dx.types.ResourceProperties", pModule); + m_pResourcePropertiesType = + hlsl::resource_helper::GetResourcePropertiesType(*pModule); m_pNodePropertiesType = GetOrCreateStructType( m_Ctx, {Type::getInt32Ty(m_Ctx), Type::getInt32Ty(m_Ctx)}, "dx.types.NodeInfo", pModule); diff --git a/lib/DXIL/DxilResourceProperties.cpp b/lib/DXIL/DxilResourceProperties.cpp index 54ab24f36e..8ad6db4b72 100644 --- a/lib/DXIL/DxilResourceProperties.cpp +++ b/lib/DXIL/DxilResourceProperties.cpp @@ -19,6 +19,7 @@ #include "llvm/IR/Constant.h" #include "llvm/IR/Constants.h" #include "llvm/IR/DerivedTypes.h" +#include "llvm/IR/Module.h" using namespace llvm; @@ -104,6 +105,17 @@ Constant *getAsConstant(const DxilResourceProperties &RP, Type *Ty, return nullptr; } +llvm::Type *GetResourcePropertiesType(Module &M) { + LLVMContext &Ctx = M.getContext(); + StringRef Name = "dx.types.ResourceProperties"; + if (StructType *ST = M.getTypeByName(Name)) + return ST; + + Type *Int32Ty = Type::getInt32Ty(Ctx); + Type *Elements[] = {Int32Ty, Int32Ty}; + return StructType::create(Ctx, Elements, Name); +} + DxilResourceProperties loadPropsFromConstant(const Constant &C) { DxilResourceProperties RP; diff --git a/tools/clang/test/CodeGenDXIL/hlsl/functions/hl-annotation-resource-param.hlsl b/tools/clang/test/CodeGenDXIL/hlsl/functions/hl-annotation-resource-param.hlsl new file mode 100644 index 0000000000..65047015d3 --- /dev/null +++ b/tools/clang/test/CodeGenDXIL/hlsl/functions/hl-annotation-resource-param.hlsl @@ -0,0 +1,21 @@ +// RUN: %dxc -T cs_6_8 -fcgl %s | FileCheck %s + +// CHECK: %dx.types.ResourceProperties = type { i32, i32 } +// CHECK: !dx.typeAnnotations = !{![[TYPE_ANNOTATIONS:[0-9]+]]} +// CHECK: ![[TYPE_ANNOTATIONS]] = !{ +// CHECK-SAME: void (%struct.RWByteAddressBuffer*)* @"\01?foo@@YAXURWByteAddressBuffer@@@Z", ![[FN_ANN:[0-9]+]] +// CHECK: ![[FN_ANN]] = !{!{{[0-9]+}}, ![[PARAM_ANN:[0-9]+]]} +// CHECK: ![[PARAM_ANN]] = !{i32 0, ![[FIELD_ANN:[0-9]+]], !{{[0-9]+}}} +// CHECK: ![[FIELD_ANN]] = !{ +// CHECK-SAME: i32 10, %dx.types.ResourceProperties { i32 4107, i32 0 } + +RWByteAddressBuffer OutBuff : register(u0); + +void foo(RWByteAddressBuffer Buf) { + Buf.Store(0, 42); +} + +[numthreads(8, 1, 1)] +void main() { + foo(OutBuff); +} From 6ded42145ee9ab097993c167daa91e9b9207cee7 Mon Sep 17 00:00:00 2001 From: Tex Riddell Date: Mon, 11 May 2026 15:43:05 -0700 Subject: [PATCH 2/2] require dxil 1.8 to avoid downlevel validator breaks --- .../CodeGenDXIL/hlsl/functions/hl-annotation-resource-param.hlsl | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/clang/test/CodeGenDXIL/hlsl/functions/hl-annotation-resource-param.hlsl b/tools/clang/test/CodeGenDXIL/hlsl/functions/hl-annotation-resource-param.hlsl index 65047015d3..8df685f0d5 100644 --- a/tools/clang/test/CodeGenDXIL/hlsl/functions/hl-annotation-resource-param.hlsl +++ b/tools/clang/test/CodeGenDXIL/hlsl/functions/hl-annotation-resource-param.hlsl @@ -1,3 +1,4 @@ +// REQUIRES: dxil-1-8 // RUN: %dxc -T cs_6_8 -fcgl %s | FileCheck %s // CHECK: %dx.types.ResourceProperties = type { i32, i32 }