Bug type: crash
Component: MergeFunctions
Bug location: llvm/lib/Transforms/IPO/MergeFunctions.cpp: canCreateThunkFor
Issue title: [MergeFunc] Creates calls for calling convention that does not allow calls
Labels: ipo
crash
Reported by: Emilyaxe

When running opt on the following IR, opt crashes. 
I am not sure whether this is a valid case. If using opt in this way is not appropriate, I am open to closing this issue.
godbolt: https://godbolt.org/z/873edWGzK

llvm version 50ae726bb3498

test.ll

```
define amdgpu_kernel void @hamming_search_inner_loop1() {
entry:
  br label %end

end:                                              ; preds = %entry
  ret void
}

define amdgpu_kernel void @hamming_search_inner_loop2() {
entry:
  br label %end

end:                                              ; preds = %entry
  ret void
}

```


commads
./bin/opt test.ll -passes=mergefunc -S

stacktrace

```
calling convention does not permit calls
  tail call amdgpu_kernel void @hamming_search_inner_loop1()
LLVM ERROR: Broken module found, compilation aborted!
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0.	Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/opt -o /app/output.s -S -passes=mergefunc -S <source>
1.	Running pass "verify" on module "<source>"
 #0 0x0000000005a44d98 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x5a44d98)
 #1 0x0000000005a41c44 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #2 0x00007bd9c3c42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x00007bd9c3c969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x00007bd9c3c42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x00007bd9c3c287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x00000000008339a1 llvm::json::operator==(llvm::json::Value const&, llvm::json::Value const&) (.cold) JSON.cpp:0:0
 #7 0x0000000005979301 (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x5979301)
 #8 0x0000000005862a88 (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x5862a88)
 #9 0x000000000097d40e llvm::detail::PassModel<llvm::Module, llvm::VerifierPass, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x97d40e)
#10 0x000000000581f0b1 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x581f0b1)
#11 0x000000000098776a llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRef<llvm::PassPlugin>, llvm::ArrayRef<std::function<void (llvm::PassBuilder&)>>, llvm::opt_tool::OutputKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x98776a)
#12 0x000000000097b5e8 optMain (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x97b5e8)
#13 0x00007bd9c3c29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#14 0x00007bd9c3c29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#15 0x0000000000972085 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x972085)
Program terminated with signal: SIGSEGV
Compiler returned: 139
```


Comments: author: nikic
body: This is basically the same as https://github.com/llvm/llvm-project/issues/39579, though this one produces a verifier error.
author: Flakebi
body: I opened a PR to fix both of these issues: #174254

Fix patch:
commit 4d66cccffe179d0d2bbec833ace041349cf872a4
Author: Sebastian Neubauer <flakebi@t-online.de>
Date:   Sun Jan 4 13:35:16 2026 +0100

    [MergeFunc] Do not merge kernel functions (#174254)
    
    Kernels cannot be called, so we cannot introduce calls to them in
    MergeFunctions.
    
    The test uses a `--implicit-check-not=call` to make sure that only the
    function with C calling convention gets merged.
    
    Fixes #39579. Fixes #173355.

diff --git a/llvm/lib/Transforms/IPO/MergeFunctions.cpp b/llvm/lib/Transforms/IPO/MergeFunctions.cpp
index 0faa36a495ac..fcf3f7c89c71 100644
--- a/llvm/lib/Transforms/IPO/MergeFunctions.cpp
+++ b/llvm/lib/Transforms/IPO/MergeFunctions.cpp
@@ -697,6 +697,9 @@ static bool canCreateThunkFor(Function *F) {
   if (F->isVarArg())
     return false;
 
+  if (F->hasKernelCallingConv())
+    return false;
+
   // Don't merge tiny functions using a thunk, since it can just end up
   // making the function larger.
   if (F->size() == 1) {


Regression tests:
file: llvm/test/Transforms/MergeFunc/merge-calling-conv.ll
commands: opt -S -passes=mergefunc < %s
tests: test_name: <module>
test_body: 
; Check that no calls are generated for certain calling conventions

@debug = global i32 0


define void @normal(i32 %a) unnamed_addr {
  %b = xor i32 %a, 0
  store i32 %b, ptr @debug
  ret void
}

define void @as_normal(i32 %a) unnamed_addr {
  %b = xor i32 %a, 0
  store i32 %b, ptr @debug
  ret void
}

define amdgpu_kernel void @amdgpu_kernel(i32 %a) unnamed_addr {
  %b = xor i32 %a, 1
  store i32 %b, ptr @debug
  ret void
}

define amdgpu_kernel void @as_amdgpu_kernel(i32 %a) unnamed_addr {
  %b = xor i32 %a, 1
  store i32 %b, ptr @debug
  ret void
}

define ptx_kernel void @ptx_kernel(i32 %a) unnamed_addr {
  %b = xor i32 %a, 2
  store i32 %b, ptr @debug
  ret void
}

define ptx_kernel void @as_ptx_kernel(i32 %a) unnamed_addr {
  %b = xor i32 %a, 2
  store i32 %b, ptr @debug
  ret void
}

define spir_kernel void @spir_kernel(i32 %a) unnamed_addr {
  %b = xor i32 %a, 3
  store i32 %b, ptr @debug
  ret void
}

define spir_kernel void @as_spir_kernel(i32 %a) unnamed_addr {
  %b = xor i32 %a, 3
  store i32 %b, ptr @debug
  ret void
}