[Clang] Crash on UO Real/Imag on scalar with type promotion (original) (raw)

This valid code crashes when compiling with any clang version and -triple x86_64-unknown-linux-gun flag

void real_on_scalar_with_type_promotion() { _Float16 _Complex a; _Float16 b = real(real a); }

void imag_on_scalar_with_type_promotion() { _Float16 _Complex a; _Float16 b = real(imag a); }

Crash

Assertion failed: (isa<T>(CanonicalType)), function castAs, file TypeBase.h, line 9170.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.	Program arguments: ./bin/clang++ -cc1 -std=c++20 -triple x86_64-unknown-linux-gun -emit-llvm main.cpp -o main_original.ll
1.	main.cpp:6:1: current parser token 'void'
2.	main.cpp:1:6: LLVM IR generation of declaration 'real_on_scalar_with_type_promotion'
3.	main.cpp:1:6: Generating code for declaration 'real_on_scalar_with_type_promotion'
 #0 0x000000010517ef4c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x102e56f4c)
 #1 0x000000010517f510 PrintStackTraceSignalHandler(void*) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x102e57510)
 #2 0x000000010517cea0 llvm::sys::RunSignalHandlers() (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x102e54ea0)
 #3 0x0000000105180eb8 SignalHandler(int, __siginfo*, void*) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x102e58eb8)
 #4 0x0000000184f0d6a4 (/usr/lib/system/libsystem_platform.dylib+0x1804ad6a4)
 #5 0x0000000184ed388c (/usr/lib/system/libsystem_pthread.dylib+0x18047388c)
 #6 0x0000000184ddca3c (/usr/lib/system/libsystem_c.dylib+0x18037ca3c)
 #7 0x0000000184ddbc70 (/usr/lib/system/libsystem_c.dylib+0x18037bc70)
 #8 0x0000000105b7cd00 clang::ComplexType const* clang::Type::castAs<clang::ComplexType>() const (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103854d00)
 #9 0x0000000105ede1b0 clang::CodeGen::CodeGenFunction::EmitPromotedValue(std::__1::pair<llvm::Value*, llvm::Value*>, clang::QualType) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103bb61b0)
#10 0x0000000105f43a68 (anonymous namespace)::ScalarExprEmitter::VisitReal(clang::UnaryOperator const*, clang::QualType) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103c1ba68)
#11 0x0000000105f2b5b8 (anonymous namespace)::ScalarExprEmitter::EmitPromoted(clang::Expr const*, clang::QualType) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103c035b8)
#12 0x0000000105f2b33c clang::CodeGen::CodeGenFunction::EmitPromotedScalarExpr(clang::Expr const*, clang::QualType) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103c0333c)
#13 0x0000000105f43b48 (anonymous namespace)::ScalarExprEmitter::VisitReal(clang::UnaryOperator const*, clang::QualType) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103c1bb48)
#14 0x0000000105f380f8 (anonymous namespace)::ScalarExprEmitter::VisitUnaryReal(clang::UnaryOperator const*, clang::QualType) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103c100f8)
#15 0x0000000105f33180 clang::StmtVisitorBase<std::__1::add_pointer, (anonymous namespace)::ScalarExprEmitter, llvm::Value*>::Visit(clang::Stmt*) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103c0b180)
#16 0x0000000105f29e58 (anonymous namespace)::ScalarExprEmitter::Visit(clang::Expr*) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103c01e58)
#17 0x0000000105f46ac4 (anonymous namespace)::ScalarExprEmitter::VisitCastExpr(clang::CastExpr*) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103c1eac4)
#18 0x0000000105f3c69c clang::StmtVisitorBase<std::__1::add_pointer, (anonymous namespace)::ScalarExprEmitter, llvm::Value*>::VisitImplicitCastExpr(clang::ImplicitCastExpr*) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103c1469c)
#19 0x0000000105f33858 clang::StmtVisitorBase<std::__1::add_pointer, (anonymous namespace)::ScalarExprEmitter, llvm::Value*>::Visit(clang::Stmt*) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103c0b858)
#20 0x0000000105f29e58 (anonymous namespace)::ScalarExprEmitter::Visit(clang::Expr*) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103c01e58)
#21 0x0000000105f29c4c clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103c01c4c)
#22 0x0000000105e34bfc clang::CodeGen::CodeGenFunction::EmitScalarInit(clang::Expr const*, clang::ValueDecl const*, clang::CodeGen::LValue, bool) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103b0cbfc)
#23 0x0000000105e3bf38 clang::CodeGen::CodeGenFunction::EmitExprAsInit(clang::Expr const*, clang::ValueDecl const*, clang::CodeGen::LValue, bool) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103b13f38)
#24 0x0000000105e38a4c clang::CodeGen::CodeGenFunction::EmitAutoVarInit(clang::CodeGen::CodeGenFunction::AutoVarEmission const&) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103b10a4c)
#25 0x0000000105e333d4 clang::CodeGen::CodeGenFunction::EmitAutoVarDecl(clang::VarDecl const&) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103b0b3d4)
#26 0x0000000105e327a4 clang::CodeGen::CodeGenFunction::EmitVarDecl(clang::VarDecl const&) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103b0a7a4)
#27 0x0000000105e32098 clang::CodeGen::CodeGenFunction::EmitDecl(clang::Decl const&, bool) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103b0a098)
#28 0x000000010619a50c clang::CodeGen::CodeGenFunction::EmitDeclStmt(clang::DeclStmt const&) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103e7250c)
#29 0x0000000106190af8 clang::CodeGen::CodeGenFunction::EmitSimpleStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103e68af8)
#30 0x000000010618fc14 clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103e67c14)
#31 0x000000010619bb34 clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103e73b34)
#32 0x0000000106266ed4 clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103f3eed4)
#33 0x0000000106267f30 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103f3ff30)
#34 0x000000010629c6e0 clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103f746e0)
#35 0x0000000106292cec clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103f6acec)
#36 0x0000000106298058 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103f70058)
#37 0x000000010629144c clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103f6944c)
#38 0x00000001064a85c0 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x1041805c0)
#39 0x00000001062498c0 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103f218c0)
#40 0x000000010a3c7120 clang::ParseAST(clang::Sema&, bool, bool) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x10809f120)
#41 0x0000000106bbec24 clang::ASTFrontendAction::ExecuteAction() (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x104896c24)
#42 0x000000010624ff9c clang::CodeGenAction::ExecuteAction() (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x103f27f9c)
#43 0x0000000106bbe468 clang::FrontendAction::Execute() (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x104896468)
#44 0x0000000106ad7c64 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x1047afc64)
#45 0x0000000106cf8280 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x1049d0280)
#46 0x00000001023387f0 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x1000107f0)
#47 0x000000010232b708 ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x100003708)
#48 0x000000010232a47c clang_main(int, char**, llvm::ToolContext const&) (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x10000247c)
#49 0x000000010235f9dc main (/Users/amr.hesham/Desktop/Software/llvm-project/build/bin/clang-22+0x1000379dc)
#50 0x0000000184b32b98
[1]    30108 abort      ./bin/clang++ -cc1 -std=c++20 -triple x86_64-unknown-linux-gun -emit-llvm  -o

Godbolt:
https://godbolt.org/z/E7h8h8MaK

The reason:
In __real__(__imag__ a), We calculate type promotion for real as float32 because subexpression type is float16 (__imag__ a), but when we visit __imag__ a we pass the promoted type and reuse it again which shouldn't happend because now the type promotion should be Complex<float16> to Complex<float32>

Some Solutions: