Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S966039AbcKOEI2 (ORCPT ); Mon, 14 Nov 2016 23:08:28 -0500 Received: from szxga03-in.huawei.com ([119.145.14.66]:25399 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965920AbcKOEIS (ORCPT ); Mon, 14 Nov 2016 23:08:18 -0500 From: Wang Nan To: , CC: , , , , Wang Nan , Jiri Olsa Subject: [PATCH 23/34] perf clang jit: Insignt BPF and JIT functions in a Module Date: Tue, 15 Nov 2016 04:06:06 +0000 Message-ID: <20161115040617.69788-24-wangnan0@huawei.com> X-Mailer: git-send-email 2.10.1 In-Reply-To: <20161115040617.69788-1-wangnan0@huawei.com> References: <20161115040617.69788-1-wangnan0@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.193.248] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2962 Lines: 99 Identify BPF functions, JIT functions and maps during init. Functions in section starting with "perfhook:" are JIT functions. They will be JIT compiled and hooked at perfhooks. During init of PerfModule, mark JIT functions as AvailableExternallyLinkage. LLVM skips functions with linkage like this so they won't be compiled into BPF objects. Signed-off-by: Wang Nan Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: He Kuang Cc: Jiri Olsa Cc: Zefan Li Cc: pi3orama@163.com --- tools/perf/util/c++/clang.cpp | 32 ++++++++++++++++++++++++++++++++ tools/perf/util/c++/clang.h | 7 +++++++ 2 files changed, 39 insertions(+) diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp index d31b0a5..98d05e2 100644 --- a/tools/perf/util/c++/clang.cpp +++ b/tools/perf/util/c++/clang.cpp @@ -115,15 +115,47 @@ getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path) PerfModule::PerfModule(std::unique_ptr&& M) : Module(std::move(M)) { + for (llvm::Function& F : *Module) { + if (F.getLinkage() != llvm::GlobalValue::ExternalLinkage) + continue; + + if (StringRef(F.getSection()).startswith("perfhook:")) + JITFunctions.insert(&F); + else + BPFFunctions.insert(&F); + } + for (auto V = Module->global_begin(); V != Module->global_end(); V++) { + llvm::GlobalVariable *GV = &*V; + if (StringRef(GV->getSection()) == llvm::StringRef("maps")) + Maps.insert(GV); + } } +void PerfModule::prepareBPF(void) +{ + for (llvm::Function *F : JITFunctions) + F->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage); + for (llvm::Function *F : BPFFunctions) + F->setLinkage(llvm::GlobalValue::ExternalLinkage); + +} + +void PerfModule::prepareJIT(void) +{ + for (llvm::Function *F : BPFFunctions) + F->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage); + for (llvm::Function *F : JITFunctions) + F->setLinkage(llvm::GlobalValue::ExternalLinkage); + +} std::unique_ptr> PerfModule::toBPFObject(void) { using namespace llvm; + prepareBPF(); std::string TargetTriple("bpf-pc-linux"); std::string Error; const Target* Target = TargetRegistry::lookupTarget(TargetTriple, Error); diff --git a/tools/perf/util/c++/clang.h b/tools/perf/util/c++/clang.h index cbb291b..1eb71a6 100644 --- a/tools/perf/util/c++/clang.h +++ b/tools/perf/util/c++/clang.h @@ -6,6 +6,7 @@ #include "llvm/IR/Module.h" #include "llvm/Option/Option.h" #include +#include namespace perf { @@ -14,6 +15,12 @@ using namespace llvm; class PerfModule { private: std::unique_ptr Module; + + std::set Maps; + std::set BPFFunctions; + std::set JITFunctions; + void prepareBPF(void); + void prepareJIT(void); public: inline llvm::Module *getModule(void) { -- 2.10.1