OmniSciDB  c07336695a
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 ()
 
int compileForGpu ()
 

Private Attributes

std::string udf_file_name_
 
std::string udf_ast_file_name_
 

Detailed Description

Definition at line 31 of file UDFCompiler.h.

Constructor & Destructor Documentation

◆ UdfCompiler()

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

Definition at line 291 of file UDFCompiler.cpp.

References replaceExtn(), and udf_ast_file_name_.

292  : udf_file_name_(file_name), udf_ast_file_name_(file_name) {
294 }
std::string udf_ast_file_name_
Definition: UDFCompiler.h:52
std::string udf_file_name_
Definition: UDFCompiler.h:51
void replaceExtn(std::string &s, const std::string &new_ext)
+ Here is the call graph for this function:

Member Function Documentation

◆ compileForGpu()

int UdfCompiler::compileForGpu ( )
private

Definition at line 307 of file UDFCompiler.cpp.

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

Referenced by compileUdf().

307  {
308  int gpu_compile_result = 1;
309 
310  if (on_search_path("nvcc")) {
311  gpu_compile_result = compileToGpuByteCode(udf_file_name_.c_str(), false);
312  }
313 
314  // If gpu compilation fails but cpu compilation has succeeded, try compiling
315  // for the cpu with the assumption the user does not have the CUDA toolkit
316  // installed
317  if (gpu_compile_result != 0) {
318  gpu_compile_result = compileToGpuByteCode(udf_file_name_.c_str(), true);
319  }
320 
321  return gpu_compile_result;
322 }
int compileToGpuByteCode(const char *udf_file_name, bool cpu_mode)
std::string udf_file_name_
Definition: UDFCompiler.h:51
bool on_search_path(const std::string file)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ compileFromCommandLine()

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

Definition at line 179 of file UDFCompiler.cpp.

References logger::FATAL, LOG, and run-benchmark-import::res.

179  {
180  auto a_path = llvm::sys::findProgramByName("clang++");
181  auto clang_path = a_path.get();
182 
183  llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> diag_options(
184  new DiagnosticOptions());
185 
186  clang::DiagnosticConsumer* diag_client =
187  new TextDiagnosticPrinter(llvm::errs(), diag_options.get());
188 
189  llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diag_id(new clang::DiagnosticIDs());
190  clang::DiagnosticsEngine diags(diag_id, diag_options.get(), diag_client);
191 
192  std::unique_ptr<clang::DiagnosticConsumer> diag_client_owner(diags.takeClient());
193 
194  clang::driver::Driver the_driver(
195  clang_path.c_str(), llvm::sys::getDefaultTargetTriple(), diags);
196 
197  the_driver.CCPrintOptions = 0;
198  std::unique_ptr<driver::Compilation> compilation(
199  the_driver.BuildCompilation(command_line));
200 
201  if (!compilation) {
202  LOG(FATAL) << "failed to build compilation object!\n";
203  }
204 
205  llvm::SmallVector<std::pair<int, const driver::Command*>, 10> failing_commands;
206  int res = the_driver.ExecuteCompilation(*compilation, failing_commands);
207 
208  if (res < 0) {
209  for (const std::pair<int, const driver::Command*>& p : failing_commands) {
210  if (p.first) {
211  the_driver.generateCompilationDiagnostics(*compilation, *p.second);
212  }
213  }
214  }
215 
216  return res;
217 }
#define LOG(tag)
Definition: Logger.h:182

◆ compileToCpuByteCode()

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

Definition at line 247 of file UDFCompiler.cpp.

Referenced by compileUdf().

247  {
248  auto a_path = llvm::sys::findProgramByName("clang++");
249  auto clang_path = a_path.get();
250 
251  std::string cpu_outName(genCpuIrFilename(udf_file_name));
252 
253  std::vector<const char*> command_line{clang_path.c_str(),
254  "-c",
255  "-O2",
256  "-emit-llvm",
257  "-o",
258  cpu_outName.c_str(),
259  "-std=c++14",
260  udf_file_name};
261 
262  return compileFromCommandLine(command_line);
263 }
std::string genCpuIrFilename(const char *udf_file_name)
int compileFromCommandLine(std::vector< const char *> &command_line)
+ Here is the caller graph for this function:

◆ compileToGpuByteCode()

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

Definition at line 219 of file UDFCompiler.cpp.

Referenced by compileForGpu().

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

◆ compileUdf()

int UdfCompiler::compileUdf ( )

Definition at line 324 of file UDFCompiler.cpp.

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

Referenced by MapDHandler::MapDHandler(), anonymous_namespace{UdfTest.cpp}::SQLTestEnv::SetUp(), and TEST_F().

324  {
325  if (on_search_path("clang++")) {
326  LOG(INFO) << "UDFCompiler filename to compiler: " << udf_file_name_ << std::endl;
327  if (!boost::filesystem::exists(udf_file_name_)) {
328  LOG(FATAL) << "User defined function file " << udf_file_name_ << " does not exist.";
329  return 1;
330  }
331 
332  auto ast_result = parseToAst(udf_file_name_.c_str());
333 
334  if (ast_result == 0) {
335  // Compile udf file to generate cpu and gpu bytecode files
336 
337  int cpu_compile_result = compileToCpuByteCode(udf_file_name_.c_str());
338  int gpu_compile_result = 1;
339 
340  if (cpu_compile_result == 0) {
341  gpu_compile_result = compileForGpu();
342 
343  if (gpu_compile_result == 0) {
345  } else {
346  LOG(FATAL) << "Unable to compile UDF file for gpu" << std::endl;
347  return 1;
348  }
349  } else {
350  LOG(FATAL) << "Unable to compile UDF file for cpu" << std::endl;
351  return 1;
352  }
353  } else {
354  LOG(FATAL) << "Unable to create AST file for udf compilation" << std::endl;
355  return 1;
356  }
357  } else {
358  LOG(FATAL) << "Unable to compile udfs due to absence of clang++" << std::endl;
359  return 1;
360  }
361 
362  return 0;
363 }
int compileToCpuByteCode(const char *udf_file_name)
void readCompiledModules()
#define LOG(tag)
Definition: Logger.h:182
int parseToAst(const char *file_name)
std::string udf_file_name_
Definition: UDFCompiler.h:51
bool on_search_path(const std::string file)
int compileForGpu()
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ genCpuIrFilename()

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

Definition at line 172 of file UDFCompiler.cpp.

Referenced by readCompiledModules().

172  {
173  std::string cpu_file_name(removeFileExtension(udf_fileName));
174 
175  cpu_file_name += "_cpu.bc";
176  return cpu_file_name;
177 }
std::string removeFileExtension(const std::string &path)
+ Here is the caller graph for this function:

◆ genGpuIrFilename()

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

Definition at line 165 of file UDFCompiler.cpp.

Referenced by readCompiledModules().

165  {
166  std::string gpu_file_name(removeFileExtension(udf_file_name));
167 
168  gpu_file_name += "_gpu.bc";
169  return gpu_file_name;
170 }
std::string removeFileExtension(const std::string &path)
+ Here is the caller graph for this function:

◆ getAstFileName()

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

Definition at line 287 of file UDFCompiler.cpp.

Referenced by MapDHandler::MapDHandler().

287  {
288  return udf_ast_file_name_;
289 }
std::string udf_ast_file_name_
Definition: UDFCompiler.h:52
+ Here is the caller graph for this function:

◆ getFileExt()

std::string UdfCompiler::getFileExt ( std::string &  s)
private

Definition at line 150 of file UDFCompiler.cpp.

150  {
151  size_t i = s.rfind('.', s.length());
152  if (1 != std::string::npos) {
153  return (s.substr(i + 1, s.length() - i));
154  }
155 }

◆ parseToAst()

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

Definition at line 265 of file UDFCompiler.cpp.

References error_code, and ToolingSampleCategory().

Referenced by compileUdf().

265  {
266  int num_args = 3;
267  const char arg0[] = "astparser";
268  const char* arg1 = file_name;
269  const char arg2[] = "--";
270  const char* arg_vector[3] = {arg0, arg1, arg2};
271 
272  CommonOptionsParser op(num_args, arg_vector, ToolingSampleCategory);
273  ClangTool tool(op.getCompilations(), op.getSourcePathList());
274 
275  std::string out_name(file_name);
276  std::string file_ext("ast");
277  replaceExtn(out_name, file_ext);
278 
279  std::error_code out_error_info;
280  llvm::raw_fd_ostream out_file(
281  llvm::StringRef(out_name), out_error_info, llvm::sys::fs::F_None);
282 
283  auto factory = llvm::make_unique<ToolFactory>(out_file);
284  return tool.run(factory.get());
285 }
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:

◆ readCompiledModules()

void UdfCompiler::readCompiledModules ( )
private

Definition at line 296 of file UDFCompiler.cpp.

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

Referenced by compileUdf().

296  {
297  std::string cpu_ir_file(genCpuIrFilename(udf_file_name_.c_str()));
298  std::string gpu_ir_file(genGpuIrFilename(udf_file_name_.c_str()));
299 
300  VLOG(1) << "UDFCompiler cpu bc file = " << cpu_ir_file << std::endl;
301  VLOG(1) << "UDFCompiler gpu bc file = " << gpu_ir_file << std::endl;
302 
303  read_udf_cpu_module(cpu_ir_file);
304  read_udf_gpu_module(gpu_ir_file);
305 }
std::string genCpuIrFilename(const char *udf_file_name)
void read_udf_cpu_module(const std::string &udf_ir_filename)
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:51
#define VLOG(n)
Definition: Logger.h:277
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ removeFileExtension()

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

Definition at line 137 of file UDFCompiler.cpp.

137  {
138  if (path == "." || path == "..") {
139  return path;
140  }
141 
142  size_t pos = path.find_last_of("\\/.");
143  if (pos != std::string::npos && path[pos] == '.') {
144  return path.substr(0, pos);
145  }
146 
147  return path;
148 }

◆ replaceExtn()

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

Definition at line 157 of file UDFCompiler.cpp.

Referenced by UdfCompiler().

157  {
158  std::string::size_type i = s.rfind('.', s.length());
159 
160  if (i != std::string::npos) {
161  s.replace(i + 1, getFileExt(s).length(), new_ext);
162  }
163 }
std::string getFileExt(std::string &s)
+ Here is the caller graph for this function:

Member Data Documentation

◆ udf_ast_file_name_

std::string UdfCompiler::udf_ast_file_name_
private

Definition at line 52 of file UDFCompiler.h.

Referenced by UdfCompiler().

◆ udf_file_name_

std::string UdfCompiler::udf_file_name_
private

Definition at line 51 of file UDFCompiler.h.

Referenced by compileForGpu(), compileUdf(), and readCompiledModules().


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