diff options
| author | Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> | 2025-01-13 15:50:40 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-13 12:50:40 -0800 |
| commit | 9a926058f45c7adfa569946b4f6b15a8772ac035 (patch) | |
| tree | e98b715e1b8b49bc0227657cee4c8b1099e8d2bf | |
| parent | 7bcc1a817ece5e708bd0fe2fd846cfe54c55ba64 (diff) | |
Don't initialize temp var for out parameters. (#6076)
| -rw-r--r-- | source/slang/slang-ir-autodiff-rev.cpp | 10 | ||||
| -rw-r--r-- | tests/autodiff/out-parameters-2.slang | 49 |
2 files changed, 55 insertions, 4 deletions
diff --git a/source/slang/slang-ir-autodiff-rev.cpp b/source/slang/slang-ir-autodiff-rev.cpp index 65ce69877..3237ba3b2 100644 --- a/source/slang/slang-ir-autodiff-rev.cpp +++ b/source/slang/slang-ir-autodiff-rev.cpp @@ -528,10 +528,12 @@ InstPair BackwardDiffTranscriber::transcribeFuncHeader(IRBuilder* inBuilder, IRF // If primal parameter is mutable, we need to pass in a temp var. auto tempVar = builder.emitVar(primalParamPtrType->getValueType()); - // We also need to setup the initial value of the temp var, otherwise - // the temp var will be uninitialized which could cause undefined behavior - // in the primal function. - builder.emitStore(tempVar, primalArg); + // If the parameter is not a pure 'out' param, we also need to setup the initial + // value of the temp var, otherwise the temp var will be uninitialized which could + // cause undefined behavior in the primal function. + // + if (!as<IROutType>(primalParamType)) + builder.emitStore(tempVar, primalArg); primalArgs.add(tempVar); } diff --git a/tests/autodiff/out-parameters-2.slang b/tests/autodiff/out-parameters-2.slang new file mode 100644 index 000000000..b4c4b07c6 --- /dev/null +++ b/tests/autodiff/out-parameters-2.slang @@ -0,0 +1,49 @@ +//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-slang -compute -shaderobj -output-using-type +//TEST(compute, vulkan):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-vk -compute -shaderobj -output-using-type + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +typedef DifferentialPair<float> dpfloat; + +struct Foo : IDifferentiable +{ + float a; + int b; +} + +[PreferCheckpoint] +float k() +{ + return outputBuffer[3] + 1; +} + +[Differentiable] +void h(float x, float y, out Foo result) +{ + float p = no_diff k(); + float m = x + y + p; + float n = x - y; + float r = m * n + 2 * x * y; + + result = {r, 2}; +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + float x = 2.0; + float y = 3.5; + float dx = 1.0; + float dy = 0.5; + + dpfloat dresult; + dpfloat dpx = diffPair(x); + dpfloat dpy = diffPair(y); + Foo.Differential dFoo; + dFoo.a = 1.0; + bwd_diff(h)(dpx, dpy, dFoo); + + outputBuffer[0] = dpx.d; // CHECK: 12.0 + outputBuffer[1] = dpy.d; // CHECK: -4.0 +}
\ No newline at end of file |
