From patchwork Tue Dec 6 07:13:45 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 86694 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp1880936qgi; Mon, 5 Dec 2016 23:17:25 -0800 (PST) X-Received: by 10.84.173.4 with SMTP id o4mr135230920plb.123.1481008645922; Mon, 05 Dec 2016 23:17:25 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z5si18331299plh.229.2016.12.05.23.17.25; Mon, 05 Dec 2016 23:17:25 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753103AbcLFHRW (ORCPT + 25 others); Tue, 6 Dec 2016 02:17:22 -0500 Received: from szxga02-in.huawei.com ([119.145.14.65]:46180 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752306AbcLFHOp (ORCPT ); Tue, 6 Dec 2016 02:14:45 -0500 Received: from 172.24.1.137 (EHLO szxeml431-hub.china.huawei.com) ([172.24.1.137]) by szxrg02-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id DRT24859; Tue, 06 Dec 2016 15:14:19 +0800 (CST) Received: from linux-4hy3.site (10.107.193.248) by szxeml431-hub.china.huawei.com (10.82.67.208) with Microsoft SMTP Server id 14.3.235.1; Tue, 6 Dec 2016 15:14:09 +0800 From: Wang Nan To: CC: , , Wang Nan , Arnaldo Carvalho de Melo , He Kuang , Jiri Olsa , Zefan Li , Subject: [PATCH v4 07/18] perf clang jit: Insignt BPF and JIT functions in a Module Date: Tue, 6 Dec 2016 07:13:45 +0000 Message-ID: <20161206071356.5312-8-wangnan0@huawei.com> X-Mailer: git-send-email 2.10.1 In-Reply-To: <20161206071356.5312-1-wangnan0@huawei.com> References: <20161206071356.5312-1-wangnan0@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.107.193.248] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Acked-by: Alexei Starovoitov Cc: Arnaldo Carvalho de Melo Cc: He Kuang Cc: Jiri Olsa Cc: Zefan Li Cc: pi3orama@163.com --- tools/perf/util/c++/clang.cpp | 38 ++++++++++++++++++++++++++++++++++++++ tools/perf/util/c++/clang.h | 7 +++++++ 2 files changed, 45 insertions(+) -- 2.10.1 diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp index 042db68..8a0f818 100644 --- a/tools/perf/util/c++/clang.cpp +++ b/tools/perf/util/c++/clang.cpp @@ -112,15 +112,53 @@ 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) +{ + /* + * setLinkage(AvailableExternallyLinkage) causes LLVM + * blindly skip the function. They still exist in Module + * so we can bring them back by resetting linkage to + * ExternalLinkage. + */ + 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) {