From 18c0c080320cbe78bfd2a210c8917c1cccf1d4c2 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Fri, 16 Jun 2017 12:22:42 -0700 Subject: [PATCH] 1. Remove ResIndex from ResourceLinkInfo. 2. For none library profile, remove unused functions except entry and patchconstant function. 3. Fix issue caused by lost instanceCount for GS function props. --- include/dxc/HLSL/DxilModule.h | 5 ++--- lib/HLSL/DxilCondenseResources.cpp | 15 --------------- lib/HLSL/DxilGenerationPass.cpp | 14 +++++++++++--- lib/HLSL/DxilMetadataHelper.cpp | 3 +++ lib/HLSL/DxilModule.cpp | 14 ++++---------- tools/clang/test/CodeGenHLSL/lib_resource.hlsl | 6 ++---- 6 files changed, 22 insertions(+), 35 deletions(-) diff --git a/include/dxc/HLSL/DxilModule.h b/include/dxc/HLSL/DxilModule.h index 79cd9d129..fa372519b 100644 --- a/include/dxc/HLSL/DxilModule.h +++ b/include/dxc/HLSL/DxilModule.h @@ -357,11 +357,10 @@ public: void SetShaderProperties(DxilFunctionProps *props); // Shader resource information only needed before linking. - // Use constant as rangeID and index for resource in a library. - // When link the library, replace these constants with real rangeID and index. + // Use constant as rangeID for resource in a library. + // When link the library, replace these constants with real rangeID. struct ResourceLinkInfo { llvm::Constant *ResRangeID; - llvm::Constant *ResIndex; }; private: diff --git a/lib/HLSL/DxilCondenseResources.cpp b/lib/HLSL/DxilCondenseResources.cpp index f23121e22..360be2ef4 100644 --- a/lib/HLSL/DxilCondenseResources.cpp +++ b/lib/HLSL/DxilCondenseResources.cpp @@ -460,7 +460,6 @@ void DxilCondenseResources::PatchCreateHandleForLib(DxilModule &DM) { Function *createHandle = DM.GetOP()->GetOpFunc(DXIL::OpCode::CreateHandle, Type::getVoidTy(DM.GetCtx())); DM.CreateResourceLinkInfo(); - Value *zeroIndex = ConstantInt::get(Type::getInt32Ty(DM.GetCtx()), 0); for (User *U : createHandle->users()) { CallInst *handle = cast(U); DxilInst_CreateHandle createHandle(handle); @@ -484,20 +483,6 @@ void DxilCondenseResources::PatchCreateHandleForLib(DxilModule &DM) { // Update rangeID to linkinfo rangeID. handle->setArgOperand(DXIL::OperandIndex::kCreateHandleResIDOpIdx, linkRangeID); - - Value *Index = createHandle.get_index(); - Value *linkIndex = Builder.CreateLoad(linkInfo.ResIndex); - - if (Index == zeroIndex) { - // Update index to linkinfo index. - handle->setArgOperand(DXIL::OperandIndex::kCreateHandleResIndexOpIdx, - linkIndex); - } else { - // Add linkinfo index to index. - Value *newIdx = Builder.CreateAdd(Index, linkIndex); - handle->setArgOperand(DXIL::OperandIndex::kCreateHandleResIndexOpIdx, - newIdx); - } } } diff --git a/lib/HLSL/DxilGenerationPass.cpp b/lib/HLSL/DxilGenerationPass.cpp index 1e98611c3..f98746b80 100644 --- a/lib/HLSL/DxilGenerationPass.cpp +++ b/lib/HLSL/DxilGenerationPass.cpp @@ -1468,6 +1468,7 @@ public: bool runOnModule(Module &M) override { if (M.HasDxilModule()) { + DxilModule &DM = M.GetDxilModule(); // Remove store undef output. hlsl::OP *hlslOP = M.GetDxilModule().GetOP(); unsigned ValMajor = 0; @@ -1511,9 +1512,17 @@ public: } } // Remove unused external functions. + // For none library profile, remove unused functions except entry and + // patchconstant function. + Function *EntryFunc = DM.GetEntryFunction(); + Function *PatchConstantFunc = DM.GetPatchConstantFunction(); + bool IsLib = DM.GetShaderModel()->IsLib(); + std::vector deadList; for (iplist::iterator F : M.getFunctionList()) { - if (F->isDeclaration()) { + if (&(*F) == EntryFunc || &(*F) == PatchConstantFunc) + continue; + if (F->isDeclaration() || !IsLib) { if (F->user_empty()) deadList.emplace_back(F); } @@ -1563,11 +1572,10 @@ public: } } - DxilModule &DM = M.GetDxilModule(); DenseMap FunctionDIs = makeSubprogramMap(M); // Strip parameters of entry function. - if (!DM.GetShaderModel()->IsLib()) { + if (!IsLib) { if (Function *PatchConstantFunc = DM.GetPatchConstantFunction()) { PatchConstantFunc = StripFunctionParameter(PatchConstantFunc, DM, FunctionDIs); diff --git a/lib/HLSL/DxilMetadataHelper.cpp b/lib/HLSL/DxilMetadataHelper.cpp index dcf83d7b5..adae62299 100644 --- a/lib/HLSL/DxilMetadataHelper.cpp +++ b/lib/HLSL/DxilMetadataHelper.cpp @@ -930,6 +930,8 @@ Function *DxilMDHelper::LoadDxilFunctionProps(MDTuple *pProps, (DXIL::InputPrimitive)ConstMDToUint32(pProps->getOperand(idx++)); props->ShaderProps.GS.maxVertexCount = ConstMDToUint32(pProps->getOperand(idx++)); + props->ShaderProps.GS.instanceCount = + ConstMDToUint32(pProps->getOperand(idx++)); for (size_t i = 0; i < _countof(props->ShaderProps.GS.streamPrimitiveTopologies); ++i) props->ShaderProps.GS.streamPrimitiveTopologies[i] = @@ -985,6 +987,7 @@ DxilMDHelper::EmitDxilFunctionProps(const hlsl::DxilFunctionProps *props, MDVals[valIdx++] = Uint8ToConstMD((uint8_t)props->ShaderProps.GS.inputPrimitive); MDVals[valIdx++] = Uint32ToConstMD(props->ShaderProps.GS.maxVertexCount); + MDVals[valIdx++] = Uint32ToConstMD(props->ShaderProps.GS.instanceCount); for (size_t i = 0; i < _countof(props->ShaderProps.GS.streamPrimitiveTopologies); ++i) MDVals[valIdx++] = Uint8ToConstMD( diff --git a/lib/HLSL/DxilModule.cpp b/lib/HLSL/DxilModule.cpp index 9c6fedaef..76e465bd2 100644 --- a/lib/HLSL/DxilModule.cpp +++ b/lib/HLSL/DxilModule.cpp @@ -860,11 +860,8 @@ static void CreateResourceLinkConstant(Module &M, DxilResourceBase *pRes, GlobalVariable *rangeID = new GlobalVariable( M, i32Ty, IsConstantTrue, llvm::GlobalValue::ExternalLinkage, NullInitVal, pRes->GetGlobalName() + "_rangeID"); - GlobalVariable *index = new GlobalVariable( - M, i32Ty, IsConstantTrue, llvm::GlobalValue::ExternalLinkage, NullInitVal, - pRes->GetGlobalName() + "_index"); - resLinkInfo.emplace_back(DxilModule::ResourceLinkInfo{rangeID, index}); + resLinkInfo.emplace_back(DxilModule::ResourceLinkInfo{rangeID}); } void DxilModule::CreateResourceLinkInfo() { @@ -1402,7 +1399,6 @@ static MDTuple *CreateResourcesLinkInfo(std::vector MDVals; for (size_t i = 0; i < size; i++) { MDVals.emplace_back(ValueAsMetadata::get(LinkInfoList[i].ResRangeID)); - MDVals.emplace_back(ValueAsMetadata::get(LinkInfoList[i].ResIndex)); } return MDNode::get(Ctx, MDVals); } @@ -1440,13 +1436,11 @@ LoadResourcesLinkInfo(const llvm::MDTuple *pMD, return; } unsigned operandSize = pMD->getNumOperands(); - IFTBOOL(operandSize == (2 * size), DXC_E_INCORRECT_DXIL_METADATA); - for (unsigned i = 0; i < operandSize; i += 2) { + IFTBOOL(operandSize == size, DXC_E_INCORRECT_DXIL_METADATA); + for (unsigned i = 0; i < operandSize; i++) { Constant *rangeID = dyn_cast(pMDHelper->ValueMDToValue(pMD->getOperand(i))); - Constant *index = - dyn_cast(pMDHelper->ValueMDToValue(pMD->getOperand(i + 1))); - LinkInfoList.emplace_back(DxilModule::ResourceLinkInfo{rangeID, index}); + LinkInfoList.emplace_back(DxilModule::ResourceLinkInfo{rangeID}); } } diff --git a/tools/clang/test/CodeGenHLSL/lib_resource.hlsl b/tools/clang/test/CodeGenHLSL/lib_resource.hlsl index be1957025..bafd4eb44 100644 --- a/tools/clang/test/CodeGenHLSL/lib_resource.hlsl +++ b/tools/clang/test/CodeGenHLSL/lib_resource.hlsl @@ -2,14 +2,12 @@ // Make sure globals for link info exist. // CHECK: g_txDiffuse_rangeID -// CHECK: g_txDiffuse_index // CHECK: g_samLinear_rangeID -// CHECK: g_samLinear_index // Make sure link info metadata exist. // CHECK: dx.resources.link.info -// CHECK: !{i32* @g_txDiffuse_rangeID, i32* @g_txDiffuse_index} -// CHECK: !{i32* @g_samLinear_rangeID, i32* @g_samLinear_index} +// CHECK: !{i32* @g_txDiffuse_rangeID} +// CHECK: !{i32* @g_samLinear_rangeID} Texture2D g_txDiffuse;