summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYong He <yhe@nvidia.com>2020-06-04 12:42:17 -0700
committerYong He <yhe@nvidia.com>2020-06-04 12:42:17 -0700
commit4e2f2771eb2f8991014d957848a6a25aa49c0aaf (patch)
tree5d80cc746b1b83ca9feb46cbaab51012506243cf
parent1b8731c809761c4e2dbec81dcee207f8a4621903 (diff)
Emit [loop] attribute to output HLSL.
-rw-r--r--source/slang/slang-emit-hlsl.cpp9
-rw-r--r--source/slang/slang-ir-insts.h1
-rw-r--r--source/slang/slang-lower-to-ir.cpp4
-rw-r--r--tests/cross-compile/loop-attribs.slang19
-rw-r--r--tests/cross-compile/loop-attribs.slang.expected61
5 files changed, 93 insertions, 1 deletions
diff --git a/source/slang/slang-emit-hlsl.cpp b/source/slang/slang-emit-hlsl.cpp
index 5ebd7e9fc..b15680f29 100644
--- a/source/slang/slang-emit-hlsl.cpp
+++ b/source/slang/slang-emit-hlsl.cpp
@@ -673,9 +673,16 @@ void HLSLSourceEmitter::emitVectorTypeNameImpl(IRType* elementType, IRIntegerVal
void HLSLSourceEmitter::emitLoopControlDecorationImpl(IRLoopControlDecoration* decl)
{
- if (decl->getMode() == kIRLoopControl_Unroll)
+ switch (decl->getMode())
{
+ case kIRLoopControl_Unroll:
m_writer->emit("[unroll]\n");
+ break;
+ case kIRLoopControl_Loop:
+ m_writer->emit("[loop]\n");
+ break;
+ default:
+ SLANG_UNREACHABLE("emit loop control decoration");
}
}
diff --git a/source/slang/slang-ir-insts.h b/source/slang/slang-ir-insts.h
index 06a5c1412..0ae05311b 100644
--- a/source/slang/slang-ir-insts.h
+++ b/source/slang/slang-ir-insts.h
@@ -42,6 +42,7 @@ struct IRHighLevelDeclDecoration : IRDecoration
enum IRLoopControl
{
kIRLoopControl_Unroll,
+ kIRLoopControl_Loop,
};
struct IRLoopControlDecoration : IRDecoration
diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp
index f369729d2..030c5fc74 100644
--- a/source/slang/slang-lower-to-ir.cpp
+++ b/source/slang/slang-lower-to-ir.cpp
@@ -3281,6 +3281,10 @@ struct StmtLoweringVisitor : StmtVisitor<StmtLoweringVisitor>
{
getBuilder()->addLoopControlDecoration(inst, kIRLoopControl_Unroll);
}
+ else if( stmt->findModifier<LoopAttribute>() )
+ {
+ getBuilder()->addLoopControlDecoration(inst, kIRLoopControl_Loop);
+ }
// TODO: handle other cases here
}
diff --git a/tests/cross-compile/loop-attribs.slang b/tests/cross-compile/loop-attribs.slang
new file mode 100644
index 000000000..ddedb67df
--- /dev/null
+++ b/tests/cross-compile/loop-attribs.slang
@@ -0,0 +1,19 @@
+// loop-attribs.slang
+// Test that loop attributes are correctly emitted to the resulting HLSL.
+
+//TEST:SIMPLE:-target hlsl -entry main -stage fragment -profile sm_6_0
+
+float4 main() : SV_Target
+{
+ float sum = 0.0f;
+
+ [loop]
+ for (int i = 0; i < 100; i++)
+ sum += float(i);
+
+ [unroll(10)]
+ for (int j = 0; j < 100; j++)
+ sum += float(j);
+
+ return float4(sum, 0, 0, 0);
+} \ No newline at end of file
diff --git a/tests/cross-compile/loop-attribs.slang.expected b/tests/cross-compile/loop-attribs.slang.expected
new file mode 100644
index 000000000..0ecf0194f
--- /dev/null
+++ b/tests/cross-compile/loop-attribs.slang.expected
@@ -0,0 +1,61 @@
+result code = 0
+standard error = {
+}
+standard output = {
+#pragma pack_matrix(column_major)
+
+#line 6 "tests/cross-compile/loop-attribs.slang"
+vector<float,4> main() : SV_TARGET
+{
+ int i_0;
+ float sum_0;
+ int j_0;
+ float sum_1;
+ i_0 = int(0);
+ sum_0 = 0.00000000000000000000;
+ [loop]
+ for(;;)
+ {
+
+#line 11
+ if(i_0 < int(100))
+ {
+ }
+ else
+ {
+ break;
+ }
+ float _S1 = sum_0 + (float) i_0;
+
+#line 11
+ int _S2 = i_0 + (int) int(1);
+ i_0 = _S2;
+ sum_0 = _S1;
+ }
+ j_0 = int(0);
+ sum_1 = sum_0;
+ [unroll]
+ for(;;)
+ {
+
+#line 15
+ if(j_0 < int(100))
+ {
+ }
+ else
+ {
+ break;
+ }
+ float _S3 = sum_1 + (float) j_0;
+
+#line 15
+ int _S4 = j_0 + (int) int(1);
+ j_0 = _S4;
+ sum_1 = _S3;
+ }
+
+#line 18
+ return vector<float,4>(sum_1, (float) int(0), (float) int(0), (float) int(0));
+}
+
+}