1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
//===- 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(
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->getOperand(5)}); /* name */
IB.Call->replaceAllUsesWith(NewCall);
IB.Call->eraseFromParent();
Changed = true;
}
for (Function *F : FunctionsToMaybeRemove) {
if (F->user_empty()) {
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())
return PreservedAnalyses::all();
if (!assignBindings(M, DRBI, DRTM))
return PreservedAnalyses::all();
PreservedAnalyses PA;
PA.preserve<DXILResourceBindingAnalysis>();
PA.preserve<DXILResourceTypeAnalysis>();
return PA;
}
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>();
AU.addPreserved<DXILResourceTypeWrapperPass>();
AU.addPreserved<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();
}
|