Skip to content

Commit 713e995

Browse files
authored
Merge pull request #18409 from JuliaLang/tb/always_inline
llvmcall: use AlwaysInliner + test inlining behavior
2 parents e665592 + 9139878 commit 713e995

File tree

4 files changed

+7
-27
lines changed

4 files changed

+7
-27
lines changed

src/ccall.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,7 @@ static jl_cgval_t emit_llvmcall(jl_value_t **args, size_t nargs, jl_codectx_t *c
10001000
assert(isPtr);
10011001
// Create Function skeleton
10021002
f = (llvm::Function*)jl_unbox_voidpointer(ir);
1003+
assert(!f->isDeclaration());
10031004
assert(f->getReturnType() == rettype);
10041005
int i = 0;
10051006
for (std::vector<Type *>::iterator it = argtypes.begin();
@@ -1025,15 +1026,6 @@ static jl_cgval_t emit_llvmcall(jl_value_t **args, size_t nargs, jl_codectx_t *c
10251026
}
10261027
}
10271028

1028-
/*
1029-
* It might be tempting to just try to set the Always inline attribute on the function
1030-
* and hope for the best. However, this doesn't work since that would require an inlining
1031-
* pass (which is a Call Graph pass and cannot be managed by a FunctionPassManager). Instead
1032-
* We are sneaky and call the inliner directly. This however doesn't work until we've actually
1033-
* generated the entire function, so we need to store it in the context until the end of the
1034-
* function. This also has the benefit of looking exactly like we cut/pasted it in in `code_llvm`.
1035-
*/
1036-
10371029
// Since we dumped all of f's dependencies into the active module,
10381030
// we cannot reasonably inline it, so leave it there and just emit
10391031
// a regular call
@@ -1064,7 +1056,7 @@ static jl_cgval_t emit_llvmcall(jl_value_t **args, size_t nargs, jl_codectx_t *c
10641056
mark_gc_uses(gc_uses);
10651057
CallInst *inst = builder.CreateCall(f, ArrayRef<Value*>(&argvals[0], nargt));
10661058
if (isString)
1067-
ctx->to_inline.push_back(inst);
1059+
f->addFnAttr(Attribute::AlwaysInline);
10681060
mark_gc_uses(gc_uses);
10691061

10701062
JL_GC_POP();

src/codegen.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,6 @@ typedef struct {
548548

549549
bool debug_enabled;
550550
bool is_inbounds{false};
551-
std::vector<CallInst*> to_inline;
552551
} jl_codectx_t;
553552

554553
static jl_cgval_t emit_expr(jl_value_t *expr, jl_codectx_t *ctx);
@@ -5025,22 +5024,7 @@ static std::unique_ptr<Module> emit_function(jl_method_instance_t *lam, jl_code_
50255024
builder.SetCurrentDebugLocation(noDbg);
50265025
builder.ClearInsertionPoint();
50275026

5028-
// step 13, Apply LLVM level inlining
5029-
for(std::vector<CallInst*>::iterator it = ctx.to_inline.begin(); it != ctx.to_inline.end(); ++it) {
5030-
Function *inlinef = (*it)->getCalledFunction();
5031-
assert(inlinef->getParent());
5032-
InlineFunctionInfo info;
5033-
if (!InlineFunction(*it,info))
5034-
jl_error("Inlining Pass failed");
5035-
if (inlinef->getParent())
5036-
inlinef->eraseFromParent();
5037-
else {
5038-
inlinef->dropAllReferences();
5039-
delete inlinef;
5040-
}
5041-
}
5042-
5043-
// step 14. Perform any delayed instantiations
5027+
// step 13. Perform any delayed instantiations
50445028
if (ctx.debug_enabled) {
50455029
dbuilder.finalize();
50465030
}

src/jitlayers.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <polly/CodeGen/CodegenCleanup.h>
2828
#endif
2929

30+
#include <llvm/Transforms/IPO.h>
3031
#include <llvm/Transforms/Scalar.h>
3132
#include <llvm/Transforms/Utils/BasicBlockUtils.h>
3233
#include <llvm/Transforms/Instrumentation.h>
@@ -141,6 +142,7 @@ void addOptimizationPasses(PassManager *PM)
141142
// list of passes from vmkit
142143
PM->add(createCFGSimplificationPass()); // Clean up disgusting code
143144
PM->add(createPromoteMemoryToRegisterPass());// Kill useless allocas
145+
PM->add(createAlwaysInlinerPass()); // Respect always_inline
144146

145147
#ifndef INSTCOMBINE_BUG
146148
PM->add(createInstructionCombiningPass()); // Cleanup for scalarrepl.

test/llvmcall.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ function declared_floor(x::Float64)
8585
Float64, Tuple{Float64}, x)
8686
end
8787
@test declared_floor(4.2) 4.
88+
ir = sprint(io->code_llvm(io, declared_floor, Tuple{Float64}))
89+
@test contains(ir, "call double @llvm.floor.f64") # should be inlined
8890

8991
function doubly_declared_floor(x::Float64)
9092
llvmcall(

0 commit comments

Comments
 (0)