1- // ===- SPIRVCBufferAccess.cpp - Translate CBuffer Loads
2- // --------------------===//
1+ // ===- SPIRVCBufferAccess.cpp - Translate CBuffer Loads ---------*- C++ -*-===//
32//
43// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
54// See https://llvm.org/LICENSE.txt for license information.
87// ===----------------------------------------------------------------------===//
98//
109// This pass replaces all accesses to constant buffer global variables with
11- // accesses to the proper SPIR-V resource. It's designed to run after the
12- // DXIL preparation passes and before the main SPIR-V legalization passes.
10+ // accesses to the proper SPIR-V resource.
1311//
1412// The pass operates as follows:
1513// 1. It finds all constant buffers by looking for the `!hlsl.cbs` metadata.
@@ -61,19 +59,7 @@ static bool replaceCBufferAccesses(Module &M) {
6159
6260 for (const hlsl::CBufferMapping &Mapping : *CBufMD) {
6361 Instruction *HandleDef = findHandleDef (Mapping.Handle );
64- if (!HandleDef) {
65- // If there's no handle definition, it might be because the cbuffer is
66- // unused. In this case, we can just clean up the globals.
67- if (Mapping.Handle ->use_empty ()) {
68- for (const auto &Member : Mapping.Members ) {
69- if (Member.GV ->use_empty ()) {
70- Member.GV ->eraseFromParent ();
71- }
72- }
73- Mapping.Handle ->eraseFromParent ();
74- }
75- continue ;
76- }
62+ // TODO: Issue error if HandleDef is nullptr.
7763
7864 // The handle definition should dominate all uses of the cbuffer members.
7965 // We'll insert our getpointer calls right after it.
@@ -95,28 +81,29 @@ static bool replaceCBufferAccesses(Module &M) {
9581 // ConstantExprs, which cannot be replaced with non-constants.
9682 SmallVector<User *, 4 > Users (MemberGV->users ());
9783 for (User *U : Users) {
98- if (auto *CE = dyn_cast<ConstantExpr>(U)) {
99- SmallVector<Instruction *, 4 > Insts;
100- std::function<void (ConstantExpr *)> findInstructions =
101- [&](ConstantExpr *Const) {
102- for (User *ConstU : Const->users ()) {
103- if (auto *ConstCE = dyn_cast<ConstantExpr>(ConstU)) {
104- findInstructions (ConstCE);
105- } else if (auto *I = dyn_cast<Instruction>(ConstU)) {
106- Insts.push_back (I);
107- }
108- }
109- };
110- findInstructions (CE);
111-
112- for (Instruction *I : Insts) {
113- Instruction *NewInst = CE->getAsInstruction ();
114- NewInst->insertBefore (I->getIterator ());
115- I->replaceUsesOfWith (CE, NewInst);
116- NewInst->replaceUsesOfWith (MemberGV, GetPointerCall);
117- }
118- } else {
84+ auto *CE = dyn_cast<ConstantExpr>(U);
85+ if (!CE) {
11986 U->replaceUsesOfWith (MemberGV, GetPointerCall);
87+ continue ;
88+ }
89+ SmallVector<Instruction *, 4 > Insts;
90+ std::function<void (ConstantExpr *)> findInstructions =
91+ [&](ConstantExpr *Const) {
92+ for (User *ConstU : Const->users ()) {
93+ if (auto *ConstCE = dyn_cast<ConstantExpr>(ConstU)) {
94+ findInstructions (ConstCE);
95+ } else if (auto *I = dyn_cast<Instruction>(ConstU)) {
96+ Insts.push_back (I);
97+ }
98+ }
99+ };
100+ findInstructions (CE);
101+
102+ for (Instruction *I : Insts) {
103+ Instruction *NewInst = CE->getAsInstruction ();
104+ NewInst->insertBefore (I->getIterator ());
105+ I->replaceUsesOfWith (CE, NewInst);
106+ NewInst->replaceUsesOfWith (MemberGV, GetPointerCall);
120107 }
121108 }
122109 }
0 commit comments