From 3f6589a30bc50b5b9f1fa7197473cee2123973ce Mon Sep 17 00:00:00 2001 From: Tex Riddell Date: Tue, 16 Mar 2021 08:26:56 -0700 Subject: [PATCH] Fix coverage bing used to init dx.ishelper when only discard is used (#3589) --- lib/HLSL/DxilPreparePasses.cpp | 14 ++++++++++---- .../helper/IsHelperLane_fallback_no_use.hlsl | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 tools/clang/test/HLSLFileCheck/hlsl/intrinsics/helper/IsHelperLane_fallback_no_use.hlsl diff --git a/lib/HLSL/DxilPreparePasses.cpp b/lib/HLSL/DxilPreparePasses.cpp index f6a017af3a..4b45394c79 100644 --- a/lib/HLSL/DxilPreparePasses.cpp +++ b/lib/HLSL/DxilPreparePasses.cpp @@ -516,9 +516,11 @@ class DxilFinalizeModule : public ModulePass { } } + GlobalVariable *GetIsHelperGV(Module &M) { + return M.getGlobalVariable(DXIL::kDxIsHelperGlobalName, /*AllowLocal*/ true); + } GlobalVariable *GetOrCreateIsHelperGV(Module &M, hlsl::OP *hlslOP) { - GlobalVariable *GV = - M.getGlobalVariable(DXIL::kDxIsHelperGlobalName, /*AllowLocal*/ true); + GlobalVariable *GV = GetIsHelperGV(M); if (GV) return GV; DxilModule &DM = M.GetDxilModule(); @@ -593,7 +595,11 @@ class DxilFinalizeModule : public ModulePass { for (auto uit = F->user_begin(); uit != F->user_end();) { CallInst *CI = cast(*(uit++)); if (!GV) - GV = GetOrCreateIsHelperGV(*F->getParent(), hlslOP); + GV = GetIsHelperGV(*F->getParent()); + // If we don't already have a global for this, + // we didn't have any IsHelper() calls, so no need to add one now. + if (!GV) + return; IRBuilder<> Builder(CI); Value *Cond = Builder.CreateZExt(DxilInst_Discard(CI).get_condition(), I32Ty); @@ -618,7 +624,7 @@ class DxilFinalizeModule : public ModulePass { // in an exported function linked to a PS in another library in this case. // But it won't pass validation otherwise. if (pSM->IsLib() && DXIL::CompareVersions(ValMajor, ValMinor, 1, 6) < 1) { - if (GlobalVariable *GV = M.getGlobalVariable(DXIL::kDxIsHelperGlobalName, /*AllowLocal*/ true)) { + if (GlobalVariable *GV = GetIsHelperGV(M)) { GV->setLinkage(GlobalValue::InternalLinkage); } } diff --git a/tools/clang/test/HLSLFileCheck/hlsl/intrinsics/helper/IsHelperLane_fallback_no_use.hlsl b/tools/clang/test/HLSLFileCheck/hlsl/intrinsics/helper/IsHelperLane_fallback_no_use.hlsl new file mode 100644 index 0000000000..cf880c9155 --- /dev/null +++ b/tools/clang/test/HLSLFileCheck/hlsl/intrinsics/helper/IsHelperLane_fallback_no_use.hlsl @@ -0,0 +1,15 @@ +// RUN: %dxc -E ps -T ps_6_0 %s | FileCheck %s + +// Make sure we don't initialize @dx.ishelper with coverage when IsHelperLane is not used, but discard is. +// CHECK-NOT: call i32 @dx.op.coverage.i32 + +float4 a; + +[shader("pixel")] +float4 ps(float f : IN): SV_Target +{ + if (f < 0.0) + discard; + float4 result = a; + return ddx(result); +}