llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.36k stars 12.14k forks source link

[DirectX] DXIL Data Scalarization crash #117273

Open llvm-beanz opened 1 week ago

llvm-beanz commented 1 week ago

The following shader to compute a mandelbrot set crashes the DXIL Data Scalarizer:

RWBuffer<float4> Tex;

const static float3 Palette[8] = {float3(0.0, 0.0, 0.0), float3(0.5, 0.5, 0.5),
                                  float3(1.0, 0.5, 0.5), float3(0.5, 1.0, 0.5),
                                  float3(0.5, 0.5, 1.0), float3(0.5, 1.0, 1.0),
                                  float3(1.0, 0.5, 1.0), float3(1.0, 1.0, 0.5)};

const static int Dimension = 4096;

[numthreads(1024, 1, 1)] void main(uint3 DID
                                   : SV_DispatchThreadID) {
  float scale = 1.5 / pow(2.0, 16.0 * abs(sin(0.25 / 16.0)));
  float2 offset = float2(-1.0, 0.0);
  uint2 Index =
      uint2(DID.x % Dimension, DID.x / Dimension + (Dimension * DID.y));
  uint2 DispatchSize = Dimension.xx;
  float X0 =
      scale * (2.0 * (float)Index.x / (float)DispatchSize.x - 1.5) + offset.x;
  float Y0 =
      scale * (2.0 * (float)Index.y / (float)DispatchSize.y - 1.0) + offset.y;

  // Implement Mandelbrot set
  float X = X0;
  float Y = Y0;
  uint Iteration = 0;
  uint MaxIteration = 2000;
  float XTmp = 0.0;
  bool Diverged = false;
  for (; Iteration < MaxIteration; ++Iteration) {
    if (X * X + Y * Y > 2000 * 2000) {
      Diverged = true;
      break;
    }
    XTmp = X * X - Y * Y + X0;
    Y = 2 * X * Y + Y0;
    X = XTmp;
  }

  float3 Color = float3(0, 0, 0);
  if (Diverged) {
    float Gradient = 1.0;
    float Smooth = log2(log2(X * X + Y * Y) / 2.0);
    float ColorIdx = sqrt((float)Iteration + 10.0 - Smooth) * Gradient;
    float LerpSize = frac(ColorIdx);
    LerpSize = LerpSize * LerpSize * (3.0 - 2.0 * LerpSize);
    int ColorIdx1 = (int)ColorIdx % 8;
    int ColorIdx2 = (ColorIdx1 + 1) % 8;
    Color = lerp(Palette[ColorIdx1], Palette[ColorIdx2], LerpSize.xxx);
  }

  Tex[DID.x] = float4(Color, 1.0);
}

Crash backtrace:

0.      Program arguments: C:\\Users\\cbieneman\\dev\\llvm-project\\llvm\\out\\build\\x64-Clang-Debug\\bin\\clang-dxc.exe -cc1 -triple dxilv1.0-unknown-shadermodel6.0-compute -emit-obj -dumpdir a- -disable-free -clear-ast-before-backend -main-file-name Mandelbrot.hlsl -mrelocation-model static -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -debugger-tuning=gdb -fdebug-compilation-dir=C:\\Users\\cbieneman\\dev\\llvm-project\\llvm\\out\\build\\x64-Clang-Debug -fcoverage-compilation-dir=C:\\Users\\cbieneman\\dev\\llvm-project\\llvm\\out\\build\\x64-Clang-Debug -resource-dir C:\\Users\\cbieneman\\dev\\llvm-project\\llvm\\out\\build\\x64-Clang-Debug\\lib\\clang\\20 -O3 -ferror-limit 19 -fmessage-length=350 -O3 -finclude-default-header -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcolor-diagnostics -vectorize-loops -vectorize-slp -o mandelbrot.yaml -x hlsl C:\\Users\\cbieneman\\dev\\hlsl-test-suite\\test\\Basic\\Inputs\\Mandelbrot.hlsl
1.      <eof> parser at end of file
2.      Code generation
3.      Running pass 'DXIL Data Scalarization' on module 'C:\Users\cbieneman\dev\hlsl-test-suite\test\Basic\Inputs\Mandelbrot.hlsl'.
Exception Code: 0xC0000005
 #0 0x00007ff735544409 llvm::Value::getValueID(void) const C:\Users\cbieneman\dev\llvm-project\llvm\include\llvm\IR\Value.h:533:0
 #1 0x00007ff73556dc13 llvm::ConstantExpr::classof(class llvm::Value const *) C:\Users\cbieneman\dev\llvm-project\llvm\include\llvm\IR\Constants.h:1385:0
 #2 0x00007ff7365085d3 llvm::isa_impl<class llvm::ConstantExpr, class llvm::User, void>::doit(class llvm::User const &) C:\Users\cbieneman\dev\llvm-project\llvm\include\llvm\Support\Casting.h:64:0
 #3 0x00007ff7367fd679 llvm::isa_impl_cl<class llvm::ConstantExpr, class llvm::User const *>::doit(class llvm::User const *) C:\Users\cbieneman\dev\llvm-project\llvm\include\llvm\Support\Casting.h:110:0
 #4 0x00007ff7367fd636 llvm::isa_impl_wrap<class llvm::ConstantExpr, class llvm::User const *, class llvm::User const *>::doit(class llvm::User const *const &) C:\Users\cbieneman\dev\llvm-project\llvm\include\llvm\Support\Casting.h:137:0
 #5 0x00007ff7367fd601 llvm::isa_impl_wrap<class llvm::ConstantExpr, class llvm::User const *const, class llvm::User const *>::doit(class llvm::User const *const &) C:\Users\cbieneman\dev\llvm-project\llvm\include\llvm\Support\Casting.h:127:0
 #6 0x00007ff7367fd5c3 llvm::CastIsPossible<class llvm::ConstantExpr, class llvm::User const *, void>::isPossible(class llvm::User const *const &) C:\Users\cbieneman\dev\llvm-project\llvm\include\llvm\Support\Casting.h:255:0
 #7 0x00007ff7367fd591 llvm::CastInfo<class llvm::ConstantExpr, class llvm::User *const, void>::isPossible(class llvm::User *const &) C:\Users\cbieneman\dev\llvm-project\llvm\include\llvm\Support\Casting.h:509:0
 #8 0x00007ff7367fc583 llvm::isa<class llvm::ConstantExpr, class llvm::User *>(class llvm::User *const &) C:\Users\cbieneman\dev\llvm-project\llvm\include\llvm\Support\Casting.h:549:0
 #9 0x00007ff7367fb947 findAndReplaceVectors C:\Users\cbieneman\dev\llvm-project\llvm\lib\Target\DirectX\DXILDataScalarization.cpp:248:0
#10 0x00007ff7367fbb68 DXILDataScalarizationLegacy::runOnModule(class llvm::Module &) C:\Users\cbieneman\dev\llvm-project\llvm\lib\Target\DirectX\DXILDataScalarization.cpp:284:0
#11 0x00007ff73594e646 `anonymous namespace'::MPPassManager::runOnModule C:\Users\cbieneman\dev\llvm-project\llvm\lib\IR\LegacyPassManager.cpp:1521:0
#12 0x00007ff73594e10a llvm::legacy::PassManagerImpl::run(class llvm::Module &) C:\Users\cbieneman\dev\llvm-project\llvm\lib\IR\LegacyPassManager.cpp:539:0
#13 0x00007ff735952de1 llvm::legacy::PassManager::run(class llvm::Module &) C:\Users\cbieneman\dev\llvm-project\llvm\lib\IR\LegacyPassManager.cpp:1648:0
#14 0x00007ff737667208 `anonymous namespace'::EmitAssemblyHelper::RunCodegenPipeline C:\Users\cbieneman\dev\llvm-project\clang\lib\CodeGen\BackendUtil.cpp:1197:0
#15 0x00007ff737660f2b `anonymous namespace'::EmitAssemblyHelper::EmitAssembly C:\Users\cbieneman\dev\llvm-project\clang\lib\CodeGen\BackendUtil.cpp:1221:0
#16 0x00007ff7376601ef clang::EmitBackendOutput(class clang::DiagnosticsEngine &, class clang::HeaderSearchOptions const &, class clang::CodeGenOptions const &, class clang::TargetOptions const &, class clang::LangOptions const &, class llvm::StringRef, class llvm::Module *, enum clang::BackendAction, class llvm::IntrusiveRefCntPtr<class llvm::vfs::FileSystem>, class std::unique_ptr<class llvm::raw_pwrite_stream, struct std::default_delete<class llvm::raw_pwrite_stream>>, class clang::BackendConsumer *) C:\Users\cbieneman\dev\llvm-project\clang\lib\CodeGen\BackendUtil.cpp:1386:0
#17 0x00007ff738031450 clang::BackendConsumer::HandleTranslationUnit(class clang::ASTContext &) C:\Users\cbieneman\dev\llvm-project\clang\lib\CodeGen\CodeGenAction.cpp:355:0
#18 0x00007ff73ade1e03 clang::ParseAST(class clang::Sema &, bool, bool) C:\Users\cbieneman\dev\llvm-project\clang\lib\Parse\ParseAST.cpp:191:0
#19 0x00007ff737d94987 clang::ASTFrontendAction::ExecuteAction(void) C:\Users\cbieneman\dev\llvm-project\clang\lib\Frontend\FrontendAction.cpp:1193:0
#20 0x00007ff738035a7a clang::CodeGenAction::ExecuteAction(void) C:\Users\cbieneman\dev\llvm-project\clang\lib\CodeGen\CodeGenAction.cpp:1140:0
#21 0x00007ff737d9434d clang::FrontendAction::Execute(void) C:\Users\cbieneman\dev\llvm-project\clang\lib\Frontend\FrontendAction.cpp:1081:0
#22 0x00007ff735fff094 clang::CompilerInstance::ExecuteAction(class clang::FrontendAction &) C:\Users\cbieneman\dev\llvm-project\clang\lib\Frontend\CompilerInstance.cpp:1065:0
#23 0x00007ff73611c5ab clang::ExecuteCompilerInvocation(class clang::CompilerInstance *) C:\Users\cbieneman\dev\llvm-project\clang\lib\FrontendTool\ExecuteCompilerInvocation.cpp:296:0
#24 0x00007ff735510fd1 cc1_main(class llvm::ArrayRef<char const *>, char const *, void *) C:\Users\cbieneman\dev\llvm-project\clang\tools\driver\cc1_main.cpp:285:0
#25 0x00007ff7355027db ExecuteCC1Tool C:\Users\cbieneman\dev\llvm-project\clang\tools\driver\driver.cpp:218:0
#26 0x00007ff7355014db clang_main(int, char **, struct llvm::ToolContext const &) C:\Users\cbieneman\dev\llvm-project\clang\tools\driver\driver.cpp:259:0
#27 0x00007ff735538f92 main C:\Users\cbieneman\dev\llvm-project\llvm\out\build\x64-Clang-Debug\tools\clang\tools\driver\clang-driver.cpp:17:0
#28 0x00007ff73f0def79 invoke_main D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:79:0
#29 0x00007ff73f0df0b2 __scrt_common_main_seh D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288:0
#30 0x00007ff73f0df13e __scrt_common_main D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:331:0
#31 0x00007ff73f0df15e mainCRTStartup D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp:17:0
#32 0x00007ffb446bdbe7 (C:\WINDOWS\System32\KERNEL32.DLL+0x2dbe7)
#33 0x00007ffb4515fbec (C:\WINDOWS\SYSTEM32\ntdll.dll+0xdfbec)
llvmbot commented 3 days ago

@llvm/issue-subscribers-bug

Author: Chris B (llvm-beanz)

The following shader to compute a mandelbrot set crashes the DXIL Data Scalarizer: ```hlsl RWBuffer<float4> Tex; const static float3 Palette[8] = {float3(0.0, 0.0, 0.0), float3(0.5, 0.5, 0.5), float3(1.0, 0.5, 0.5), float3(0.5, 1.0, 0.5), float3(0.5, 0.5, 1.0), float3(0.5, 1.0, 1.0), float3(1.0, 0.5, 1.0), float3(1.0, 1.0, 0.5)}; const static int Dimension = 4096; [numthreads(1024, 1, 1)] void main(uint3 DID : SV_DispatchThreadID) { float scale = 1.5 / pow(2.0, 16.0 * abs(sin(0.25 / 16.0))); float2 offset = float2(-1.0, 0.0); uint2 Index = uint2(DID.x % Dimension, DID.x / Dimension + (Dimension * DID.y)); uint2 DispatchSize = Dimension.xx; float X0 = scale * (2.0 * (float)Index.x / (float)DispatchSize.x - 1.5) + offset.x; float Y0 = scale * (2.0 * (float)Index.y / (float)DispatchSize.y - 1.0) + offset.y; // Implement Mandelbrot set float X = X0; float Y = Y0; uint Iteration = 0; uint MaxIteration = 2000; float XTmp = 0.0; bool Diverged = false; for (; Iteration < MaxIteration; ++Iteration) { if (X * X + Y * Y > 2000 * 2000) { Diverged = true; break; } XTmp = X * X - Y * Y + X0; Y = 2 * X * Y + Y0; X = XTmp; } float3 Color = float3(0, 0, 0); if (Diverged) { float Gradient = 1.0; float Smooth = log2(log2(X * X + Y * Y) / 2.0); float ColorIdx = sqrt((float)Iteration + 10.0 - Smooth) * Gradient; float LerpSize = frac(ColorIdx); LerpSize = LerpSize * LerpSize * (3.0 - 2.0 * LerpSize); int ColorIdx1 = (int)ColorIdx % 8; int ColorIdx2 = (ColorIdx1 + 1) % 8; Color = lerp(Palette[ColorIdx1], Palette[ColorIdx2], LerpSize.xxx); } Tex[DID.x] = float4(Color, 1.0); } ``` Crash backtrace: ``` 0. Program arguments: C:\\Users\\cbieneman\\dev\\llvm-project\\llvm\\out\\build\\x64-Clang-Debug\\bin\\clang-dxc.exe -cc1 -triple dxilv1.0-unknown-shadermodel6.0-compute -emit-obj -dumpdir a- -disable-free -clear-ast-before-backend -main-file-name Mandelbrot.hlsl -mrelocation-model static -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -debugger-tuning=gdb -fdebug-compilation-dir=C:\\Users\\cbieneman\\dev\\llvm-project\\llvm\\out\\build\\x64-Clang-Debug -fcoverage-compilation-dir=C:\\Users\\cbieneman\\dev\\llvm-project\\llvm\\out\\build\\x64-Clang-Debug -resource-dir C:\\Users\\cbieneman\\dev\\llvm-project\\llvm\\out\\build\\x64-Clang-Debug\\lib\\clang\\20 -O3 -ferror-limit 19 -fmessage-length=350 -O3 -finclude-default-header -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcolor-diagnostics -vectorize-loops -vectorize-slp -o mandelbrot.yaml -x hlsl C:\\Users\\cbieneman\\dev\\hlsl-test-suite\\test\\Basic\\Inputs\\Mandelbrot.hlsl 1. <eof> parser at end of file 2. Code generation 3. Running pass 'DXIL Data Scalarization' on module 'C:\Users\cbieneman\dev\hlsl-test-suite\test\Basic\Inputs\Mandelbrot.hlsl'. Exception Code: 0xC0000005 #0 0x00007ff735544409 llvm::Value::getValueID(void) const C:\Users\cbieneman\dev\llvm-project\llvm\include\llvm\IR\Value.h:533:0 #1 0x00007ff73556dc13 llvm::ConstantExpr::classof(class llvm::Value const *) C:\Users\cbieneman\dev\llvm-project\llvm\include\llvm\IR\Constants.h:1385:0 #2 0x00007ff7365085d3 llvm::isa_impl<class llvm::ConstantExpr, class llvm::User, void>::doit(class llvm::User const &) C:\Users\cbieneman\dev\llvm-project\llvm\include\llvm\Support\Casting.h:64:0 #3 0x00007ff7367fd679 llvm::isa_impl_cl<class llvm::ConstantExpr, class llvm::User const *>::doit(class llvm::User const *) C:\Users\cbieneman\dev\llvm-project\llvm\include\llvm\Support\Casting.h:110:0 #4 0x00007ff7367fd636 llvm::isa_impl_wrap<class llvm::ConstantExpr, class llvm::User const *, class llvm::User const *>::doit(class llvm::User const *const &) C:\Users\cbieneman\dev\llvm-project\llvm\include\llvm\Support\Casting.h:137:0 #5 0x00007ff7367fd601 llvm::isa_impl_wrap<class llvm::ConstantExpr, class llvm::User const *const, class llvm::User const *>::doit(class llvm::User const *const &) C:\Users\cbieneman\dev\llvm-project\llvm\include\llvm\Support\Casting.h:127:0 #6 0x00007ff7367fd5c3 llvm::CastIsPossible<class llvm::ConstantExpr, class llvm::User const *, void>::isPossible(class llvm::User const *const &) C:\Users\cbieneman\dev\llvm-project\llvm\include\llvm\Support\Casting.h:255:0 #7 0x00007ff7367fd591 llvm::CastInfo<class llvm::ConstantExpr, class llvm::User *const, void>::isPossible(class llvm::User *const &) C:\Users\cbieneman\dev\llvm-project\llvm\include\llvm\Support\Casting.h:509:0 #8 0x00007ff7367fc583 llvm::isa<class llvm::ConstantExpr, class llvm::User *>(class llvm::User *const &) C:\Users\cbieneman\dev\llvm-project\llvm\include\llvm\Support\Casting.h:549:0 #9 0x00007ff7367fb947 findAndReplaceVectors C:\Users\cbieneman\dev\llvm-project\llvm\lib\Target\DirectX\DXILDataScalarization.cpp:248:0 #10 0x00007ff7367fbb68 DXILDataScalarizationLegacy::runOnModule(class llvm::Module &) C:\Users\cbieneman\dev\llvm-project\llvm\lib\Target\DirectX\DXILDataScalarization.cpp:284:0 #11 0x00007ff73594e646 `anonymous namespace'::MPPassManager::runOnModule C:\Users\cbieneman\dev\llvm-project\llvm\lib\IR\LegacyPassManager.cpp:1521:0 #12 0x00007ff73594e10a llvm::legacy::PassManagerImpl::run(class llvm::Module &) C:\Users\cbieneman\dev\llvm-project\llvm\lib\IR\LegacyPassManager.cpp:539:0 #13 0x00007ff735952de1 llvm::legacy::PassManager::run(class llvm::Module &) C:\Users\cbieneman\dev\llvm-project\llvm\lib\IR\LegacyPassManager.cpp:1648:0 #14 0x00007ff737667208 `anonymous namespace'::EmitAssemblyHelper::RunCodegenPipeline C:\Users\cbieneman\dev\llvm-project\clang\lib\CodeGen\BackendUtil.cpp:1197:0 #15 0x00007ff737660f2b `anonymous namespace'::EmitAssemblyHelper::EmitAssembly C:\Users\cbieneman\dev\llvm-project\clang\lib\CodeGen\BackendUtil.cpp:1221:0 #16 0x00007ff7376601ef clang::EmitBackendOutput(class clang::DiagnosticsEngine &, class clang::HeaderSearchOptions const &, class clang::CodeGenOptions const &, class clang::TargetOptions const &, class clang::LangOptions const &, class llvm::StringRef, class llvm::Module *, enum clang::BackendAction, class llvm::IntrusiveRefCntPtr<class llvm::vfs::FileSystem>, class std::unique_ptr<class llvm::raw_pwrite_stream, struct std::default_delete<class llvm::raw_pwrite_stream>>, class clang::BackendConsumer *) C:\Users\cbieneman\dev\llvm-project\clang\lib\CodeGen\BackendUtil.cpp:1386:0 #17 0x00007ff738031450 clang::BackendConsumer::HandleTranslationUnit(class clang::ASTContext &) C:\Users\cbieneman\dev\llvm-project\clang\lib\CodeGen\CodeGenAction.cpp:355:0 #18 0x00007ff73ade1e03 clang::ParseAST(class clang::Sema &, bool, bool) C:\Users\cbieneman\dev\llvm-project\clang\lib\Parse\ParseAST.cpp:191:0 #19 0x00007ff737d94987 clang::ASTFrontendAction::ExecuteAction(void) C:\Users\cbieneman\dev\llvm-project\clang\lib\Frontend\FrontendAction.cpp:1193:0 #20 0x00007ff738035a7a clang::CodeGenAction::ExecuteAction(void) C:\Users\cbieneman\dev\llvm-project\clang\lib\CodeGen\CodeGenAction.cpp:1140:0 #21 0x00007ff737d9434d clang::FrontendAction::Execute(void) C:\Users\cbieneman\dev\llvm-project\clang\lib\Frontend\FrontendAction.cpp:1081:0 #22 0x00007ff735fff094 clang::CompilerInstance::ExecuteAction(class clang::FrontendAction &) C:\Users\cbieneman\dev\llvm-project\clang\lib\Frontend\CompilerInstance.cpp:1065:0 #23 0x00007ff73611c5ab clang::ExecuteCompilerInvocation(class clang::CompilerInstance *) C:\Users\cbieneman\dev\llvm-project\clang\lib\FrontendTool\ExecuteCompilerInvocation.cpp:296:0 #24 0x00007ff735510fd1 cc1_main(class llvm::ArrayRef<char const *>, char const *, void *) C:\Users\cbieneman\dev\llvm-project\clang\tools\driver\cc1_main.cpp:285:0 #25 0x00007ff7355027db ExecuteCC1Tool C:\Users\cbieneman\dev\llvm-project\clang\tools\driver\driver.cpp:218:0 #26 0x00007ff7355014db clang_main(int, char **, struct llvm::ToolContext const &) C:\Users\cbieneman\dev\llvm-project\clang\tools\driver\driver.cpp:259:0 #27 0x00007ff735538f92 main C:\Users\cbieneman\dev\llvm-project\llvm\out\build\x64-Clang-Debug\tools\clang\tools\driver\clang-driver.cpp:17:0 #28 0x00007ff73f0def79 invoke_main D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:79:0 #29 0x00007ff73f0df0b2 __scrt_common_main_seh D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288:0 #30 0x00007ff73f0df13e __scrt_common_main D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:331:0 #31 0x00007ff73f0df15e mainCRTStartup D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp:17:0 #32 0x00007ffb446bdbe7 (C:\WINDOWS\System32\KERNEL32.DLL+0x2dbe7) #33 0x00007ffb4515fbec (C:\WINDOWS\SYSTEM32\ntdll.dll+0xdfbec) ```