diff options
| author | kaizhangNV <149626564+kaizhangNV@users.noreply.github.com> | 2025-07-09 11:25:29 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-09 09:25:29 -0700 |
| commit | a670bafc121c20168624f70a388dbe8556402c7f (patch) | |
| tree | 79b48a80e7abc0744193716e400bb57a6c026bad /tests/diagnostics/force-no-diff-this.slang | |
| parent | a7cb36901ccaf8297136c58c1451d6e04420af73 (diff) | |
no_diff diagnostics improvement (#7655)
close #6286.
This PR is to improve the diagnostics for no_diff usage.
In a differentiable function, any calls to a non-diff function with constant arguments should not require no_diff attribute.
This PR adds this extra check at `checkAutoDiffUsages` where it checks the differentiability on IR.
In a differentiable method, we will force to use `[NoDiffThis]` attribute if there is access to non-differentiable `This` type. Once this access is detected we will report a warning to bring users attention that this access won't generate any derivative, they have to use `[NoDiffThis]` to suppress that warning.
This PR adds this check at type checking stage, because it's the easiest way to find out all the `This` accesses.
Diffstat (limited to 'tests/diagnostics/force-no-diff-this.slang')
| -rw-r--r-- | tests/diagnostics/force-no-diff-this.slang | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/tests/diagnostics/force-no-diff-this.slang b/tests/diagnostics/force-no-diff-this.slang new file mode 100644 index 000000000..ae1464ffb --- /dev/null +++ b/tests/diagnostics/force-no-diff-this.slang @@ -0,0 +1,42 @@ +//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): + +struct MyStruct<T> where T: __BuiltinFloatingPointType +{ + float a; + __init(float a) { this.a = a;} + + [Differentiable] + T eval(T x) + { + //CHECK: ([[# @LINE+1]]): warning 31159 + return exp(x * T(a) * T(a)); + } + + [Differentiable] + [NoDiffThis] + T eval1(T x) + { + //CHECK-NOT: ([[# @LINE+1]]): warning 31159 + return exp(x * T(a) * T(a)); + } +}; + +[Differentiable] +float evalFunc(float x) +{ + MyStruct<float> s = {x}; + return s.eval(x) + s.eval1(x); +} + +RWStructuredBuffer<float> output; + +[shader("compute")] +[numthreads(1,1,1)] +void computeMain(uint id : SV_DispatchThreadID) +{ + var x = diffPair(2.0f); + bwd_diff(evalFunc)(x, 1.0f); + + output[0] = x.d; +} + |
