瀏覽代碼

Generate global variable for local constant.

Xiang Li 7 年之前
父節點
當前提交
b3cfa21fa1
共有 2 個文件被更改,包括 23 次插入0 次删除
  1. 12 0
      tools/clang/lib/CodeGen/CGDecl.cpp
  2. 11 0
      tools/clang/test/CodeGenHLSL/quick-test/local_constant.hlsl

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

@@ -138,6 +138,18 @@ void CodeGenFunction::EmitVarDecl(const VarDecl &D) {
 
 
     return EmitStaticVarDecl(D, Linkage);
     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())
   if (D.hasExternalStorage())
     // Don't emit it now, allow it to be emitted lazily on its first use.
     // 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];
+}