Browse Source

Do not generate LoopID metadata if no attribute is added. (#271)

Xiang Li 8 years ago
parent
commit
66f99308c0

+ 6 - 1
tools/clang/lib/CodeGen/CGStmt.cpp

@@ -709,7 +709,12 @@ void CodeGenFunction::EmitCondBrHints(llvm::LLVMContext &Context,
   }
 
   // FIXME: This condition is never false.  Should it be an assert?
-  if (!Metadata.empty()) {
+  if ( // HLSL Change Begin.
+       // We only want to enter this if we found a llvm loop attribute and we
+       // know we found an llvm attribute if the metadata size > 1.
+      Metadata.size() > 1
+      // HLSL Change End.
+      ) {
     // Add llvm.loop MDNode to CondBr.
     llvm::MDNode *LoopID = llvm::MDNode::get(Context, Metadata);
     LoopID->replaceOperandWith(0, LoopID); // First op points to itself.

+ 27 - 0
tools/clang/test/CodeGenHLSL/unroll_dbg.hlsl

@@ -0,0 +1,27 @@
+// RUN: %dxc -E main -T vs_6_0  -Zi %s
+
+struct X {
+   float a;
+   float b;
+};
+float a;
+float b;
+static X g = { a, b};
+
+float4 loop(float4 position) {
+    const uint loopCount = 4;
+
+    [unroll(loopCount)]
+    for(uint i = 0; i < loopCount; ++i)
+        position.x += 1.0f;
+    return position;
+}
+
+float4 main() : SV_Position
+{
+    float4 position = g.b;
+
+
+
+    return loop(position) + g.a;
+}

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

@@ -583,6 +583,7 @@ public:
   TEST_METHOD(CodeGenUint64_2)
   TEST_METHOD(CodeGenUintSample)
   TEST_METHOD(CodeGenUmaxObjectAtomic)
+  TEST_METHOD(CodeGenUnrollDbg)
   TEST_METHOD(CodeGenUnsignedShortHandMatrixVector)
   TEST_METHOD(CodeGenUnusedCB)
   TEST_METHOD(CodeGenUpdateCounter)
@@ -3234,6 +3235,10 @@ TEST_F(CompilerTest, CodeGenUmaxObjectAtomic) {
   CodeGenTestCheck(L"..\\CodeGenHLSL\\umaxObjectAtomic.hlsl");
 }
 
+TEST_F(CompilerTest, CodeGenUnrollDbg) {
+  CodeGenTest(L"..\\CodeGenHLSL\\unroll_dbg.hlsl");
+}
+
 TEST_F(CompilerTest, CodeGenUnsignedShortHandMatrixVector) {
   CodeGenTestCheck(L"..\\CodeGenHLSL\\unsignedShortHandMatrixVector.hlsl");
 }