Prechádzať zdrojové kódy

Generate global variable for local constant.

Xiang Li 7 rokov pred
rodič
commit
b3cfa21fa1

+ 12 - 0
tools/clang/lib/CodeGen/CGDecl.cpp

@@ -138,6 +138,18 @@ void CodeGenFunction::EmitVarDecl(const VarDecl &D) {
 
     return EmitStaticVarDecl(D, Linkage);
   }
+  // HLSL Change Begin - treat local constant as static.
+  // Global variable will be generated instead of alloca.
+  if (D.getType().isConstQualified() && D.isLocalVarDecl()) {
+    llvm::Constant *Init = CGM.EmitConstantInit(D, this);
+    // Only create global when has constant init.
+    if (Init) {
+      llvm::GlobalValue::LinkageTypes Linkage =
+          CGM.getLLVMLinkageVarDefinition(&D, /*isConstant=*/false);
+      return EmitStaticVarDecl(D, Linkage);
+    }
+  }
+  // HLSL Change End.
 
   if (D.hasExternalStorage())
     // Don't emit it now, allow it to be emitted lazily on its first use.

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

@@ -0,0 +1,11 @@
+// RUN: %dxc -T ps_6_0 -E main  -fcgl %s | FileCheck %s
+
+// Make sure global variable is created for local constant.
+// CHECK: internal constant [3 x float]
+// CHECK-NOT: alloca [3 x float]
+
+float main(uint i : A) : SV_TARGET
+{
+  const float cb[] = {1.3, 1.2, 3.3};
+  return cb[i];
+}