Skip to content

Commit a036ce6

Browse files
committed
Changing the support for loading LLVM libraries to not rely on path to the filesystem
1 parent 8217c2e commit a036ce6

File tree

9 files changed

+63
-54
lines changed

9 files changed

+63
-54
lines changed

mlir/include/mlir/Dialect/GPU/IR/CompilationInterfaces.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class TargetOptions {
5151
/// obtaining the parent symbol table. The default compilation target is
5252
/// `Fatbin`.
5353
TargetOptions(
54-
StringRef toolkitPath = {}, ArrayRef<std::string> linkFiles = {},
54+
StringRef toolkitPath = {}, ArrayRef<Attribute> librariesToLink = {},
5555
StringRef cmdOptions = {}, StringRef elfSection = {},
5656
CompilationTarget compilationTarget = getDefaultCompilationTarget(),
5757
function_ref<SymbolTable *()> getSymbolTableCallback = {},
@@ -66,8 +66,8 @@ class TargetOptions {
6666
/// Returns the toolkit path.
6767
StringRef getToolkitPath() const;
6868

69-
/// Returns the files to link to.
70-
ArrayRef<std::string> getLinkFiles() const;
69+
/// Returns the LLVM libraries to link to.
70+
ArrayRef<Attribute> getLibrariesToLink() const;
7171

7272
/// Returns the command line options.
7373
StringRef getCmdOptions() const;
@@ -113,7 +113,7 @@ class TargetOptions {
113113
/// appropiate value: ie. `TargetOptions(TypeID::get<DerivedClass>())`.
114114
TargetOptions(
115115
TypeID typeID, StringRef toolkitPath = {},
116-
ArrayRef<std::string> linkFiles = {}, StringRef cmdOptions = {},
116+
ArrayRef<Attribute> librariesToLink = {}, StringRef cmdOptions = {},
117117
StringRef elfSection = {},
118118
CompilationTarget compilationTarget = getDefaultCompilationTarget(),
119119
function_ref<SymbolTable *()> getSymbolTableCallback = {},
@@ -126,7 +126,7 @@ class TargetOptions {
126126
std::string toolkitPath;
127127

128128
/// List of files to link with the LLVM module.
129-
SmallVector<std::string> linkFiles;
129+
SmallVector<Attribute> librariesToLink;
130130

131131
/// An optional set of command line options to be used by the compilation
132132
/// process.

mlir/include/mlir/Target/LLVM/ModuleToObject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class ModuleToObject {
8383

8484
/// Loads multiple bitcode files.
8585
LogicalResult loadBitcodeFilesFromList(
86-
llvm::LLVMContext &context, ArrayRef<std::string> fileList,
86+
llvm::LLVMContext &context, ArrayRef<Attribute> librariesToLink,
8787
SmallVector<std::unique_ptr<llvm::Module>> &llvmModules,
8888
bool failureOnError = true);
8989

mlir/include/mlir/Target/LLVM/NVVM/Utils.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ class SerializeGPUModuleBase : public LLVM::ModuleToObject {
4646
/// Returns the CUDA toolkit path.
4747
StringRef getToolkitPath() const;
4848

49-
/// Returns the bitcode files to be loaded.
50-
ArrayRef<std::string> getFileList() const;
49+
/// Returns the bitcode libraries to be linked into the gpu module after
50+
/// translation to LLVM IR.
51+
ArrayRef<Attribute> getLibrariesToLink() const;
5152

52-
/// Appends `nvvm/libdevice.bc` into `fileList`. Returns failure if the
53+
/// Appends `nvvm/libdevice.bc` into `librariesToLink`. Returns failure if the
5354
/// library couldn't be found.
5455
LogicalResult appendStandardLibs();
5556

56-
/// Loads the bitcode files in `fileList`.
57+
/// Loads the bitcode files in `librariesToLink`.
5758
virtual std::optional<SmallVector<std::unique_ptr<llvm::Module>>>
5859
loadBitcodeFiles(llvm::Module &module) override;
5960

@@ -64,8 +65,10 @@ class SerializeGPUModuleBase : public LLVM::ModuleToObject {
6465
/// CUDA toolkit path.
6566
std::string toolkitPath;
6667

67-
/// List of LLVM bitcode files to link to.
68-
SmallVector<std::string> fileList;
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.
71+
SmallVector<Attribute> librariesToLink;
6972
};
7073
} // namespace NVVM
7174
} // namespace mlir

mlir/include/mlir/Target/LLVM/ROCDL/Utils.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "mlir/Dialect/GPU/IR/CompilationInterfaces.h"
1717
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
1818
#include "mlir/Dialect/LLVMIR/ROCDLDialect.h"
19+
#include "mlir/IR/Attributes.h"
1920
#include "mlir/Support/LLVM.h"
2021
#include "mlir/Target/LLVM/ModuleToObject.h"
2122

@@ -61,8 +62,8 @@ class SerializeGPUModuleBase : public LLVM::ModuleToObject {
6162
/// Returns the ROCM toolkit path.
6263
StringRef getToolkitPath() const;
6364

64-
/// Returns the bitcode files to be loaded.
65-
ArrayRef<std::string> getFileList() const;
65+
/// Returns the LLVM bitcode libraries to be linked.
66+
ArrayRef<Attribute> getLibrariesToLink() const;
6667

6768
/// Appends standard ROCm device libraries to `fileList`.
6869
LogicalResult appendStandardLibs(AMDGCNLibraries libs);
@@ -107,7 +108,7 @@ class SerializeGPUModuleBase : public LLVM::ModuleToObject {
107108
std::string toolkitPath;
108109

109110
/// List of LLVM bitcode files to link to.
110-
SmallVector<std::string> fileList;
111+
SmallVector<Attribute> librariesToLink;
111112

112113
/// AMD GCN libraries to use when linking, the default is using none.
113114
AMDGCNLibraries deviceLibs = AMDGCNLibraries::None;

mlir/lib/Dialect/GPU/IR/GPUDialect.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2483,30 +2483,30 @@ KernelMetadataAttr KernelTableAttr::lookup(StringAttr key) const {
24832483
//===----------------------------------------------------------------------===//
24842484

24852485
TargetOptions::TargetOptions(
2486-
StringRef toolkitPath, ArrayRef<std::string> linkFiles,
2486+
StringRef toolkitPath, ArrayRef<Attribute> librariesToLink,
24872487
StringRef cmdOptions, StringRef elfSection,
24882488
CompilationTarget compilationTarget,
24892489
function_ref<SymbolTable *()> getSymbolTableCallback,
24902490
function_ref<void(llvm::Module &)> initialLlvmIRCallback,
24912491
function_ref<void(llvm::Module &)> linkedLlvmIRCallback,
24922492
function_ref<void(llvm::Module &)> optimizedLlvmIRCallback,
24932493
function_ref<void(StringRef)> isaCallback)
2494-
: TargetOptions(TypeID::get<TargetOptions>(), toolkitPath, linkFiles,
2494+
: TargetOptions(TypeID::get<TargetOptions>(), toolkitPath, librariesToLink,
24952495
cmdOptions, elfSection, compilationTarget,
24962496
getSymbolTableCallback, initialLlvmIRCallback,
24972497
linkedLlvmIRCallback, optimizedLlvmIRCallback,
24982498
isaCallback) {}
24992499

25002500
TargetOptions::TargetOptions(
2501-
TypeID typeID, StringRef toolkitPath, ArrayRef<std::string> linkFiles,
2501+
TypeID typeID, StringRef toolkitPath, ArrayRef<Attribute> librariesToLink,
25022502
StringRef cmdOptions, StringRef elfSection,
25032503
CompilationTarget compilationTarget,
25042504
function_ref<SymbolTable *()> getSymbolTableCallback,
25052505
function_ref<void(llvm::Module &)> initialLlvmIRCallback,
25062506
function_ref<void(llvm::Module &)> linkedLlvmIRCallback,
25072507
function_ref<void(llvm::Module &)> optimizedLlvmIRCallback,
25082508
function_ref<void(StringRef)> isaCallback)
2509-
: toolkitPath(toolkitPath.str()), linkFiles(linkFiles),
2509+
: toolkitPath(toolkitPath.str()), librariesToLink(librariesToLink),
25102510
cmdOptions(cmdOptions.str()), elfSection(elfSection.str()),
25112511
compilationTarget(compilationTarget),
25122512
getSymbolTableCallback(getSymbolTableCallback),
@@ -2519,7 +2519,9 @@ TypeID TargetOptions::getTypeID() const { return typeID; }
25192519

25202520
StringRef TargetOptions::getToolkitPath() const { return toolkitPath; }
25212521

2522-
ArrayRef<std::string> TargetOptions::getLinkFiles() const { return linkFiles; }
2522+
ArrayRef<Attribute> TargetOptions::getLibrariesToLink() const {
2523+
return librariesToLink;
2524+
}
25232525

25242526
StringRef TargetOptions::getCmdOptions() const { return cmdOptions; }
25252527

mlir/lib/Dialect/GPU/Transforms/ModuleToBinary.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ void GpuModuleToBinaryPass::runOnOperation() {
6868
}
6969
return &parentTable.value();
7070
};
71-
72-
TargetOptions targetOptions(toolkitPath, linkFiles, cmdOptions, elfSection,
71+
SmallVector<Attribute> librariesToLink;
72+
for (const std::string &path : linkFiles)
73+
librariesToLink.push_back(StringAttr::get(&getContext(), path));
74+
TargetOptions targetOptions(toolkitPath, librariesToLink, cmdOptions, elfSection,
7375
*targetFormat, lazyTableBuilder);
7476
if (failed(transformGpuModulesToBinaries(
7577
getOperation(), OffloadingLLVMTranslationAttrInterface(nullptr),

mlir/lib/Target/LLVM/ModuleToObject.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,29 @@ ModuleToObject::loadBitcodeFile(llvm::LLVMContext &context, StringRef path) {
8989
}
9090

9191
LogicalResult ModuleToObject::loadBitcodeFilesFromList(
92-
llvm::LLVMContext &context, ArrayRef<std::string> fileList,
92+
llvm::LLVMContext &context, ArrayRef<Attribute> librariesToLink,
9393
SmallVector<std::unique_ptr<llvm::Module>> &llvmModules,
9494
bool failureOnError) {
95-
for (const std::string &str : fileList) {
96-
// Test if the path exists, if it doesn't abort.
97-
StringRef pathRef = StringRef(str.data(), str.size());
98-
if (!llvm::sys::fs::is_regular_file(pathRef)) {
95+
for (Attribute linkLib : librariesToLink) {
96+
if (auto filePath = dyn_cast<StringAttr>(linkLib)) {
97+
// Test if the path exists, if it doesn't abort.
98+
if (!llvm::sys::fs::is_regular_file(filePath.strref())) {
99+
getOperation().emitError()
100+
<< "File path: " << filePath << " does not exist or is not a file.";
101+
return failure();
102+
}
103+
// Load the file or abort on error.
104+
if (auto bcFile = loadBitcodeFile(context, filePath))
105+
llvmModules.push_back(std::move(bcFile));
106+
else if (failureOnError)
107+
return failure();
108+
continue;
109+
}
110+
if (failureOnError) {
99111
getOperation().emitError()
100-
<< "File path: " << pathRef << " does not exist or is not a file.\n";
112+
<< "Unknown attribute describing LLVM library to load: " << linkLib;
101113
return failure();
102114
}
103-
// Load the file or abort on error.
104-
if (auto bcFile = loadBitcodeFile(context, pathRef))
105-
llvmModules.push_back(std::move(bcFile));
106-
else if (failureOnError)
107-
return failure();
108115
}
109116
return success();
110117
}

mlir/lib/Target/LLVM/NVVM/Target.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,14 @@ SerializeGPUModuleBase::SerializeGPUModuleBase(
9393
targetOptions.getOptimizedLlvmIRCallback(),
9494
targetOptions.getISACallback()),
9595
target(target), toolkitPath(targetOptions.getToolkitPath()),
96-
fileList(targetOptions.getLinkFiles()) {
96+
librariesToLink(targetOptions.getLibrariesToLink()) {
9797

9898
// If `targetOptions` have an empty toolkitPath use `getCUDAToolkitPath`
9999
if (toolkitPath.empty())
100100
toolkitPath = getCUDAToolkitPath();
101101

102102
// Append the files in the target attribute.
103-
if (ArrayAttr files = target.getLink())
104-
for (Attribute attr : files.getValue())
105-
if (auto file = dyn_cast<StringAttr>(attr))
106-
fileList.push_back(file.str());
103+
librariesToLink.append(target.getLink().begin(), target.getLink().end());
107104

108105
// Append libdevice to the files to be loaded.
109106
(void)appendStandardLibs();
@@ -126,8 +123,8 @@ NVVMTargetAttr SerializeGPUModuleBase::getTarget() const { return target; }
126123

127124
StringRef SerializeGPUModuleBase::getToolkitPath() const { return toolkitPath; }
128125

129-
ArrayRef<std::string> SerializeGPUModuleBase::getFileList() const {
130-
return fileList;
126+
ArrayRef<Attribute> SerializeGPUModuleBase::getLibrariesToLink() const {
127+
return librariesToLink;
131128
}
132129

133130
// Try to append `libdevice` from a CUDA toolkit installation.
@@ -149,16 +146,16 @@ LogicalResult SerializeGPUModuleBase::appendStandardLibs() {
149146
<< " does not exist or is not a file.\n";
150147
return failure();
151148
}
152-
fileList.push_back(pathRef.str());
149+
librariesToLink.push_back(StringAttr::get(target.getContext(), pathRef));
153150
}
154151
return success();
155152
}
156153

157154
std::optional<SmallVector<std::unique_ptr<llvm::Module>>>
158155
SerializeGPUModuleBase::loadBitcodeFiles(llvm::Module &module) {
159156
SmallVector<std::unique_ptr<llvm::Module>> bcFiles;
160-
if (failed(loadBitcodeFilesFromList(module.getContext(), fileList, bcFiles,
161-
true)))
157+
if (failed(loadBitcodeFilesFromList(module.getContext(), librariesToLink,
158+
bcFiles, true)))
162159
return std::nullopt;
163160
return std::move(bcFiles);
164161
}

mlir/lib/Target/LLVM/ROCDL/Target.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,14 @@ SerializeGPUModuleBase::SerializeGPUModuleBase(
9797
: ModuleToObject(module, target.getTriple(), target.getChip(),
9898
target.getFeatures(), target.getO()),
9999
target(target), toolkitPath(targetOptions.getToolkitPath()),
100-
fileList(targetOptions.getLinkFiles()) {
100+
librariesToLink(targetOptions.getLibrariesToLink()) {
101101

102102
// If `targetOptions` has an empty toolkitPath use `getROCMPath`
103103
if (toolkitPath.empty())
104104
toolkitPath = getROCMPath();
105105

106106
// Append the files in the target attribute.
107-
if (ArrayAttr files = target.getLink())
108-
for (Attribute attr : files.getValue())
109-
if (auto file = dyn_cast<StringAttr>(attr))
110-
fileList.push_back(file.str());
107+
librariesToLink.append(target.getLink().begin(), target.getLink().end());
111108
}
112109

113110
void SerializeGPUModuleBase::init() {
@@ -128,8 +125,8 @@ ROCDLTargetAttr SerializeGPUModuleBase::getTarget() const { return target; }
128125

129126
StringRef SerializeGPUModuleBase::getToolkitPath() const { return toolkitPath; }
130127

131-
ArrayRef<std::string> SerializeGPUModuleBase::getFileList() const {
132-
return fileList;
128+
ArrayRef<Attribute> SerializeGPUModuleBase::getLibrariesToLink() const {
129+
return librariesToLink;
133130
}
134131

135132
LogicalResult SerializeGPUModuleBase::appendStandardLibs(AMDGCNLibraries libs) {
@@ -160,7 +157,7 @@ LogicalResult SerializeGPUModuleBase::appendStandardLibs(AMDGCNLibraries libs) {
160157
<< " does not exist or is not a file";
161158
return true;
162159
}
163-
fileList.push_back(pathRef.str());
160+
librariesToLink.push_back(StringAttr::get(target.getContext(), pathRef));
164161
path.truncate(baseSize);
165162
return false;
166163
};
@@ -178,13 +175,13 @@ LogicalResult SerializeGPUModuleBase::appendStandardLibs(AMDGCNLibraries libs) {
178175
std::optional<SmallVector<std::unique_ptr<llvm::Module>>>
179176
SerializeGPUModuleBase::loadBitcodeFiles(llvm::Module &module) {
180177
// Return if there are no libs to load.
181-
if (deviceLibs == AMDGCNLibraries::None && fileList.empty())
178+
if (deviceLibs == AMDGCNLibraries::None && librariesToLink.empty())
182179
return SmallVector<std::unique_ptr<llvm::Module>>();
183180
if (failed(appendStandardLibs(deviceLibs)))
184181
return std::nullopt;
185182
SmallVector<std::unique_ptr<llvm::Module>> bcFiles;
186-
if (failed(loadBitcodeFilesFromList(module.getContext(), fileList, bcFiles,
187-
true)))
183+
if (failed(loadBitcodeFilesFromList(module.getContext(), librariesToLink,
184+
bcFiles, true)))
188185
return std::nullopt;
189186
return std::move(bcFiles);
190187
}

0 commit comments

Comments
 (0)