From 2226ae0bbbf2cbd5ea2da8aaaa04c9c466af56c3 Mon Sep 17 00:00:00 2001 From: winmad Date: Fri, 14 Apr 2023 11:36:19 -0700 Subject: Bugfix: compiler will run forever to eliminate dead code (#2809) * Add a test case that the compile will run forever * Fix. * fix. --------- Co-authored-by: Lifan Wu Co-authored-by: Yong He --- source/slang/slang-ir.cpp | 2 ++ tests/autodiff/for-loop-eliminate-dead-code.slang | 41 ++++++++++++++++++++++ ...for-loop-eliminate-dead-code.slang.expected.txt | 4 +++ tools/gfx/d3d12/d3d12-device.cpp | 2 +- 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/autodiff/for-loop-eliminate-dead-code.slang create mode 100644 tests/autodiff/for-loop-eliminate-dead-code.slang.expected.txt diff --git a/source/slang/slang-ir.cpp b/source/slang/slang-ir.cpp index 92da981ad..dd3034da1 100644 --- a/source/slang/slang-ir.cpp +++ b/source/slang/slang-ir.cpp @@ -6889,6 +6889,8 @@ namespace Slang SLANG_ASSERT(other); if (other->getPrevInst() == this) return; + if (other == this) + return; _insertAt(other->getPrevInst(), other, other->getParent()); } diff --git a/tests/autodiff/for-loop-eliminate-dead-code.slang b/tests/autodiff/for-loop-eliminate-dead-code.slang new file mode 100644 index 000000000..be1093c23 --- /dev/null +++ b/tests/autodiff/for-loop-eliminate-dead-code.slang @@ -0,0 +1,41 @@ +//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type + +//TEST_INPUT:ubuffer(data=[0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + +static const uint kMaxSurfaceBounces = 2; + +[BackwardDifferentiable] +void updatePathContrib(inout float3 result, float3 val) +{ + result = result + val; +} + +[BackwardDifferentiable] +float3 evalPath(uint2 pixel) +{ + float3 result = float3(0.f); + float3 thp = float3(1.f); + + [MaxIters(kMaxSurfaceBounces)] + for (int i = 0; i < kMaxSurfaceBounces; i++) + { + float3 neeContrib = float3(1.f); + updatePathContrib(result, thp * neeContrib); + thp = thp * float3(0.9f); + } + return result; +} + +void execute(const uint2 pixel) +{ + float3 dColor = float3(1.f); + __bwd_diff(evalPath)(pixel, dColor); +} + +[numthreads(64, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + execute(dispatchThreadID.xy); +} diff --git a/tests/autodiff/for-loop-eliminate-dead-code.slang.expected.txt b/tests/autodiff/for-loop-eliminate-dead-code.slang.expected.txt new file mode 100644 index 000000000..4465d8c34 --- /dev/null +++ b/tests/autodiff/for-loop-eliminate-dead-code.slang.expected.txt @@ -0,0 +1,4 @@ +type: float +0.000000 +0.000000 +0.000000 \ No newline at end of file diff --git a/tools/gfx/d3d12/d3d12-device.cpp b/tools/gfx/d3d12/d3d12-device.cpp index 9bf54a01b..ad71388c4 100644 --- a/tools/gfx/d3d12/d3d12-device.cpp +++ b/tools/gfx/d3d12/d3d12-device.cpp @@ -407,7 +407,7 @@ Result DeviceImpl::initialize(const Desc& desc) SharedLibrary::Handle d3dModule; #if SLANG_WINDOWS_FAMILY - const char* libName = "d3d11"; + const char* libName = "d3d12"; #else const char* libName = "vkd3d-proton-d3d12"; #endif -- cgit v1.2.3