Browse Source

Put createHandle for CBuffer to entry block when possible. (#42)

Xiang Li 8 years ago
parent
commit
e1f6fdbc19

+ 14 - 2
lib/HLSL/DxilGenerationPass.cpp

@@ -2054,7 +2054,10 @@ void DxilGenerationPass::GenerateDxilCBufferHandles(std::unordered_map<Instructi
       for (auto U : GV->users()) {
         // Must CBufferSubscript.
         CallInst *CI = cast<CallInst>((U));
-        IRBuilder<> Builder(CI);
+        // Put createHandle to entry block.
+        auto InsertPt =
+            CI->getParent()->getParent()->getEntryBlock().getFirstInsertionPt();
+        IRBuilder<> Builder(InsertPt);
 
         CallInst *handle = Builder.CreateCall(createHandle, args, handleName);
         if (m_HasDbgInfo) {
@@ -2068,8 +2071,17 @@ void DxilGenerationPass::GenerateDxilCBufferHandles(std::unordered_map<Instructi
         // Must CBufferSubscript.
         CallInst *CI = cast<CallInst>(U);
         IRBuilder<> Builder(CI);
+        Value *CBIndex = CI->getArgOperand(HLOperandIndex::kSubscriptIndexOpIdx);
         args[DXIL::OperandIndex::kCreateHandleResIndexOpIdx] =
-            CI->getArgOperand(HLOperandIndex::kSubscriptIndexOpIdx);
+            CBIndex;
+        if (isa<ConstantInt>(CBIndex)) {
+          // Put createHandle to entry block for const index.
+          auto InsertPt = CI->getParent()
+                              ->getParent()
+                              ->getEntryBlock()
+                              .getFirstInsertionPt();
+          Builder.SetInsertPoint(InsertPt);
+        }
         CallInst *handle = Builder.CreateCall(createHandle, args, handleName);
         handleMap[CI] = handle;
       }

+ 6 - 5
tools/clang/test/CodeGenHLSL/bindings1.hlsl

@@ -93,11 +93,6 @@
 // CHECK: ; RWTex1                                UAV     f32          2d      U3             u0     4
 
 // CHECK: %struct.Resources = type { %class.Texture2D, %class.Texture2D.0, %class.Texture2D, %class.Texture2D.0, %class.RWTexture2D, %class.RWTexture2D, %class.RWTexture2D, %class.RWTexture2D, %struct.SamplerComparisonState, %struct.SamplerState, %struct.SamplerComparisonState, %struct.SamplerState, <4 x float> }
-
-//                                                CreateHandle(resourceClass,rangeId,index,nonUniformIndex)
-// CHECK: %RWTex2_UAV_2d = call %dx.types.Handle @dx.op.createHandle(i32 59, i8 1, i32 0, i32 7, i1 false)
-// CHECK: %Tex1_texture_2d = call %dx.types.Handle @dx.op.createHandle(i32 59, i8 0, i32 0, i32 0, i1 false)
-// CHECK: %Samp2_sampler = call %dx.types.Handle @dx.op.createHandle(i32 59, i8 3, i32 0, i32 0, i1 false)
 // CHECK: %tbuf4_buffer = call %dx.types.Handle @dx.op.createHandle(i32 59, i8 0, i32 8, i32 4, i1 false)
 // CHECK: %tbuf2_buffer = call %dx.types.Handle @dx.op.createHandle(i32 59, i8 0, i32 7, i32 2, i1 false)
 // CHECK: %tbuf3_buffer = call %dx.types.Handle @dx.op.createHandle(i32 59, i8 0, i32 6, i32 6, i1 false)
@@ -105,8 +100,14 @@
 // CHECK: %buf2_buffer = call %dx.types.Handle @dx.op.createHandle(i32 59, i8 2, i32 4, i32 55, i1 false)
 // CHECK: %buf1_buffer = call %dx.types.Handle @dx.op.createHandle(i32 59, i8 2, i32 3, i32 104, i1 false)
 // CHECK: %buf4_buffer = call %dx.types.Handle @dx.op.createHandle(i32 59, i8 2, i32 2, i32 1, i1 false)
+
 // CHECK: %MyCB_buffer = call %dx.types.Handle @dx.op.createHandle(i32 59, i8 2, i32 0, i32 11, i1 false)
 // CHECK: %MyTB_buffer = call %dx.types.Handle @dx.op.createHandle(i32 59, i8 0, i32 4, i32 11, i1 false)
+
+// CHECK: %RWTex2_UAV_2d = call %dx.types.Handle @dx.op.createHandle(i32 59, i8 1, i32 0, i32 7, i1 false)
+// CHECK: %Tex1_texture_2d = call %dx.types.Handle @dx.op.createHandle(i32 59, i8 0, i32 0, i32 0, i1 false)
+// CHECK: %Samp2_sampler = call %dx.types.Handle @dx.op.createHandle(i32 59, i8 3, i32 0, i32 0, i1 false)
+
 // CHECK: %Tex2_texture_2d = call %dx.types.Handle @dx.op.createHandle(i32 59, i8 0, i32 1, i32 30, i1 false)
 // CHECK: %Tex3_texture_2d = call %dx.types.Handle @dx.op.createHandle(i32 59, i8 0, i32 2, i32 94, i1 false)
 // CHECK: %Tex4_texture_2d = call %dx.types.Handle @dx.op.createHandle(i32 59, i8 0, i32 3, i32 10, i1 false)

+ 18 - 0
tools/clang/test/CodeGenHLSL/cbufferInLoop.hlsl

@@ -0,0 +1,18 @@
+// RUN: %dxc -E main -T ps_6_0 %s
+
+float4 c;
+
+float4 GetCB() {
+  return c;
+}
+
+uint n;
+
+float4 main() : SV_TARGET
+{
+  float4 g=0;
+  for (int i=0;i<n;i++) {
+    g += GetCB();
+  }
+  return g;
+}

+ 5 - 0
tools/clang/unittests/HLSL/CompilerTest.cpp

@@ -322,6 +322,7 @@ public:
   TEST_METHOD(CodeGenCbuffer6_51)
   TEST_METHOD(CodeGenCbufferAlloc)
   TEST_METHOD(CodeGenCbufferAllocLegacy)
+  TEST_METHOD(CodeGenCbufferInLoop)
   TEST_METHOD(CodeGenClipPlanes)
   TEST_METHOD(CodeGenConstoperand1)
   TEST_METHOD(CodeGenDiscard)
@@ -1802,6 +1803,10 @@ TEST_F(CompilerTest, CodeGenCbufferAllocLegacy) {
   CodeGenTestCheck(L"..\\CodeGenHLSL\\cbufferAlloc_legacy.hlsl");
 }
 
+TEST_F(CompilerTest, CodeGenCbufferInLoop) {
+  CodeGenTest(L"..\\CodeGenHLSL\\cbufferInLoop.hlsl");
+}
+
 TEST_F(CompilerTest, CodeGenClipPlanes) {
   CodeGenTestCheck(L"..\\CodeGenHLSL\\clip_planes.hlsl");
 }