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

    <tr>
        <th>Summary</th>
        <td>
            JIT can't overwrite a weak function content for re-execution
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    I'm making a JIT REPL, where I made a while loop for each iteration of the REPL. Before the loop starts up, it initializes the LLVM and the LLJIT:

```cpp
 llvm::InitializeNativeTarget();
 llvm::InitializeNativeTargetAsmPrinter();
 llvm::InitializeNativeTargetAsmParser();

    auto JIT = llvm::orc::LLJITBuilder().create();
    if (!JIT) {
        llvm::errs() << "Error initializing LLJIT: " << llvm::toString(JIT.takeError()) << "\n";
        return 1;
    }
    llvm::LLVMContext TheContext;
```

Now, inside the loop, for each iteration I've made a redefinition of the Module and the Builder, so every time I press enter, it starts from scratch.

```cpp
auto TheModule = std::make_unique<llvm::Module>("GalaxyJIT", TheContext);
llvm::IRBuilder<llvm::NoFolder> Builder(TheContext);
```
But I kept track of declaration of functions and variables, variables attributions and extern functions, so it can be reloaded in the code generation of my language. 

After this, I create an `entry.main` function which is a weak function, where all the code is stored:

```cpp
 llvm::FunctionType *mainFuncType = llvm::FunctionType::get(Builder.getVoidTy(), false);
        llvm::Function *mainFunc = llvm::Function::Create(mainFuncType, llvm::GlobalValue::WeakODRLinkage, "entry.main", *TheModule);

 llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(TheContext, "entry", mainFunc);
        Builder.SetInsertPoint(entryBlock);

 generate_ir(ast, TheContext, *TheModule, Builder);

 Builder.CreateRetVoid();
```

And after all of this I add the module to the JIT: 

```cpp
        if (auto Err = (*JIT)->addIRModule(llvm::orc::ThreadSafeModule(std::move(TheModule), std::make_unique<llvm::LLVMContext>()))) {
            llvm::errs() << "Error adding the IR module to JIT: " << llvm::toString(std::move(Err)) << "\n";
            free_ast_node(ast);
            freeTokens(tokens, count);
            return 1;
        }

        auto Func = (*JIT)->lookup("entry.main");
        if (!Func) {
 llvm::errs() << "Error: entry.main not found.\n";
 free_ast_node(ast);
            freeTokens(tokens, count);
 continue;
        }
```

So that's the design of my REPL. But the problem is: from my understanding of reading the docs, only weak functions can be redefined. And I emmited the LLVM of each iteration, confirming that it happens. But even when the function is overwritten, the JIT engine cant see the new function with the new IR inside. It only works on the first iteration. But the weirdest thing is that on the emmited LLVM everything seems to be fine, since all the IR is indeed generated, but not executed. It seems like the JIT engine only "remembers" the first function's state. How can I solve it?
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysV11v4jgX_jXm5qgIHMrHBReUlnkz6sw7omj2snLiE_CS2Kx9AmV__cpOQgLDarvSokiNHft8POc5HxXOqa1GnLPHJ_b43BMl7Yydn9CVa7HFXmLkeR4zPimgEHultyDga7yB9cuPV8aXcNqhRYihEBJBwGmncoTcmANkxgKKdAeK0ApSRoPJgHYY7vbhCTNjMWyE846EJQflwYtVBEorUiJXf6ILh15ff34DoWW9-BpvWLRgg_CMB9WTHg5ssIA8Pxb-Y7SIL0K-C1JH3Ai7RWJ8yviMRU-fObxwxQ-rNKH919eEdde3_EUAECWZgCKLnjuCjE2rl-DdU6ly2VzvpxYF4ZUFAKAyCFtDjwafAZs0X_yvlYzWuuousGjJoiUwzl-sNbaF2ce2gdV_bk62Usi8kVV6y_j0a7zpk9hjkFFbdSWcPS61_xN1DbJIpdUwbHfZ5Ll-a9X4QC-NJvwg2Oywfq0BbCJdgfndnAJbtFOypZLfusM-z-IjNky1KDELzrfE_GZkmeOFZJcILMEZwCPaM5AqPN0PFp0DrFgR6FrTN7OmAJdaQemuf5edIfibHdbKPAUcycr1QuzxvdTqjxJZtGwhqc6y6CVAzb-IXHycQ8y5V99BqWFHh57rxo-uxO9mZarNl9bP6R1BXcSfSoIY9nggICvSvcdNYp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