Skip to content

[mlir][gpu] Add a symbol table field to TargetOptions and adjust GpuModuleToBinary #65797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions mlir/include/mlir/Dialect/GPU/IR/CompilationInterfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class IRBuilderBase;
}

namespace mlir {
class SymbolTable;
namespace LLVM {
class ModuleTranslation;
}
Expand Down Expand Up @@ -59,7 +60,8 @@ class TargetOptions {
/// compilation target is `binary`.
TargetOptions(StringRef toolkitPath = {},
ArrayRef<std::string> linkFiles = {}, StringRef cmdOptions = {},
CompilationTarget compilationTarget = binOrFatbin);
CompilationTarget compilationTarget = binOrFatbin,
SymbolTable *parentTable = nullptr);

/// Returns the typeID.
TypeID getTypeID() const;
Expand All @@ -80,12 +82,16 @@ class TargetOptions {
/// Returns the compilation target.
CompilationTarget getCompilationTarget() const;

/// Returns the provided parent symbol table.
SymbolTable *getParentTable() const;

protected:
/// Derived classes must use this constructor to initialize `typeID` to the
/// appropiate value: ie. `TargetOptions(TypeID::get<DerivedClass>())`.
TargetOptions(TypeID typeID, StringRef toolkitPath = {},
ArrayRef<std::string> linkFiles = {}, StringRef cmdOptions = {},
CompilationTarget compilationTarget = binOrFatbin);
CompilationTarget compilationTarget = binOrFatbin,
SymbolTable *parentTable = nullptr);

/// Path to the target toolkit.
std::string toolkitPath;
Expand All @@ -100,6 +106,10 @@ class TargetOptions {
/// Compilation process target representation.
CompilationTarget compilationTarget;

/// Parent symbol table of all the GPU modules being serialized. By default
/// this member is null as it is not required by most targets.
SymbolTable *parentTable;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this a callback that would lazy initialize it on demand if the target wants it?
Then we don't need a pass option and we can remove this weird coupling where initializing the pass requires to know if the target wants a SymbolTable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've switched it.


private:
TypeID typeID;
};
Expand Down
13 changes: 9 additions & 4 deletions mlir/include/mlir/Dialect/GPU/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ def GpuModuleToBinaryPass
with an object for every target.

The `format` argument can have the following values:
1. `offloading`, `llvm`: producing an offloading representation.
2. `assembly`, `isa`: producing assembly code.
3. `binary`, `bin`: producing binaries.
1. `offloading`, `llvm`: produces an offloading representation.
2. `assembly`, `isa`: produces assembly code.
3. `binary`, `bin`: produces binaries.
4. `fatbinary`, `fatbin`: produces fatbinaries.
5. `binOrFatbin`: produces bins or fatbins, the target decides which.
}];
let options = [
Option<"offloadingHandler", "handler", "Attribute", "nullptr",
Expand All @@ -78,7 +80,10 @@ def GpuModuleToBinaryPass
Option<"cmdOptions", "opts", "std::string", [{""}],
"Command line options to pass to the tools.">,
Option<"compilationTarget", "format", "std::string", [{"binOrFatbin"}],
"The target representation of the compilation process.">
"The target representation of the compilation process.">,
Option<"constructSymbolTable", "symbol-table", "bool", [{false}],
"Enable building a symbol table enclosing the modules to serialize."
"Most targets can safely disable this.">
];
}

Expand Down
12 changes: 8 additions & 4 deletions mlir/lib/Dialect/GPU/IR/GPUDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1982,17 +1982,19 @@ gpu::SelectObjectAttr::verify(function_ref<InFlightDiagnostic()> emitError,
TargetOptions::TargetOptions(StringRef toolkitPath,
ArrayRef<std::string> linkFiles,
StringRef cmdOptions,
CompilationTarget compilationTarget)
CompilationTarget compilationTarget,
SymbolTable *parentTable)
: TargetOptions(TypeID::get<TargetOptions>(), toolkitPath, linkFiles,
cmdOptions, compilationTarget) {}
cmdOptions, compilationTarget, parentTable) {}

TargetOptions::TargetOptions(TypeID typeID, StringRef toolkitPath,
ArrayRef<std::string> linkFiles,
StringRef cmdOptions,
CompilationTarget compilationTarget)
CompilationTarget compilationTarget,
SymbolTable *parentTable)
: toolkitPath(toolkitPath.str()), linkFiles(linkFiles),
cmdOptions(cmdOptions.str()), compilationTarget(compilationTarget),
typeID(typeID) {}
parentTable(parentTable), typeID(typeID) {}

TypeID TargetOptions::getTypeID() const { return typeID; }

Expand All @@ -2002,6 +2004,8 @@ ArrayRef<std::string> TargetOptions::getLinkFiles() const { return linkFiles; }

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

SymbolTable *TargetOptions::getParentTable() const { return parentTable; }

std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>>
TargetOptions::tokenizeCmdOptions() const {
std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>> options;
Expand Down
9 changes: 8 additions & 1 deletion mlir/lib/Dialect/GPU/Transforms/ModuleToBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,16 @@ void GpuModuleToBinaryPass::runOnOperation() {
.Default(-1);
if (targetFormat == -1)
getOperation()->emitError() << "Invalid format specified.";

std::unique_ptr<SymbolTable> parentTable;
// Create the symbol table if it was requested in the pass options.
if (constructSymbolTable)
parentTable = std::unique_ptr<SymbolTable>(new SymbolTable(getOperation()));

TargetOptions targetOptions(
toolkitPath, linkFiles, cmdOptions,
static_cast<TargetOptions::CompilationTarget>(targetFormat));
static_cast<TargetOptions::CompilationTarget>(targetFormat),
parentTable.get());
if (failed(transformGpuModulesToBinaries(
getOperation(),
offloadingHandler ? dyn_cast<OffloadingLLVMTranslationAttrInterface>(
Expand Down