[DirectX] `createTypedBufferLoad` Trys To Replace A Vector Load With A Scalar Float ExtractValue Insttruction In DXILResourceAccess.cpp

by ADMIN 136 views

Introduction

In this article, we will explore the issue of createTypedBufferLoad trying to replace a vector load with a scalar float extractValue instruction in DXILResourceAccess.cpp. This issue is related to the replacement of loads and the types being different, which cannot be handled by replaceAllUsesWith.

Location of Assert

The location of the assert can be found in the DXILResourceAccess.cpp file, specifically at line 146.

https://github.com/llvm/llvm-project/blob/ee4c8b556c5cf42c55ce9540bbb0e29c11894a71/llvm/lib/Target/DirectX/DXILResourceAccess.cpp#L146

Type Differences

The type differences between the vector load and the scalar float extractValue instruction can be seen in the following GDB output:

expr V->dump()
  %32 = extractvalue { float, i1 } %31, 0
(lldb) expr *V->getType()
(llvm::Type) $1 = {
  Context = 0x000000013f8229c0
  ID = FloatTyID
  SubclassData = 0
  NumContainedTys = 0
  ContainedTys = nullptr
}

(lldb) expr LI->dump()
  %33 = load <1 x float>, ptr %30, align 4
(lldb) expr *LI->getType()
(llvm::Type) $2 = {
  Context = 0x000000013f8229c0
  ID = FixedVectorTyID
  SubclassData = 0
  NumContainedTys = 1
  ContainedTys = 0x000000012904ad10

Crash Dump

The crash dump can be seen in the following GDB output:

* thread #1, queue = 'com.apple.main-thread', stop reason = instruction step into
  * frame #0: 0x0000000102054088 clang-dxc`llvm::Value::replaceAllUsesWith(this=0x0000000128850150, New=0x000000016fdf3ea8) at Value.cpp:533
    frame #1: 0x0000000100110174 clang-dxc`createTypedBufferLoad(II=0x0000000128850150, LI=0x000000012883b2d0, Offset=0x0000000000000000, RTI=0x0000000129013408) at DXILResourceAccess.cpp:146:7
    frame #2: 0x000000010010ef38 clang-dxc`createLoadIntrinsic(II=0x0000000128850150, LI=0x000000012883b2d0, Offset=0x0000000000000000, RTI=0x0000000129013408) at DXILResourceAccess.cpp:167:12
    frame #3: 0x000000010010e0a4 clang-dxc`replaceAccess(II=0x0000000128850150, RTI=0x0000000129013408) at DXILResourceAccess.cpp:224:7
    frame #4: x000000010010d9c8 clang-dxc`transformResourcePointers(F=0x0000000128804ac8, DRTM=0x0000000128814740) at DXILResourceAccess.cpp:249:5
    frame #5: 0x0000000100110b04 clang-dxc`(anonymous namespace)::DXILResourceAccessLegacy::runOnFunction(this=0x000000012883e150, F=0x0000000128804ac8) at DXILResourceAccess.cpp:278:12
    frame #6: 0x0000000101f5feb4 clang-dxc`llvm::FPPassManager::runOnFunction(this=0x0000000128859e70, F=0x0000000128804ac8) at LegacyPassManager.cpp:1406:27
    frame #7: 0x0000000101f66a48 clang-dxc`llvm::FPPassManager::runOnModule(this=0x0000000128859e70, M=0x000000012ee0d2e0) at LegacyPassManager.cpp:1452:16
    frame #8: 0x0000000101f60774 clang-dxc`(anonymous namespace)::MPPassManager::runOnModule(this=0x000000012880e360, M=0x000000012ee0d2e0) at LegacyPassManager.cpp:1521:27
    frame #9: 0x0000000101f602e8 clang-dxc`llvm::legacy::PassManagerImpl::run(this=0x0000000129010600, M=0x000000012ee0d2e0) at LegacyPassManager.cpp:539:44
    frame #10: 0x0000000101f66e50 clang-dxc`llvm::legacy::PassManager::run(this=0x000000016fdf47e8, M=0x000000012ee0d2e0) at LegacyPassManager.cpp:1648:14
    frame #11: 0x0000000103b41860 clang-dxc`(anonymous namespace)::EmitAssemblyHelper::RunCodegenPipeline(this=0x000000016fdf4c60, Action=Backend_EmitAssembly, OS=llvm::raw_pwrite_stream @ 0x000000012ee0cfb0, DwoOS=nullptr) at BackendUtil.cpp:1244:19
    frame #12: 0x0000000103b32000 clang-dxc`(anonymous namespace)::EmitAssemblyHelper::emitAssembly(this=0x000000016fdf4c60, Action=Backend_EmitAssembly, OS=llvm::raw_pwrite_stream @ 0x000000012ee0cfb0, BC=0x000000012ee0d160) at BackendUtil.cpp:1268:3
    frame #13: 0x0000000103b3151c clang-dxc`clang::emitBackendOutput(CI=0x000000013f8235b0, CGOpts=0x000000014002be18, TDesc=(Data = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64", Length = 78), M=0x000000012ee0d2e0, Action=Backend_EmitAssembly, VFS=IntrRefCntPtr<llvm::vfs::FileSystem> @ 0x000000016fdf4f68, OS=nullptr, BC=0x000000012ee0d160) at BackendUtil.cpp:1433:13
    frame #14: 0x00000001042aac7c clang-dxc`clang::BackendConsumer::HandleTranslationUnit(this=0x000000012ee0d160, C=0x000000012f01c600) at CodeGenAction.cpp:316:3
    frame #15: 0x0000000106aca0b8 clang-dxc`clang::ParseAST(S=0x000000012f03d800, PrintStats=false, SkipFunctionBodies=false) at ParseAST.cpp:184:13
    frame #16: 0x0000000104e2175c clang-dxc`clang::ASTFrontendAction::ExecuteAction(this=0x000000013f829060) at FrontendAction.cpp:1345:3
    frame #17: 0x00000001042b06f0 clang-dxc`clang::CodeGenAction::ExecuteAction(this=0x000000013f829060) at CodeGenAction.cpp:1111:30
    frame #18: 0x0000000104e20fd4 clang-dxc`clang::FrontendAction::Execute(this=0x000000013f829060) at FrontendAction.cpp:1227:3
    frame #19: 0x0000000104d3ac18 clang-dxc`clang::CompilerInstance::ExecuteAction(this=0x000000013f8235b0, Act=0x000000013f829060) at CompilerInstance.cpp:1056:33
    frame #20: 0x0000000104f5dbf4 clang-dxc`clang::ExecuteCompilerInvocation(CI=0x000000013f8235b0) at ExecuteCompilerInvocation.cpp:300:25
    frame #21: 0x0000000100014a60 clang-dxc`cc1_main(Argv=ArrayRef<const char *> @ 0x000000016fdf5ea8, Argv0="/Users/farzonlotfi/Projects/llvm_debug_build/bin/clang-20", MainAddr=0x0000000100004980) at cc1_main.cpp:294:15
    frame #22: 0x0000000100006328 clang-dxc`ExecuteCC1Tool(ArgV=0x000000016fdf9698, ToolContext=0x000<br/>
**Q&A: DirectX `createTypedBufferLoad` Tries to Replace a Vector Load with a Scalar Float ExtractValue Instruction in DXILResourceAccess.cpp**
===========================================================

**Q: What is the issue with `createTypedBufferLoad` in DXILResourceAccess.cpp?**
--------------------------------------------------------------------------------

A: The issue is that `createTypedBufferLoad` tries to replace a vector load with a scalar float extractValue instruction, but the types are different, which cannot be handled by `replaceAllUsesWith`.

**Q: What are the type differences between the vector load and the scalar float extractValue instruction?**
-----------------------------------------------------------------------------------------

A: The type differences can be seen in the following GDB output:

```gdb
expr V->dump()
  %32 = extractvalue { float, i1 } %31, 0
(lldb) expr *V->getType()
(llvm::Type) $1 = {
  Context = 0x000000013f8229c0
  ID = FloatTyID
  SubclassData = 0
  NumContainedTys = 0
  ContainedTys = nullptr
}

(lldb) expr LI->dump()
  %33 = load <1 x float>, ptr %30, align 4
(lldb) expr *LI->getType()
(llvm::Type) $2 = {
  Context = 0x000000013f8229c0
  ID = FixedVectorTyID
  SubclassData = 0
  NumContainedTys = 1
  ContainedTys = 0x000000012904ad10

Q: What is the location of the assert in DXILResourceAccess.cpp?

A: The location of the assert can be found in the DXILResourceAccess.cpp file, specifically at line 146.

https://github.com/llvm/llvm-project/blob/ee4c8b556c5cf42c55ce9540bbb0e29c11894a71/llvm/lib/Target/DirectX/DXILResourceAccess.cpp#L146

Q: What is the crash dump for this issue?

A: The crash dump can be seen in the following GDB output:

* thread #1, queue = 'com.apple.main-thread', stop reason = instruction step into
  * frame #0: 0x0000000102054088 clang-dxc`llvm::Value::replaceAllUsesWith(this=0x0000000128850150, New=0x000000016fdf3ea8) at Value.cpp:533
    frame #1: 0x0000000100110174 clang-dxc`createTypedBufferLoad(II=0x0000000128850150, LI=0x000000012883b2d0, Offset=0x0000000000000000, RTI=0x0000000129013408) at DXILResourceAccess.cpp:146:7
    frame #2: 0x000000010010ef38 clang-dxc`createLoadIntrinsic(II=0x0000000128850150, LI=0x000000012883b2d0, Offset=0x0000000000000000, RTI=0x0000000129013408) at DXILResourceAccess.cpp:167:12
    frame #3: 0x000000010010e0a4 clang-d`replaceAccess(II=0x0000000128850150, RTI=0x0000000129013408) at DXILResourceAccess.cpp:224:7
    frame #4: x000000010010d9c8 clang-dxc`transformResourcePointers(F=0x0000000128804ac8, DRTM=0x0000000128814740) at DXILResourceAccess.cpp:249:5
    frame #5: 0x0000000100110b04 clang-dxc`(anonymous namespace)::DXILResourceAccessLegacy::runOnFunction(this=0x000000012883e150, F=0x0000000128804ac8) at DXILResourceAccess.cpp:278:12
    frame #6: 0x0000000101f5feb4 clang-dxc`llvm::FPPassManager::runOnFunction(this=0x0000000128859e70, F=0x0000000128804ac8) at LegacyPassManager.cpp:1406:27
    frame #7: 0x0000000101f66a48 clang-dxc`llvm::FPPassManager::runOnModule(this=0x0000000128859e70, M=0x000000012ee0d2e0) at LegacyPassManager.cpp:1452:16
    frame #8: 0x0000000101f60774 clang-dxc`(anonymous namespace)::MPPassManager::runOnModule(this=0x000000012880e360, M=0x000000012ee0d2e0) at LegacyPassManager.cpp:1521:27
    frame #9: 0x0000000101f602e8 clang-dxc`llvm::legacy::PassManagerImpl::run(this=0x0000000129010600, M=0x000000012ee0d2e0) at LegacyPassManager.cpp:539:44
    frame #10: 0x0000000101f66e50 clang-dxc`llvm::legacy::PassManager::run(this=0x000000016fdf47e8, M=0x000000012ee0d2e0) at LegacyPassManager.cpp:1648:14
    frame #11: 0x0000000103b41860 clang-dxc`(anonymous namespace)::EmitAssemblyHelper::RunCodegenPipeline(this=0x000000016fdf4c60, Action=Backend_EmitAssembly, OS=llvm::raw_pwrite_stream @ 0x000000012ee0cfb0, DwoOS=nullptr) at BackendUtil.cpp:1244:19
    frame #12: 0x0000000103b32000 clang-dxc`(anonymous namespace)::EmitAssemblyHelper::emitAssembly(this=0x000000016fdf4c60, Action=Backend_EmitAssembly, OS=llvm::raw_pwrite_stream @ 0x000000012ee0cfb0, BC=0x000000012ee0d160) at BackendUtil.cpp:1268:3
    frame #13: 0x0000000103b3151c clang-dxc`clang::emitBackendOutput(CI=0x000000013f8235b0, CGOpts=0x000000014002be18, TDesc=(Data = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:3264:64-n8:16:32:64", Length = 78), M=0x000000012ee0d2e0, Action=Backend_EmitAssembly, VFS=IntrRefCntPtr<llvm::vfs::FileSystem> @ 0x000000016fdf4f68, OS=nullptr, BC=0x000000012ee0d160) at BackendUtil.cpp:1433:13
    frame #14: 0x00000001042aac7c clang-dxc`clang::BackendConsumer::HandleTranslationUnit(this=0x000000012ee0d160, C=0x000000012f01c600) at CodeGenAction.cpp:316:3
    frame #15: 0x0000000106aca0b8 clang-dxc`clang::ParseAST(S=0x000000012f03d800, PrintStats=false, SkipFunctionBodies=false) at ParseAST.cpp:184:13
    frame #16: 0x0000000104e2175c clang-dxc`clang::ASTFrontendAction::ExecuteAction(this=0x000000013f829060) at FrontendAction.cpp:1345:3
    frame #17: 0x00000001042b06f0 clang-dxc`clang::CodeGenAction::ExecuteAction(this=0x000000013f829060) at CodeGenAction.cpp:1111:30
    frame #18: 0x0000000104e20fd4 clang-dxc`clang::FrontendAction::Execute(this=0x000000013f829060) at FrontendAction.cpp:1227:3
    frame #19: 0x0000000104d3ac18 clang-dxc`clang::CompilerInstance::ExecuteAction(this=0x000000013f8235b0, Act=0x000000013f829060) at CompilerInstance.cpp:1056:33
    frame #20: 0x0000000104f5dbf4 clang-dxc`clang::ExecuteCompilerInvocation(CI=0x000000013f8235b0) at ExecuteCompilerInvocation.cpp:300:25
    frame #21: 0x0000000100014a60 clang-dxc`cc1_main(Argv=ArrayRef<const char *> @ 0x000000016fdf5ea8, Argv0="/Users/farzonlotfi/Projects/llvm_debug_build/bin/clang-20", MainAddr=0x0000000100004980) at cc1_main.cpp:294:15
    frame #22: 0x