Преглед изворни кода

Make sure default val for cbuffer element is ignored. (#921)

Xiang Li пре 7 година
родитељ
комит
6539689175

+ 5 - 0
tools/clang/lib/AST/ExprConstant.cpp

@@ -2038,6 +2038,11 @@ static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
     Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
     return false;
   }
+  // HLSL Change Begin - External variable is in cbuffer, cannot use as immediate.
+  if (VD->hasExternalFormalLinkage() &&
+      !isa<EnumConstantDecl>(VD))
+    return false;
+  // HLSL Change End.
 
   // Check that we can fold the initializer. In C++, we will have already done
   // this in the cases where it matters for conformance.

+ 7 - 1
tools/clang/lib/CodeGen/CGExprConstant.cpp

@@ -1209,7 +1209,13 @@ llvm::Constant *CodeGenModule::EmitConstantInit(const VarDecl &D,
           return EmitNullConstant(D.getType());
       }
   }
-  
+
+  // HLSL Change Begin - External variable is in cbuffer, cannot use as immediate.
+  if (D.hasExternalFormalLinkage() &&
+      !isa<EnumConstantDecl>(&D))
+    return nullptr;
+  // HLSL Change End.
+
   if (const APValue *Value = D.evaluateValue())
     return EmitConstantValueForMemory(*Value, D.getType(), CGF);
 

+ 11 - 0
tools/clang/test/CodeGenHLSL/quick-test/const_init.hlsl

@@ -0,0 +1,11 @@
+// RUN: %dxc -E main -T ps_6_0 %s | FileCheck %s
+
+// Make sure default val for cbuffer element is ignored.
+// CHECK: fadd
+
+float c = 0.91;
+
+float main() : SV_Target {
+  const float x = 1+c;
+  return x;
+}