OmniSciDB  b24e664e58
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UdfCompiler Class Reference

#include <UDFCompiler.h>

+ Collaboration diagram for UdfCompiler:

Public Member Functions

 UdfCompiler (const std::string &)
 
int compileUdf ()
 
const std::string & getAstFileName () const
 

Private Member Functions

std::string removeFileExtension (const std::string &path)
 
std::string getFileExt (std::string &s)
 
int parseToAst (const char *file_name)
 
std::string genGpuIrFilename (const char *udf_file_name)
 
std::string genCpuIrFilename (const char *udf_file_name)
 
int compileToGpuByteCode (const char *udf_file_name, bool cpu_mode)
 
int compileToCpuByteCode (const char *udf_file_name)
 
void replaceExtn (std::string &s, const std::string &new_ext)
 
int compileFromCommandLine (std::vector< const char * > &command_line)
 
void readCompiledModules ()
 
void readGpuCompiledModule ()
 
void readCpuCompiledModule ()
 
int compileForGpu ()
 

Private Attributes

std::string udf_file_name_
 
std::string udf_ast_file_name_
 

Detailed Description

Definition at line 51 of file UDFCompiler.h.

Constructor & Destructor Documentation

UdfCompiler::UdfCompiler ( const std::string &  file_name)

Definition at line 292 of file UDFCompiler.cpp.

References replaceExtn(), and udf_ast_file_name_.

293  : udf_file_name_(file_name), udf_ast_file_name_(file_name) {
295 }
std::string udf_ast_file_name_
Definition: UDFCompiler.h:74
std::string udf_file_name_
Definition: UDFCompiler.h:73
void replaceExtn(std::string &s, const std::string &new_ext)

+ Here is the call graph for this function:

Member Function Documentation

int UdfCompiler::compileForGpu ( )
private

Definition at line 318 of file UDFCompiler.cpp.

References compileToGpuByteCode(), anonymous_namespace{UDFCompiler.cpp}::on_search_path(), and udf_file_name_.

Referenced by compileUdf().

318  {
319  int gpu_compile_result = 1;
320 
321  if (on_search_path("nvcc")) {
322  gpu_compile_result = compileToGpuByteCode(udf_file_name_.c_str(), false);
323  }
324 
325  // If gpu compilation fails but cpu compilation has succeeded, try compiling
326  // for the cpu with the assumption the user does not have the CUDA toolkit
327  // installed
328  if (gpu_compile_result != 0) {
329  gpu_compile_result = compileToGpuByteCode(udf_file_name_.c_str(), true);
330  }
331 
332  return gpu_compile_result;
333 }
int compileToGpuByteCode(const char *udf_file_name, bool cpu_mode)
std::string udf_file_name_
Definition: UDFCompiler.h:73
bool on_search_path(const std::string file)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int UdfCompiler::compileFromCommandLine ( std::vector< const char * > &  command_line)
private

Definition at line 188 of file UDFCompiler.cpp.

References logger::FATAL, UdfClangDriver::getClangDriver(), LOG, and run_benchmark_import::res.

Referenced by compileToCpuByteCode(), and compileToGpuByteCode().

188  {
189  UdfClangDriver compiler_driver;
190  auto the_driver(compiler_driver.getClangDriver());
191 
192  the_driver->CCPrintOptions = 0;
193  std::unique_ptr<driver::Compilation> compilation(
194  the_driver->BuildCompilation(command_line));
195 
196  if (!compilation) {
197  LOG(FATAL) << "failed to build compilation object!\n";
198  }
199 
200  llvm::SmallVector<std::pair<int, const driver::Command*>, 10> failing_commands;
201  int res = the_driver->ExecuteCompilation(*compilation, failing_commands);
202 
203  if (res < 0) {
204  for (const std::pair<int, const driver::Command*>& p : failing_commands) {
205  if (p.first) {
206  the_driver->generateCompilationDiagnostics(*compilation, *p.second);
207  }
208  }
209  }
210 
211  return res;
212 }
clang::driver::Driver * getClangDriver()
Definition: UDFCompiler.h:39
#define LOG(tag)
Definition: Logger.h:185

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int UdfCompiler::compileToCpuByteCode ( const char *  udf_file_name)
private

Definition at line 242 of file UDFCompiler.cpp.

References compileFromCommandLine(), and genCpuIrFilename().

Referenced by compileUdf().

242  {
243  auto a_path = llvm::sys::findProgramByName("clang++");
244  auto clang_path = a_path.get();
245 
246  std::string cpu_outName(genCpuIrFilename(udf_file_name));
247 
248  std::vector<const char*> command_line{clang_path.c_str(),
249  "-c",
250  "-O2",
251  "-emit-llvm",
252  "-o",
253  cpu_outName.c_str(),
254  "-std=c++14",
255  udf_file_name};
256 
257  return compileFromCommandLine(command_line);
258 }
std::string genCpuIrFilename(const char *udf_file_name)
int compileFromCommandLine(std::vector< const char * > &command_line)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int UdfCompiler::compileToGpuByteCode ( const char *  udf_file_name,
bool  cpu_mode 
)
private

Definition at line 214 of file UDFCompiler.cpp.

References compileFromCommandLine(), and genGpuIrFilename().

Referenced by compileForGpu().

214  {
215  auto a_path = llvm::sys::findProgramByName("clang++");
216  auto clang_path = a_path.get();
217 
218  std::string gpu_outName(genGpuIrFilename(udf_file_name));
219 
220  std::vector<const char*> command_line{clang_path.c_str(),
221  "-c",
222  "-O2",
223  "-emit-llvm",
224  "-o",
225  gpu_outName.c_str(),
226  "-std=c++14"};
227 
228  // If we are not compiling for cpu mode, then target the gpu
229  // Otherwise assume we can generic ir that will
230  // be translated to gpu code during target code generation
231  if (!cpu_mode) {
232  command_line.emplace_back("--cuda-gpu-arch=sm_30");
233  command_line.emplace_back("--cuda-device-only");
234  command_line.emplace_back("-xcuda");
235  }
236 
237  command_line.emplace_back(udf_file_name);
238 
239  return compileFromCommandLine(command_line);
240 }
std::string genGpuIrFilename(const char *udf_file_name)
int compileFromCommandLine(std::vector< const char * > &command_line)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int UdfCompiler::compileUdf ( )

Definition at line 335 of file UDFCompiler.cpp.

References compileForGpu(), compileToCpuByteCode(), logger::FATAL, logger::INFO, LOG, anonymous_namespace{UDFCompiler.cpp}::on_search_path(), parseToAst(), readCpuCompiledModule(), readGpuCompiledModule(), and udf_file_name_.

335  {
336  if (on_search_path("clang++")) {
337  LOG(INFO) << "UDFCompiler filename to compiler: " << udf_file_name_ << std::endl;
338  if (!boost::filesystem::exists(udf_file_name_)) {
339  LOG(FATAL) << "User defined function file " << udf_file_name_ << " does not exist.";
340  return 1;
341  }
342 
343  auto ast_result = parseToAst(udf_file_name_.c_str());
344 
345  if (ast_result == 0) {
346  // Compile udf file to generate cpu and gpu bytecode files
347 
348  int cpu_compile_result = compileToCpuByteCode(udf_file_name_.c_str());
349 #ifdef HAVE_CUDA
350  int gpu_compile_result = 1;
351 #endif
352 
353  if (cpu_compile_result == 0) {
355 #ifdef HAVE_CUDA
356  gpu_compile_result = compileForGpu();
357 
358  if (gpu_compile_result == 0) {
360  } else {
361  LOG(FATAL) << "Unable to compile UDF file for gpu" << std::endl;
362  return 1;
363  }
364 #endif
365  } else {
366  LOG(FATAL) << "Unable to compile UDF file for cpu" << std::endl;
367  return 1;
368  }
369  } else {
370  LOG(FATAL) << "Unable to create AST file for udf compilation" << std::endl;
371  return 1;
372  }
373  } else {
374  LOG(FATAL) << "Unable to compile udfs due to absence of clang++" << std::endl;
375  return 1;
376  }
377 
378  return 0;
379 }
int compileToCpuByteCode(const char *udf_file_name)
#define LOG(tag)
Definition: Logger.h:185
void readCpuCompiledModule()
int parseToAst(const char *file_name)
std::string udf_file_name_
Definition: UDFCompiler.h:73
bool on_search_path(const std::string file)
void readGpuCompiledModule()
int compileForGpu()

+ Here is the call graph for this function:

std::string UdfCompiler::genCpuIrFilename ( const char *  udf_file_name)
private

Definition at line 181 of file UDFCompiler.cpp.

References removeFileExtension().

Referenced by compileToCpuByteCode(), and readCpuCompiledModule().

181  {
182  std::string cpu_file_name(removeFileExtension(udf_fileName));
183 
184  cpu_file_name += "_cpu.bc";
185  return cpu_file_name;
186 }
std::string removeFileExtension(const std::string &path)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::string UdfCompiler::genGpuIrFilename ( const char *  udf_file_name)
private

Definition at line 174 of file UDFCompiler.cpp.

References removeFileExtension().

Referenced by compileToGpuByteCode(), and readGpuCompiledModule().

174  {
175  std::string gpu_file_name(removeFileExtension(udf_file_name));
176 
177  gpu_file_name += "_gpu.bc";
178  return gpu_file_name;
179 }
std::string removeFileExtension(const std::string &path)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

const std::string & UdfCompiler::getAstFileName ( ) const

Definition at line 288 of file UDFCompiler.cpp.

References udf_ast_file_name_.

288  {
289  return udf_ast_file_name_;
290 }
std::string udf_ast_file_name_
Definition: UDFCompiler.h:74
std::string UdfCompiler::getFileExt ( std::string &  s)
private

Definition at line 159 of file UDFCompiler.cpp.

Referenced by replaceExtn().

159  {
160  size_t i = s.rfind('.', s.length());
161  if (1 != std::string::npos) {
162  return (s.substr(i + 1, s.length() - i));
163  }
164 }

+ Here is the caller graph for this function:

int UdfCompiler::parseToAst ( const char *  file_name)
private

Definition at line 260 of file UDFCompiler.cpp.

References error_code, UdfClangDriver::getClangDriver(), replaceExtn(), and ToolingSampleCategory().

Referenced by compileUdf().

260  {
261  UdfClangDriver the_driver;
262  std::string resource_path = the_driver.getClangDriver()->ResourceDir;
263  std::string include_option =
264  std::string("-I") + resource_path + std::string("/include");
265 
266  const char arg0[] = "astparser";
267  const char* arg1 = file_name;
268  const char arg2[] = "--";
269  const char* arg3 = include_option.c_str();
270  const char* arg_vector[] = {arg0, arg1, arg2, arg3};
271 
272  int num_args = sizeof(arg_vector) / sizeof(arg_vector[0]);
273  CommonOptionsParser op(num_args, arg_vector, ToolingSampleCategory);
274  ClangTool tool(op.getCompilations(), op.getSourcePathList());
275 
276  std::string out_name(file_name);
277  std::string file_ext("ast");
278  replaceExtn(out_name, file_ext);
279 
280  std::error_code out_error_info;
281  llvm::raw_fd_ostream out_file(
282  llvm::StringRef(out_name), out_error_info, llvm::sys::fs::F_None);
283 
284  auto factory = llvm::make_unique<ToolFactory>(out_file);
285  return tool.run(factory.get());
286 }
clang::driver::Driver * getClangDriver()
Definition: UDFCompiler.h:39
static llvm::cl::OptionCategory ToolingSampleCategory("UDF Tooling")
const int8_t const int64_t const uint64_t const int32_t const int64_t int64_t uint32_t const int64_t int32_t * error_code
void replaceExtn(std::string &s, const std::string &new_ext)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void UdfCompiler::readCompiledModules ( )
private

Definition at line 313 of file UDFCompiler.cpp.

References readCpuCompiledModule(), and readGpuCompiledModule().

313  {
316 }
void readCpuCompiledModule()
void readGpuCompiledModule()

+ Here is the call graph for this function:

void UdfCompiler::readCpuCompiledModule ( )
private

Definition at line 297 of file UDFCompiler.cpp.

References genCpuIrFilename(), read_udf_cpu_module(), udf_file_name_, and VLOG.

Referenced by compileUdf(), and readCompiledModules().

297  {
298  std::string cpu_ir_file(genCpuIrFilename(udf_file_name_.c_str()));
299 
300  VLOG(1) << "UDFCompiler cpu bc file = " << cpu_ir_file << std::endl;
301 
302  read_udf_cpu_module(cpu_ir_file);
303 }
std::string genCpuIrFilename(const char *udf_file_name)
void read_udf_cpu_module(const std::string &udf_ir_filename)
std::string udf_file_name_
Definition: UDFCompiler.h:73
#define VLOG(n)
Definition: Logger.h:280

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void UdfCompiler::readGpuCompiledModule ( )
private

Definition at line 305 of file UDFCompiler.cpp.

References genGpuIrFilename(), read_udf_gpu_module(), udf_file_name_, and VLOG.

Referenced by compileUdf(), and readCompiledModules().

305  {
306  std::string gpu_ir_file(genGpuIrFilename(udf_file_name_.c_str()));
307 
308  VLOG(1) << "UDFCompiler gpu bc file = " << gpu_ir_file << std::endl;
309 
310  read_udf_gpu_module(gpu_ir_file);
311 }
void read_udf_gpu_module(const std::string &udf_ir_filename)
std::string genGpuIrFilename(const char *udf_file_name)
std::string udf_file_name_
Definition: UDFCompiler.h:73
#define VLOG(n)
Definition: Logger.h:280

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::string UdfCompiler::removeFileExtension ( const std::string &  path)
private

Definition at line 146 of file UDFCompiler.cpp.

Referenced by genCpuIrFilename(), and genGpuIrFilename().

146  {
147  if (path == "." || path == "..") {
148  return path;
149  }
150 
151  size_t pos = path.find_last_of("\\/.");
152  if (pos != std::string::npos && path[pos] == '.') {
153  return path.substr(0, pos);
154  }
155 
156  return path;
157 }

+ Here is the caller graph for this function:

void UdfCompiler::replaceExtn ( std::string &  s,
const std::string &  new_ext 
)
private

Definition at line 166 of file UDFCompiler.cpp.

References getFileExt().

Referenced by parseToAst(), and UdfCompiler().

166  {
167  std::string::size_type i = s.rfind('.', s.length());
168 
169  if (i != std::string::npos) {
170  s.replace(i + 1, getFileExt(s).length(), new_ext);
171  }
172 }
std::string getFileExt(std::string &s)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

std::string UdfCompiler::udf_ast_file_name_
private

Definition at line 74 of file UDFCompiler.h.

Referenced by getAstFileName(), and UdfCompiler().

std::string UdfCompiler::udf_file_name_
private

The documentation for this class was generated from the following files: