Skip to content

Commit c9be1d4

Browse files
committed
Review feedback
1 parent f9c6ef6 commit c9be1d4

File tree

9 files changed

+71
-27
lines changed

9 files changed

+71
-27
lines changed

llvm/include/llvm/Frontend/HLSL/CBuffer.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ class NamedMDNode;
2626
namespace hlsl {
2727

2828
struct CBufferMember {
29-
CBufferMember(GlobalVariable *GV, size_t Offset) : GV(GV), Offset(Offset) {}
30-
3129
GlobalVariable *GV;
3230
size_t Offset;
31+
32+
CBufferMember(GlobalVariable *GV, size_t Offset) : GV(GV), Offset(Offset) {}
3333
};
3434

3535
struct CBufferMapping {
36-
CBufferMapping(GlobalVariable *Handle) : Handle(Handle) {}
37-
3836
GlobalVariable *Handle;
3937
SmallVector<CBufferMember> Members;
38+
39+
CBufferMapping(GlobalVariable *Handle) : Handle(Handle) {}
4040
};
4141

4242
class CBufferMetadata {

llvm/lib/Frontend/HLSL/CBuffer.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/Frontend/HLSL/CBuffer.h"
10+
#include "llvm/Frontend/HLSL/HLSLResource.h"
1011
#include "llvm/IR/DerivedTypes.h"
1112
#include "llvm/IR/Metadata.h"
1213
#include "llvm/IR/Module.h"
@@ -65,6 +66,6 @@ void CBufferMetadata::eraseFromModule() {
6566
APInt hlsl::translateCBufArrayOffset(const DataLayout &DL, APInt Offset,
6667
ArrayType *Ty) {
6768
int64_t TypeSize = DL.getTypeSizeInBits(Ty->getElementType()) / 8;
68-
int64_t RoundUp = alignTo(TypeSize, Align(16));
69+
int64_t RoundUp = alignTo(TypeSize, Align(CBufferRowSizeInBytes));
6970
return Offset.udiv(TypeSize) * RoundUp;
7071
}

llvm/lib/Target/DirectX/DXILCBufferAccess.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "DXILCBufferAccess.h"
1010
#include "DirectX.h"
1111
#include "llvm/Frontend/HLSL/CBuffer.h"
12+
#include "llvm/Frontend/HLSL/HLSLResource.h"
1213
#include "llvm/IR/IRBuilder.h"
1314
#include "llvm/IR/IntrinsicsDirectX.h"
1415
#include "llvm/InitializePasses.h"
@@ -93,8 +94,9 @@ static void replaceAccess(LoadInst *LI, GlobalVariable *Global,
9394
Type *Ty = LI->getType();
9495
CBufferRowIntrin Intrin(DL, Ty->getScalarType());
9596
// The cbuffer consists of some number of 16-byte rows.
96-
unsigned int CurrentRow = Offset / 16;
97-
unsigned int CurrentIndex = (Offset % 16) / Intrin.EltSize;
97+
unsigned int CurrentRow = Offset / hlsl::CBufferRowSizeInBytes;
98+
unsigned int CurrentIndex =
99+
(Offset % hlsl::CBufferRowSizeInBytes) / Intrin.EltSize;
98100

99101
auto *CBufLoad = Builder.CreateIntrinsic(
100102
Intrin.RetTy, Intrin.IID,
@@ -111,10 +113,11 @@ static void replaceAccess(LoadInst *LI, GlobalVariable *Global,
111113
Result = Elt;
112114

113115
// However, if we loaded a <1 x T>, then we need to adjust the type here.
114-
if (auto *VT = dyn_cast<FixedVectorType>(LI->getType()))
115-
if (VT->getNumElements() == 1)
116-
Result = Builder.CreateInsertElement(PoisonValue::get(VT), Result,
117-
Builder.getInt32(0));
116+
if (auto *VT = dyn_cast<FixedVectorType>(LI->getType())) {
117+
assert(VT->getNumElements() == 1 && "Can't have multiple elements here");
118+
Result = Builder.CreateInsertElement(PoisonValue::get(VT), Result,
119+
Builder.getInt32(0));
120+
}
118121
} else {
119122
// Walk each element and extract it, wrapping to new rows as needed.
120123
SmallVector<Value *> Extracts{Elt};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
; RUN: opt -S -dxil-cbuffer-access -mtriple=dxil--shadermodel6.3-library %s | FileCheck %s
2+
3+
; cbuffer CB : register(b0) {
4+
; float a1[3];
5+
; }
6+
%__cblayout_CB = type <{ [3 x float] }>
7+
8+
@CB.cb = global target("dx.CBuffer", target("dx.Layout", %__cblayout_CB, 36, 0)) poison
9+
; CHECK: @CB.cb =
10+
; CHECK-NOT: external {{.*}} addrspace(2) global
11+
@a1 = external addrspace(2) global [3 x float], align 4
12+
13+
; CHECK: define void @f
14+
define void @f(ptr %dst) {
15+
entry:
16+
%CB.cb_h = call target("dx.CBuffer", target("dx.Layout", %__cblayout_CB, 36, 0)) @llvm.dx.resource.handlefrombinding.tdx.CBuffer_tdx.Layout_s___cblayout_CBs_36_0tt(i32 0, i32 0, i32 1, i32 0, i1 false)
17+
store target("dx.CBuffer", target("dx.Layout", %__cblayout_CB, 36, 0)) %CB.cb_h, ptr @CB.cb, align 4
18+
19+
; CHECK: [[CB:%.*]] = load target("dx.CBuffer", {{.*}})), ptr @CB.cb
20+
; CHECK: [[LOAD:%.*]] = call { float, float, float, float } @llvm.dx.resource.load.cbufferrow.4.{{.*}}(target("dx.CBuffer", {{.*}})) [[CB]], i32 1)
21+
; CHECK: [[X:%.*]] = extractvalue { float, float, float, float } [[LOAD]], 0
22+
; CHECK: store float [[X]], ptr %dst
23+
%a1 = load float, ptr addrspace(2) getelementptr inbounds ([3 x float], ptr addrspace(2) @a1, i32 0, i32 1), align 4
24+
store float %a1, ptr %dst, align 32
25+
26+
ret void
27+
}
28+
29+
; CHECK-NOT: !hlsl.cbs =
30+
!hlsl.cbs = !{!0}
31+
32+
!0 = !{ptr @CB.cb, ptr addrspace(2) @a1}

llvm/test/CodeGen/DirectX/CBufferAccess/arrays.ll

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
; bool a8[4];
1212
; }
1313
%__cblayout_CB = type <{ [3 x float], [2 x <3 x double>], [2 x [2 x half]], [3 x i64], [2 x [3 x [4 x <4 x i32>]]], [1 x i16], [2 x i64], [4 x i32] }>
14-
%struct.S = type { float, <3 x double>, half, i64, <4 x i32>, i16, i64, i32, [12 x i8] }
1514

1615
@CB.cb = local_unnamed_addr global target("dx.CBuffer", target("dx.Layout", %__cblayout_CB, 708, 0, 48, 112, 176, 224, 608, 624, 656)) poison
16+
; CHECK: @CB.cb =
17+
; CHECK-NOT: external {{.*}} addrspace(2) global
1718
@a1 = external local_unnamed_addr addrspace(2) global [3 x float], align 4
1819
@a2 = external local_unnamed_addr addrspace(2) global [2 x <3 x double>], align 32
1920
@a3 = external local_unnamed_addr addrspace(2) global [2 x [2 x half]], align 2
@@ -23,6 +24,7 @@
2324
@a7 = external local_unnamed_addr addrspace(2) global [2 x i64], align 8
2425
@a8 = external local_unnamed_addr addrspace(2) global [4 x i32], align 4
2526

27+
; CHECK: define void @f
2628
define void @f(ptr %dst) {
2729
entry:
2830
%CB.cb_h.i.i = tail call target("dx.CBuffer", target("dx.Layout", %__cblayout_CB, 708, 0, 48, 112, 176, 224, 608, 624, 656)) @llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false)
@@ -114,6 +116,7 @@ entry:
114116
ret void
115117
}
116118

119+
; CHECK-NOT: !hlsl.cbs =
117120
!hlsl.cbs = !{!0}
118121

119122
!0 = !{ptr @CB.cb, ptr addrspace(2) @a1, ptr addrspace(2) @a2, ptr addrspace(2) @a3, ptr addrspace(2) @a4, ptr addrspace(2) @a5, ptr addrspace(2) @a6, ptr addrspace(2) @a7, ptr addrspace(2) @a8}

llvm/test/CodeGen/DirectX/CBufferAccess/float.ll

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
; RUN: opt -S -dxil-cbuffer-access -mtriple=dxil--shadermodel6.3-library %s | FileCheck %s
22

33
%__cblayout_CB = type <{ float }>
4-
%struct.S = type { float }
54

65
@CB.cb = local_unnamed_addr global target("dx.CBuffer", target("dx.Layout", %__cblayout_CB, 4, 0)) poison
6+
; CHECK: @CB.cb =
7+
; CHECK-NOT: external {{.*}} addrspace(2) global
78
@x = external local_unnamed_addr addrspace(2) global float, align 4
89

10+
; CHECK: define void @f
911
define void @f(ptr %dst) {
1012
entry:
1113
; CHECK: [[CB:%.*]] = load target("dx.CBuffer", {{.*}})), ptr @CB.cb
@@ -17,6 +19,7 @@ entry:
1719
ret void
1820
}
1921

22+
; CHECK-NOT: !hlsl.cbs =
2023
!hlsl.cbs = !{!0}
2124

2225
!0 = !{ptr @CB.cb, ptr addrspace(2) @x}

llvm/test/CodeGen/DirectX/CBufferAccess/gep-ce-two-uses.ll

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
; }
66
%__cblayout_CB = type <{ [3 x float] }>
77

8-
@CB.cb = local_unnamed_addr global target("dx.CBuffer", target("dx.Layout", %__cblayout_CB, 708, 0, 48, 112, 176, 224, 608, 624, 656)) poison
8+
@CB.cb = local_unnamed_addr global target("dx.CBuffer", target("dx.Layout", %__cblayout_CB, 36, 0)) poison
9+
; CHECK: @CB.cb =
10+
; CHECK-NOT: external {{.*}} addrspace(2) global
911
@a1 = external local_unnamed_addr addrspace(2) global [3 x float], align 4
1012

13+
; CHECK: define void @f
1114
define void @f(ptr %dst) {
1215
entry:
1316
; CHECK: [[CB:%.*]] = load target("dx.CBuffer", {{.*}})), ptr @CB.cb
@@ -27,6 +30,7 @@ entry:
2730
ret void
2831
}
2932

33+
; CHECK-NOT: !hlsl.cbs =
3034
!hlsl.cbs = !{!0}
3135

3236
!0 = !{ptr @CB.cb, ptr addrspace(2) @a1}

llvm/test/CodeGen/DirectX/CBufferAccess/scalars.ll

+4-9
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,19 @@
1010
; int64_t a7; // offset 24, size 8
1111
; }
1212
%__cblayout_CB = type <{ float, i32, i32, half, i16, double, i64 }>
13-
%struct.Scalars = type { float, i32, i32, half, i16, double, i64 }
1413

15-
; CHECK: @CB.cb =
1614
@CB.cb = local_unnamed_addr global target("dx.CBuffer", target("dx.Layout", %__cblayout_CB, 32, 0, 4, 8, 12, 14, 16, 24)) poison
17-
; CHECK-NOT: @a1 =
15+
; CHECK: @CB.cb =
16+
; CHECK-NOT: external {{.*}} addrspace(2) global
1817
@a1 = external local_unnamed_addr addrspace(2) global float, align 4
19-
; CHECK-NOT: @a2 =
2018
@a2 = external local_unnamed_addr addrspace(2) global i32, align 4
21-
; CHECK-NOT: @a3 =
2219
@a3 = external local_unnamed_addr addrspace(2) global i32, align 4
23-
; CHECK-NOT: @a4 =
2420
@a4 = external local_unnamed_addr addrspace(2) global half, align 2
25-
; CHECK-NOT: @a5 =
2621
@a5 = external local_unnamed_addr addrspace(2) global i16, align 2
27-
; CHECK-NOT: @a6 =
2822
@a6 = external local_unnamed_addr addrspace(2) global double, align 8
29-
; CHECK-NOT: @a7 =
3023
@a7 = external local_unnamed_addr addrspace(2) global i64, align 8
3124

25+
; CHECK: define void @f
3226
define void @f(ptr %dst) {
3327
entry:
3428
%CB.cb_h.i.i = tail call target("dx.CBuffer", target("dx.Layout", %__cblayout_CB, 32, 0, 4, 8, 12, 14, 16, 24)) @llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false)
@@ -98,6 +92,7 @@ entry:
9892
ret void
9993
}
10094

95+
; CHECK-NOT: !hlsl.cbs =
10196
!hlsl.cbs = !{!0}
10297

10398
!0 = !{ptr @CB.cb, ptr addrspace(2) @a1, ptr addrspace(2) @a2, ptr addrspace(2) @a3, ptr addrspace(2) @a4, ptr addrspace(2) @a5, ptr addrspace(2) @a6, ptr addrspace(2) @a7}

llvm/test/CodeGen/DirectX/CBufferAccess/vectors.ll

+7-4
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,22 @@
99
; uint16_t3 a6; // offset 96, size 6 (+10)
1010
; };
1111
%__cblayout_CB = type <{ <3 x float>, <3 x double>, <2 x half>, <3 x i64>, <4 x i32>, <3 x i16> }>
12-
%struct.S = type { <3 x float>, <3 x double>, <2 x half>, <3 x i64>, <4 x i32>, <3 x i16> }
1312

14-
@CB.cb = local_unnamed_addr global target("dx.CBuffer", target("dx.Layout", %__cblayout_CB, 136, 0, 16, 40, 48, 80, 96)) poison
13+
@CB.cb = local_unnamed_addr global target("dx.CBuffer", target("dx.Layout", %__cblayout_CB, 102, 0, 16, 40, 48, 80, 96)) poison
14+
; CHECK: @CB.cb =
15+
; CHECK-NOT: external {{.*}} addrspace(2) global
1516
@a1 = external local_unnamed_addr addrspace(2) global <3 x float>, align 16
1617
@a2 = external local_unnamed_addr addrspace(2) global <3 x double>, align 32
1718
@a3 = external local_unnamed_addr addrspace(2) global <2 x half>, align 4
1819
@a4 = external local_unnamed_addr addrspace(2) global <3 x i64>, align 32
1920
@a5 = external local_unnamed_addr addrspace(2) global <4 x i32>, align 16
2021
@a6 = external local_unnamed_addr addrspace(2) global <3 x i16>, align 8
2122

23+
; CHECK: define void @f
2224
define void @f(ptr %dst) {
2325
entry:
24-
%CB.cb_h.i.i = tail call target("dx.CBuffer", target("dx.Layout", %__cblayout_CB, 136, 0, 16, 40, 48, 80, 96)) @llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false)
25-
store target("dx.CBuffer", target("dx.Layout", %__cblayout_CB, 136, 0, 16, 40, 48, 80, 96)) %CB.cb_h.i.i, ptr @CB.cb, align 4
26+
%CB.cb_h.i.i = tail call target("dx.CBuffer", target("dx.Layout", %__cblayout_CB, 102, 0, 16, 40, 48, 80, 96)) @llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false)
27+
store target("dx.CBuffer", target("dx.Layout", %__cblayout_CB, 102, 0, 16, 40, 48, 80, 96)) %CB.cb_h.i.i, ptr @CB.cb, align 4
2628

2729
; CHECK: [[CB:%.*]] = load target("dx.CBuffer", {{.*}})), ptr @CB.cb
2830
; CHECK: [[LOAD:%.*]] = call { float, float, float, float } @llvm.dx.resource.load.cbufferrow.4.{{.*}}(target("dx.CBuffer", {{.*}})) [[CB]], i32 0)
@@ -111,6 +113,7 @@ entry:
111113
ret void
112114
}
113115

116+
; CHECK-NOT: !hlsl.cbs =
114117
!hlsl.cbs = !{!0}
115118

116119
!0 = !{ptr @CB.cb, ptr addrspace(2) @a1, ptr addrspace(2) @a2, ptr addrspace(2) @a3, ptr addrspace(2) @a4, ptr addrspace(2) @a5, ptr addrspace(2) @a6}

0 commit comments

Comments
 (0)