-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[DirectX] Implement DXILResourceImplicitBinding pass #138043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d5685ec
[HLSL] Implement DXILResourceBindingAnalysis
hekota d8a4a3f
Rename DXILResourceBindingsInfo to DXILResourceBindingInfo
hekota d79900c
Merge branch 'main' of https://github.com/llvm/llvm-project into reso…
hekota c718763
Add wrapper pass
hekota 9a70f06
update signature
hekota bca1bf3
code review feedback - rename flags, remove unnecessary functions, do…
hekota f58e2d5
[HLSL] Implementation of DXILResourceImplicitBinding pass
hekota b65d3bc
fix bug & update tests
hekota 075e27e
Merge branch 'main' of https://github.com/llvm/llvm-project into reso…
hekota 2f0aba1
Remove extra new line
hekota 79c2e7b
Merge branch 'main' of https://github.com/llvm/llvm-project into impl…
hekota e9747e5
Merge branch 'main' of https://github.com/llvm/llvm-project into reso…
hekota da838a9
Merge branch 'users/hekota/pr137258-resource-binding-analysis' of htt…
hekota 8a27708
code review feedback - use std::optional, renames
hekota bc93994
Merge branch 'main' of https://github.com/llvm/llvm-project into impl…
hekota 7000351
code review feedback - use std::optional fixups
hekota 0c828fc
use PreservedAnalyses::none()
hekota 79a82a4
Merge branch 'main' of https://github.com/llvm/llvm-project into impl…
hekota File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
llvm/lib/Target/DirectX/DXILResourceImplicitBinding.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
//===- DXILResourceImplicitBinding.cpp -----------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "DXILResourceImplicitBinding.h" | ||
#include "DirectX.h" | ||
#include "llvm/ADT/APInt.h" | ||
#include "llvm/ADT/STLExtras.h" | ||
#include "llvm/Analysis/DXILResource.h" | ||
#include "llvm/IR/Analysis.h" | ||
#include "llvm/IR/Constants.h" | ||
#include "llvm/IR/DiagnosticInfo.h" | ||
#include "llvm/IR/Function.h" | ||
#include "llvm/IR/IRBuilder.h" | ||
#include "llvm/IR/Instructions.h" | ||
#include "llvm/IR/IntrinsicsDirectX.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/InitializePasses.h" | ||
#include <cstdint> | ||
|
||
#define DEBUG_TYPE "dxil-resource-implicit-binding" | ||
|
||
using namespace llvm; | ||
using namespace llvm::dxil; | ||
|
||
namespace { | ||
|
||
static void diagnoseImplicitBindingNotFound(CallInst *ImplBindingCall) { | ||
Function *F = ImplBindingCall->getFunction(); | ||
LLVMContext &Context = F->getParent()->getContext(); | ||
// FIXME: include the name of the resource in the error message | ||
// (llvm/llvm-project#137868) | ||
Context.diagnose( | ||
V-FEXrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
DiagnosticInfoGenericWithLoc("resource cannot be allocated", *F, | ||
ImplBindingCall->getDebugLoc(), DS_Error)); | ||
} | ||
|
||
static bool assignBindings(Module &M, DXILResourceBindingInfo &DRBI, | ||
DXILResourceTypeMap &DRTM) { | ||
struct ImplicitBindingCall { | ||
int OrderID; | ||
CallInst *Call; | ||
ImplicitBindingCall(int OrderID, CallInst *Call) | ||
: OrderID(OrderID), Call(Call) {} | ||
}; | ||
SmallVector<ImplicitBindingCall> Calls; | ||
SmallVector<Function *> FunctionsToMaybeRemove; | ||
|
||
// collect all of the llvm.dx.resource.handlefromImplicitbinding calls | ||
for (Function &F : M.functions()) { | ||
if (!F.isDeclaration()) | ||
continue; | ||
|
||
if (F.getIntrinsicID() != Intrinsic::dx_resource_handlefromimplicitbinding) | ||
continue; | ||
|
||
for (User *U : F.users()) { | ||
if (CallInst *CI = dyn_cast<CallInst>(U)) { | ||
int OrderID = cast<ConstantInt>(CI->getArgOperand(0))->getZExtValue(); | ||
Calls.emplace_back(OrderID, CI); | ||
} | ||
} | ||
FunctionsToMaybeRemove.emplace_back(&F); | ||
} | ||
|
||
// sort all the collected implicit bindings by OrderID | ||
llvm::stable_sort( | ||
Calls, [](auto &LHS, auto &RHS) { return LHS.OrderID < RHS.OrderID; }); | ||
|
||
// iterate over sorted calls, find binding for each new OrderID and replace | ||
// each call with dx_resource_handlefrombinding using the new binding | ||
int LastOrderID = -1; | ||
llvm::TargetExtType *HandleTy = nullptr; | ||
ConstantInt *RegSlotOp = nullptr; | ||
bool AllBindingsAssigned = true; | ||
bool Changed = false; | ||
|
||
for (ImplicitBindingCall &IB : Calls) { | ||
IRBuilder<> Builder(IB.Call); | ||
|
||
if (IB.OrderID != LastOrderID) { | ||
LastOrderID = IB.OrderID; | ||
HandleTy = cast<TargetExtType>(IB.Call->getType()); | ||
ResourceTypeInfo &RTI = DRTM[HandleTy]; | ||
|
||
uint32_t Space = | ||
cast<ConstantInt>(IB.Call->getArgOperand(1))->getZExtValue(); | ||
int32_t Size = | ||
cast<ConstantInt>(IB.Call->getArgOperand(2))->getZExtValue(); | ||
|
||
std::optional<uint32_t> RegSlot = | ||
DRBI.findAvailableBinding(RTI.getResourceClass(), Space, Size); | ||
if (!RegSlot) { | ||
diagnoseImplicitBindingNotFound(IB.Call); | ||
AllBindingsAssigned = false; | ||
continue; | ||
} | ||
RegSlotOp = ConstantInt::get(Builder.getInt32Ty(), RegSlot.value()); | ||
} | ||
|
||
if (!RegSlotOp) | ||
continue; | ||
|
||
auto *NewCall = Builder.CreateIntrinsic( | ||
HandleTy, Intrinsic::dx_resource_handlefrombinding, | ||
{IB.Call->getOperand(1), /* space */ | ||
RegSlotOp, /* register slot */ | ||
IB.Call->getOperand(2), /* size */ | ||
IB.Call->getOperand(3), /* index */ | ||
IB.Call->getOperand(4)}); /* non-uniform flag */ | ||
IB.Call->replaceAllUsesWith(NewCall); | ||
V-FEXrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
IB.Call->eraseFromParent(); | ||
Changed = true; | ||
} | ||
|
||
for (Function *F : FunctionsToMaybeRemove) { | ||
if (F->user_empty()) { | ||
hekota marked this conversation as resolved.
Show resolved
Hide resolved
|
||
F->eraseFromParent(); | ||
Changed = true; | ||
} | ||
} | ||
|
||
DRBI.setHasImplicitBinding(!AllBindingsAssigned); | ||
return Changed; | ||
} | ||
|
||
} // end anonymous namespace | ||
|
||
PreservedAnalyses DXILResourceImplicitBinding::run(Module &M, | ||
ModuleAnalysisManager &AM) { | ||
|
||
DXILResourceBindingInfo &DRBI = AM.getResult<DXILResourceBindingAnalysis>(M); | ||
DXILResourceTypeMap &DRTM = AM.getResult<DXILResourceTypeAnalysis>(M); | ||
if (DRBI.hasImplicitBinding()) | ||
if (assignBindings(M, DRBI, DRTM)) | ||
return PreservedAnalyses::none(); | ||
return PreservedAnalyses::all(); | ||
} | ||
|
||
namespace { | ||
|
||
class DXILResourceImplicitBindingLegacy : public ModulePass { | ||
public: | ||
DXILResourceImplicitBindingLegacy() : ModulePass(ID) {} | ||
|
||
bool runOnModule(Module &M) override { | ||
DXILResourceTypeMap &DRTM = | ||
getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap(); | ||
DXILResourceBindingInfo &DRBI = | ||
getAnalysis<DXILResourceBindingWrapperPass>().getBindingInfo(); | ||
|
||
if (DRBI.hasImplicitBinding()) | ||
return assignBindings(M, DRBI, DRTM); | ||
return false; | ||
} | ||
|
||
static char ID; // Pass identification. | ||
void getAnalysisUsage(llvm::AnalysisUsage &AU) const override { | ||
AU.addRequired<DXILResourceTypeWrapperPass>(); | ||
AU.addRequired<DXILResourceBindingWrapperPass>(); | ||
} | ||
}; | ||
|
||
char DXILResourceImplicitBindingLegacy::ID = 0; | ||
} // end anonymous namespace | ||
|
||
INITIALIZE_PASS_BEGIN(DXILResourceImplicitBindingLegacy, DEBUG_TYPE, | ||
"DXIL Resource Implicit Binding", false, false) | ||
INITIALIZE_PASS_DEPENDENCY(DXILResourceTypeWrapperPass) | ||
INITIALIZE_PASS_DEPENDENCY(DXILResourceBindingWrapperPass) | ||
INITIALIZE_PASS_END(DXILResourceImplicitBindingLegacy, DEBUG_TYPE, | ||
"DXIL Resource Implicit Binding", false, false) | ||
|
||
ModulePass *llvm::createDXILResourceImplicitBindingLegacyPass() { | ||
return new DXILResourceImplicitBindingLegacy(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//===- DXILResourceImplicitBindings.h --_____________-----------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// \file Assign register slots to resources without explicit binding. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIB_TARGET_DIRECTX_DXILRESOURCEIMPLICITBINDING_H | ||
#define LLVM_LIB_TARGET_DIRECTX_DXILRESOURCEIMPLICITBINDING_H | ||
|
||
#include "llvm/IR/PassManager.h" | ||
#include "llvm/Pass.h" | ||
|
||
namespace llvm { | ||
|
||
class DXILResourceImplicitBinding | ||
: public PassInfoMixin<DXILResourceImplicitBinding> { | ||
public: | ||
PreservedAnalyses run(Module &M, ModuleAnalysisManager &); | ||
}; | ||
|
||
} // namespace llvm | ||
|
||
#endif // LLVM_LIB_TARGET_DIRECTX_DXILRESOURCEIMPLICITBINDING_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.