MLIR 22.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
20namespace mlir {
21namespace 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.
28StringRef getCUDAToolkitPath();
29
30/// Base class for all NVVM serializations from GPU modules into binary strings.
31/// By default this class serializes into LLVM bitcode.
33public:
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 libraries to be linked into the gpu module after
50 /// translation to LLVM IR.
52
53 /// Appends `nvvm/libdevice.bc` into `librariesToLink`. Returns failure if the
54 /// library couldn't be found.
55 LogicalResult appendStandardLibs();
56
57 /// Loads the bitcode files in `librariesToLink`.
58 std::optional<SmallVector<std::unique_ptr<llvm::Module>>>
59 loadBitcodeFiles(llvm::Module &module) override;
60
61protected:
62 /// NVVM target attribute.
63 NVVMTargetAttr target;
64
65 /// CUDA toolkit path.
66 std::string toolkitPath;
67
68 /// List of LLVM bitcode to link into after translation to LLVM IR.
69 /// The attributes can be StringAttr pointing to a file path, or
70 /// a Resource blob pointing to the LLVM bitcode in-memory.
72};
73} // namespace NVVM
74} // namespace mlir
75
76#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.
ArrayRef< Attribute > getLibrariesToLink() const
Returns the bitcode libraries to be linked into the gpu module after translation to LLVM IR.
Definition Target.cpp:139
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:98
NVVMTargetAttr target
NVVM target attribute.
Definition Utils.h:63
std::string toolkitPath
CUDA toolkit path.
Definition Utils.h:66
SmallVector< Attribute > librariesToLink
List of LLVM bitcode to link into after translation to LLVM IR.
Definition Utils.h:71
std::optional< SmallVector< std::unique_ptr< llvm::Module > > > loadBitcodeFiles(llvm::Module &module) override
Loads the bitcode files in librariesToLink.
Definition Target.cpp:197
LogicalResult appendStandardLibs()
Appends nvvm/libdevice.bc into librariesToLink.
Definition Target.cpp:144
static void init()
Initializes the LLVM NVPTX target by safely calling LLVMInitializeNVPTX* methods if available.
Definition Target.cpp:122
StringRef getToolkitPath() const
Returns the CUDA toolkit path.
Definition Target.cpp:137
NVVMTargetAttr getTarget() const
Returns the target attribute.
Definition Target.cpp:135
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:88
Include the generated interface declarations.