linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: tip-bot for Wang Nan <tipbot@zytor.com>
To: linux-tip-commits@vger.kernel.org
Cc: hekuang@huawei.com, jolsa@kernel.org, tglx@linutronix.de,
	joe@ovn.org, hpa@zytor.com, ast@fb.com,
	linux-kernel@vger.kernel.org, acme@redhat.com,
	wangnan0@huawei.com, mingo@kernel.org, lizefan@huawei.com
Subject: [tip:perf/core] perf clang: Add builtin clang support ant test case
Date: Tue, 6 Dec 2016 00:23:59 -0800	[thread overview]
Message-ID: <tip-00b86691c77c6576861b82a3cfe4d609800758fe@git.kernel.org> (raw)
In-Reply-To: <20161126070354.141764-11-wangnan0@huawei.com>

Commit-ID:  00b86691c77c6576861b82a3cfe4d609800758fe
Gitweb:     http://git.kernel.org/tip/00b86691c77c6576861b82a3cfe4d609800758fe
Author:     Wang Nan <wangnan0@huawei.com>
AuthorDate: Sat, 26 Nov 2016 07:03:34 +0000
Committer:  Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Mon, 5 Dec 2016 15:51:43 -0300

perf clang: Add builtin clang support ant test case

Add basic clang support in clang.cpp and test__clang() testcase. The
first testcase checks if builtin clang is able to generate LLVM IR.

tests/clang.c is a proxy. Real testcase resides in
utils/c++/clang-test.cpp in c++ and exports C interface to perf test
subsystem.

Test result:

   $ perf test -v clang
   51: builtin clang support                               :
   51.1: Test builtin clang compile C source to IR              :
   --- start ---
   test child forked, pid 13215
   test child finished with 0
   ---- end ----
   Test builtin clang support subtest 0: Ok

Committer note:

Make sure you've enabled CLANG and LLVM builtin support by setting
the LIBCLANGLLVM variable on the make command line, e.g.:

  make LIBCLANGLLVM=1 O=/tmp/build/perf -C tools/perf install-bin

Otherwise you'll get this when trying to do the 'perf test' call above:

  # perf test clang
  51: builtin clang support                      : Skip (not compiled in)
  #

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexei Starovoitov <ast@fb.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Joe Stringer <joe@ovn.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/20161126070354.141764-11-wangnan0@huawei.com
[ Removed "Test" from descriptions, redundant and already removed from all the other entries ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/tests/Build             |  1 +
 tools/perf/tests/builtin-test.c    |  9 ++++
 tools/perf/tests/clang.c           | 42 +++++++++++++++++
 tools/perf/tests/tests.h           |  3 ++
 tools/perf/util/Build              |  2 +
 tools/perf/util/c++/Build          |  2 +
 tools/perf/util/c++/clang-c.h      | 16 +++++++
 tools/perf/util/c++/clang-test.cpp | 31 ++++++++++++
 tools/perf/util/c++/clang.cpp      | 96 ++++++++++++++++++++++++++++++++++++++
 tools/perf/util/c++/clang.h        | 16 +++++++
 10 files changed, 218 insertions(+)

diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index af3ec94..6676c2d 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -43,6 +43,7 @@ perf-y += sdt.o
 perf-y += is_printable_array.o
 perf-y += bitmap.o
 perf-y += perf-hooks.o
+perf-y += clang.o
 
 $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
 	$(call rule_mkdir)
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index d1bec04..2360520 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -234,6 +234,15 @@ static struct test generic_tests[] = {
 		.func = test__perf_hooks,
 	},
 	{
+		.desc = "builtin clang support",
+		.func = test__clang,
+		.subtest = {
+			.skip_if_fail	= true,
+			.get_nr		= test__clang_subtest_get_nr,
+			.get_desc	= test__clang_subtest_get_desc,
+		}
+	},
+	{
 		.func = NULL,
 	},
 };
diff --git a/tools/perf/tests/clang.c b/tools/perf/tests/clang.c
new file mode 100644
index 0000000..636d6d0
--- /dev/null
+++ b/tools/perf/tests/clang.c
@@ -0,0 +1,42 @@
+#include "tests.h"
+#include "debug.h"
+#include "util.h"
+#include "c++/clang-c.h"
+
+static struct {
+	int (*func)(void);
+	const char *desc;
+} clang_testcase_table[] = {
+#ifdef HAVE_LIBCLANGLLVM_SUPPORT
+	{
+		.func = test__clang_to_IR,
+		.desc = "builtin clang compile C source to IR",
+	},
+#endif
+};
+
+int test__clang_subtest_get_nr(void)
+{
+	return (int)ARRAY_SIZE(clang_testcase_table);
+}
+
+const char *test__clang_subtest_get_desc(int i)
+{
+	if (i < 0 || i >= (int)ARRAY_SIZE(clang_testcase_table))
+		return NULL;
+	return clang_testcase_table[i].desc;
+}
+
+#ifndef HAVE_LIBCLANGLLVM_SUPPORT
+int test__clang(int i __maybe_unused)
+{
+	return TEST_SKIP;
+}
+#else
+int test__clang(int i __maybe_unused)
+{
+	if (i < 0 || i >= (int)ARRAY_SIZE(clang_testcase_table))
+		return TEST_FAIL;
+	return clang_testcase_table[i].func();
+}
+#endif
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index 3a1f98f..0d7b251 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -92,6 +92,9 @@ int test__sdt_event(int subtest);
 int test__is_printable_array(int subtest);
 int test__bitmap_print(int subtest);
 int test__perf_hooks(int subtest);
+int test__clang(int subtest);
+const char *test__clang_subtest_get_desc(int subtest);
+int test__clang_subtest_get_nr(void);
 
 #if defined(__arm__) || defined(__aarch64__)
 #ifdef HAVE_DWARF_UNWIND_SUPPORT
diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index bdad82a..3840e3a 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -126,6 +126,8 @@ endif
 
 libperf-y += perf-hooks.o
 
+libperf-$(CONFIG_CXX) += c++/
+
 CFLAGS_config.o   += -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
 # avoid compiler warnings in 32-bit mode
 CFLAGS_genelf_debug.o  += -Wno-packed
diff --git a/tools/perf/util/c++/Build b/tools/perf/util/c++/Build
new file mode 100644
index 0000000..988fef1
--- /dev/null
+++ b/tools/perf/util/c++/Build
@@ -0,0 +1,2 @@
+libperf-$(CONFIG_CLANGLLVM) += clang.o
+libperf-$(CONFIG_CLANGLLVM) += clang-test.o
diff --git a/tools/perf/util/c++/clang-c.h b/tools/perf/util/c++/clang-c.h
new file mode 100644
index 0000000..dcde4b5
--- /dev/null
+++ b/tools/perf/util/c++/clang-c.h
@@ -0,0 +1,16 @@
+#ifndef PERF_UTIL_CLANG_C_H
+#define PERF_UTIL_CLANG_C_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern void perf_clang__init(void);
+extern void perf_clang__cleanup(void);
+
+extern int test__clang_to_IR(void);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/tools/perf/util/c++/clang-test.cpp b/tools/perf/util/c++/clang-test.cpp
new file mode 100644
index 0000000..3da6bfa
--- /dev/null
+++ b/tools/perf/util/c++/clang-test.cpp
@@ -0,0 +1,31 @@
+#include "clang.h"
+#include "clang-c.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/LLVMContext.h"
+
+class perf_clang_scope {
+public:
+	explicit perf_clang_scope() {perf_clang__init();}
+	~perf_clang_scope() {perf_clang__cleanup();}
+};
+
+extern "C" {
+
+int test__clang_to_IR(void)
+{
+	perf_clang_scope _scope;
+
+	std::unique_ptr<llvm::Module> M =
+		perf::getModuleFromSource("perf-test.c",
+					  "int myfunc(void) {return 1;}");
+
+	if (!M)
+		return -1;
+
+	for (llvm::Function& F : *M)
+		if (F.getName() == "myfunc")
+			return 0;
+	return -1;
+}
+
+}
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
new file mode 100644
index 0000000..c17b117
--- /dev/null
+++ b/tools/perf/util/c++/clang.cpp
@@ -0,0 +1,96 @@
+/*
+ * llvm C frontend for perf. Support dynamically compile C file
+ *
+ * Inspired by clang example code:
+ * http://llvm.org/svn/llvm-project/cfe/trunk/examples/clang-interpreter/main.cpp
+ *
+ * Copyright (C) 2016 Wang Nan <wangnan0@huawei.com>
+ * Copyright (C) 2016 Huawei Inc.
+ */
+
+#include "clang/CodeGen/CodeGenAction.h"
+#include "clang/Frontend/CompilerInvocation.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Frontend/TextDiagnosticPrinter.h"
+#include "clang/Tooling/Tooling.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Option/Option.h"
+#include "llvm/Support/ManagedStatic.h"
+#include <memory>
+
+#include "clang.h"
+#include "clang-c.h"
+
+namespace perf {
+
+static std::unique_ptr<llvm::LLVMContext> LLVMCtx;
+
+using namespace clang;
+
+static vfs::InMemoryFileSystem *
+buildVFS(StringRef& Name, StringRef& Content)
+{
+	vfs::InMemoryFileSystem *VFS = new vfs::InMemoryFileSystem(true);
+	VFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content));
+	return VFS;
+}
+
+static CompilerInvocation *
+createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags)
+{
+	llvm::opt::ArgStringList CCArgs {
+		"-cc1",
+		"-triple", "bpf-pc-linux",
+		"-fsyntax-only",
+		"-ferror-limit", "19",
+		"-fmessage-length", "127",
+		"-O2",
+		"-nostdsysteminc",
+		"-nobuiltininc",
+		"-vectorize-loops",
+		"-vectorize-slp",
+		"-Wno-unused-value",
+		"-Wno-pointer-sign",
+		"-x", "c"};
+	CompilerInvocation *CI = tooling::newInvocation(&Diags, CCArgs);
+
+	FrontendOptions& Opts = CI->getFrontendOpts();
+	Opts.Inputs.clear();
+	Opts.Inputs.emplace_back(Path, IK_C);
+	return CI;
+}
+
+std::unique_ptr<llvm::Module>
+getModuleFromSource(StringRef Name, StringRef Content)
+{
+	CompilerInstance Clang;
+	Clang.createDiagnostics();
+
+	IntrusiveRefCntPtr<vfs::FileSystem> VFS = buildVFS(Name, Content);
+	Clang.setVirtualFileSystem(&*VFS);
+
+	IntrusiveRefCntPtr<CompilerInvocation> CI =
+		createCompilerInvocation(Name, Clang.getDiagnostics());
+	Clang.setInvocation(&*CI);
+
+	std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction(&*LLVMCtx));
+	if (!Clang.ExecuteAction(*Act))
+		return std::unique_ptr<llvm::Module>(nullptr);
+
+	return Act->takeModule();
+}
+
+}
+
+extern "C" {
+void perf_clang__init(void)
+{
+	perf::LLVMCtx.reset(new llvm::LLVMContext());
+}
+
+void perf_clang__cleanup(void)
+{
+	perf::LLVMCtx.reset(nullptr);
+	llvm::llvm_shutdown();
+}
+}
diff --git a/tools/perf/util/c++/clang.h b/tools/perf/util/c++/clang.h
new file mode 100644
index 0000000..f64483b
--- /dev/null
+++ b/tools/perf/util/c++/clang.h
@@ -0,0 +1,16 @@
+#ifndef PERF_UTIL_CLANG_H
+#define PERF_UTIL_CLANG_H
+
+#include "llvm/ADT/StringRef.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include <memory>
+namespace perf {
+
+using namespace llvm;
+
+std::unique_ptr<Module>
+getModuleFromSource(StringRef Name, StringRef Content);
+
+}
+#endif

  parent reply	other threads:[~2016-12-06  8:24 UTC|newest]

Thread overview: 73+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-26  7:03 [PATCH v3 00/30] perf clang: Builtin clang and perfhook support Wang Nan
2016-11-26  7:03 ` [PATCH v3 01/30] tools lib bpf: Add missing BPF functions Wang Nan
2016-11-26 17:10   ` Alexei Starovoitov
2016-12-02 10:37   ` [tip:perf/core] " tip-bot for Wang Nan
2016-11-26  7:03 ` [PATCH v3 02/30] tools lib bpf: Add private field for bpf_object Wang Nan
2016-11-26 17:11   ` Alexei Starovoitov
2016-12-02 10:37   ` [tip:perf/core] " tip-bot for Wang Nan
2016-11-26  7:03 ` [PATCH v3 03/30] tools lib bpf: Retrive bpf_map through offset of bpf_map_def Wang Nan
2016-11-26 17:12   ` Alexei Starovoitov
2016-12-02 10:38   ` [tip:perf/core] " tip-bot for Wang Nan
2016-11-26  7:03 ` [PATCH v3 04/30] perf tools: Introduce perf hooks Wang Nan
2016-12-02 10:38   ` [tip:perf/core] " tip-bot for Wang Nan
2016-11-26  7:03 ` [PATCH v3 05/30] perf tools: Pass context to perf hook functions Wang Nan
2016-12-06  8:21   ` [tip:perf/core] " tip-bot for Wang Nan
2016-11-26  7:03 ` [PATCH v3 06/30] perf llvm: Extract helpers in llvm-utils.c Wang Nan
2016-12-06  8:21   ` [tip:perf/core] " tip-bot for Wang Nan
2016-11-26  7:03 ` [PATCH v3 07/30] tools build: Add feature detection for LLVM Wang Nan
2016-12-06  8:22   ` [tip:perf/core] " tip-bot for Wang Nan
2016-11-26  7:03 ` [PATCH v3 08/30] tools build: Add feature detection for clang Wang Nan
2016-12-06  8:22   ` [tip:perf/core] " tip-bot for Wang Nan
2016-11-26  7:03 ` [PATCH v3 09/30] perf build: Add clang and llvm compile and linking support Wang Nan
2016-12-06  8:23   ` [tip:perf/core] " tip-bot for Wang Nan
2016-11-26  7:03 ` [PATCH v3 10/30] perf clang: Add builtin clang support ant test case Wang Nan
2016-11-26 17:17   ` Alexei Starovoitov
2016-11-28  7:41     ` Wangnan (F)
2016-12-02 15:44   ` Arnaldo Carvalho de Melo
2016-12-05  2:36     ` Wangnan (F)
2016-12-05  4:41       ` Wangnan (F)
2017-01-17 13:38         ` Arnaldo Carvalho de Melo
2016-12-05 16:51     ` Alexei Starovoitov
2016-12-05 21:02       ` Arnaldo Carvalho de Melo
2016-12-05 21:48         ` Arnaldo Carvalho de Melo
2016-12-06  2:07           ` Wangnan (F)
2016-12-06 13:43             ` Arnaldo Carvalho de Melo
2016-12-06 19:19       ` Arnaldo Carvalho de Melo
2016-12-06 21:02         ` Arnaldo Carvalho de Melo
2016-12-06  8:23   ` tip-bot for Wang Nan [this message]
2016-11-26  7:03 ` [PATCH v3 11/30] perf clang: Use real file system for #include Wang Nan
2016-12-06  8:24   ` [tip:perf/core] " tip-bot for Wang Nan
2016-11-26  7:03 ` [PATCH v3 12/30] perf clang: Allow passing CFLAGS to builtin clang Wang Nan
2016-12-06  8:25   ` [tip:perf/core] " tip-bot for Wang Nan
2016-11-26  7:03 ` [PATCH v3 13/30] perf clang: Update test case to use real BPF script Wang Nan
2016-12-06  8:25   ` [tip:perf/core] " tip-bot for Wang Nan
2016-11-26  7:03 ` [PATCH v3 14/30] perf clang: Support compile IR to BPF object and add testcase Wang Nan
2016-11-26 17:25   ` Alexei Starovoitov
2016-11-28  6:32     ` Wangnan (F)
2016-11-28 10:31       ` Wangnan (F)
2016-11-28 19:33         ` Alexei Starovoitov
2016-12-06  8:26   ` [tip:perf/core] " tip-bot for Wang Nan
2016-11-26  7:03 ` [PATCH v3 15/30] perf clang: Compile BPF script use builtin clang support Wang Nan
2016-12-06  8:26   ` [tip:perf/core] perf clang: Compile BPF script using " tip-bot for Wang Nan
2016-11-26  7:03 ` [PATCH v3 16/30] perf clang: Pass full path to builtin clang Wang Nan
2016-11-26  7:03 ` [PATCH v3 17/30] perf clang: Pass CFLAGS " Wang Nan
2016-11-26  7:03 ` [PATCH v3 18/30] perf clang jit: Wrap llvm::Module using PerfModule Wang Nan
2016-11-26  7:03 ` [PATCH v3 19/30] perf clang jit: Insignt BPF and JIT functions in a Module Wang Nan
2016-11-26 17:32   ` Alexei Starovoitov
2016-11-26  7:03 ` [PATCH v3 20/30] perf clang jit: add PerfModule::doJIT to JIT perfhook functions Wang Nan
2016-11-26 17:29   ` Alexei Starovoitov
2016-11-28  6:42     ` Wangnan (F)
2016-11-26  7:03 ` [PATCH v3 21/30] perf clang jit: Export functions for jitted code Wang Nan
2016-11-26  7:03 ` [PATCH v3 22/30] perf clang jit: Actually JIT and hook in bpf loader Wang Nan
2016-11-26  7:03 ` [PATCH v3 23/30] perf clang jit: Collect the lowest address in maps section as map_base Wang Nan
2016-11-26  7:03 ` [PATCH v3 24/30] perf clang jit: Retrive fd of BPF map from its offset Wang Nan
2016-11-26  7:03 ` [PATCH v3 25/30] perf clang jit: Allow jitted perf hook access BPF maps Wang Nan
2016-11-26 17:09   ` Alexei Starovoitov
2016-11-26  7:03 ` [PATCH v3 26/30] perf clang: Link BPF functions declaration into perf Wang Nan
2016-11-26 17:40   ` Alexei Starovoitov
2016-11-30 16:12     ` Arnaldo Carvalho de Melo
2016-12-01  1:56       ` [PATCH v3 26/30 - cleanup] " Wang Nan
2016-11-26  7:03 ` [PATCH v3 27/30] perf clang: Declare BPF functions for BPF scripts automatically Wang Nan
2016-11-26  7:03 ` [PATCH v3 28/30] perf clang: Include helpers to BPF scripts Wang Nan
2016-11-26  7:03 ` [PATCH v3 29/30] perf clang builtin: Define hook helpers by default Wang Nan
2016-11-26  7:03 ` [PATCH v3 30/30] perf clang jit: Export getpid() to perf hook Wang Nan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tip-00b86691c77c6576861b82a3cfe4d609800758fe@git.kernel.org \
    --to=tipbot@zytor.com \
    --cc=acme@redhat.com \
    --cc=ast@fb.com \
    --cc=hekuang@huawei.com \
    --cc=hpa@zytor.com \
    --cc=joe@ovn.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=lizefan@huawei.com \
    --cc=mingo@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=wangnan0@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).