MLIR  20.0.0git
Utils.h
Go to the documentation of this file.
1 //===- Utils.h - MLIR NVVM target utils -------------------------*- 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 declares NVVM target related utility classes and functions.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_TARGET_LLVM_NVVM_UTILS_H
14 #define MLIR_TARGET_LLVM_NVVM_UTILS_H
15 
19 
20 namespace mlir {
21 namespace NVVM {
22 /// Searches & returns the path CUDA toolkit path, the search order is:
23 /// 1. The `CUDA_ROOT` environment variable.
24 /// 2. The `CUDA_HOME` environment variable.
25 /// 3. The `CUDA_PATH` environment variable.
26 /// 4. The CUDA toolkit path detected by CMake.
27 /// 5. Returns an empty string.
28 StringRef getCUDAToolkitPath();
29 
30 /// Base class for all NVVM serializations from GPU modules into binary strings.
31 /// By default this class serializes into LLVM bitcode.
33 public:
34  /// Initializes the `toolkitPath` with the path in `targetOptions` or if empty
35  /// with the path in `getCUDAToolkitPath`.
37  const gpu::TargetOptions &targetOptions = {});
38 
39  /// Initializes the LLVM NVPTX target by safely calling `LLVMInitializeNVPTX*`
40  /// methods if available.
41  static void init();
42 
43  /// Returns the target attribute.
44  NVVMTargetAttr getTarget() const;
45 
46  /// Returns the CUDA toolkit path.
47  StringRef getToolkitPath() const;
48 
49  /// Returns the bitcode files to be loaded.
51 
52  /// Appends `nvvm/libdevice.bc` into `fileList`. Returns failure if the
53  /// library couldn't be found.
54  LogicalResult appendStandardLibs();
55 
56  /// Loads the bitcode files in `fileList`.
57  virtual std::optional<SmallVector<std::unique_ptr<llvm::Module>>>
58  loadBitcodeFiles(llvm::Module &module) override;
59 
60 protected:
61  /// NVVM target attribute.
62  NVVMTargetAttr target;
63 
64  /// CUDA toolkit path.
65  std::string toolkitPath;
66 
67  /// List of LLVM bitcode files to link to.
69 };
70 } // namespace NVVM
71 } // namespace mlir
72 
73 #endif // MLIR_TARGET_LLVM_NVVM_UTILS_H
Utility base class for transforming operations into binary objects, by default it returns the seriali...
Operation & module
Module to transform to a binary object.
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:128
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:85
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:157
LogicalResult appendStandardLibs()
Appends nvvm/libdevice.bc into fileList.
Definition: Target.cpp:133
static void init()
Initializes the LLVM NVPTX target by safely calling LLVMInitializeNVPTX* methods if available.
Definition: Target.cpp:111
StringRef getToolkitPath() const
Returns the CUDA toolkit path.
Definition: Target.cpp:126
NVVMTargetAttr getTarget() const
Returns the target attribute.
Definition: Target.cpp:124
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
This class serves as an opaque interface for passing options to the TargetAttrInterface methods.
StringRef getCUDAToolkitPath()
Searches & returns the path CUDA toolkit path, the search order is:
Definition: Target.cpp:75
Include the generated interface declarations.