Skip to content

Commit 227a6c0

Browse files
committed
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
1 parent 348fc78 commit 227a6c0

5 files changed

Lines changed: 38 additions & 5 deletions

File tree

include/dxc/DXIL/DxilResourceProperties.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
namespace llvm {
1717
class Constant;
1818
class Type;
19+
class Module;
1920
} // namespace llvm
2021

2122
namespace hlsl {
@@ -89,6 +90,7 @@ struct DxilInst_AnnotateHandle;
8990
namespace resource_helper {
9091
llvm::Constant *getAsConstant(const DxilResourceProperties &, llvm::Type *Ty,
9192
const ShaderModel &);
93+
llvm::Type *GetResourcePropertiesType(llvm::Module &M);
9294
DxilResourceProperties loadPropsFromConstant(const llvm::Constant &C);
9395
DxilResourceProperties
9496
loadPropsFromAnnotateHandle(DxilInst_AnnotateHandle &annotateHandle,

lib/DXIL/DxilMetadataHelper.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,8 +1345,7 @@ Metadata *DxilMDHelper::EmitDxilFieldAnnotation(const DxilFieldAnnotation &FA) {
13451345
MDVals.emplace_back(Uint32ToConstMD(kDxilFieldAnnotationResPropTag));
13461346
MDVals.emplace_back(ValueAsMetadata::get(resource_helper::getAsConstant(
13471347
FA.GetResourceProperties(),
1348-
m_pModule->GetDxilModule().GetOP()->GetResourcePropertiesType(),
1349-
*m_pSM)));
1348+
resource_helper::GetResourcePropertiesType(*m_pModule), *m_pSM)));
13501349
}
13511350
if (DXIL::CompareVersions(m_MinValMajor, m_MinValMinor, 1, 7) >= 0) {
13521351
if (FA.HasBitFields()) {

lib/DXIL/DxilOperations.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4118,9 +4118,8 @@ OP::OP(LLVMContext &Ctx, Module *pModule)
41184118
"dx.types.NodeHandle", pModule);
41194119
m_pNodeRecordHandleType = GetOrCreateStructType(
41204120
m_Ctx, Type::getInt8PtrTy(m_Ctx), "dx.types.NodeRecordHandle", pModule);
4121-
m_pResourcePropertiesType = GetOrCreateStructType(
4122-
m_Ctx, {Type::getInt32Ty(m_Ctx), Type::getInt32Ty(m_Ctx)},
4123-
"dx.types.ResourceProperties", pModule);
4121+
m_pResourcePropertiesType =
4122+
hlsl::resource_helper::GetResourcePropertiesType(*pModule);
41244123
m_pNodePropertiesType = GetOrCreateStructType(
41254124
m_Ctx, {Type::getInt32Ty(m_Ctx), Type::getInt32Ty(m_Ctx)},
41264125
"dx.types.NodeInfo", pModule);

lib/DXIL/DxilResourceProperties.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/IR/Constant.h"
2020
#include "llvm/IR/Constants.h"
2121
#include "llvm/IR/DerivedTypes.h"
22+
#include "llvm/IR/Module.h"
2223

2324
using namespace llvm;
2425

@@ -104,6 +105,17 @@ Constant *getAsConstant(const DxilResourceProperties &RP, Type *Ty,
104105
return nullptr;
105106
}
106107

108+
llvm::Type *GetResourcePropertiesType(Module &M) {
109+
LLVMContext &Ctx = M.getContext();
110+
StringRef Name = "dx.types.ResourceProperties";
111+
if (StructType *ST = M.getTypeByName(Name))
112+
return ST;
113+
114+
Type *Int32Ty = Type::getInt32Ty(Ctx);
115+
Type *Elements[] = {Int32Ty, Int32Ty};
116+
return StructType::create(Ctx, Elements, Name);
117+
}
118+
107119
DxilResourceProperties loadPropsFromConstant(const Constant &C) {
108120
DxilResourceProperties RP;
109121

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %dxc -T cs_6_8 -fcgl %s | FileCheck %s
2+
3+
// CHECK: %dx.types.ResourceProperties = type { i32, i32 }
4+
// CHECK: !dx.typeAnnotations = !{![[TYPE_ANNOTATIONS:[0-9]+]]}
5+
// CHECK: ![[TYPE_ANNOTATIONS]] = !{
6+
// CHECK-SAME: void (%struct.RWByteAddressBuffer*)* @"\01?foo@@YAXURWByteAddressBuffer@@@Z", ![[FN_ANN:[0-9]+]]
7+
// CHECK: ![[FN_ANN]] = !{!{{[0-9]+}}, ![[PARAM_ANN:[0-9]+]]}
8+
// CHECK: ![[PARAM_ANN]] = !{i32 0, ![[FIELD_ANN:[0-9]+]], !{{[0-9]+}}}
9+
// CHECK: ![[FIELD_ANN]] = !{
10+
// CHECK-SAME: i32 10, %dx.types.ResourceProperties { i32 4107, i32 0 }
11+
12+
RWByteAddressBuffer OutBuff : register(u0);
13+
14+
void foo(RWByteAddressBuffer Buf) {
15+
Buf.Store(0, 42);
16+
}
17+
18+
[numthreads(8, 1, 1)]
19+
void main() {
20+
foo(OutBuff);
21+
}

0 commit comments

Comments
 (0)