Skip to content

Commit 3407fb3

Browse files
vchuravymaleadt
andcommitted
Emit aliases into the system image
- Put the interposer in llvm.compiler.used. - Injecting the aliases after optimization: Our multiversioning pass interacts badly with the llvm.compiler.used gvar. Co-authored-by: Tim Besard <tim.besard@gmail.com> Co-authored-by: Valentin Churavy <v.churavy@gmail.com>
1 parent ff36015 commit 3407fb3

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/aotcompile.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <llvm/Analysis/BasicAliasAnalysis.h>
2121
#include <llvm/Analysis/TypeBasedAliasAnalysis.h>
2222
#include <llvm/Analysis/ScopedNoAliasAA.h>
23+
#include <llvm/IR/IRBuilder.h>
2324
#include <llvm/IR/PassManager.h>
2425
#include <llvm/IR/Verifier.h>
2526
#include <llvm/Transforms/IPO.h>
@@ -32,6 +33,7 @@
3233
#include <llvm/Transforms/InstCombine/InstCombine.h>
3334
#include <llvm/Transforms/Scalar/InstSimplifyPass.h>
3435
#include <llvm/Transforms/Utils/SimplifyCFGOptions.h>
36+
#include <llvm/Transforms/Utils/ModuleUtils.h>
3537
#include <llvm/Passes/PassBuilder.h>
3638
#include <llvm/Passes/PassPlugin.h>
3739
#if defined(USE_POLLY)
@@ -434,6 +436,23 @@ static void reportWriterError(const ErrorInfoBase &E)
434436
jl_safe_printf("ERROR: failed to emit output file %s\n", err.c_str());
435437
}
436438

439+
static void injectCRTAlias(Module &M, StringRef name, StringRef alias, FunctionType *FT)
440+
{
441+
Function *target = M.getFunction(alias);
442+
if (!target) {
443+
target = Function::Create(FT, Function::ExternalLinkage, alias, M);
444+
}
445+
Function *interposer = Function::Create(FT, Function::WeakAnyLinkage, name, M);
446+
appendToCompilerUsed(M, {interposer});
447+
448+
llvm::IRBuilder<> builder(BasicBlock::Create(M.getContext(), "top", interposer));
449+
SmallVector<Value *, 4> CallArgs;
450+
for (auto &arg : interposer->args())
451+
CallArgs.push_back(&arg);
452+
auto val = builder.CreateCall(target, CallArgs);
453+
builder.CreateRet(val);
454+
}
455+
437456

438457
// takes the running content that has collected in the shadow module and dump it to disk
439458
// this builds the object file portion of the sysimage files for fast startup
@@ -550,7 +569,24 @@ void jl_dump_native_impl(void *native_code,
550569
auto add_output = [&] (Module &M, StringRef unopt_bc_Name, StringRef bc_Name, StringRef obj_Name, StringRef asm_Name) {
551570
preopt.run(M);
552571
optimizer.run(M);
572+
573+
// We would like to emit an alias or an weakref alias to redirect these symbols
574+
// but LLVM doesn't let us emit a GlobalAlias to a declaration...
575+
// So for now we inject a definition of these functions that calls our runtime
576+
// functions. We do so after optimization to avoid cloning these functions.
577+
injectCRTAlias(M, "__gnu_h2f_ieee", "julia__gnu_h2f_ieee",
578+
FunctionType::get(Type::getFloatTy(Context), { Type::getHalfTy(Context) }, false));
579+
injectCRTAlias(M, "__extendhfsf2", "julia__gnu_h2f_ieee",
580+
FunctionType::get(Type::getFloatTy(Context), { Type::getHalfTy(Context) }, false));
581+
injectCRTAlias(M, "__gnu_f2h_ieee", "julia__gnu_f2h_ieee",
582+
FunctionType::get(Type::getHalfTy(Context), { Type::getFloatTy(Context) }, false));
583+
injectCRTAlias(M, "__truncsfhf2", "julia__gnu_f2h_ieee",
584+
FunctionType::get(Type::getHalfTy(Context), { Type::getFloatTy(Context) }, false));
585+
injectCRTAlias(M, "__truncdfhf2", "julia__truncdfhf2",
586+
FunctionType::get(Type::getHalfTy(Context), { Type::getDoubleTy(Context) }, false));
587+
553588
postopt.run(M);
589+
554590
if (unopt_bc_fname)
555591
emit_result(unopt_bc_Archive, unopt_bc_Buffer, unopt_bc_Name, outputs);
556592
if (bc_fname)

0 commit comments

Comments
 (0)