Commit 51500dfb authored by Brendan Gregg's avatar Brendan Gregg Committed by GitHub

Merge pull request #149 from mlen/master

Initial LLVM 7 port
parents 5d00fa3b de5e6d81
......@@ -164,6 +164,7 @@ void CodegenLLVM::visit(Call &call)
AllocaInst *newval = b_.CreateAllocaBPF(map.type, map.ident + "_val");
call.vargs->front()->accept(*this);
expr_ = b_.CreateIntCast(expr_, b_.getInt64Ty(), false); // promote int to 64-bit
b_.CreateStore(b_.CreateAdd(expr_, oldval), newval);
b_.CreateMapUpdateElem(map, key, newval);
......@@ -183,6 +184,7 @@ void CodegenLLVM::visit(Call &call)
// elements will always store on the first occurrance. Revent this later when printing.
Function *parent = b_.GetInsertBlock()->getParent();
call.vargs->front()->accept(*this);
expr_ = b_.CreateIntCast(expr_, b_.getInt64Ty(), false); // promote int to 64-bit
Value *inverted = b_.CreateSub(b_.getInt64(0xffffffff), expr_);
BasicBlock *lt = BasicBlock::Create(module_->getContext(), "min.lt", parent);
BasicBlock *ge = BasicBlock::Create(module_->getContext(), "min.ge", parent);
......@@ -207,6 +209,7 @@ void CodegenLLVM::visit(Call &call)
Function *parent = b_.GetInsertBlock()->getParent();
call.vargs->front()->accept(*this);
expr_ = b_.CreateIntCast(expr_, b_.getInt64Ty(), false); // promote int to 64-bit
BasicBlock *lt = BasicBlock::Create(module_->getContext(), "min.lt", parent);
BasicBlock *ge = BasicBlock::Create(module_->getContext(), "min.ge", parent);
b_.CreateCondBr(b_.CreateICmpSGE(expr_, oldval), ge, lt);
......@@ -239,6 +242,7 @@ void CodegenLLVM::visit(Call &call)
Value *total_old = b_.CreateMapLookupElem(map, total_key);
AllocaInst *total_new = b_.CreateAllocaBPF(map.type, map.ident + "_val");
call.vargs->front()->accept(*this);
expr_ = b_.CreateIntCast(expr_, b_.getInt64Ty(), false); // promote int to 64-bit
b_.CreateStore(b_.CreateAdd(expr_, total_old), total_new);
b_.CreateMapUpdateElem(map, total_key, total_new);
b_.CreateLifetimeEnd(total_key);
......@@ -250,6 +254,7 @@ void CodegenLLVM::visit(Call &call)
{
Map &map = *call.map;
call.vargs->front()->accept(*this);
expr_ = b_.CreateIntCast(expr_, b_.getInt64Ty(), false); // promote int to 64-bit
Function *log2_func = module_->getFunction("log2");
Value *log2 = b_.CreateCall(log2_func, expr_, "log2");
AllocaInst *key = getHistMapKey(map, log2);
......@@ -285,6 +290,12 @@ void CodegenLLVM::visit(Call &call)
step_arg.accept(*this);
step = expr_;
// promote int to 64-bit
value = b_.CreateIntCast(value, b_.getInt64Ty(), false);
min = b_.CreateIntCast(min, b_.getInt64Ty(), false);
max = b_.CreateIntCast(max, b_.getInt64Ty(), false);
step = b_.CreateIntCast(step, b_.getInt64Ty(), false);
Value *linear = b_.CreateCall(linear_func, {value, min, max, step} , "linear");
AllocaInst *key = getHistMapKey(map, linear);
......@@ -433,7 +444,7 @@ void CodegenLLVM::visit(Call &call)
arg.accept(*this);
Value *offset = b_.CreateGEP(printf_args, {b_.getInt32(0), b_.getInt32(i)});
if (arg.type.IsArray())
b_.CreateMemCpy(offset, expr_, arg.type.size, 1);
b_.CREATE_MEMCPY(offset, expr_, arg.type.size, 1);
else
b_.CreateStore(expr_, offset);
}
......@@ -482,7 +493,7 @@ void CodegenLLVM::visit(Call &call)
arg.accept(*this);
Value *offset = b_.CreateGEP(system_args, {b_.getInt32(0), b_.getInt32(i)});
if (arg.type.IsArray())
b_.CreateMemCpy(offset, expr_, arg.type.size, 1);
b_.CREATE_MEMCPY(offset, expr_, arg.type.size, 1);
else
b_.CreateStore(expr_, offset);
}
......@@ -555,7 +566,7 @@ void CodegenLLVM::visit(Call &call)
b_.CreateStore(b_.getInt64(0), b_.CreateGEP(perfdata, {b_.getInt64(0), b_.getInt64(sizeof(uint64_t) + sizeof(uint64_t))}));
// store map ident:
b_.CreateMemCpy(b_.CreateGEP(perfdata, {b_.getInt64(0), b_.getInt64(sizeof(uint64_t) + 2 * sizeof(uint64_t))}), str_buf, map.ident.length() + 1, 1);
b_.CREATE_MEMCPY(b_.CreateGEP(perfdata, {b_.getInt64(0), b_.getInt64(sizeof(uint64_t) + 2 * sizeof(uint64_t))}), str_buf, map.ident.length() + 1, 1);
b_.CreatePerfEventOutput(ctx_, perfdata, sizeof(uint64_t) + 2 * sizeof(uint64_t) + map.ident.length() + 1);
b_.CreateLifetimeEnd(perfdata);
expr_ = nullptr;
......@@ -574,7 +585,7 @@ void CodegenLLVM::visit(Call &call)
b_.CreateStore(b_.getInt64(asyncactionint(AsyncAction::clear)), perfdata);
else
b_.CreateStore(b_.getInt64(asyncactionint(AsyncAction::zero)), perfdata);
b_.CreateMemCpy(b_.CreateGEP(perfdata, {b_.getInt64(0), b_.getInt64(sizeof(uint64_t))}), str_buf, map.ident.length() + 1, 1);
b_.CREATE_MEMCPY(b_.CreateGEP(perfdata, {b_.getInt64(0), b_.getInt64(sizeof(uint64_t))}), str_buf, map.ident.length() + 1, 1);
b_.CreatePerfEventOutput(ctx_, perfdata, sizeof(uint64_t) + map.ident.length() + 1);
b_.CreateLifetimeEnd(perfdata);
expr_ = nullptr;
......@@ -768,12 +779,12 @@ void CodegenLLVM::visit(Ternary &ternary)
// copy selected string via CreateMemCpy
b_.SetInsertPoint(left_block);
ternary.left->accept(*this);
b_.CreateMemCpy(buf, expr_, ternary.type.size, 1);
b_.CREATE_MEMCPY(buf, expr_, ternary.type.size, 1);
b_.CreateBr(done);
b_.SetInsertPoint(right_block);
ternary.right->accept(*this);
b_.CreateMemCpy(buf, expr_, ternary.type.size, 1);
b_.CREATE_MEMCPY(buf, expr_, ternary.type.size, 1);
b_.CreateBr(done);
b_.SetInsertPoint(done);
......@@ -800,7 +811,7 @@ void CodegenLLVM::visit(FieldAccess &acc)
{
// TODO This should be do-able without allocating more memory here
AllocaInst *dst = b_.CreateAllocaBPF(field.type, "internal_" + type.cast_type + "." + acc.field);
b_.CreateMemCpy(dst, src, field.type.size, 1);
b_.CREATE_MEMCPY(dst, src, field.type.size, 1);
expr_ = dst;
// TODO clean up dst memory?
}
......@@ -924,7 +935,7 @@ void CodegenLLVM::visit(AssignVarStatement &assignment)
}
else
{
b_.CreateMemCpy(variables_[var.ident], expr_, var.type.size, 1);
b_.CREATE_MEMCPY(variables_[var.ident], expr_, var.type.size, 1);
}
}
......@@ -1151,7 +1162,7 @@ AllocaInst *CodegenLLVM::getMapKey(Map &map)
expr->accept(*this);
Value *offset_val = b_.CreateGEP(key, {b_.getInt64(0), b_.getInt64(offset)});
if (expr->type.type == Type::string || expr->type.type == Type::usym)
b_.CreateMemCpy(offset_val, expr_, expr->type.size, 1);
b_.CREATE_MEMCPY(offset_val, expr_, expr->type.size, 1);
else
b_.CreateStore(expr_, offset_val);
offset += expr->type.size;
......@@ -1181,7 +1192,7 @@ AllocaInst *CodegenLLVM::getHistMapKey(Map &map, Value *log2)
expr->accept(*this);
Value *offset_val = b_.CreateGEP(key, {b_.getInt64(0), b_.getInt64(offset)});
if (expr->type.type == Type::string || expr->type.type == Type::usym)
b_.CreateMemCpy(offset_val, expr_, expr->type.size, 1);
b_.CREATE_MEMCPY(offset_val, expr_, expr->type.size, 1);
else
b_.CreateStore(expr_, offset_val);
offset += expr->type.size;
......
......@@ -193,7 +193,7 @@ Value *IRBuilderBPF::CreateMapLookupElem(Map &map, AllocaInst *key)
SetInsertPoint(lookup_success_block);
if (map.type.type == Type::string || map.type.type == Type::cast)
CreateMemCpy(value, call, map.type.size, 1);
CREATE_MEMCPY(value, call, map.type.size, 1);
else
CreateStore(CreateLoad(getInt64Ty(), call), value);
CreateBr(lookup_merge_block);
......
......@@ -6,6 +6,15 @@
#include "types.h"
#include <llvm/IR/IRBuilder.h>
#include <llvm/Config/llvm-config.h>
#if LLVM_VERSION_MAJOR >= 5 && LLVM_VERSION_MAJOR < 7
#define CREATE_MEMCPY(dst, src, size, algn) CreateMemCpy((dst), (src), (size), (algn))
#elif LLVM_VERSION_MAJOR >= 7
#define CREATE_MEMCPY(dst, src, size, algn) CreateMemCpy((dst), (algn), (src), (algn), (size))
#else
#error Unsupported LLVM version
#endif
namespace bpftrace {
namespace ast {
......
......@@ -8,6 +8,7 @@
#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Config/llvm-config.h"
namespace bpftrace {
......@@ -37,6 +38,7 @@ private:
std::map<std::string, std::tuple<uint8_t *, uintptr_t>> &sections_;
};
#if LLVM_VERSION_MAJOR >= 5 && LLVM_VERSION_MAJOR < 7
class BpfOrc
{
private:
......@@ -71,5 +73,40 @@ public:
return cantFail(CompileLayer.addModule(std::move(M), std::move(Resolver)));
}
};
#elif LLVM_VERSION_MAJOR >= 7
class BpfOrc
{
private:
ExecutionSession ES;
std::unique_ptr<TargetMachine> TM;
std::shared_ptr<SymbolResolver> Resolver;
RTDyldObjectLinkingLayer ObjectLayer;
IRCompileLayer<decltype(ObjectLayer), SimpleCompiler> CompileLayer;
public:
std::map<std::string, std::tuple<uint8_t *, uintptr_t>> sections_;
BpfOrc(TargetMachine *TM_)
: TM(TM_),
Resolver(createLegacyLookupResolver(ES,
[](const std::string &Name) -> JITSymbol { return nullptr; },
[](Error Err) { cantFail(std::move(Err), "lookup failed"); })),
ObjectLayer(ES, [this](VModuleKey) { return RTDyldObjectLinkingLayer::Resources{std::make_shared<MemoryManager>(sections_), Resolver}; }),
CompileLayer(ObjectLayer, SimpleCompiler(*TM)) {}
void compileModule(std::unique_ptr<Module> M) {
auto K = addModule(move(M));
CompileLayer.emitAndFinalize(K);
}
VModuleKey addModule(std::unique_ptr<Module> M) {
auto K = ES.allocateVModule();
cantFail(CompileLayer.addModule(K, std::move(M)));
return K;
}
};
#else
#error Unsupported LLVM version
#endif
} // namespace bpftrace
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment