<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/54797>54797</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            ORC cannot run initializers with non-void signatures
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            orcjit
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          hahnjo
      </td>
    </tr>
</table>

<pre>
    Consider the following program in C:
```c
#include <stdio.h>

__attribute__((constructor(1001)))
static int constructor(void *ptr) {
  puts("constructor");
  return 0;
}

int main() {
  return 0;
}
```

or the (stripped down) version in LLVM IR:
```llvm
@.str = private unnamed_addr constant [12 x i8] c"constructor\00", align 1
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 1001, void ()* bitcast (i32 (i8*)* @constructor to void ()*), i8* null }]

define internal i32 @constructor(i8* noundef %ptr) {
entry:
  %ptr.addr = alloca i8*, align 8
  store i8* %ptr, i8** %ptr.addr, align 8
  %call = call i32 @puts(i8* noundef getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
  ret i32 0
}

declare i32 @puts(i8* noundef)

define dso_local i32 @main() {
entry:
  %retval = alloca i32, align 4
  store i32 0, i32* %retval, align 4
  ret i32 0
}
```

When trying to run with `lli --jit-kind=orc constructor.min.ll`, there is a segmentation fault because the `llvm::Function *` and its type are null pointers. This is because the `CtorDtorIterator` in `ExecutionUtils.cpp` expects another `ConstantExpr` in the first operand of a cast, that could be fixed as follows:
```diff
diff --git a/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp b/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp
index ae2d47fb8c5e..cbc27980f57a 100644
--- a/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp
@@ -62,7 +62,7 @@ CtorDtorIterator::Element CtorDtorIterator::operator*() const {
       break;
     } else if (ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(FuncC)) {
       if (CE->isCast())
-        FuncC = dyn_cast_or_null<ConstantExpr>(CE->getOperand(0));
+        FuncC = CE->getOperand(0);
       else
         break;
     } else {
```

However, then an assertion trips because the code expects to call a function without parameters:
```
llvm/lib/IR/Instructions.cpp:525: void llvm::CallInst::init(llvm::FunctionType*, llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::ArrayRef<llvm::OperandBundleDefT<llvm::Value*> >, const llvm::Twine&): Assertion `(Args.size() == FTy->getNumParams() || (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && "Calling a function with bad signature!"' failed.
```

This at least matches MCJIT:
```
llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp:523: virtual llvm::GenericValue llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef<llvm::GenericValue>): Assertion `(FTy->getNumParams() == ArgValues.size() || (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) && "Wrong number of arguments passed into function!"' failed.
```

@lhames 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJylV9ty2zYQ_RrpBSMNRer6oAealtt0kqaTcZNHDUiCEhIK0ACgL_n6ngUvoizZdRsPLYrgYnexe_bsKtX58zrRyspcGOb2ghW6LPWjVDt2NHpn-IFJxZJBFA-C20EQD-ZBfWXNcxhJlZVVLtggSqzLpR7vB9Gmees_t1vunJFp5cR2OwiXuDKYdKbKnDZ4mgTBZBCumsvvsY47mcG2Y-eyD1rmbBDGR4enFRssbuoNjB0rZ7328GxHSDqjTsoIVxnFgm5psLjtO0sWD1wqr-lM_xs726D0Fek6ntADX-TxKHKW60dFSh-EsVIriuzHj18_sQ9fLuNblg-HZmkajKEC8b1FTuQDd4JVSvGDyLc8z00dIA6_B7ObSciemFwOZrcsexGJWRIEPhwJ46XcKTbp9JOx8a7UKS-3JGu9MQ6nVU5QqF95_VCPm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