Skip to content

Commit e84c902

Browse files
committed
Rearrange the getHLSLType code and map resource class to HLSL virtual register letter for mangling
1 parent 3074cc7 commit e84c902

File tree

4 files changed

+28
-34
lines changed

4 files changed

+28
-34
lines changed

clang/lib/AST/ItaniumMangle.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4495,18 +4495,19 @@ void CXXNameMangler::mangleType(const ArrayParameterType *T) {
44954495
void CXXNameMangler::mangleType(const HLSLAttributedResourceType *T) {
44964496
mangleType(T->getWrappedType());
44974497
const HLSLAttributedResourceType::Attributes &Attrs = T->getAttrs();
4498+
// map resource class to HLSL virtual register letter
44984499
switch (Attrs.ResourceClass) {
44994500
case llvm::dxil::ResourceClass::UAV:
4500-
Out << 'U';
4501+
Out << 'u';
45014502
break;
45024503
case llvm::dxil::ResourceClass::SRV:
4503-
Out << 'T';
4504+
Out << 't';
45044505
break;
45054506
case llvm::dxil::ResourceClass::CBuffer:
4506-
Out << 'C';
4507+
Out << 'b';
45074508
break;
45084509
case llvm::dxil::ResourceClass::Sampler:
4509-
Out << 'S';
4510+
Out << 's';
45104511
break;
45114512
}
45124513
mangleNumber(Attrs.IsROV);

clang/lib/AST/MicrosoftMangle.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3758,18 +3758,19 @@ void MicrosoftCXXNameMangler::mangleType(const HLSLAttributedResourceType *T,
37583758
Qualifiers, SourceRange Range) {
37593759
mangleType(T->getWrappedType(), SourceRange(), QMM_Escape);
37603760
const HLSLAttributedResourceType::Attributes &Attrs = T->getAttrs();
3761+
// map resource class to HLSL virtual register letter
37613762
switch (Attrs.ResourceClass) {
37623763
case llvm::dxil::ResourceClass::UAV:
3763-
Out << 'U';
3764+
Out << 'u';
37643765
break;
37653766
case llvm::dxil::ResourceClass::SRV:
3766-
Out << 'T';
3767+
Out << 't';
37673768
break;
37683769
case llvm::dxil::ResourceClass::CBuffer:
3769-
Out << 'C';
3770+
Out << 'b';
37703771
break;
37713772
case llvm::dxil::ResourceClass::Sampler:
3772-
Out << 'S';
3773+
Out << 's';
37733774
break;
37743775
}
37753776
mangleNumber(Attrs.IsROV);

clang/lib/CodeGen/Targets/DirectX.cpp

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,23 @@ llvm::Type *DirectXTargetCodeGenInfo::getHLSLType(CodeGenModule &CGM,
3838
switch (ResAttrs.ResourceClass) {
3939
case llvm::dxil::ResourceClass::UAV:
4040
case llvm::dxil::ResourceClass::SRV: {
41-
// convert element type
41+
// TypedBuffer and RawBuffer both need element type
4242
QualType ContainedTy = ResType->getContainedType();
43-
llvm::Type *ElemType = nullptr;
44-
if (!ContainedTy.isNull())
45-
ElemType = CGM.getTypes().ConvertType(ContainedTy);
46-
47-
if (ResAttrs.RawBuffer) {
48-
// RawBuffer needs element type
49-
if (ContainedTy.isNull())
50-
return nullptr;
51-
return llvm::TargetExtType::get(Ctx, "dx.RawBuffer", {ElemType},
52-
{/*IsWriteable*/ ResAttrs.ResourceClass ==
53-
llvm::dxil::ResourceClass::UAV,
54-
/*IsROV*/ ResAttrs.IsROV});
55-
}
56-
57-
// TypedBuffer needs element type
5843
if (ContainedTy.isNull())
5944
return nullptr;
60-
return llvm::TargetExtType::get(
61-
Ctx, "dx.TypedBuffer", {ElemType},
62-
{/*IsWriteable*/ ResAttrs.ResourceClass ==
63-
llvm::dxil::ResourceClass::UAV,
64-
/*IsROV*/ ResAttrs.IsROV,
65-
/*IsSigned*/ ContainedTy->isSignedIntegerType()});
45+
46+
// convert element type
47+
llvm::Type *ElemType = CGM.getTypes().ConvertType(ContainedTy);
48+
49+
const char* TypeName = ResAttrs.RawBuffer ? "dx.RawBuffer" : "dx.TypedBuffer";
50+
SmallVector<unsigned, 3> Ints = {
51+
/*IsWriteable*/ ResAttrs.ResourceClass == llvm::dxil::ResourceClass::UAV,
52+
/*IsROV*/ ResAttrs.IsROV
53+
};
54+
if (!ResAttrs.RawBuffer)
55+
Ints.push_back(/*IsSigned*/ ContainedTy->isSignedIntegerType());
56+
57+
return llvm::TargetExtType::get(Ctx, TypeName, {ElemType}, Ints);
6658
}
6759
case llvm::dxil::ResourceClass::CBuffer:
6860
llvm_unreachable("dx.CBuffer handles are not implemented yet");

clang/test/CodeGenHLSL/builtins/hlsl_resource_t.hlsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ using handle_float_t = __hlsl_resource_t [[hlsl::resource_class(UAV)]] [[hlsl::c
55
// CHECK: %"class.hlsl::RWBuffer" = type { target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
66
// CHECK: %"class.hlsl::StructuredBuffer" = type { target("dx.RawBuffer", %struct.MyStruct = type { <4 x float>, <2 x i32>, [8 x i8] }, 1, 0)
77

8-
// CHECK: define void @"?fa@@YAXU__hlsl_resource_t@@UA@A@M@Z"(ptr noundef byval(target("dx.TypedBuffer", float, 1, 0, 0)) align 4 %a)
9-
// CHECK: call void @"?foo1@@YAXU__hlsl_resource_t@@UA@A@M@Z"(ptr noundef byval(target("dx.TypedBuffer", float, 1, 0, 0)) align 4 %a)
10-
// CHECK: declare void @"?foo1@@YAXU__hlsl_resource_t@@UA@A@M@Z"(ptr noundef byval(target("dx.TypedBuffer", float, 1, 0, 0)) align 4)
8+
// CHECK: define void @"?fa@@YAXU__hlsl_resource_t@@uA@A@M@Z"(ptr noundef byval(target("dx.TypedBuffer", float, 1, 0, 0)) align 4 %a)
9+
// CHECK: call void @"?foo1@@YAXU__hlsl_resource_t@@uA@A@M@Z"(ptr noundef byval(target("dx.TypedBuffer", float, 1, 0, 0)) align 4 %a)
10+
// CHECK: declare void @"?foo1@@YAXU__hlsl_resource_t@@uA@A@M@Z"(ptr noundef byval(target("dx.TypedBuffer", float, 1, 0, 0)) align 4)
1111

1212
void foo1(handle_float_t res);
1313

1414
void fa(handle_float_t a) {
1515
foo1(a);
1616
}
1717

18-
// CHECK: define void @"?fb@@YAXU__hlsl_resource_t@@UA@A@M@Z"(ptr noundef byval(target("dx.TypedBuffer", float, 1, 0, 0)) align 4 %a)
18+
// CHECK: define void @"?fb@@YAXU__hlsl_resource_t@@uA@A@M@Z"(ptr noundef byval(target("dx.TypedBuffer", float, 1, 0, 0)) align 4 %a)
1919
void fb(handle_float_t a) {
2020
handle_float_t b = a;
2121
}

0 commit comments

Comments
 (0)