瀏覽代碼

More fix for integer cbuffer variable has init val. (#924)

Xiang Li 7 年之前
父節點
當前提交
be1e6b130e

+ 11 - 0
tools/clang/lib/AST/Expr.cpp

@@ -3338,6 +3338,17 @@ Expr::isNullPointerConstant(ASTContext &Ctx,
       (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
       (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
     return NPCK_NotNull;
     return NPCK_NotNull;
 
 
+  // HLSL Change Begin -External variable is in cbuffer, cannot use as immediate.
+  if (getStmtClass() == Stmt::DeclRefExprClass && Ctx.getLangOpts().HLSL) {
+    const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(this);
+    const ValueDecl *VD = DRE->getDecl();
+    // External variable is in cbuffer, cannot use as immediate.
+    if (VD->hasExternalFormalLinkage() &&
+        !isa<EnumConstantDecl>(VD))
+      return NPCK_NotNull;
+  }
+  // HLSL Change End.
+
   if (Ctx.getLangOpts().CPlusPlus11) {
   if (Ctx.getLangOpts().CPlusPlus11) {
     // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
     // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
     // value zero or a prvalue of type std::nullptr_t.
     // value zero or a prvalue of type std::nullptr_t.

+ 8 - 0
tools/clang/lib/Sema/SemaExpr.cpp

@@ -13500,6 +13500,14 @@ static void DoMarkVarDeclReferenced(Sema &SemaRef, SourceLocation Loc,
 
 
   if(!MarkODRUsed) return;
   if(!MarkODRUsed) return;
 
 
+  // HLSL Change Begin -External variable is in cbuffer, cannot use as immediate.
+  // Mark used for referenced external variable.
+  if (SemaRef.getLangOpts().HLSL && Var->hasExternalFormalLinkage() &&
+      !isa<EnumConstantDecl>(Var))
+    MarkVarDeclODRUsed(Var, Loc, SemaRef,
+                       /*MaxFunctionScopeIndex ptr*/ nullptr);
+  // HLSL Change End.
+
   // Per C++11 [basic.def.odr], a variable is odr-used "unless it satisfies
   // Per C++11 [basic.def.odr], a variable is odr-used "unless it satisfies
   // the requirements for appearing in a constant expression (5.19) and, if
   // the requirements for appearing in a constant expression (5.19) and, if
   // it is an object, the lvalue-to-rvalue conversion (4.1)
   // it is an object, the lvalue-to-rvalue conversion (4.1)

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

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