llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang][CodeGen] Vector inline-asm output tied to a larger integer input: invalid bitcast, release IR fails the verifier

Open skaeads12 opened this issue 2 months ago • 1 comments

https://godbolt.org/z/TjEaesrPE (-O0, x86_64)

__attribute__((vector_size(4))) int v;
void f(long x) { __asm__("" : "=r"(v) : "0"(x)); }
clang: llvm/lib/IR/Instructions.cpp:3164: 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.
 #11 clang::AsmConstraintsInfo::EmitAsmStores(llvm::ArrayRef<llvm::Value*>)
 #12 clang::AsmConstraintsInfo::EmitAsmStmt()
 #13 clang::CodeGen::CodeGenFunction::EmitAsmStmt(clang::AsmStmt const&)

Release builds emit IR that fails the verifier (https://godbolt.org/z/n4xrKsfY1):

  %4 = call i64 asm "", "=r,0,~{dirflag},~{fpsr},~{flags}"(i64 %3)
  %5 = bitcast i64 %4 to <1 x i32>

With -fverify-intermediate-code, the release build stops with Invalid bitcast and fatal error: error in backend: Broken module found, compilation aborted!. Without that flag, the backend turns the invalid IR into movq %rax, v(%rip), an 8-byte store to the 4-byte v. GCC accepts the code and emits movl %eax, v(%rip).

The input is tied ("0") to a smaller output. Sema::ActOnGCCAsmStmt accepts the pair on its "extend the shorter one" path: the input is an integer or pointer, the output allows a register, and on that path a non-scalar output is rejected only if it does not fit in a GPR (#206230). CodeGen then makes the asm return the input type (i64). EmitAsmStores converts that back to the output type with CreateBitCast, which is invalid because the sizes differ. int and struct { int } outputs compile fine, and a float output is rejected (unsupported inline asm: input with type 'long' matching output with type 'float'). The crash is the same with a void * input (https://godbolt.org/z/W4qav6aao) and with "=Q".

A second tied-operand crash, this one in SelectionDAG, uses the multi-alternative constraint "=rm" (https://godbolt.org/z/rWPxoTcT8, -O2; -O0 crashes too):

long double a;
double b;
void c() { __asm__("" : "=rm"(b) : "0"(a)); }
clang: llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp:7384: llvm::SDValue llvm::SelectionDAG::getNode(unsigned int, const llvm::SDLoc&, llvm::EVT, llvm::SDValue, llvm::SDNodeFlags): Assertion `VT.getSizeInBits() == N1.getValueSizeInBits() && "Cannot BITCAST between types of different sizes!"' failed.
 #11 getRegistersForValue(...) SelectionDAGBuilder.cpp
 #12 prepareDAGLevelOperands(...) SelectionDAGBuilder.cpp
 #13 llvm::SelectionDAGBuilder::visitInlineAsm(llvm::CallBase const&, llvm::BasicBlock const*)

GCC 16.2 accepts this and "=r", and rejects only "=m". Clang rejects "=m" in Sema (https://godbolt.org/z/89hPc943q) and "=r" in the backend (could not allocate output register for constraint 'r'), but "=rm" asserts. Release builds compile it without a diagnostic. At -O0 clang emits call void asm "", "=*rm,0,~{dirflag},~{fpsr},~{flags}"(ptr elementtype(double) @b, x86_fp80 %1). The reduced IR below passes opt -passes=verify, and llc asserts on it directly (GCC, release trunk and llc: https://godbolt.org/z/v8dx8v1zP):

target triple = "x86_64-unknown-linux-gnu"
@b = global double 0.0
define void @c(x86_fp80 %x) {
  call void asm "", "=*rm,0"(ptr elementtype(double) @b, x86_fp80 %x)
  ret void
}

So SelectionDAG probably needs to diagnose this instead of asserting, whatever Sema does. I can move this case to its own issue if that is preferred.

Found by a fuzzer (generated C program), reduced by hand. Command: clang --target=x86_64-unknown-linux-gnu -O0 -c repro.c (first case); clang --target=x86_64-unknown-linux-gnu -O2 -c repro2.c (second case; -O0 also asserts) Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29): both assertions above. Release trunk f4085066128a: the first case emits the IR above, and the second compiles without a diagnostic. History: long-standing, not bisected. The first case hits Invalid cast! in every assertions build I checked: 3.9, 6.0, 10.0, 13.0, 16.0, 20.1, 22.1, 23.1 and trunk. The second case hits the BITCAST assertion in 10.0, 16.0, 20.1, 23.1 and trunk; 3.9 asserts earlier, in getCopyToParts ("Unknown mismatch!"). Classification: crash on valid code (GCC 16.2 accepts both cases). Related: #48610, #48510 and #29436 (all still open) are int/FP versions of this tied-operand mismatch. Trunk now rejects all three reproducers with "unsupported inline asm: input with type ... matching output with type ..." (#48510 is now labelled rejects-valid). That check came in #119098 (for #118892); #48610's reproducer still asserts in 19.1 and is rejected from 20.1 on. #206230 (for #204775) added the check for non-scalar outputs that are larger than a GPR. Neither check covers a vector output that is smaller than its tied input. #171795 is a different Invalid cast! in EmitAsmStmt (a vector in constraint "k", not tied). Open PR #197798 reworks how multi-alternative constraints such as "rm" are lowered; I have not checked whether it changes the second case. I found no existing issue for either case (searched EmitAsmStores, "Invalid cast" + inline asm + vector, "Cannot BITCAST between types of different sizes" + asm). #225038 hits the same Invalid cast! assertion, but from ScalarExprEmitter::VisitCastExpr (lax conversion between long double and integer vectors); it has a different root cause.

skaeads12 avatar Sep 29 '26 14:09 skaeads12

@llvm/issue-subscribers-clang-codegen

Author: skaeads12

https://godbolt.org/z/TjEaesrPE (`-O0`, x86_64)
__attribute__((vector_size(4))) int v;
void f(long x) { __asm__("" : "=r"(v) : "0"(x)); }
clang: llvm/lib/IR/Instructions.cpp:3164: static llvm::CastInst* llvm::CastInst::Create(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, const llvm::Twine&amp;, llvm::InsertPosition): Assertion `castIsValid(op, S, Ty) &amp;&amp; "Invalid cast!"' failed.
 #<!-- -->11 clang::AsmConstraintsInfo::EmitAsmStores(llvm::ArrayRef&lt;llvm::Value*&gt;)
 #<!-- -->12 clang::AsmConstraintsInfo::EmitAsmStmt()
 #<!-- -->13 clang::CodeGen::CodeGenFunction::EmitAsmStmt(clang::AsmStmt const&amp;)

Release builds emit IR that fails the verifier (https://godbolt.org/z/n4xrKsfY1):

  %4 = call i64 asm "", "=r,0,~{dirflag},~{fpsr},~{flags}"(i64 %3)
  %5 = bitcast i64 %4 to &lt;1 x i32&gt;

With -fverify-intermediate-code, the release build stops with Invalid bitcast and fatal error: error in backend: Broken module found, compilation aborted!. Without that flag, the backend turns the invalid IR into movq %rax, v(%rip), an 8-byte store to the 4-byte v. GCC accepts the code and emits movl %eax, v(%rip).

The input is tied ("0") to a smaller output. Sema::ActOnGCCAsmStmt accepts the pair on its "extend the shorter one" path: the input is an integer or pointer, the output allows a register, and on that path a non-scalar output is rejected only if it does not fit in a GPR (#206230). CodeGen then makes the asm return the input type (i64). EmitAsmStores converts that back to the output type with CreateBitCast, which is invalid because the sizes differ. int and struct { int } outputs compile fine, and a float output is rejected (unsupported inline asm: input with type 'long' matching output with type 'float'). The crash is the same with a void * input (https://godbolt.org/z/W4qav6aao) and with "=Q".

A second tied-operand crash, this one in SelectionDAG, uses the multi-alternative constraint "=rm" (https://godbolt.org/z/rWPxoTcT8, -O2; -O0 crashes too):

long double a;
double b;
void c() { __asm__("" : "=rm"(b) : "0"(a)); }
clang: llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp:7384: llvm::SDValue llvm::SelectionDAG::getNode(unsigned int, const llvm::SDLoc&amp;, llvm::EVT, llvm::SDValue, llvm::SDNodeFlags): Assertion `VT.getSizeInBits() == N1.getValueSizeInBits() &amp;&amp; "Cannot BITCAST between types of different sizes!"' failed.
 #<!-- -->11 getRegistersForValue(...) SelectionDAGBuilder.cpp
 #<!-- -->12 prepareDAGLevelOperands(...) SelectionDAGBuilder.cpp
 #<!-- -->13 llvm::SelectionDAGBuilder::visitInlineAsm(llvm::CallBase const&amp;, llvm::BasicBlock const*)

GCC 16.2 accepts this and "=r", and rejects only "=m". Clang rejects "=m" in Sema (https://godbolt.org/z/89hPc943q) and "=r" in the backend (could not allocate output register for constraint 'r'), but "=rm" asserts. Release builds compile it without a diagnostic. At -O0 clang emits call void asm "", "=*rm,0,~{dirflag},~{fpsr},~{flags}"(ptr elementtype(double) @<!-- -->b, x86_fp80 %1). The reduced IR below passes opt -passes=verify, and llc asserts on it directly (GCC, release trunk and llc: https://godbolt.org/z/v8dx8v1zP):

target triple = "x86_64-unknown-linux-gnu"
@<!-- -->b = global double 0.0
define void @<!-- -->c(x86_fp80 %x) {
  call void asm "", "=*rm,0"(ptr elementtype(double) @<!-- -->b, x86_fp80 %x)
  ret void
}

So SelectionDAG probably needs to diagnose this instead of asserting, whatever Sema does. I can move this case to its own issue if that is preferred.

Found by a fuzzer (generated C program), reduced by hand. Command: clang --target=x86_64-unknown-linux-gnu -O0 -c repro.c (first case); clang --target=x86_64-unknown-linux-gnu -O2 -c repro2.c (second case; -O0 also asserts) Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29): both assertions above. Release trunk f4085066128a: the first case emits the IR above, and the second compiles without a diagnostic. History: long-standing, not bisected. The first case hits Invalid cast! in every assertions build I checked: 3.9, 6.0, 10.0, 13.0, 16.0, 20.1, 22.1, 23.1 and trunk. The second case hits the BITCAST assertion in 10.0, 16.0, 20.1, 23.1 and trunk; 3.9 asserts earlier, in getCopyToParts ("Unknown mismatch!"). Classification: crash on valid code (GCC 16.2 accepts both cases). Related: #48610, #48510 and #29436 (all still open) are int/FP versions of this tied-operand mismatch. Trunk now rejects all three reproducers with "unsupported inline asm: input with type ... matching output with type ..." (#48510 is now labelled rejects-valid). That check came in #119098 (for #118892); #48610's reproducer still asserts in 19.1 and is rejected from 20.1 on. #206230 (for #204775) added the check for non-scalar outputs that are larger than a GPR. Neither check covers a vector output that is smaller than its tied input. #171795 is a different Invalid cast! in EmitAsmStmt (a vector in constraint "k", not tied). Open PR #197798 reworks how multi-alternative constraints such as "rm" are lowered; I have not checked whether it changes the second case. I found no existing issue for either case (searched EmitAsmStores, "Invalid cast" + inline asm + vector, "Cannot BITCAST between types of different sizes" + asm). #225038 hits the same Invalid cast! assertion, but from ScalarExprEmitter::VisitCastExpr (lax conversion between long double and integer vectors); it has a different root cause.