diff options
author | Wang Nan <wangnan0@huawei.com> | 2016-11-26 02:03:34 -0500 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2016-12-05 13:51:43 -0500 |
commit | 00b86691c77c6576861b82a3cfe4d609800758fe (patch) | |
tree | 50161d349254c75fb8707c370c96cd8a205f3e9c /tools/perf/util/c++/clang.cpp | |
parent | d58ac0bf8d1e6ffbfcb0a77e459cf4737b131b75 (diff) |
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>
Diffstat (limited to 'tools/perf/util/c++/clang.cpp')
-rw-r--r-- | tools/perf/util/c++/clang.cpp | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp new file mode 100644 index 000000000000..c17b1176e25d --- /dev/null +++ b/tools/perf/util/c++/clang.cpp | |||
@@ -0,0 +1,96 @@ | |||
1 | /* | ||
2 | * llvm C frontend for perf. Support dynamically compile C file | ||
3 | * | ||
4 | * Inspired by clang example code: | ||
5 | * http://llvm.org/svn/llvm-project/cfe/trunk/examples/clang-interpreter/main.cpp | ||
6 | * | ||
7 | * Copyright (C) 2016 Wang Nan <wangnan0@huawei.com> | ||
8 | * Copyright (C) 2016 Huawei Inc. | ||
9 | */ | ||
10 | |||
11 | #include "clang/CodeGen/CodeGenAction.h" | ||
12 | #include "clang/Frontend/CompilerInvocation.h" | ||
13 | #include "clang/Frontend/CompilerInstance.h" | ||
14 | #include "clang/Frontend/TextDiagnosticPrinter.h" | ||
15 | #include "clang/Tooling/Tooling.h" | ||
16 | #include "llvm/IR/Module.h" | ||
17 | #include "llvm/Option/Option.h" | ||
18 | #include "llvm/Support/ManagedStatic.h" | ||
19 | #include <memory> | ||
20 | |||
21 | #include "clang.h" | ||
22 | #include "clang-c.h" | ||
23 | |||
24 | namespace perf { | ||
25 | |||
26 | static std::unique_ptr<llvm::LLVMContext> LLVMCtx; | ||
27 | |||
28 | using namespace clang; | ||
29 | |||
30 | static vfs::InMemoryFileSystem * | ||
31 | buildVFS(StringRef& Name, StringRef& Content) | ||
32 | { | ||
33 | vfs::InMemoryFileSystem *VFS = new vfs::InMemoryFileSystem(true); | ||
34 | VFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content)); | ||
35 | return VFS; | ||
36 | } | ||
37 | |||
38 | static CompilerInvocation * | ||
39 | createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags) | ||
40 | { | ||
41 | llvm::opt::ArgStringList CCArgs { | ||
42 | "-cc1", | ||
43 | "-triple", "bpf-pc-linux", | ||
44 | "-fsyntax-only", | ||
45 | "-ferror-limit", "19", | ||
46 | "-fmessage-length", "127", | ||
47 | "-O2", | ||
48 | "-nostdsysteminc", | ||
49 | "-nobuiltininc", | ||
50 | "-vectorize-loops", | ||
51 | "-vectorize-slp", | ||
52 | "-Wno-unused-value", | ||
53 | "-Wno-pointer-sign", | ||
54 | "-x", "c"}; | ||
55 | CompilerInvocation *CI = tooling::newInvocation(&Diags, CCArgs); | ||
56 | |||
57 | FrontendOptions& Opts = CI->getFrontendOpts(); | ||
58 | Opts.Inputs.clear(); | ||
59 | Opts.Inputs.emplace_back(Path, IK_C); | ||
60 | return CI; | ||
61 | } | ||
62 | |||
63 | std::unique_ptr<llvm::Module> | ||
64 | getModuleFromSource(StringRef Name, StringRef Content) | ||
65 | { | ||
66 | CompilerInstance Clang; | ||
67 | Clang.createDiagnostics(); | ||
68 | |||
69 | IntrusiveRefCntPtr<vfs::FileSystem> VFS = buildVFS(Name, Content); | ||
70 | Clang.setVirtualFileSystem(&*VFS); | ||
71 | |||
72 | IntrusiveRefCntPtr<CompilerInvocation> CI = | ||
73 | createCompilerInvocation(Name, Clang.getDiagnostics()); | ||
74 | Clang.setInvocation(&*CI); | ||
75 | |||
76 | std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction(&*LLVMCtx)); | ||
77 | if (!Clang.ExecuteAction(*Act)) | ||
78 | return std::unique_ptr<llvm::Module>(nullptr); | ||
79 | |||
80 | return Act->takeModule(); | ||
81 | } | ||
82 | |||
83 | } | ||
84 | |||
85 | extern "C" { | ||
86 | void perf_clang__init(void) | ||
87 | { | ||
88 | perf::LLVMCtx.reset(new llvm::LLVMContext()); | ||
89 | } | ||
90 | |||
91 | void perf_clang__cleanup(void) | ||
92 | { | ||
93 | perf::LLVMCtx.reset(nullptr); | ||
94 | llvm::llvm_shutdown(); | ||
95 | } | ||
96 | } | ||