1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213 |
- ///////////////////////////////////////////////////////////////////////////////
- // //
- // DxilModule.cpp //
- // Copyright (C) Microsoft Corporation. All rights reserved. //
- // Licensed under the MIT license. See COPYRIGHT in the project root for //
- // full license information. //
- // //
- ///////////////////////////////////////////////////////////////////////////////
- #include "dxc/Support/Global.h"
- #include "dxc/HLSL/DxilOperations.h"
- #include "dxc/HLSL/DxilModule.h"
- #include "dxc/HLSL/DxilShaderModel.h"
- #include "dxc/HLSL/DxilSignatureElement.h"
- #include "dxc/HLSL/DxilContainer.h"
- #include "dxc/HLSL/DxilRootSignature.h"
- #include "llvm/IR/Constants.h"
- #include "llvm/IR/Function.h"
- #include "llvm/IR/Instructions.h"
- #include "llvm/IR/LLVMContext.h"
- #include "llvm/IR/Metadata.h"
- #include "llvm/IR/Module.h"
- #include "llvm/IR/DebugInfo.h"
- #include "llvm/Support/raw_ostream.h"
- #include <unordered_set>
- using namespace llvm;
- using std::string;
- using std::vector;
- using std::unique_ptr;
- namespace hlsl {
- //------------------------------------------------------------------------------
- //
- // DxilModule methods.
- //
- DxilModule::DxilModule(Module *pModule)
- : m_Ctx(pModule->getContext())
- , m_pModule(pModule)
- , m_pOP(std::make_unique<OP>(pModule->getContext(), pModule))
- , m_pTypeSystem(std::make_unique<DxilTypeSystem>(pModule))
- , m_pMDHelper(std::make_unique<DxilMDHelper>(pModule, std::make_unique<DxilExtraPropertyHelper>(pModule)))
- , m_pDebugInfoFinder(nullptr)
- , m_pEntryFunc(nullptr)
- , m_EntryName("")
- , m_pPatchConstantFunc(nullptr)
- , m_pSM(nullptr)
- , m_DxilMajor(DXIL::kDxilMajor)
- , m_DxilMinor(DXIL::kDxilMinor)
- , m_InputPrimitive(DXIL::InputPrimitive::Undefined)
- , m_MaxVertexCount(0)
- , m_StreamPrimitiveTopology(DXIL::PrimitiveTopology::Undefined)
- , m_ActiveStreamMask(0)
- , m_NumGSInstances(1)
- , m_InputControlPointCount(0)
- , m_TessellatorDomain(DXIL::TessellatorDomain::Undefined)
- , m_OutputControlPointCount(0)
- , m_TessellatorPartitioning(DXIL::TessellatorPartitioning::Undefined)
- , m_TessellatorOutputPrimitive(DXIL::TessellatorOutputPrimitive::Undefined)
- , m_MaxTessellationFactor(0.f)
- , m_RootSignature(nullptr) {
- DXASSERT_NOMSG(m_pModule != nullptr);
- m_NumThreads[0] = m_NumThreads[1] = m_NumThreads[2] = 0;
- #ifdef _DEBUG
- // Pin LLVM dump methods.
- void (__thiscall Module::*pfnModuleDump)() const = &Module::dump;
- void (__thiscall Type::*pfnTypeDump)() const = &Type::dump;
- void (__thiscall Function::*pfnViewCFGOnly)() const = &Function::viewCFGOnly;
- m_pUnused = (char *)&pfnModuleDump - (char *)&pfnTypeDump;
- m_pUnused -= (size_t)&pfnViewCFGOnly;
- #endif
- }
- DxilModule::~DxilModule() {
- }
- DxilModule::ShaderFlags::ShaderFlags():
- m_bDisableOptimizations(false)
- , m_bDisableMathRefactoring(false)
- , m_bEnableDoublePrecision(false)
- , m_bForceEarlyDepthStencil(false)
- , m_bEnableRawAndStructuredBuffers(false)
- , m_bEnableMinPrecision(false)
- , m_bEnableDoubleExtensions(false)
- , m_bEnableMSAD(false)
- , m_bAllResourcesBound(false)
- , m_bViewportAndRTArrayIndex(false)
- , m_bInnerCoverage(false)
- , m_bStencilRef(false)
- , m_bTiledResources(false)
- , m_bUAVLoadAdditionalFormats(false)
- , m_bLevel9ComparisonFiltering(false)
- , m_bCSRawAndStructuredViaShader4X(false)
- , m_b64UAVs(false)
- , m_UAVsAtEveryStage(false)
- , m_bROVS(false)
- , m_bWaveOps(false)
- , m_bInt64Ops(false)
- , m_align0(0)
- , m_align1(0)
- {}
- LLVMContext &DxilModule::GetCtx() const { return m_Ctx; }
- Module *DxilModule::GetModule() const { return m_pModule; }
- OP *DxilModule::GetOP() const { return m_pOP.get(); }
- void DxilModule::SetShaderModel(const ShaderModel *pSM) {
- DXASSERT(m_pSM == nullptr, "shader model must not change for the module");
- m_pSM = pSM;
- m_pMDHelper->SetShaderModel(m_pSM);
- DXIL::ShaderKind shaderKind = pSM->GetKind();
- m_InputSignature.reset(new DxilSignature(shaderKind, DXIL::SignatureKind::Input));
- m_OutputSignature.reset(new DxilSignature(shaderKind, DXIL::SignatureKind::Output));
- m_PatchConstantSignature.reset(new DxilSignature(shaderKind, DXIL::SignatureKind::PatchConstant));
- m_RootSignature.reset(new RootSignatureHandle());
- }
- const ShaderModel *DxilModule::GetShaderModel() const {
- return m_pSM;
- }
- Function *DxilModule::GetEntryFunction() {
- return m_pEntryFunc;
- }
- const Function *DxilModule::GetEntryFunction() const {
- return m_pEntryFunc;
- }
- void DxilModule::SetEntryFunction(Function *pEntryFunc) {
- m_pEntryFunc = pEntryFunc;
- }
- const string &DxilModule::GetEntryFunctionName() const {
- return m_EntryName;
- }
- void DxilModule::SetEntryFunctionName(const string &name) {
- m_EntryName = name;
- }
- llvm::Function *DxilModule::GetPatchConstantFunction() {
- return m_pPatchConstantFunc;
- }
- const llvm::Function *DxilModule::GetPatchConstantFunction() const {
- return m_pPatchConstantFunc;
- }
- void DxilModule::SetPatchConstantFunction(llvm::Function *pFunc) {
- m_pPatchConstantFunc = pFunc;
- }
- unsigned DxilModule::ShaderFlags::GetGlobalFlags() const {
- unsigned Flags = 0;
- Flags |= m_bDisableOptimizations ? DXIL::kDisableOptimizations : 0;
- Flags |= m_bDisableMathRefactoring ? DXIL::kDisableMathRefactoring : 0;
- Flags |= m_bEnableDoublePrecision ? DXIL::kEnableDoublePrecision : 0;
- Flags |= m_bForceEarlyDepthStencil ? DXIL::kForceEarlyDepthStencil : 0;
- Flags |= m_bEnableRawAndStructuredBuffers ? DXIL::kEnableRawAndStructuredBuffers : 0;
- Flags |= m_bEnableMinPrecision ? DXIL::kEnableMinPrecision : 0;
- Flags |= m_bEnableDoubleExtensions ? DXIL::kEnableDoubleExtensions : 0;
- Flags |= m_bEnableMSAD ? DXIL::kEnableMSAD : 0;
- Flags |= m_bAllResourcesBound ? DXIL::kAllResourcesBound : 0;
- return Flags;
- }
- uint64_t DxilModule::ShaderFlags::GetFeatureInfo() const {
- uint64_t Flags = 0;
- Flags |= m_bEnableDoublePrecision ? hlsl::ShaderFeatureInfo_Doubles : 0;
- Flags |= m_bEnableMinPrecision ? hlsl::ShaderFeatureInfo_MininumPrecision : 0;
- Flags |= m_bEnableDoubleExtensions ? hlsl::ShaderFeatureInfo_11_1_DoubleExtensions : 0;
- Flags |= m_bWaveOps ? hlsl::ShaderFeatureInfo_WaveOps : 0;
- Flags |= m_bInt64Ops ? hlsl::ShaderFeatureInfo_Int64Ops : 0;
- Flags |= m_bROVS ? hlsl::ShaderFeatureInfo_ROVs : 0;
- Flags |= m_bViewportAndRTArrayIndex ? hlsl::ShaderFeatureInfo_ViewportAndRTArrayIndexFromAnyShaderFeedingRasterizer : 0;
- Flags |= m_bInnerCoverage ? hlsl::ShaderFeatureInfo_InnerCoverage : 0;
- Flags |= m_bStencilRef ? hlsl::ShaderFeatureInfo_StencilRef : 0;
- Flags |= m_bTiledResources ? hlsl::ShaderFeatureInfo_TiledResources : 0;
- Flags |= m_bEnableMSAD ? hlsl::ShaderFeatureInfo_11_1_ShaderExtensions : 0;
- Flags |= m_bCSRawAndStructuredViaShader4X ? hlsl::ShaderFeatureInfo_ComputeShadersPlusRawAndStructuredBuffersViaShader4X : 0;
- Flags |= m_UAVsAtEveryStage ? hlsl::ShaderFeatureInfo_UAVsAtEveryStage : 0;
- Flags |= m_b64UAVs ? hlsl::ShaderFeatureInfo_64UAVs : 0;
- Flags |= m_bLevel9ComparisonFiltering ? hlsl::ShaderFeatureInfo_LEVEL9ComparisonFiltering : 0;
- Flags |= m_bUAVLoadAdditionalFormats ? hlsl::ShaderFeatureInfo_TypedUAVLoadAdditionalFormats : 0;
- return Flags;
- }
- uint64_t DxilModule::ShaderFlags::GetShaderFlagsRaw() const {
- union Cast {
- Cast(const DxilModule::ShaderFlags &flags) {
- shaderFlags = flags;
- }
- DxilModule::ShaderFlags shaderFlags;
- uint64_t rawData;
- };
- static_assert(sizeof(uint64_t) == sizeof(DxilModule::ShaderFlags),
- "size must match to make sure no undefined bits when cast");
- Cast rawCast(*this);
- return rawCast.rawData;
- }
- void DxilModule::ShaderFlags::SetShaderFlagsRaw(uint64_t data) {
- union Cast {
- Cast(uint64_t data) {
- rawData = data;
- }
- DxilModule::ShaderFlags shaderFlags;
- uint64_t rawData;
- };
- Cast rawCast(data);
- *this = rawCast.shaderFlags;
- }
- unsigned DxilModule::GetGlobalFlags() const {
- unsigned Flags = m_ShaderFlags.GetGlobalFlags();
- return Flags;
- }
- void DxilModule::CollectShaderFlags(ShaderFlags &Flags) {
- bool hasDouble = false;
- // ddiv dfma drcp d2i d2u i2d u2d.
- // fma has dxil op. Others should check IR instruction div/cast.
- bool hasDoubleExtension = false;
- bool has64Int = false;
- bool has16FloatInt = false;
- bool hasWaveOps = false;
- bool hasCheckAccessFully = false;
- bool hasMSAD = false;
- bool hasMulticomponentUAVLoads = false;
- bool hasInnerCoverage = false;
- Type *int16Ty = Type::getInt16Ty(GetCtx());
- Type *int64Ty = Type::getInt64Ty(GetCtx());
- for (Function &F : GetModule()->functions()) {
- for (BasicBlock &BB : F.getBasicBlockList()) {
- for (Instruction &I : BB.getInstList()) {
- // Skip none dxil function call.
- if (CallInst *CI = dyn_cast<CallInst>(&I)) {
- if (!OP::IsDxilOpFunc(CI->getCalledFunction()))
- continue;
- }
- Type *Ty = I.getType();
- bool isDouble = Ty->isDoubleTy();
- bool isHalf = Ty->isHalfTy();
- bool isInt16 = Ty == int16Ty;
- bool isInt64 = Ty == int64Ty;
- if (isa<ExtractElementInst>(&I) ||
- isa<InsertElementInst>(&I))
- continue;
- for (Value *operand : I.operands()) {
- Type *Ty = operand->getType();
- isDouble |= Ty->isDoubleTy();
- isHalf |= Ty->isHalfTy();
- isInt16 |= Ty == int16Ty;
- isInt64 |= Ty == int64Ty;
- }
- if (isDouble) {
- hasDouble = true;
- switch (I.getOpcode()) {
- case Instruction::FDiv:
- case Instruction::UIToFP:
- case Instruction::SIToFP:
- case Instruction::FPToUI:
- case Instruction::FPToSI:
- hasDoubleExtension = true;
- break;
- }
- }
-
- has16FloatInt |= isHalf;
- has16FloatInt |= isInt16;
- has64Int |= isInt64;
- if (CallInst *CI = dyn_cast<CallInst>(&I)) {
- if (!OP::IsDxilOpFunc(CI->getCalledFunction()))
- continue;
- Value *opcodeArg = CI->getArgOperand(DXIL::OperandIndex::kOpcodeIdx);
- ConstantInt *opcodeConst = dyn_cast<ConstantInt>(opcodeArg);
- DXASSERT(opcodeConst, "DXIL opcode arg must be immediate");
- unsigned opcode = opcodeConst->getLimitedValue();
- DXASSERT(opcode < static_cast<unsigned>(DXIL::OpCode::NumOpCodes),
- "invalid DXIL opcode");
- DXIL::OpCode dxilOp = static_cast<DXIL::OpCode>(opcode);
- if (hlsl::OP::IsDxilOpWave(dxilOp))
- hasWaveOps = true;
- switch (dxilOp) {
- case DXIL::OpCode::CheckAccessFullyMapped:
- hasCheckAccessFully = true;
- break;
- case DXIL::OpCode::Msad:
- hasMSAD = true;
- break;
- case DXIL::OpCode::BufferLoad:
- case DXIL::OpCode::TextureLoad: {
- Value *resHandle = CI->getArgOperand(DXIL::OperandIndex::kBufferStoreHandleOpIdx);
- CallInst *handleCall = cast<CallInst>(resHandle);
- if (ConstantInt *resClassArg =
- dyn_cast<ConstantInt>(handleCall->getArgOperand(
- DXIL::OperandIndex::kCreateHandleResClassOpIdx))) {
- DXIL::ResourceClass resClass = static_cast<DXIL::ResourceClass>(
- resClassArg->getLimitedValue());
- if (resClass == DXIL::ResourceClass::UAV) {
- // For DXIL, all uav load is multi component load.
- hasMulticomponentUAVLoads = true;
- }
- } else if (PHINode *resClassPhi = dyn_cast<
- PHINode>(handleCall->getArgOperand(
- DXIL::OperandIndex::kCreateHandleResClassOpIdx))) {
- unsigned numOperands = resClassPhi->getNumOperands();
- for (unsigned i = 0; i < numOperands; i++) {
- if (ConstantInt *resClassArg = dyn_cast<ConstantInt>(
- resClassPhi->getIncomingValue(i))) {
- DXIL::ResourceClass resClass =
- static_cast<DXIL::ResourceClass>(
- resClassArg->getLimitedValue());
- if (resClass == DXIL::ResourceClass::UAV) {
- // For DXIL, all uav load is multi component load.
- hasMulticomponentUAVLoads = true;
- break;
- }
- }
- }
- }
- } break;
- case DXIL::OpCode::Fma:
- hasDoubleExtension |= isDouble;
- break;
- case DXIL::OpCode::InnerCoverage:
- hasInnerCoverage = true;
- break;
- }
- }
- }
- }
- }
- Flags.SetEnableDoublePrecision(hasDouble);
- Flags.SetInt64Ops(has64Int);
- Flags.SetEnableMinPrecision(has16FloatInt);
- Flags.SetEnableDoubleExtensions(hasDoubleExtension);
- Flags.SetWaveOps(hasWaveOps);
- Flags.SetTiledResources(hasCheckAccessFully);
- Flags.SetEnableMSAD(hasMSAD);
- Flags.SetUAVLoadAdditionalFormats(hasMulticomponentUAVLoads);
- const ShaderModel *SM = GetShaderModel();
- if (SM->IsPS()) {
- bool hasStencilRef = false;
- DxilSignature &outS = GetOutputSignature();
- for (auto &&E : outS.GetElements()) {
- if (E->GetKind() == Semantic::Kind::StencilRef) {
- hasStencilRef = true;
- } else if (E->GetKind() == Semantic::Kind::InnerCoverage) {
- hasInnerCoverage = true;
- }
- }
- Flags.SetStencilRef(hasStencilRef);
- Flags.SetInnerCoverage(hasInnerCoverage);
- }
- bool checkInputRTArrayIndex =
- SM->IsGS() || SM->IsDS() || SM->IsHS() || SM->IsPS();
- if (checkInputRTArrayIndex) {
- bool hasViewportArrayIndex = false;
- bool hasRenderTargetArrayIndex = false;
- DxilSignature &inS = GetInputSignature();
- for (auto &E : inS.GetElements()) {
- if (E->GetKind() == Semantic::Kind::ViewPortArrayIndex) {
- hasViewportArrayIndex = true;
- } else if (E->GetKind() == Semantic::Kind::RenderTargetArrayIndex) {
- hasRenderTargetArrayIndex = true;
- }
- }
- Flags.SetViewportAndRTArrayIndex(hasViewportArrayIndex |
- hasRenderTargetArrayIndex);
- }
- bool checkOutputRTArrayIndex =
- SM->IsVS() || SM->IsDS() || SM->IsHS() || SM->IsPS();
- if (checkOutputRTArrayIndex) {
- bool hasViewportArrayIndex = false;
- bool hasRenderTargetArrayIndex = false;
- DxilSignature &outS = GetOutputSignature();
- for (auto &E : outS.GetElements()) {
- if (E->GetKind() == Semantic::Kind::ViewPortArrayIndex) {
- hasViewportArrayIndex = true;
- } else if (E->GetKind() == Semantic::Kind::RenderTargetArrayIndex) {
- hasRenderTargetArrayIndex = true;
- }
- }
- Flags.SetViewportAndRTArrayIndex(hasViewportArrayIndex |
- hasRenderTargetArrayIndex);
- }
- unsigned NumUAVs = m_UAVs.size();
- const unsigned kSmallUAVCount = 8;
- if (NumUAVs > kSmallUAVCount)
- Flags.Set64UAVs(true);
- if (NumUAVs && !(SM->IsCS() || SM->IsPS()))
- Flags.SetUAVsAtEveryStage(true);
- bool hasRawAndStructuredBuffer = false;
- for (auto &UAV : m_UAVs) {
- if (UAV->IsROV())
- Flags.SetROVs(true);
- switch (UAV->GetKind()) {
- case DXIL::ResourceKind::RawBuffer:
- case DXIL::ResourceKind::StructuredBuffer:
- hasRawAndStructuredBuffer = true;
- break;
- }
- }
- for (auto &SRV : m_SRVs) {
- switch (SRV->GetKind()) {
- case DXIL::ResourceKind::RawBuffer:
- case DXIL::ResourceKind::StructuredBuffer:
- hasRawAndStructuredBuffer = true;
- break;
- }
- }
-
- Flags.SetEnableRawAndStructuredBuffers(hasRawAndStructuredBuffer);
- bool hasCSRawAndStructuredViaShader4X =
- hasRawAndStructuredBuffer && m_pSM->GetMajor() == 4 && m_pSM->IsCS();
- Flags.SetCSRawAndStructuredViaShader4X(hasCSRawAndStructuredViaShader4X);
- }
- void DxilModule::CollectShaderFlags() {
- CollectShaderFlags(m_ShaderFlags);
- }
- uint64_t DxilModule::ShaderFlags::GetShaderFlagsRawForCollection() {
- // This should be all the flags that can be set by DxilModule::CollectShaderFlags.
- ShaderFlags Flags;
- Flags.SetEnableDoublePrecision(true);
- Flags.SetInt64Ops(true);
- Flags.SetEnableMinPrecision(true);
- Flags.SetEnableDoubleExtensions(true);
- Flags.SetWaveOps(true);
- Flags.SetTiledResources(true);
- Flags.SetEnableMSAD(true);
- Flags.SetUAVLoadAdditionalFormats(true);
- Flags.SetStencilRef(true);
- Flags.SetInnerCoverage(true);
- Flags.SetViewportAndRTArrayIndex(true);
- Flags.Set64UAVs(true);
- Flags.SetUAVsAtEveryStage(true);
- Flags.SetEnableRawAndStructuredBuffers(true);
- Flags.SetCSRawAndStructuredViaShader4X(true);
- return Flags.GetShaderFlagsRaw();
- }
- DXIL::InputPrimitive DxilModule::GetInputPrimitive() const {
- return m_InputPrimitive;
- }
- void DxilModule::SetInputPrimitive(DXIL::InputPrimitive IP) {
- DXASSERT_NOMSG(m_InputPrimitive == DXIL::InputPrimitive::Undefined);
- DXASSERT_NOMSG(DXIL::InputPrimitive::Undefined < IP && IP < DXIL::InputPrimitive::LastEntry);
- m_InputPrimitive = IP;
- }
- unsigned DxilModule::GetMaxVertexCount() const {
- DXASSERT_NOMSG(m_MaxVertexCount != 0);
- return m_MaxVertexCount;
- }
- void DxilModule::SetMaxVertexCount(unsigned Count) {
- DXASSERT_NOMSG(m_MaxVertexCount == 0);
- m_MaxVertexCount = Count;
- }
- DXIL::PrimitiveTopology DxilModule::GetStreamPrimitiveTopology() const {
- return m_StreamPrimitiveTopology;
- }
- void DxilModule::SetStreamPrimitiveTopology(DXIL::PrimitiveTopology Topology) {
- m_StreamPrimitiveTopology = Topology;
- }
- bool DxilModule::HasMultipleOutputStreams() const {
- if (!m_pSM->IsGS()) {
- return false;
- } else {
- unsigned NumStreams = (m_ActiveStreamMask & 0x1) +
- ((m_ActiveStreamMask & 0x2) >> 1) +
- ((m_ActiveStreamMask & 0x4) >> 2) +
- ((m_ActiveStreamMask & 0x8) >> 3);
- DXASSERT_NOMSG(NumStreams <= DXIL::kNumOutputStreams);
- return NumStreams > 1;
- }
- }
- unsigned DxilModule::GetOutputStream() const {
- if (!m_pSM->IsGS()) {
- return 0;
- } else {
- DXASSERT_NOMSG(!HasMultipleOutputStreams());
- switch (m_ActiveStreamMask) {
- case 0x1: return 0;
- case 0x2: return 1;
- case 0x4: return 2;
- case 0x8: return 3;
- default: DXASSERT_NOMSG(false);
- }
- return (unsigned)(-1);
- }
- }
- unsigned DxilModule::GetGSInstanceCount() const {
- return m_NumGSInstances;
- }
- void DxilModule::SetGSInstanceCount(unsigned Count) {
- m_NumGSInstances = Count;
- }
- bool DxilModule::IsStreamActive(unsigned Stream) const {
- return (m_ActiveStreamMask & (1<<Stream)) != 0;
- }
- void DxilModule::SetStreamActive(unsigned Stream, bool bActive) {
- if (bActive) {
- m_ActiveStreamMask |= (1<<Stream);
- } else {
- m_ActiveStreamMask &= ~(1<<Stream);
- }
- }
- void DxilModule::SetActiveStreamMask(unsigned Mask) {
- m_ActiveStreamMask = Mask;
- }
- unsigned DxilModule::GetActiveStreamMask() const {
- return m_ActiveStreamMask;
- }
- unsigned DxilModule::GetInputControlPointCount() const {
- return m_InputControlPointCount;
- }
- void DxilModule::SetInputControlPointCount(unsigned NumICPs) {
- m_InputControlPointCount = NumICPs;
- }
- DXIL::TessellatorDomain DxilModule::GetTessellatorDomain() const {
- return m_TessellatorDomain;
- }
- void DxilModule::SetTessellatorDomain(DXIL::TessellatorDomain TessDomain) {
- m_TessellatorDomain = TessDomain;
- }
- unsigned DxilModule::GetOutputControlPointCount() const {
- return m_OutputControlPointCount;
- }
- void DxilModule::SetOutputControlPointCount(unsigned NumOCPs) {
- m_OutputControlPointCount = NumOCPs;
- }
- DXIL::TessellatorPartitioning DxilModule::GetTessellatorPartitioning() const {
- return m_TessellatorPartitioning;
- }
- void DxilModule::SetTessellatorPartitioning(DXIL::TessellatorPartitioning TessPartitioning) {
- m_TessellatorPartitioning = TessPartitioning;
- }
- DXIL::TessellatorOutputPrimitive DxilModule::GetTessellatorOutputPrimitive() const {
- return m_TessellatorOutputPrimitive;
- }
- void DxilModule::SetTessellatorOutputPrimitive(DXIL::TessellatorOutputPrimitive TessOutputPrimitive) {
- m_TessellatorOutputPrimitive = TessOutputPrimitive;
- }
- float DxilModule::GetMaxTessellationFactor() const {
- return m_MaxTessellationFactor;
- }
- void DxilModule::SetMaxTessellationFactor(float MaxTessellationFactor) {
- m_MaxTessellationFactor = MaxTessellationFactor;
- }
- template<typename T> unsigned
- DxilModule::AddResource(vector<unique_ptr<T> > &Vec, unique_ptr<T> pRes) {
- DXASSERT_NOMSG((unsigned)Vec.size() < UINT_MAX);
- unsigned Id = (unsigned)Vec.size();
- Vec.emplace_back(std::move(pRes));
- return Id;
- }
- unsigned DxilModule::AddCBuffer(unique_ptr<DxilCBuffer> pCB) {
- return AddResource<DxilCBuffer>(m_CBuffers, std::move(pCB));
- }
- DxilCBuffer &DxilModule::GetCBuffer(unsigned idx) {
- return *m_CBuffers[idx];
- }
- const DxilCBuffer &DxilModule::GetCBuffer(unsigned idx) const {
- return *m_CBuffers[idx];
- }
- const vector<unique_ptr<DxilCBuffer> > &DxilModule::GetCBuffers() const {
- return m_CBuffers;
- }
- unsigned DxilModule::AddSampler(unique_ptr<DxilSampler> pSampler) {
- return AddResource<DxilSampler>(m_Samplers, std::move(pSampler));
- }
- DxilSampler &DxilModule::GetSampler(unsigned idx) {
- return *m_Samplers[idx];
- }
- const DxilSampler &DxilModule::GetSampler(unsigned idx) const {
- return *m_Samplers[idx];
- }
- const vector<unique_ptr<DxilSampler> > &DxilModule::GetSamplers() const {
- return m_Samplers;
- }
- unsigned DxilModule::AddSRV(unique_ptr<DxilResource> pSRV) {
- return AddResource<DxilResource>(m_SRVs, std::move(pSRV));
- }
- DxilResource &DxilModule::GetSRV(unsigned idx) {
- return *m_SRVs[idx];
- }
- const DxilResource &DxilModule::GetSRV(unsigned idx) const {
- return *m_SRVs[idx];
- }
- const vector<unique_ptr<DxilResource> > &DxilModule::GetSRVs() const {
- return m_SRVs;
- }
- unsigned DxilModule::AddUAV(unique_ptr<DxilResource> pUAV) {
- return AddResource<DxilResource>(m_UAVs, std::move(pUAV));
- }
- DxilResource &DxilModule::GetUAV(unsigned idx) {
- return *m_UAVs[idx];
- }
- const DxilResource &DxilModule::GetUAV(unsigned idx) const {
- return *m_UAVs[idx];
- }
- const vector<unique_ptr<DxilResource> > &DxilModule::GetUAVs() const {
- return m_UAVs;
- }
- template <typename TResource>
- static void RemoveResources(std::vector<std::unique_ptr<TResource>> &vec,
- std::unordered_set<unsigned> &immResID) {
- for (std::vector<std::unique_ptr<TResource>>::iterator p = vec.begin(); p != vec.end();) {
- std::vector<std::unique_ptr<TResource>>::iterator c = p++;
- if (immResID.count((*c)->GetID()) == 0) {
- p = vec.erase(c);
- }
- }
- }
- static void CollectUsedResource(Value *resID,
- std::unordered_set<Value *> &usedResID) {
- if (usedResID.count(resID) > 0)
- return;
- usedResID.insert(resID);
- if (ConstantInt *cResID = dyn_cast<ConstantInt>(resID)) {
- // Do nothing
- } else if (ZExtInst *ZEI = dyn_cast<ZExtInst>(resID)) {
- if (ZEI->getSrcTy()->isIntegerTy()) {
- IntegerType *ITy = cast<IntegerType>(ZEI->getSrcTy());
- if (ITy->getBitWidth() == 1) {
- usedResID.insert(ConstantInt::get(ZEI->getDestTy(), 0));
- usedResID.insert(ConstantInt::get(ZEI->getDestTy(), 1));
- }
- }
- } else if (SelectInst *SI = dyn_cast<SelectInst>(resID)) {
- CollectUsedResource(SI->getTrueValue(), usedResID);
- CollectUsedResource(SI->getFalseValue(), usedResID);
- } else {
- PHINode *Phi = cast<PHINode>(resID);
- for (Use &U : Phi->incoming_values()) {
- CollectUsedResource(U.get(), usedResID);
- }
- }
- }
- static void ConvertUsedResource(std::unordered_set<unsigned> &immResID,
- std::unordered_set<Value *> &usedResID) {
- for (Value *V : usedResID) {
- if (ConstantInt *cResID = dyn_cast<ConstantInt>(V)) {
- immResID.insert(cResID->getLimitedValue());
- }
- }
- }
- void DxilModule::RemoveUnusedResources() {
- hlsl::OP *hlslOP = GetOP();
- Function *createHandleFunc = hlslOP->GetOpFunc(DXIL::OpCode::CreateHandle, Type::getVoidTy(GetCtx()));
- if (createHandleFunc->user_empty()) {
- m_CBuffers.clear();
- m_UAVs.clear();
- m_SRVs.clear();
- m_Samplers.clear();
- createHandleFunc->eraseFromParent();
- return;
- }
- std::unordered_set<Value *> usedUAVID;
- std::unordered_set<Value *> usedSRVID;
- std::unordered_set<Value *> usedSamplerID;
- std::unordered_set<Value *> usedCBufID;
- // Collect used ID.
- for (User *U : createHandleFunc->users()) {
- CallInst *CI = cast<CallInst>(U);
- Value *vResClass =
- CI->getArgOperand(DXIL::OperandIndex::kCreateHandleResClassOpIdx);
- ConstantInt *cResClass = cast<ConstantInt>(vResClass);
- DXIL::ResourceClass resClass =
- static_cast<DXIL::ResourceClass>(cResClass->getLimitedValue());
- // Skip unused resource handle.
- if (CI->user_empty())
- continue;
- Value *resID =
- CI->getArgOperand(DXIL::OperandIndex::kCreateHandleResIDOpIdx);
- switch (resClass) {
- case DXIL::ResourceClass::CBuffer:
- CollectUsedResource(resID, usedCBufID);
- break;
- case DXIL::ResourceClass::Sampler:
- CollectUsedResource(resID, usedSamplerID);
- break;
- case DXIL::ResourceClass::SRV:
- CollectUsedResource(resID, usedSRVID);
- break;
- case DXIL::ResourceClass::UAV:
- CollectUsedResource(resID, usedUAVID);
- break;
- default:
- DXASSERT(0, "invalid res class");
- break;
- }
- }
- std::unordered_set<unsigned> immUAVID;
- std::unordered_set<unsigned> immSRVID;
- std::unordered_set<unsigned> immSamplerID;
- std::unordered_set<unsigned> immCBufID;
- ConvertUsedResource(immUAVID, usedUAVID);
- RemoveResources(m_UAVs, immUAVID);
- ConvertUsedResource(immSRVID, usedSRVID);
- ConvertUsedResource(immSamplerID, usedSamplerID);
- ConvertUsedResource(immCBufID, usedCBufID);
- RemoveResources(m_SRVs, immSRVID);
- RemoveResources(m_Samplers, immSamplerID);
- RemoveResources(m_CBuffers, immCBufID);
- }
- DxilSignature &DxilModule::GetInputSignature() {
- return *m_InputSignature;
- }
- const DxilSignature &DxilModule::GetInputSignature() const {
- return *m_InputSignature;
- }
- DxilSignature &DxilModule::GetOutputSignature() {
- return *m_OutputSignature;
- }
- const DxilSignature &DxilModule::GetOutputSignature() const {
- return *m_OutputSignature;
- }
- DxilSignature &DxilModule::GetPatchConstantSignature() {
- return *m_PatchConstantSignature;
- }
- const DxilSignature &DxilModule::GetPatchConstantSignature() const {
- return *m_PatchConstantSignature;
- }
- const RootSignatureHandle &DxilModule::GetRootSignature() const {
- return *m_RootSignature;
- }
- void DxilModule::ResetInputSignature(DxilSignature *pValue) {
- m_InputSignature.reset(pValue);
- }
- void DxilModule::ResetOutputSignature(DxilSignature *pValue) {
- m_OutputSignature.reset(pValue);
- }
- void DxilModule::ResetPatchConstantSignature(DxilSignature *pValue) {
- m_PatchConstantSignature.reset(pValue);
- }
- void DxilModule::ResetRootSignature(RootSignatureHandle *pValue) {
- m_RootSignature.reset(pValue);
- }
- DxilTypeSystem &DxilModule::GetTypeSystem() {
- return *m_pTypeSystem;
- }
- void DxilModule::ResetTypeSystem(DxilTypeSystem *pValue) {
- m_pTypeSystem.reset(pValue);
- }
- void DxilModule::EmitLLVMUsed() {
- if (m_LLVMUsed.empty())
- return;
- vector<llvm::Constant*> GVs;
- Type *pI8PtrType = Type::getInt8PtrTy(m_Ctx, DXIL::kDefaultAddrSpace);
- GVs.resize(m_LLVMUsed.size());
- for (size_t i = 0, e = m_LLVMUsed.size(); i != e; i++) {
- Constant *pConst = cast<Constant>(&*m_LLVMUsed[i]);
- PointerType * pPtrType = dyn_cast<PointerType>(pConst->getType());
- if (pPtrType->getPointerAddressSpace() != DXIL::kDefaultAddrSpace) {
- // Cast pointer to addrspace 0, as LLVMUsed elements must have the same type.
- GVs[i] = ConstantExpr::getAddrSpaceCast(pConst, pI8PtrType);
- } else {
- GVs[i] = ConstantExpr::getPointerCast(pConst, pI8PtrType);
- }
- }
- ArrayType *pATy = ArrayType::get(pI8PtrType, GVs.size());
- StringRef llvmUsedName = "llvm.used";
- if (GlobalVariable *oldGV = m_pModule->getGlobalVariable(llvmUsedName)) {
- oldGV->eraseFromParent();
- }
- GlobalVariable *pGV = new GlobalVariable(*m_pModule, pATy, false,
- GlobalValue::AppendingLinkage,
- ConstantArray::get(pATy, GVs),
- llvmUsedName);
- pGV->setSection("llvm.metadata");
- }
- vector<GlobalVariable* > &DxilModule::GetLLVMUsed() {
- return m_LLVMUsed;
- }
- // DXIL metadata serialization/deserialization.
- void DxilModule::EmitDxilMetadata() {
- m_pMDHelper->EmitDxilVersion(m_DxilMajor, m_DxilMinor);
- m_pMDHelper->EmitDxilShaderModel(m_pSM);
- MDTuple *pMDSignatures = m_pMDHelper->EmitDxilSignatures(*m_InputSignature,
- *m_OutputSignature,
- *m_PatchConstantSignature);
- MDTuple *pMDResources = EmitDxilResources();
- MDTuple *pMDProperties = EmitDxilShaderProperties();
- m_pMDHelper->EmitDxilTypeSystem(GetTypeSystem(), m_LLVMUsed);
- EmitLLVMUsed();
- MDTuple *pEntry = m_pMDHelper->EmitDxilEntryPointTuple(GetEntryFunction(), m_EntryName, pMDSignatures, pMDResources, pMDProperties);
- vector<MDNode *> Entries;
- Entries.emplace_back(pEntry);
- m_pMDHelper->EmitDxilEntryPoints(Entries);
- }
- bool DxilModule::IsKnownNamedMetaData(llvm::NamedMDNode &Node) {
- return DxilMDHelper::IsKnownNamedMetaData(Node);
- }
- void DxilModule::LoadDxilMetadata() {
- m_pMDHelper->LoadDxilVersion(m_DxilMajor, m_DxilMinor);
- const ShaderModel *loadedModule;
- m_pMDHelper->LoadDxilShaderModel(loadedModule);
- SetShaderModel(loadedModule);
- DXASSERT(m_InputSignature != nullptr, "else SetShaderModel didn't create input signature");
- const llvm::NamedMDNode *pEntries = m_pMDHelper->GetDxilEntryPoints();
- IFTBOOL(pEntries->getNumOperands() == 1, DXC_E_INCORRECT_DXIL_METADATA);
- Function *pEntryFunc;
- string EntryName;
- const llvm::MDOperand *pSignatures, *pResources, *pProperties;
- m_pMDHelper->GetDxilEntryPoint(pEntries->getOperand(0), pEntryFunc, EntryName, pSignatures, pResources, pProperties);
- SetEntryFunction(pEntryFunc);
- SetEntryFunctionName(EntryName);
- m_pMDHelper->LoadDxilSignatures(*pSignatures, *m_InputSignature,
- *m_OutputSignature, *m_PatchConstantSignature);
- LoadDxilResources(*pResources);
- LoadDxilShaderProperties(*pProperties);
- m_pMDHelper->LoadDxilTypeSystem(*m_pTypeSystem.get());
- }
- MDTuple *DxilModule::EmitDxilResources() {
- // Emit SRV records.
- MDTuple *pTupleSRVs = nullptr;
- if (!m_SRVs.empty()) {
- vector<Metadata *> MDVals;
- for (size_t i = 0; i < m_SRVs.size(); i++) {
- MDVals.emplace_back(m_pMDHelper->EmitDxilSRV(*m_SRVs[i]));
- }
- pTupleSRVs = MDNode::get(m_Ctx, MDVals);
- }
- // Emit UAV records.
- MDTuple *pTupleUAVs = nullptr;
- if (!m_UAVs.empty()) {
- vector<Metadata *> MDVals;
- for (size_t i = 0; i < m_UAVs.size(); i++) {
- MDVals.emplace_back(m_pMDHelper->EmitDxilUAV(*m_UAVs[i]));
- }
- pTupleUAVs = MDNode::get(m_Ctx, MDVals);
- }
- // Emit CBuffer records.
- MDTuple *pTupleCBuffers = nullptr;
- if (!m_CBuffers.empty()) {
- vector<Metadata *> MDVals;
- for (size_t i = 0; i < m_CBuffers.size(); i++) {
- MDVals.emplace_back(m_pMDHelper->EmitDxilCBuffer(*m_CBuffers[i]));
- }
- pTupleCBuffers = MDNode::get(m_Ctx, MDVals);
- }
- // Emit Sampler records.
- MDTuple *pTupleSamplers = nullptr;
- if (!m_Samplers.empty()) {
- vector<Metadata *> MDVals;
- for (size_t i = 0; i < m_Samplers.size(); i++) {
- MDVals.emplace_back(m_pMDHelper->EmitDxilSampler(*m_Samplers[i]));
- }
- pTupleSamplers = MDNode::get(m_Ctx, MDVals);
- }
- if (pTupleSRVs != nullptr || pTupleUAVs != nullptr || pTupleCBuffers != nullptr || pTupleSamplers != nullptr) {
- return m_pMDHelper->EmitDxilResourceTuple(pTupleSRVs, pTupleUAVs, pTupleCBuffers, pTupleSamplers);
- } else {
- return nullptr;
- }
- }
- void DxilModule::LoadDxilResources(const llvm::MDOperand &MDO) {
- if (MDO.get() == nullptr)
- return;
- const llvm::MDTuple *pSRVs, *pUAVs, *pCBuffers, *pSamplers;
- m_pMDHelper->GetDxilResources(MDO, pSRVs, pUAVs, pCBuffers, pSamplers);
- // Load SRV records.
- if (pSRVs != nullptr) {
- for (unsigned i = 0; i < pSRVs->getNumOperands(); i++) {
- unique_ptr<DxilResource> pSRV(new DxilResource);
- m_pMDHelper->LoadDxilSRV(pSRVs->getOperand(i), *pSRV);
- AddSRV(std::move(pSRV));
- }
- }
- // Load UAV records.
- if (pUAVs != nullptr) {
- for (unsigned i = 0; i < pUAVs->getNumOperands(); i++) {
- unique_ptr<DxilResource> pUAV(new DxilResource);
- m_pMDHelper->LoadDxilUAV(pUAVs->getOperand(i), *pUAV);
- AddUAV(std::move(pUAV));
- }
- }
- // Load CBuffer records.
- if (pCBuffers != nullptr) {
- for (unsigned i = 0; i < pCBuffers->getNumOperands(); i++) {
- unique_ptr<DxilCBuffer> pCB(new DxilCBuffer);
- m_pMDHelper->LoadDxilCBuffer(pCBuffers->getOperand(i), *pCB);
- AddCBuffer(std::move(pCB));
- }
- }
- // Load Sampler records.
- if (pSamplers != nullptr) {
- for (unsigned i = 0; i < pSamplers->getNumOperands(); i++) {
- unique_ptr<DxilSampler> pSampler(new DxilSampler);
- m_pMDHelper->LoadDxilSampler(pSamplers->getOperand(i), *pSampler);
- AddSampler(std::move(pSampler));
- }
- }
- }
- MDTuple *DxilModule::EmitDxilShaderProperties() {
- vector<Metadata *> MDVals;
- // DXIL shader flags.
- uint64_t Flags = m_ShaderFlags.GetShaderFlagsRaw();
- if (Flags != 0) {
- MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilShaderFlagsTag));
- MDVals.emplace_back(m_pMDHelper->Uint64ToConstMD(Flags));
- }
- // Compute shader.
- if (m_pSM->IsCS()) {
- MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilNumThreadsTag));
- vector<Metadata *> NumThreadVals;
- NumThreadVals.emplace_back(m_pMDHelper->Uint32ToConstMD(m_NumThreads[0]));
- NumThreadVals.emplace_back(m_pMDHelper->Uint32ToConstMD(m_NumThreads[1]));
- NumThreadVals.emplace_back(m_pMDHelper->Uint32ToConstMD(m_NumThreads[2]));
- MDVals.emplace_back(MDNode::get(m_Ctx, NumThreadVals));
- }
- // Geometry shader.
- if (m_pSM->IsGS()) {
- MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilGSStateTag));
- MDTuple *pMDTuple = m_pMDHelper->EmitDxilGSState(m_InputPrimitive,
- m_MaxVertexCount,
- GetActiveStreamMask(),
- m_StreamPrimitiveTopology,
- m_NumGSInstances);
- MDVals.emplace_back(pMDTuple);
- }
- // Domain shader.
- if (m_pSM->IsDS()) {
- MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilDSStateTag));
- MDTuple *pMDTuple = m_pMDHelper->EmitDxilDSState(m_TessellatorDomain,
- m_InputControlPointCount);
- MDVals.emplace_back(pMDTuple);
- }
- // Hull shader.
- if (m_pSM->IsHS()) {
- MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilHSStateTag));
- MDTuple *pMDTuple = m_pMDHelper->EmitDxilHSState(m_pPatchConstantFunc,
- m_InputControlPointCount,
- m_OutputControlPointCount,
- m_TessellatorDomain,
- m_TessellatorPartitioning,
- m_TessellatorOutputPrimitive,
- m_MaxTessellationFactor);
- MDVals.emplace_back(pMDTuple);
- }
- if (!m_RootSignature->IsEmpty()) {
- MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilRootSignatureTag));
- MDVals.emplace_back(m_pMDHelper->EmitRootSignature(*m_RootSignature.get()));
- }
- if (!MDVals.empty())
- return MDNode::get(m_Ctx, MDVals);
- else
- return nullptr;
- }
- void DxilModule::LoadDxilShaderProperties(const MDOperand &MDO) {
- if (MDO.get() == nullptr)
- return;
- const MDTuple *pTupleMD = dyn_cast<MDTuple>(MDO.get());
- IFTBOOL(pTupleMD != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
- IFTBOOL((pTupleMD->getNumOperands() & 0x1) == 0, DXC_E_INCORRECT_DXIL_METADATA);
- for (unsigned iNode = 0; iNode < pTupleMD->getNumOperands(); iNode += 2) {
- unsigned Tag = DxilMDHelper::ConstMDToUint32(pTupleMD->getOperand(iNode));
- const MDOperand &MDO = pTupleMD->getOperand(iNode + 1);
- IFTBOOL(MDO.get() != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
- switch (Tag) {
- case DxilMDHelper::kDxilShaderFlagsTag:
- m_ShaderFlags.SetShaderFlagsRaw(DxilMDHelper::ConstMDToUint64(MDO));
- break;
- case DxilMDHelper::kDxilNumThreadsTag: {
- MDNode *pNode = cast<MDNode>(MDO.get());
- m_NumThreads[0] = DxilMDHelper::ConstMDToUint32(pNode->getOperand(0));
- m_NumThreads[1] = DxilMDHelper::ConstMDToUint32(pNode->getOperand(1));
- m_NumThreads[2] = DxilMDHelper::ConstMDToUint32(pNode->getOperand(2));
- break;
- }
- case DxilMDHelper::kDxilGSStateTag: {
- m_pMDHelper->LoadDxilGSState(MDO, m_InputPrimitive, m_MaxVertexCount, m_ActiveStreamMask,
- m_StreamPrimitiveTopology, m_NumGSInstances);
- break;
- }
- case DxilMDHelper::kDxilDSStateTag:
- m_pMDHelper->LoadDxilDSState(MDO, m_TessellatorDomain, m_InputControlPointCount);
- break;
- case DxilMDHelper::kDxilHSStateTag:
- m_pMDHelper->LoadDxilHSState(MDO,
- m_pPatchConstantFunc,
- m_InputControlPointCount,
- m_OutputControlPointCount,
- m_TessellatorDomain,
- m_TessellatorPartitioning,
- m_TessellatorOutputPrimitive,
- m_MaxTessellationFactor);
- break;
- case DxilMDHelper::kDxilRootSignatureTag:
- m_pMDHelper->LoadRootSignature(MDO, *m_RootSignature.get());
- break;
- default:
- DXASSERT(false, "Unknown extended shader properties tag");
- break;
- }
- }
- }
- void DxilModule::StripDebugRelatedCode() {
- // Remove all users of global resources.
- for (GlobalVariable &GV : m_pModule->globals()) {
- if (GV.hasInternalLinkage())
- continue;
- if (GV.getType()->getPointerAddressSpace() == DXIL::kTGSMAddrSpace)
- continue;
- for (auto git = GV.user_begin(); git != GV.user_end();) {
- User *U = *(git++);
- // Try to remove load of GV.
- if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
- for (auto it = LI->user_begin(); it != LI->user_end();) {
- Instruction *LIUser = cast<Instruction>(*(it++));
- if (StoreInst *SI = dyn_cast<StoreInst>(LIUser)) {
- Value *Ptr = SI->getPointerOperand();
- SI->eraseFromParent();
- if (Instruction *PtrInst = dyn_cast<Instruction>(Ptr)) {
- if (Ptr->user_empty())
- PtrInst->eraseFromParent();
- }
- }
- }
- if (LI->user_empty())
- LI->eraseFromParent();
- } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
- for (auto GEPIt = GEP->user_begin(); GEPIt != GEP->user_end();) {
- User *GEPU = *(GEPIt++);
- // Try to remove load of GEP.
- if (LoadInst *LI = dyn_cast<LoadInst>(GEPU)) {
- for (auto it = LI->user_begin(); it != LI->user_end();) {
- Instruction *LIUser = cast<Instruction>(*(it++));
- if (StoreInst *SI = dyn_cast<StoreInst>(LIUser)) {
- Value *Ptr = SI->getPointerOperand();
- SI->eraseFromParent();
- if (Instruction *PtrInst = dyn_cast<Instruction>(Ptr)) {
- if (Ptr->user_empty())
- PtrInst->eraseFromParent();
- }
- }
- if (LI->user_empty())
- LI->eraseFromParent();
- }
- }
- }
- if (GEP->user_empty())
- GEP->eraseFromParent();
- }
- }
- }
- }
- DebugInfoFinder &DxilModule::GetOrCreateDebugInfoFinder() {
- if (m_pDebugInfoFinder == nullptr) {
- m_pDebugInfoFinder = std::make_unique<llvm::DebugInfoFinder>();
- m_pDebugInfoFinder->processModule(*m_pModule);
- }
- return *m_pDebugInfoFinder;
- }
- } // namespace hlsl
- namespace llvm {
- hlsl::DxilModule &Module::GetOrCreateDxilModule(bool skipInit) {
- std::unique_ptr<hlsl::DxilModule> M;
- if (!HasDxilModule()) {
- M = std::make_unique<hlsl::DxilModule>(this);
- if (!skipInit) {
- M->LoadDxilMetadata();
- }
- SetDxilModule(M.release());
- }
- return GetDxilModule();
- }
- void Module::ResetDxilModule() {
- if (HasDxilModule()) {
- delete TheDxilModule;
- TheDxilModule = nullptr;
- }
- }
- }
|