Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

llvmcall: use AlwaysInliner + test inlining behavior #18409

Merged
merged 3 commits into from
Sep 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions src/ccall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,7 @@ static jl_cgval_t emit_llvmcall(jl_value_t **args, size_t nargs, jl_codectx_t *c
assert(isPtr);
// Create Function skeleton
f = (llvm::Function*)jl_unbox_voidpointer(ir);
assert(!f->isDeclaration());
assert(f->getReturnType() == rettype);
int i = 0;
for (std::vector<Type *>::iterator it = argtypes.begin();
Expand All @@ -1025,15 +1026,6 @@ static jl_cgval_t emit_llvmcall(jl_value_t **args, size_t nargs, jl_codectx_t *c
}
}

/*
* It might be tempting to just try to set the Always inline attribute on the function
* and hope for the best. However, this doesn't work since that would require an inlining
* pass (which is a Call Graph pass and cannot be managed by a FunctionPassManager). Instead
* We are sneaky and call the inliner directly. This however doesn't work until we've actually
* generated the entire function, so we need to store it in the context until the end of the
* function. This also has the benefit of looking exactly like we cut/pasted it in in `code_llvm`.
*/

// Since we dumped all of f's dependencies into the active module,
// we cannot reasonably inline it, so leave it there and just emit
// a regular call
Expand Down Expand Up @@ -1064,7 +1056,7 @@ static jl_cgval_t emit_llvmcall(jl_value_t **args, size_t nargs, jl_codectx_t *c
mark_gc_uses(gc_uses);
CallInst *inst = builder.CreateCall(f, ArrayRef<Value*>(&argvals[0], nargt));
if (isString)
ctx->to_inline.push_back(inst);
f->addFnAttr(Attribute::AlwaysInline);
mark_gc_uses(gc_uses);

JL_GC_POP();
Expand Down
18 changes: 1 addition & 17 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,6 @@ typedef struct {

bool debug_enabled;
bool is_inbounds{false};
std::vector<CallInst*> to_inline;
} jl_codectx_t;

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

// step 13, Apply LLVM level inlining
for(std::vector<CallInst*>::iterator it = ctx.to_inline.begin(); it != ctx.to_inline.end(); ++it) {
Function *inlinef = (*it)->getCalledFunction();
assert(inlinef->getParent());
InlineFunctionInfo info;
if (!InlineFunction(*it,info))
jl_error("Inlining Pass failed");
if (inlinef->getParent())
inlinef->eraseFromParent();
else {
inlinef->dropAllReferences();
delete inlinef;
}
}

// step 14. Perform any delayed instantiations
// step 13. Perform any delayed instantiations
if (ctx.debug_enabled) {
dbuilder.finalize();
}
Expand Down
2 changes: 2 additions & 0 deletions src/jitlayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <polly/CodeGen/CodegenCleanup.h>
#endif

#include <llvm/Transforms/IPO.h>
#include <llvm/Transforms/Scalar.h>
#include <llvm/Transforms/Utils/BasicBlockUtils.h>
#include <llvm/Transforms/Instrumentation.h>
Expand Down Expand Up @@ -141,6 +142,7 @@ void addOptimizationPasses(PassManager *PM)
// list of passes from vmkit
PM->add(createCFGSimplificationPass()); // Clean up disgusting code
PM->add(createPromoteMemoryToRegisterPass());// Kill useless allocas
PM->add(createAlwaysInlinerPass()); // Respect always_inline
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to have broken build on LLVM 4.0 (not sure about 3.9)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you fixed it already, thanks.


#ifndef INSTCOMBINE_BUG
PM->add(createInstructionCombiningPass()); // Cleanup for scalarrepl.
Expand Down
2 changes: 2 additions & 0 deletions test/llvmcall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ function declared_floor(x::Float64)
Float64, Tuple{Float64}, x)
end
@test declared_floor(4.2) ≈ 4.
ir = sprint(io->code_llvm(io, declared_floor, Tuple{Float64}))
@test contains(ir, "call double @llvm.floor.f64") # should be inlined

function doubly_declared_floor(x::Float64)
llvmcall(
Expand Down