OmniSciDB  06b3bd477c
 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 &udf_file_name, CudaMgr_Namespace::NvidiaDeviceArch target_arch, const std::string &clang_path="")
 
 UdfCompiler (const std::string &udf_file_name, CudaMgr_Namespace::NvidiaDeviceArch target_arch, const std::string &clang_path, const std::vector< std::string > clang_options)
 
int compileUdf ()
 
const std::string & getAstFileName () const
 

Private Member Functions

void init (const std::string &clang_path)
 
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 (const std::vector< std::string > &command_line)
 
void readCompiledModules ()
 
void readGpuCompiledModule ()
 
void readCpuCompiledModule ()
 
int compileForGpu ()
 

Private Attributes

std::string udf_file_name_
 
std::string udf_ast_file_name_
 
CudaMgr_Namespace::NvidiaDeviceArch target_arch_
 
std::string clang_path_
 
std::vector< std::string > clang_options_
 

Detailed Description

Definition at line 50 of file UDFCompiler.h.

Constructor & Destructor Documentation

UdfCompiler::UdfCompiler ( const std::string &  udf_file_name,
CudaMgr_Namespace::NvidiaDeviceArch  target_arch,
const std::string &  clang_path = "" 
)

Definition at line 352 of file UDFCompiler.cpp.

References init().

355  : udf_file_name_(file_name)
356  , udf_ast_file_name_(file_name)
357  , target_arch_(target_arch) {
358  init(clang_path);
359 }
std::string udf_ast_file_name_
Definition: UDFCompiler.h:80
CudaMgr_Namespace::NvidiaDeviceArch target_arch_
Definition: UDFCompiler.h:81
void init(const std::string &clang_path)
std::string udf_file_name_
Definition: UDFCompiler.h:79

+ Here is the call graph for this function:

UdfCompiler::UdfCompiler ( const std::string &  udf_file_name,
CudaMgr_Namespace::NvidiaDeviceArch  target_arch,
const std::string &  clang_path,
const std::vector< std::string >  clang_options 
)

Definition at line 361 of file UDFCompiler.cpp.

References init().

365  : udf_file_name_(file_name)
366  , udf_ast_file_name_(file_name)
367  , target_arch_(target_arch)
368  , clang_options_(clang_options) {
369  init(clang_path);
370 }
std::vector< std::string > clang_options_
Definition: UDFCompiler.h:83
std::string udf_ast_file_name_
Definition: UDFCompiler.h:80
CudaMgr_Namespace::NvidiaDeviceArch target_arch_
Definition: UDFCompiler.h:81
void init(const std::string &clang_path)
std::string udf_file_name_
Definition: UDFCompiler.h:79

+ Here is the call graph for this function:

Member Function Documentation

int UdfCompiler::compileForGpu ( )
private

Definition at line 393 of file UDFCompiler.cpp.

References compileToGpuByteCode(), and udf_file_name_.

Referenced by compileUdf().

393  {
394  int gpu_compile_result = 1;
395 
396  gpu_compile_result = compileToGpuByteCode(udf_file_name_.c_str(), false);
397 
398  // If gpu compilation fails but cpu compilation has succeeded, try compiling
399  // for the cpu with the assumption the user does not have the CUDA toolkit
400  // installed
401  if (gpu_compile_result != 0) {
402  gpu_compile_result = compileToGpuByteCode(udf_file_name_.c_str(), true);
403  }
404 
405  return gpu_compile_result;
406 }
int compileToGpuByteCode(const char *udf_file_name, bool cpu_mode)
std::string udf_file_name_
Definition: UDFCompiler.h:79

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 209 of file UDFCompiler.cpp.

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

Referenced by compileToCpuByteCode(), and compileToGpuByteCode().

209  {
210  UdfClangDriver compiler_driver(clang_path_);
211  auto the_driver(compiler_driver.getClangDriver());
212 
213  std::vector<const char*> clang_command_opts;
214  clang_command_opts.reserve(command_line.size() + clang_options_.size());
215  // add required options first
216  std::transform(std::begin(command_line),
217  std::end(command_line),
218  std::back_inserter(clang_command_opts),
219  [&](const std::string& str) { return str.c_str(); });
220 
221  // If there were additional clang options passed to the system, append them here
222  if (!clang_options_.empty()) {
223  std::transform(std::begin(clang_options_),
224  std::end(clang_options_),
225  std::back_inserter(clang_command_opts),
226  [&](const std::string& str) { return str.c_str(); });
227  }
228 
229  the_driver->CCPrintOptions = 0;
230  std::unique_ptr<driver::Compilation> compilation(
231  the_driver->BuildCompilation(clang_command_opts));
232 
233  if (!compilation) {
234  LOG(FATAL) << "failed to build compilation object!\n";
235  }
236 
237  llvm::SmallVector<std::pair<int, const driver::Command*>, 10> failing_commands;
238  int res = the_driver->ExecuteCompilation(*compilation, failing_commands);
239 
240  if (res < 0) {
241  for (const std::pair<int, const driver::Command*>& p : failing_commands) {
242  if (p.first) {
243  the_driver->generateCompilationDiagnostics(*compilation, *p.second);
244  }
245  }
246  }
247 
248  return res;
249 }
std::vector< std::string > clang_options_
Definition: UDFCompiler.h:83
#define LOG(tag)
Definition: Logger.h:188
std::string clang_path_
Definition: UDFCompiler.h:82

+ 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 272 of file UDFCompiler.cpp.

References clang_path_, compileFromCommandLine(), and genCpuIrFilename().

Referenced by compileUdf().

272  {
273  std::string cpu_out_filename(genCpuIrFilename(udf_file_name));
274 
275  std::vector<std::string> command_line{clang_path_,
276  "-c",
277  "-O2",
278  "-emit-llvm",
279  "-o",
280  cpu_out_filename,
281  "-std=c++14",
282  udf_file_name};
283 
284  return compileFromCommandLine(command_line);
285 }
std::string genCpuIrFilename(const char *udf_file_name)
int compileFromCommandLine(const std::vector< std::string > &command_line)
std::string clang_path_
Definition: UDFCompiler.h:82

+ 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 251 of file UDFCompiler.cpp.

References clang_path_, compileFromCommandLine(), CudaMgr_Namespace::CudaMgr::deviceArchToSM(), genGpuIrFilename(), and target_arch_.

Referenced by compileForGpu().

251  {
252  std::string gpu_out_filename(genGpuIrFilename(udf_file_name));
253 
254  std::vector<std::string> command_line{
255  clang_path_, "-c", "-O2", "-emit-llvm", "-o", gpu_out_filename, "-std=c++14"};
256 
257  // If we are not compiling for cpu mode, then target the gpu
258  // Otherwise assume we can generic ir that will
259  // be translated to gpu code during target code generation
260  if (!cpu_mode) {
261  command_line.emplace_back("--cuda-gpu-arch=" +
263  command_line.emplace_back("--cuda-device-only");
264  command_line.emplace_back("-xcuda");
265  }
266 
267  command_line.emplace_back(udf_file_name);
268 
269  return compileFromCommandLine(command_line);
270 }
std::string genGpuIrFilename(const char *udf_file_name)
CudaMgr_Namespace::NvidiaDeviceArch target_arch_
Definition: UDFCompiler.h:81
static std::string deviceArchToSM(const NvidiaDeviceArch arch)
Definition: CudaMgr.h:157
int compileFromCommandLine(const std::vector< std::string > &command_line)
std::string clang_path_
Definition: UDFCompiler.h:82

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int UdfCompiler::compileUdf ( )

Definition at line 408 of file UDFCompiler.cpp.

References compileForGpu(), compileToCpuByteCode(), logger::FATAL, logger::INFO, LOG, parseToAst(), readCpuCompiledModule(), readGpuCompiledModule(), and udf_file_name_.

408  {
409  LOG(INFO) << "UDFCompiler filename to compile: " << udf_file_name_;
410  if (!boost::filesystem::exists(udf_file_name_)) {
411  LOG(FATAL) << "User defined function file " << udf_file_name_ << " does not exist.";
412  return 1;
413  }
414 
415  auto ast_result = parseToAst(udf_file_name_.c_str());
416 
417  if (ast_result == 0) {
418  // Compile udf file to generate cpu and gpu bytecode files
419 
420  int cpu_compile_result = compileToCpuByteCode(udf_file_name_.c_str());
421 #ifdef HAVE_CUDA
422  int gpu_compile_result = 1;
423 #endif
424 
425  if (cpu_compile_result == 0) {
427 #ifdef HAVE_CUDA
428  gpu_compile_result = compileForGpu();
429 
430  if (gpu_compile_result == 0) {
432  } else {
433  LOG(FATAL) << "Unable to compile UDF file for gpu";
434  return 1;
435  }
436 #endif
437  } else {
438  LOG(FATAL) << "Unable to compile UDF file for cpu";
439  return 1;
440  }
441  } else {
442  LOG(FATAL) << "Unable to create AST file for udf compilation";
443  return 1;
444  }
445 
446  return 0;
447 }
int compileToCpuByteCode(const char *udf_file_name)
#define LOG(tag)
Definition: Logger.h:188
void readCpuCompiledModule()
int parseToAst(const char *file_name)
std::string udf_file_name_
Definition: UDFCompiler.h:79
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 202 of file UDFCompiler.cpp.

References removeFileExtension().

Referenced by compileToCpuByteCode(), and readCpuCompiledModule().

202  {
203  std::string cpu_file_name(removeFileExtension(udf_fileName));
204 
205  cpu_file_name += "_cpu.bc";
206  return cpu_file_name;
207 }
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 195 of file UDFCompiler.cpp.

References removeFileExtension().

Referenced by compileToGpuByteCode(), and readGpuCompiledModule().

195  {
196  std::string gpu_file_name(removeFileExtension(udf_file_name));
197 
198  gpu_file_name += "_gpu.bc";
199  return gpu_file_name;
200 }
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 324 of file UDFCompiler.cpp.

References udf_ast_file_name_.

324  {
325  return udf_ast_file_name_;
326 }
std::string udf_ast_file_name_
Definition: UDFCompiler.h:80
std::string UdfCompiler::getFileExt ( std::string &  s)
private

Definition at line 180 of file UDFCompiler.cpp.

Referenced by replaceExtn().

180  {
181  size_t i = s.rfind('.', s.length());
182  if (1 != std::string::npos) {
183  return (s.substr(i + 1, s.length() - i));
184  }
185 }

+ Here is the caller graph for this function:

void UdfCompiler::init ( const std::string &  clang_path)
private

Definition at line 328 of file UDFCompiler.cpp.

References clang_path_, replaceExtn(), and udf_ast_file_name_.

Referenced by UdfCompiler().

328  {
330 
331  if (clang_path.empty()) {
332  clang_path_.assign(llvm::sys::findProgramByName("clang++").get());
333  if (clang_path_.empty()) {
334  throw std::runtime_error(
335  "Unable to find clang++ to compile user defined functions");
336  }
337  } else {
338  clang_path_.assign(clang_path);
339 
340  if (!boost::filesystem::exists(clang_path)) {
341  throw std::runtime_error("Path provided for udf compiler " + clang_path +
342  " does not exist.");
343  }
344 
345  if (boost::filesystem::is_directory(clang_path)) {
346  throw std::runtime_error("Path provided for udf compiler " + clang_path +
347  " is not to the clang++ executable.");
348  }
349  }
350 }
std::string udf_ast_file_name_
Definition: UDFCompiler.h:80
void replaceExtn(std::string &s, const std::string &new_ext)
std::string clang_path_
Definition: UDFCompiler.h:82

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 287 of file UDFCompiler.cpp.

References clang_options_, clang_path_, anonymous_namespace{UDFCompiler.cpp}::convert(), error_code, UdfClangDriver::getClangDriver(), replaceExtn(), and ToolingSampleCategory().

Referenced by compileUdf().

287  {
288  UdfClangDriver the_driver(clang_path_);
289  std::string resource_path = the_driver.getClangDriver()->ResourceDir;
290  std::string include_option =
291  std::string("-I") + resource_path + std::string("/include");
292 
293  std::vector<std::string> arg_vector;
294  arg_vector.emplace_back("astparser");
295  arg_vector.emplace_back(file_name);
296  arg_vector.emplace_back("--");
297  arg_vector.emplace_back(include_option);
298 
299  if (clang_options_.size() > 0) {
300  std::copy(
301  clang_options_.begin(), clang_options_.end(), std::back_inserter(arg_vector));
302  }
303 
304  std::vector<const char*> arg_vec2;
305  std::transform(
306  arg_vector.begin(), arg_vector.end(), std::back_inserter(arg_vec2), convert);
307 
308  int num_args = arg_vec2.size();
309  CommonOptionsParser op(num_args, &arg_vec2[0], ToolingSampleCategory);
310  ClangTool tool(op.getCompilations(), op.getSourcePathList());
311 
312  std::string out_name(file_name);
313  std::string file_ext("ast");
314  replaceExtn(out_name, file_ext);
315 
316  std::error_code out_error_info;
317  llvm::raw_fd_ostream out_file(
318  llvm::StringRef(out_name), out_error_info, llvm::sys::fs::F_None);
319 
320  auto factory = std::make_unique<ToolFactory>(out_file);
321  return tool.run(factory.get());
322 }
std::vector< std::string > clang_options_
Definition: UDFCompiler.h:83
static llvm::cl::OptionCategory ToolingSampleCategory("UDF Tooling")
const char * convert(const std::string &s)
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)
std::string clang_path_
Definition: UDFCompiler.h:82

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void UdfCompiler::readCompiledModules ( )
private

Definition at line 388 of file UDFCompiler.cpp.

References readCpuCompiledModule(), and readGpuCompiledModule().

388  {
391 }
void readCpuCompiledModule()
void readGpuCompiledModule()

+ Here is the call graph for this function:

void UdfCompiler::readCpuCompiledModule ( )
private

Definition at line 372 of file UDFCompiler.cpp.

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

Referenced by compileUdf(), and readCompiledModules().

372  {
373  std::string cpu_ir_file(genCpuIrFilename(udf_file_name_.c_str()));
374 
375  VLOG(1) << "UDFCompiler cpu bc file = " << cpu_ir_file;
376 
377  read_udf_cpu_module(cpu_ir_file);
378 }
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:79
#define VLOG(n)
Definition: Logger.h:291

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void UdfCompiler::readGpuCompiledModule ( )
private

Definition at line 380 of file UDFCompiler.cpp.

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

Referenced by compileUdf(), and readCompiledModules().

380  {
381  std::string gpu_ir_file(genGpuIrFilename(udf_file_name_.c_str()));
382 
383  VLOG(1) << "UDFCompiler gpu bc file = " << gpu_ir_file;
384 
385  read_udf_gpu_module(gpu_ir_file);
386 }
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:79
#define VLOG(n)
Definition: Logger.h:291

+ 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 167 of file UDFCompiler.cpp.

Referenced by genCpuIrFilename(), and genGpuIrFilename().

167  {
168  if (path == "." || path == "..") {
169  return path;
170  }
171 
172  size_t pos = path.find_last_of("\\/.");
173  if (pos != std::string::npos && path[pos] == '.') {
174  return path.substr(0, pos);
175  }
176 
177  return path;
178 }

+ Here is the caller graph for this function:

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

Definition at line 187 of file UDFCompiler.cpp.

References getFileExt().

Referenced by init(), and parseToAst().

187  {
188  std::string::size_type i = s.rfind('.', s.length());
189 
190  if (i != std::string::npos) {
191  s.replace(i + 1, getFileExt(s).length(), new_ext);
192  }
193 }
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::vector<std::string> UdfCompiler::clang_options_
private

Definition at line 83 of file UDFCompiler.h.

Referenced by compileFromCommandLine(), and parseToAst().

std::string UdfCompiler::clang_path_
private
CudaMgr_Namespace::NvidiaDeviceArch UdfCompiler::target_arch_
private

Definition at line 81 of file UDFCompiler.h.

Referenced by compileToGpuByteCode().

std::string UdfCompiler::udf_ast_file_name_
private

Definition at line 80 of file UDFCompiler.h.

Referenced by getAstFileName(), and init().

std::string UdfCompiler::udf_file_name_
private

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