MLIR  20.0.0git
Target.cpp
Go to the documentation of this file.
1 //===- Target.cpp - MLIR LLVM NVVM target compilation -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This files defines NVVM target related functions including registration
10 // calls for the `#nvvm.target` compilation attribute.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 
23 
24 #include "llvm/Config/llvm-config.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/FileUtilities.h"
27 #include "llvm/Support/FormatVariadic.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/Process.h"
31 #include "llvm/Support/Program.h"
32 #include "llvm/Support/TargetSelect.h"
33 
34 #include <cstdlib>
35 
36 using namespace mlir;
37 using namespace mlir::NVVM;
38 
39 #ifndef __DEFAULT_CUDATOOLKIT_PATH__
40 #define __DEFAULT_CUDATOOLKIT_PATH__ ""
41 #endif
42 
43 namespace {
44 // Implementation of the `TargetAttrInterface` model.
45 class NVVMTargetAttrImpl
46  : public gpu::TargetAttrInterface::FallbackModel<NVVMTargetAttrImpl> {
47 public:
48  std::optional<SmallVector<char, 0>>
49  serializeToObject(Attribute attribute, Operation *module,
50  const gpu::TargetOptions &options) const;
51 
52  Attribute createObject(Attribute attribute,
53  const SmallVector<char, 0> &object,
54  const gpu::TargetOptions &options) const;
55 };
56 } // namespace
57 
58 // Register the NVVM dialect, the NVVM translation & the target interface.
60  DialectRegistry &registry) {
61  registry.addExtension(+[](MLIRContext *ctx, NVVM::NVVMDialect *dialect) {
62  NVVMTargetAttr::attachInterface<NVVMTargetAttrImpl>(*ctx);
63  });
64 }
65 
67  MLIRContext &context) {
68  DialectRegistry registry;
70  context.appendDialectRegistry(registry);
71 }
72 
73 // Search for the CUDA toolkit path.
75  if (const char *var = std::getenv("CUDA_ROOT"))
76  return var;
77  if (const char *var = std::getenv("CUDA_HOME"))
78  return var;
79  if (const char *var = std::getenv("CUDA_PATH"))
80  return var;
82 }
83 
85  Operation &module, NVVMTargetAttr target,
86  const gpu::TargetOptions &targetOptions)
87  : ModuleToObject(module, target.getTriple(), target.getChip(),
88  target.getFeatures(), target.getO()),
89  target(target), toolkitPath(targetOptions.getToolkitPath()),
90  fileList(targetOptions.getLinkFiles()) {
91 
92  // If `targetOptions` have an empty toolkitPath use `getCUDAToolkitPath`
93  if (toolkitPath.empty())
95 
96  // Append the files in the target attribute.
97  if (ArrayAttr files = target.getLink())
98  for (Attribute attr : files.getValue())
99  if (auto file = dyn_cast<StringAttr>(attr))
100  fileList.push_back(file.str());
101 
102  // Append libdevice to the files to be loaded.
103  (void)appendStandardLibs();
104 }
105 
107  static llvm::once_flag initializeBackendOnce;
108  llvm::call_once(initializeBackendOnce, []() {
109  // If the `NVPTX` LLVM target was built, initialize it.
110 #if LLVM_HAS_NVPTX_TARGET
111  LLVMInitializeNVPTXTarget();
112  LLVMInitializeNVPTXTargetInfo();
113  LLVMInitializeNVPTXTargetMC();
114  LLVMInitializeNVPTXAsmPrinter();
115 #endif
116  });
117 }
118 
119 NVVMTargetAttr SerializeGPUModuleBase::getTarget() const { return target; }
120 
122 
124  return fileList;
125 }
126 
127 // Try to append `libdevice` from a CUDA toolkit installation.
129  StringRef pathRef = getToolkitPath();
130  if (!pathRef.empty()) {
132  path.insert(path.begin(), pathRef.begin(), pathRef.end());
133  pathRef = StringRef(path.data(), path.size());
134  if (!llvm::sys::fs::is_directory(pathRef)) {
135  getOperation().emitError() << "CUDA path: " << pathRef
136  << " does not exist or is not a directory.\n";
137  return failure();
138  }
139  llvm::sys::path::append(path, "nvvm", "libdevice", "libdevice.10.bc");
140  pathRef = StringRef(path.data(), path.size());
141  if (!llvm::sys::fs::is_regular_file(pathRef)) {
142  getOperation().emitError() << "LibDevice path: " << pathRef
143  << " does not exist or is not a file.\n";
144  return failure();
145  }
146  fileList.push_back(pathRef.str());
147  }
148  return success();
149 }
150 
151 std::optional<SmallVector<std::unique_ptr<llvm::Module>>>
154  if (failed(loadBitcodeFilesFromList(module.getContext(), fileList, bcFiles,
155  true)))
156  return std::nullopt;
157  return std::move(bcFiles);
158 }
159 
160 namespace {
161 class NVPTXSerializer : public SerializeGPUModuleBase {
162 public:
163  NVPTXSerializer(Operation &module, NVVMTargetAttr target,
164  const gpu::TargetOptions &targetOptions);
165 
166  /// Returns the GPU module op being serialized.
167  gpu::GPUModuleOp getOperation();
168 
169  /// Compiles PTX to cubin using `ptxas`.
170  std::optional<SmallVector<char, 0>>
171  compileToBinary(const std::string &ptxCode);
172 
173  /// Compiles PTX to cubin using the `nvptxcompiler` library.
174  std::optional<SmallVector<char, 0>>
175  compileToBinaryNVPTX(const std::string &ptxCode);
176 
177  /// Serializes the LLVM module to an object format, depending on the
178  /// compilation target selected in target options.
179  std::optional<SmallVector<char, 0>>
180  moduleToObject(llvm::Module &llvmModule) override;
181 
182 private:
183  using TmpFile = std::pair<llvm::SmallString<128>, llvm::FileRemover>;
184 
185  /// Creates a temp file.
186  std::optional<TmpFile> createTemp(StringRef name, StringRef suffix);
187 
188  /// Finds the `tool` path, where `tool` is the name of the binary to search,
189  /// i.e. `ptxas` or `fatbinary`. The search order is:
190  /// 1. The toolkit path in `targetOptions`.
191  /// 2. In the system PATH.
192  /// 3. The path from `getCUDAToolkitPath()`.
193  std::optional<std::string> findTool(StringRef tool);
194 
195  /// Target options.
196  gpu::TargetOptions targetOptions;
197 };
198 } // namespace
199 
200 NVPTXSerializer::NVPTXSerializer(Operation &module, NVVMTargetAttr target,
201  const gpu::TargetOptions &targetOptions)
202  : SerializeGPUModuleBase(module, target, targetOptions),
203  targetOptions(targetOptions) {}
204 
205 std::optional<NVPTXSerializer::TmpFile>
206 NVPTXSerializer::createTemp(StringRef name, StringRef suffix) {
207  llvm::SmallString<128> filename;
208  std::error_code ec =
209  llvm::sys::fs::createTemporaryFile(name, suffix, filename);
210  if (ec) {
211  getOperation().emitError() << "Couldn't create the temp file: `" << filename
212  << "`, error message: " << ec.message();
213  return std::nullopt;
214  }
215  return TmpFile(filename, llvm::FileRemover(filename.c_str()));
216 }
217 
218 gpu::GPUModuleOp NVPTXSerializer::getOperation() {
219  return dyn_cast<gpu::GPUModuleOp>(&SerializeGPUModuleBase::getOperation());
220 }
221 
222 std::optional<std::string> NVPTXSerializer::findTool(StringRef tool) {
223  // Find the `tool` path.
224  // 1. Check the toolkit path given in the command line.
225  StringRef pathRef = targetOptions.getToolkitPath();
227  if (!pathRef.empty()) {
228  path.insert(path.begin(), pathRef.begin(), pathRef.end());
229  llvm::sys::path::append(path, "bin", tool);
230  if (llvm::sys::fs::can_execute(path))
231  return StringRef(path.data(), path.size()).str();
232  }
233 
234  // 2. Check PATH.
235  if (std::optional<std::string> toolPath =
236  llvm::sys::Process::FindInEnvPath("PATH", tool))
237  return *toolPath;
238 
239  // 3. Check `getCUDAToolkitPath()`.
240  pathRef = getCUDAToolkitPath();
241  path.clear();
242  if (!pathRef.empty()) {
243  path.insert(path.begin(), pathRef.begin(), pathRef.end());
244  llvm::sys::path::append(path, "bin", tool);
245  if (llvm::sys::fs::can_execute(path))
246  return StringRef(path.data(), path.size()).str();
247  }
248  getOperation().emitError()
249  << "Couldn't find the `" << tool
250  << "` binary. Please specify the toolkit "
251  "path, add the compiler to $PATH, or set one of the environment "
252  "variables in `NVVM::getCUDAToolkitPath()`.";
253  return std::nullopt;
254 }
255 
256 // TODO: clean this method & have a generic tool driver or never emit binaries
257 // with this mechanism and let another stage take care of it.
258 std::optional<SmallVector<char, 0>>
259 NVPTXSerializer::compileToBinary(const std::string &ptxCode) {
260  // Determine if the serializer should create a fatbinary with the PTX embeded
261  // or a simple CUBIN binary.
262  const bool createFatbin =
263  targetOptions.getCompilationTarget() == gpu::CompilationTarget::Fatbin;
264 
265  // Find the `ptxas` & `fatbinary` tools.
266  std::optional<std::string> ptxasCompiler = findTool("ptxas");
267  if (!ptxasCompiler)
268  return std::nullopt;
269  std::optional<std::string> fatbinaryTool;
270  if (createFatbin) {
271  fatbinaryTool = findTool("fatbinary");
272  if (!fatbinaryTool)
273  return std::nullopt;
274  }
275  Location loc = getOperation().getLoc();
276 
277  // Base name for all temp files: mlir-<module name>-<target triple>-<chip>.
278  std::string basename =
279  llvm::formatv("mlir-{0}-{1}-{2}", getOperation().getNameAttr().getValue(),
280  getTarget().getTriple(), getTarget().getChip());
281 
282  // Create temp files:
283  std::optional<TmpFile> ptxFile = createTemp(basename, "ptx");
284  if (!ptxFile)
285  return std::nullopt;
286  std::optional<TmpFile> logFile = createTemp(basename, "log");
287  if (!logFile)
288  return std::nullopt;
289  std::optional<TmpFile> binaryFile = createTemp(basename, "bin");
290  if (!binaryFile)
291  return std::nullopt;
292  TmpFile cubinFile;
293  if (createFatbin) {
294  Twine cubinFilename = ptxFile->first + ".cubin";
295  cubinFile = TmpFile(cubinFilename.str(), llvm::FileRemover(cubinFilename));
296  } else {
297  cubinFile.first = binaryFile->first;
298  }
299 
300  std::error_code ec;
301  // Dump the PTX to a temp file.
302  {
303  llvm::raw_fd_ostream ptxStream(ptxFile->first, ec);
304  if (ec) {
305  emitError(loc) << "Couldn't open the file: `" << ptxFile->first
306  << "`, error message: " << ec.message();
307  return std::nullopt;
308  }
309  ptxStream << ptxCode;
310  if (ptxStream.has_error()) {
311  emitError(loc) << "An error occurred while writing the PTX to: `"
312  << ptxFile->first << "`.";
313  return std::nullopt;
314  }
315  ptxStream.flush();
316  }
317 
318  // Command redirects.
319  std::optional<StringRef> redirects[] = {
320  std::nullopt,
321  logFile->first,
322  logFile->first,
323  };
324 
325  // Get any extra args passed in `targetOptions`.
326  std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>> cmdOpts =
327  targetOptions.tokenizeCmdOptions();
328 
329  // Create ptxas args.
330  std::string optLevel = std::to_string(this->optLevel);
331  SmallVector<StringRef, 12> ptxasArgs(
332  {StringRef("ptxas"), StringRef("-arch"), getTarget().getChip(),
333  StringRef(ptxFile->first), StringRef("-o"), StringRef(cubinFile.first),
334  "--opt-level", optLevel});
335 
336  bool useFatbin32 = false;
337  for (const auto *cArg : cmdOpts.second) {
338  // All `cmdOpts` are for `ptxas` except `-32` which passes `-32` to
339  // `fatbinary`, indicating a 32-bit target. By default a 64-bit target is
340  // assumed.
341  if (StringRef arg(cArg); arg != "-32")
342  ptxasArgs.push_back(arg);
343  else
344  useFatbin32 = true;
345  }
346 
347  // Create the `fatbinary` args.
348  StringRef chip = getTarget().getChip();
349  // Remove the arch prefix to obtain the compute capability.
350  chip.consume_front("sm_"), chip.consume_front("compute_");
351  // Embed the cubin object.
352  std::string cubinArg =
353  llvm::formatv("--image3=kind=elf,sm={0},file={1}", chip, cubinFile.first)
354  .str();
355  // Embed the PTX file so the driver can JIT if needed.
356  std::string ptxArg =
357  llvm::formatv("--image3=kind=ptx,sm={0},file={1}", chip, ptxFile->first)
358  .str();
359  SmallVector<StringRef, 6> fatbinArgs({StringRef("fatbinary"),
360  useFatbin32 ? "-32" : "-64", cubinArg,
361  ptxArg, "--create", binaryFile->first});
362 
363  // Dump tool invocation commands.
364 #define DEBUG_TYPE "serialize-to-binary"
365  LLVM_DEBUG({
366  llvm::dbgs() << "Tool invocation for module: "
367  << getOperation().getNameAttr() << "\n";
368  llvm::interleave(ptxasArgs, llvm::dbgs(), " ");
369  llvm::dbgs() << "\n";
370  if (createFatbin) {
371  llvm::interleave(fatbinArgs, llvm::dbgs(), " ");
372  llvm::dbgs() << "\n";
373  }
374  });
375 #undef DEBUG_TYPE
376 
377  // Helper function for printing tool error logs.
378  std::string message;
379  auto emitLogError =
380  [&](StringRef toolName) -> std::optional<SmallVector<char, 0>> {
381  if (message.empty()) {
382  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> toolStderr =
383  llvm::MemoryBuffer::getFile(logFile->first);
384  if (toolStderr)
385  emitError(loc) << toolName << " invocation failed. Log:\n"
386  << toolStderr->get()->getBuffer();
387  else
388  emitError(loc) << toolName << " invocation failed.";
389  return std::nullopt;
390  }
391  emitError(loc) << toolName
392  << " invocation failed, error message: " << message;
393  return std::nullopt;
394  };
395 
396  // Invoke PTXAS.
397  if (llvm::sys::ExecuteAndWait(ptxasCompiler.value(), ptxasArgs,
398  /*Env=*/std::nullopt,
399  /*Redirects=*/redirects,
400  /*SecondsToWait=*/0,
401  /*MemoryLimit=*/0,
402  /*ErrMsg=*/&message))
403  return emitLogError("`ptxas`");
404 
405  // Invoke `fatbin`.
406  message.clear();
407  if (createFatbin && llvm::sys::ExecuteAndWait(*fatbinaryTool, fatbinArgs,
408  /*Env=*/std::nullopt,
409  /*Redirects=*/redirects,
410  /*SecondsToWait=*/0,
411  /*MemoryLimit=*/0,
412  /*ErrMsg=*/&message))
413  return emitLogError("`fatbinary`");
414 
415 // Dump the output of the tools, helpful if the verbose flag was passed.
416 #define DEBUG_TYPE "serialize-to-binary"
417  LLVM_DEBUG({
418  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> logBuffer =
419  llvm::MemoryBuffer::getFile(logFile->first);
420  if (logBuffer && !(*logBuffer)->getBuffer().empty()) {
421  llvm::dbgs() << "Output:\n" << (*logBuffer)->getBuffer() << "\n";
422  llvm::dbgs().flush();
423  }
424  });
425 #undef DEBUG_TYPE
426 
427  // Read the fatbin.
428  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> binaryBuffer =
429  llvm::MemoryBuffer::getFile(binaryFile->first);
430  if (!binaryBuffer) {
431  emitError(loc) << "Couldn't open the file: `" << binaryFile->first
432  << "`, error message: " << binaryBuffer.getError().message();
433  return std::nullopt;
434  }
435  StringRef fatbin = (*binaryBuffer)->getBuffer();
436  return SmallVector<char, 0>(fatbin.begin(), fatbin.end());
437 }
438 
439 #if MLIR_ENABLE_NVPTXCOMPILER
440 #include "nvPTXCompiler.h"
441 
442 #define RETURN_ON_NVPTXCOMPILER_ERROR(expr) \
443  do { \
444  if (auto status = (expr)) { \
445  emitError(loc) << llvm::Twine(#expr).concat(" failed with error code ") \
446  << status; \
447  return std::nullopt; \
448  } \
449  } while (false)
450 
451 std::optional<SmallVector<char, 0>>
452 NVPTXSerializer::compileToBinaryNVPTX(const std::string &ptxCode) {
453  Location loc = getOperation().getLoc();
454  nvPTXCompilerHandle compiler = nullptr;
455  nvPTXCompileResult status;
456  size_t logSize;
457 
458  // Create the options.
459  std::string optLevel = std::to_string(this->optLevel);
460  std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>> cmdOpts =
461  targetOptions.tokenizeCmdOptions();
462  cmdOpts.second.append(
463  {"-arch", getTarget().getChip().data(), "--opt-level", optLevel.c_str()});
464 
465  // Create the compiler handle.
466  RETURN_ON_NVPTXCOMPILER_ERROR(
467  nvPTXCompilerCreate(&compiler, ptxCode.size(), ptxCode.c_str()));
468 
469  // Try to compile the binary.
470  status = nvPTXCompilerCompile(compiler, cmdOpts.second.size(),
471  cmdOpts.second.data());
472 
473  // Check if compilation failed.
474  if (status != NVPTXCOMPILE_SUCCESS) {
475  RETURN_ON_NVPTXCOMPILER_ERROR(
476  nvPTXCompilerGetErrorLogSize(compiler, &logSize));
477  if (logSize != 0) {
478  SmallVector<char> log(logSize + 1, 0);
479  RETURN_ON_NVPTXCOMPILER_ERROR(
480  nvPTXCompilerGetErrorLog(compiler, log.data()));
481  emitError(loc) << "NVPTX compiler invocation failed, error log: "
482  << log.data();
483  } else
484  emitError(loc) << "NVPTX compiler invocation failed with error code: "
485  << status;
486  return std::nullopt;
487  }
488 
489  // Retrieve the binary.
490  size_t elfSize;
491  RETURN_ON_NVPTXCOMPILER_ERROR(
492  nvPTXCompilerGetCompiledProgramSize(compiler, &elfSize));
493  SmallVector<char, 0> binary(elfSize, 0);
494  RETURN_ON_NVPTXCOMPILER_ERROR(
495  nvPTXCompilerGetCompiledProgram(compiler, (void *)binary.data()));
496 
497 // Dump the log of the compiler, helpful if the verbose flag was passed.
498 #define DEBUG_TYPE "serialize-to-binary"
499  LLVM_DEBUG({
500  RETURN_ON_NVPTXCOMPILER_ERROR(
501  nvPTXCompilerGetInfoLogSize(compiler, &logSize));
502  if (logSize != 0) {
503  SmallVector<char> log(logSize + 1, 0);
504  RETURN_ON_NVPTXCOMPILER_ERROR(
505  nvPTXCompilerGetInfoLog(compiler, log.data()));
506  llvm::dbgs() << "NVPTX compiler invocation for module: "
507  << getOperation().getNameAttr() << "\n";
508  llvm::dbgs() << "Arguments: ";
509  llvm::interleave(cmdOpts.second, llvm::dbgs(), " ");
510  llvm::dbgs() << "\nOutput\n" << log.data() << "\n";
511  llvm::dbgs().flush();
512  }
513  });
514 #undef DEBUG_TYPE
515  RETURN_ON_NVPTXCOMPILER_ERROR(nvPTXCompilerDestroy(&compiler));
516  return binary;
517 }
518 #endif // MLIR_ENABLE_NVPTXCOMPILER
519 
520 std::optional<SmallVector<char, 0>>
521 NVPTXSerializer::moduleToObject(llvm::Module &llvmModule) {
522  // Return LLVM IR if the compilation target is `offload`.
523 #define DEBUG_TYPE "serialize-to-llvm"
524  LLVM_DEBUG({
525  llvm::dbgs() << "LLVM IR for module: " << getOperation().getNameAttr()
526  << "\n";
527  llvm::dbgs() << llvmModule << "\n";
528  llvm::dbgs().flush();
529  });
530 #undef DEBUG_TYPE
531  if (targetOptions.getCompilationTarget() == gpu::CompilationTarget::Offload)
532  return SerializeGPUModuleBase::moduleToObject(llvmModule);
533 
534 #if !LLVM_HAS_NVPTX_TARGET
535  getOperation()->emitError(
536  "The `NVPTX` target was not built. Please enable it when building LLVM.");
537  return std::nullopt;
538 #endif // LLVM_HAS_NVPTX_TARGET
539 
540  // Emit PTX code.
541  std::optional<llvm::TargetMachine *> targetMachine =
542  getOrCreateTargetMachine();
543  if (!targetMachine) {
544  getOperation().emitError() << "Target Machine unavailable for triple "
545  << triple << ", can't optimize with LLVM\n";
546  return std::nullopt;
547  }
548  std::optional<std::string> serializedISA =
549  translateToISA(llvmModule, **targetMachine);
550  if (!serializedISA) {
551  getOperation().emitError() << "Failed translating the module to ISA.";
552  return std::nullopt;
553  }
554 #define DEBUG_TYPE "serialize-to-isa"
555  LLVM_DEBUG({
556  llvm::dbgs() << "PTX for module: " << getOperation().getNameAttr() << "\n";
557  llvm::dbgs() << *serializedISA << "\n";
558  llvm::dbgs().flush();
559  });
560 #undef DEBUG_TYPE
561 
562  // Return PTX if the compilation target is `assembly`.
563  if (targetOptions.getCompilationTarget() ==
564  gpu::CompilationTarget::Assembly) {
565  // Make sure to include the null terminator.
566  StringRef bin(serializedISA->c_str(), serializedISA->size() + 1);
567  return SmallVector<char, 0>(bin.begin(), bin.end());
568  }
569 
570  // Compile to binary.
571 #if MLIR_ENABLE_NVPTXCOMPILER
572  return compileToBinaryNVPTX(*serializedISA);
573 #else
574  return compileToBinary(*serializedISA);
575 #endif // MLIR_ENABLE_NVPTXCOMPILER
576 }
577 
578 std::optional<SmallVector<char, 0>>
579 NVVMTargetAttrImpl::serializeToObject(Attribute attribute, Operation *module,
580  const gpu::TargetOptions &options) const {
581  assert(module && "The module must be non null.");
582  if (!module)
583  return std::nullopt;
584  if (!mlir::isa<gpu::GPUModuleOp>(module)) {
585  module->emitError("Module must be a GPU module.");
586  return std::nullopt;
587  }
588  NVPTXSerializer serializer(*module, cast<NVVMTargetAttr>(attribute), options);
589  serializer.init();
590  return serializer.run();
591 }
592 
593 Attribute
594 NVVMTargetAttrImpl::createObject(Attribute attribute,
595  const SmallVector<char, 0> &object,
596  const gpu::TargetOptions &options) const {
597  auto target = cast<NVVMTargetAttr>(attribute);
598  gpu::CompilationTarget format = options.getCompilationTarget();
599  DictionaryAttr objectProps;
600  Builder builder(attribute.getContext());
601  if (format == gpu::CompilationTarget::Assembly)
602  objectProps = builder.getDictionaryAttr(
603  {builder.getNamedAttr("O", builder.getI32IntegerAttr(target.getO()))});
604  return builder.getAttr<gpu::ObjectAttr>(
605  attribute, format,
606  builder.getStringAttr(StringRef(object.data(), object.size())),
607  objectProps);
608 }
#define __DEFAULT_CUDATOOLKIT_PATH__
Definition: Target.cpp:40
static llvm::ManagedStatic< PassManagerOptions > options
Attributes are known-constant values of operations.
Definition: Attributes.h:25
MLIRContext * getContext() const
Return the context this attribute belongs to.
Definition: Attributes.cpp:37
This class is a general helper class for creating context-global objects like types,...
Definition: Builders.h:50
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
void addExtension(std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
virtual std::optional< SmallVector< char, 0 > > moduleToObject(llvm::Module &llvmModule)
Serializes the LLVM IR bitcode to an object file, by default it serializes to LLVM bitcode.
Operation & getOperation()
Returns the operation being serialized.
LogicalResult loadBitcodeFilesFromList(llvm::LLVMContext &context, ArrayRef< std::string > fileList, SmallVector< std::unique_ptr< llvm::Module >> &llvmModules, bool failureOnError=true)
Loads multiple bitcode files.
Operation & module
Module to transform to a binary object.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
void appendDialectRegistry(const DialectRegistry &registry)
Append the contents of the given dialect registry to the registry associated with this context.
Base class for all NVVM serializations from GPU modules into binary strings.
Definition: Utils.h:32
ArrayRef< std::string > getFileList() const
Returns the bitcode files to be loaded.
Definition: Target.cpp:123
SerializeGPUModuleBase(Operation &module, NVVMTargetAttr target, const gpu::TargetOptions &targetOptions={})
Initializes the toolkitPath with the path in targetOptions or if empty with the path in getCUDAToolki...
Definition: Target.cpp:84
SmallVector< std::string > fileList
List of LLVM bitcode files to link to.
Definition: Utils.h:68
NVVMTargetAttr target
NVVM target attribute.
Definition: Utils.h:62
std::string toolkitPath
CUDA toolkit path.
Definition: Utils.h:65
virtual std::optional< SmallVector< std::unique_ptr< llvm::Module > > > loadBitcodeFiles(llvm::Module &module) override
Loads the bitcode files in fileList.
Definition: Target.cpp:152
LogicalResult appendStandardLibs()
Appends nvvm/libdevice.bc into fileList.
Definition: Target.cpp:128
static void init()
Initializes the LLVM NVPTX target by safely calling LLVMInitializeNVPTX* methods if available.
Definition: Target.cpp:106
StringRef getToolkitPath() const
Returns the CUDA toolkit path.
Definition: Target.cpp:121
NVVMTargetAttr getTarget() const
Returns the target attribute.
Definition: Target.cpp:119
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
MLIRContext * getContext()
Return the context this operation is associated with.
Definition: Operation.h:216
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
Definition: Operation.cpp:268
This class serves as an opaque interface for passing options to the TargetAttrInterface methods.
void registerNVVMTargetInterfaceExternalModels(DialectRegistry &registry)
Registers the TargetAttrInterface for the #nvvm.target attribute in the given registry.
Definition: Target.cpp:59
StringRef getCUDAToolkitPath()
Searches & returns the path CUDA toolkit path, the search order is:
Definition: Target.cpp:74
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.