浏览代码

Fix coverage bing used to init dx.ishelper when only discard is used (#3589)

Tex Riddell 4 年之前
父节点
当前提交
3f6589a30b

+ 10 - 4
lib/HLSL/DxilPreparePasses.cpp

@@ -516,9 +516,11 @@ public:
     }
   }
 
+  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 @@ public:
       for (auto uit = F->user_begin(); uit != F->user_end();) {
         CallInst *CI = cast<CallInst>(*(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 @@ public:
       // 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);
         }
       }

+ 15 - 0
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);
+}