Issue 239736
Summary [Clang][Sema] Crash in HLSLAggregateSplatCast with Explicit CStyleCast
Labels clang, crash-on-valid, HLSL
Assignees AmrDeveloper
Reporter AmrDeveloper
    Clang crashes on valid code when performing HLSLAggregateSplatCast with explicit CStyleCast

```hlsl
void ExplicitFloatToBoolCastThenSplat(float Value) {
 bool2x3 M = (bool2x3) Value;
}
```

But it compiles successfully with an implicit cast

```hlsl
void ImplicitFloatToBoolCastThenSplat(float Value) {
    bool2x3 M = Value;
}
```

Compiler Explorer: https://godbolt.org/z/3YGT6qrPK

Crash:

```
clang: /root/llvm-project/llvm/lib/IR/Instructions.cpp:3200: static llvm::CastInst* llvm::CastInst::Create(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, const llvm::Twine&, llvm::InsertPosition): Assertion `castIsValid(op, S, Ty) && "Invalid cast!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and dumped files.
Stack dump:
0.	Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang --driver-mode=dxc -Zi -Qembed_debug -Fc /app/output.s -fcolor-diagnostics -fno-crash-diagnostics -T vs_6_2 <source>
1.	<eof> parser at end of file
2.	<source>:1:6: LLVM IR generation of declaration 'ExplicitFloatToBoolCastThenSplat'
3.	<source>:1:6: Generating code for declaration 'ExplicitFloatToBoolCastThenSplat'
 #0 0x0000000004f17278 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4f17278)
 #1 0x0000000004f144d4 llvm::sys::RunSignalHandlers() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4f144d4)
 #2 0x0000000004f14b0c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4f14b0c)
 #3 0x0000000004e4e888 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #4 0x0000788dc3645330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
 #5 0x0000788dc369ec0c pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x9ec0c)
 #6 0x0000788dc364527e raise (/lib/x86_64-linux-gnu/libc.so.6+0x4527e)
 #7 0x0000788dc36288ff abort (/lib/x86_64-linux-gnu/libc.so.6+0x288ff)
 #8 0x0000788dc362881b (/lib/x86_64-linux-gnu/libc.so.6+0x2881b)
 #9 0x0000788dc363b517 (/lib/x86_64-linux-gnu/libc.so.6+0x3b517)
#10 0x0000000004787a66 (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4787a66)
#11 0x000000000179ed33 llvm::IRBuilderBase::CreateCast(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, llvm::Twine const&, llvm::MDNode*, llvm::FMFSource) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x179ed33)
#12 0x00000000053d2199 clang::CodeGen::CodeGenFunction::EmitToMemory(llvm::Value*, clang::QualType) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x53d2199)
#13 0x00000000053d9e09 clang::CodeGen::CodeGenFunction::EmitStoreOfScalar(llvm::Value*, clang::CodeGen::Address, bool, clang::QualType, clang::CodeGen::LValueBaseInfo, clang::CodeGen::TBAAAccessInfo, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x53d9e09)
#14 0x00000000053da6ff EmitStoreOfMatrixScalar(llvm::Value*, clang::CodeGen::LValue, bool, clang::CodeGen::CodeGenFunction&) CGExpr.cpp:0:0
#15 0x00000000053e917f clang::CodeGen::CodeGenFunction::EmitStoreThroughLValue(clang::CodeGen::RValue, clang::CodeGen::LValue, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x53e917f)
#16 0x000000000595d061 clang::CodeGen::CodeGenFunction::EmitScalarInit(clang::Expr const*, clang::ValueDecl const*, clang::CodeGen::LValue, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x595d061)
#17 0x000000000595eba7 clang::CodeGen::CodeGenFunction::EmitExprAsInit(clang::Expr const*, clang::ValueDecl const*, clang::CodeGen::LValue, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x595eba7)
#18 0x00000000059693a2 clang::CodeGen::CodeGenFunction::EmitAutoVarInit(clang::CodeGen::CodeGenFunction::AutoVarEmission const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x59693a2)
#19 0x00000000059705c9 clang::CodeGen::CodeGenFunction::EmitVarDecl(clang::VarDecl const&) (.part.0) CGDecl.cpp:0:0
#20 0x0000000005970e03 clang::CodeGen::CodeGenFunction::EmitDecl(clang::Decl const&, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x5970e03)
#21 0x0000000005590644 clang::CodeGen::CodeGenFunction::EmitDeclStmt(clang::DeclStmt const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x5590644)
#22 0x00000000055aa267 clang::CodeGen::CodeGenFunction::EmitSimpleStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x55aa267)
#23 0x00000000055a1829 clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x55a1829)
#24 0x00000000055a9e1b clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x55a9e1b)
#25 0x00000000056141f6 clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x56141f6)
#26 0x0000000005626c8c clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x5626c8c)
#27 0x000000000569709a clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x569709a)
#28 0x0000000005690715 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x5690715)
#29 0x0000000005691093 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x5691093)
#30 0x000000000569e559 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
```

The reason is that in `Sema::PerformImplicitConversion` we call `prepareMatrixSplat`, which will emit an extra cast from FloatToBoolean, for example, when the SplatCast will work to create vector/matrix from the boolean

https://github.com/llvm/llvm-project/blob/a54fb533614bea93d9ddb2704b81869a2c2c6952/clang/lib/Sema/SemaExprCXX.cpp#L5439

But in CStyleCast we don't do that for matrix, but we do for vector to emit this cast

https://github.com/llvm/llvm-project/blob/473c7fb83c7acda4f8e417aa95cf512b05121fed/clang/lib/Sema/SemaCast.cpp#L2977-L2985
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to