Skip to content

Commit

Permalink
perf clang: Allow passing CFLAGS to builtin clang
Browse files Browse the repository at this point in the history
Improve getModuleFromSource() API to accept a cflags list. This feature
will be used to pass LINUX_VERSION_CODE and -I flags.

Signed-off-by: Wang Nan <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Joe Stringer <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
  • Loading branch information
WangNan0 authored and acmel committed Dec 5, 2016
1 parent 77dfa84 commit a9495fe
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
5 changes: 3 additions & 2 deletions tools/perf/util/c++/clang-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ 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;}");
perf::getModuleFromSource({"-DRESULT=1"},
"perf-test.c",
"int myfunc(void) {return RESULT;}");

if (!M)
return -1;
Expand Down
21 changes: 13 additions & 8 deletions tools/perf/util/c++/clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ static std::unique_ptr<llvm::LLVMContext> LLVMCtx;
using namespace clang;

static CompilerInvocation *
createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags)
createCompilerInvocation(llvm::opt::ArgStringList CFlags, StringRef& Path,
DiagnosticsEngine& Diags)
{
llvm::opt::ArgStringList CCArgs {
"-cc1",
Expand All @@ -45,6 +46,8 @@ createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags)
"-Wno-unused-value",
"-Wno-pointer-sign",
"-x", "c"};

CCArgs.append(CFlags.begin(), CFlags.end());
CompilerInvocation *CI = tooling::newInvocation(&Diags, CCArgs);

FrontendOptions& Opts = CI->getFrontendOpts();
Expand All @@ -54,16 +57,17 @@ createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags)
}

static std::unique_ptr<llvm::Module>
getModuleFromSource(StringRef Path,
IntrusiveRefCntPtr<vfs::FileSystem> VFS)
getModuleFromSource(llvm::opt::ArgStringList CFlags,
StringRef Path, IntrusiveRefCntPtr<vfs::FileSystem> VFS)
{
CompilerInstance Clang;
Clang.createDiagnostics();

Clang.setVirtualFileSystem(&*VFS);

IntrusiveRefCntPtr<CompilerInvocation> CI =
createCompilerInvocation(Path, Clang.getDiagnostics());
createCompilerInvocation(std::move(CFlags), Path,
Clang.getDiagnostics());
Clang.setInvocation(&*CI);

std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction(&*LLVMCtx));
Expand All @@ -74,7 +78,8 @@ getModuleFromSource(StringRef Path,
}

std::unique_ptr<llvm::Module>
getModuleFromSource(StringRef Name, StringRef Content)
getModuleFromSource(llvm::opt::ArgStringList CFlags,
StringRef Name, StringRef Content)
{
using namespace vfs;

Expand All @@ -90,14 +95,14 @@ getModuleFromSource(StringRef Name, StringRef Content)
OverlayFS->pushOverlay(MemFS);
MemFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content));

return getModuleFromSource(Name, OverlayFS);
return getModuleFromSource(std::move(CFlags), Name, OverlayFS);
}

std::unique_ptr<llvm::Module>
getModuleFromSource(StringRef Path)
getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path)
{
IntrusiveRefCntPtr<vfs::FileSystem> VFS(vfs::getRealFileSystem());
return getModuleFromSource(Path, VFS);
return getModuleFromSource(std::move(CFlags), Path, VFS);
}

}
Expand Down
8 changes: 6 additions & 2 deletions tools/perf/util/c++/clang.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Option/Option.h"
#include <memory>

namespace perf {

using namespace llvm;

std::unique_ptr<Module>
getModuleFromSource(StringRef Name, StringRef Content);
getModuleFromSource(opt::ArgStringList CFlags,
StringRef Name, StringRef Content);

std::unique_ptr<Module>
getModuleFromSource(StringRef Path);
getModuleFromSource(opt::ArgStringList CFlags,
StringRef Path);

}
#endif

0 comments on commit a9495fe

Please sign in to comment.