summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2023-03-27 10:05:07 -0700
committerGitHub <noreply@github.com>2023-03-27 10:05:07 -0700
commit2179480e28bdd46c71cec269a8f55ba93aa54f53 (patch)
treef0b253c0b15956f3e60145b11e16aee6ef33fae1
parent333968af7344bcf0ef77818d076efcf88acd159d (diff)
Fix lowering crash in [BackwardDerivativeOf]. (#2737)
Co-authored-by: Yong He <yhe@nvidia.com>
-rw-r--r--source/slang/slang-lower-to-ir.cpp6
-rw-r--r--tests/autodiff/custom-derivative-array-param.slang30
-rw-r--r--tests/autodiff/custom-derivative-array-param.slang.expected.txt5
3 files changed, 38 insertions, 3 deletions
diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp
index 9d424d1e8..6d4e50463 100644
--- a/source/slang/slang-lower-to-ir.cpp
+++ b/source/slang/slang-lower-to-ir.cpp
@@ -8088,7 +8088,7 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo>
return type->getOp() == kIROp_ClassType;
}
- LoweredValInfo lowerFuncDeclInContext(IRGenContext* subContext, IRBuilder* subBuilder, FunctionDeclBase* decl)
+ LoweredValInfo lowerFuncDeclInContext(IRGenContext* subContext, IRBuilder* subBuilder, FunctionDeclBase* decl, bool emitBody = true)
{
auto outerGeneric = emitOuterGenerics(subContext, decl, decl);
@@ -8156,7 +8156,7 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo>
// (although we might have to give in eventually), so
// this case should really only occur for builtin declarations.
}
- else
+ else if (emitBody)
{
// This is a function definition, so we need to actually
// construct IR for the body...
@@ -8610,7 +8610,7 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo>
auto originalFuncDecl = as<FunctionDeclBase>(originalDeclRefExpr->declRef.getDecl());
SLANG_RELEASE_ASSERT(originalFuncDecl);
- auto originalFuncVal = lowerFuncDeclInContext(originalSubContext, originalSubBuilder, originalFuncDecl).val;
+ auto originalFuncVal = lowerFuncDeclInContext(originalSubContext, originalSubBuilder, originalFuncDecl, false).val;
if (auto originalFuncGeneric = as<IRGeneric>(originalFuncVal))
{
originalFuncVal = findGenericReturnVal(originalFuncGeneric);
diff --git a/tests/autodiff/custom-derivative-array-param.slang b/tests/autodiff/custom-derivative-array-param.slang
new file mode 100644
index 000000000..d50454b7a
--- /dev/null
+++ b/tests/autodiff/custom-derivative-array-param.slang
@@ -0,0 +1,30 @@
+//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type
+//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type
+
+//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<float> outputBuffer;
+
+struct NonDiff
+{
+ float a;
+}
+
+void getCode(uint2 x, out Array<float, 2> v)
+{
+ for (int i = 0; i < 8; i++)
+ v[i] = (float)i;
+}
+
+[BackwardDerivativeOf(getCode)]
+void getCode_bwd(uint2 x, Array<float, 2> dout)
+{
+ outputBuffer[0] = dout[0];
+}
+
+[numthreads(1, 1, 1)]
+void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID)
+{
+ float a = 10.0;
+ float inArray[2] = { 1, 2 };
+ __bwd_diff(getCode)(uint2(1,2), inArray);
+} \ No newline at end of file
diff --git a/tests/autodiff/custom-derivative-array-param.slang.expected.txt b/tests/autodiff/custom-derivative-array-param.slang.expected.txt
new file mode 100644
index 000000000..5fce3dc6d
--- /dev/null
+++ b/tests/autodiff/custom-derivative-array-param.slang.expected.txt
@@ -0,0 +1,5 @@
+type: float
+1.000000
+0.000000
+0.000000
+0.000000 \ No newline at end of file