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 all commits
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
23 changes: 19 additions & 4 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 @@ -55,11 +56,13 @@ class TargetOptions {
} CompilationTarget;

/// Constructor initializing the toolkit path, the list of files to link to,
/// extra command line options & the compilation target. The default
/// compilation target is `binary`.
/// extra command line options, the compilation target and a callback for
/// obtaining the parent symbol table. The default compilation target is
/// `binOrFatbin`.
TargetOptions(StringRef toolkitPath = {},
ArrayRef<std::string> linkFiles = {}, StringRef cmdOptions = {},
CompilationTarget compilationTarget = binOrFatbin);
CompilationTarget compilationTarget = binOrFatbin,
function_ref<SymbolTable *()> getSymbolTableCallback = {});

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

/// Returns the result of the `getSymbolTableCallback` callback or a nullptr
/// if no callback was provided.
/// Note: The callback itself can return nullptr. It is up to the target how
/// to react to getting a nullptr, e.g., emitting an error or constructing the
/// table.
SymbolTable *getSymbolTable() 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,
function_ref<SymbolTable *()> getSymbolTableCallback = {});

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

/// Callback for obtaining the parent symbol table of all the GPU modules
/// being serialized.
function_ref<SymbolTable *()> getSymbolTableCallback;

Choose a reason for hiding this comment

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

Is this safe?
function_ref says:
/// An efficient, type-erasing, non-owning reference to a callable. This is
/// intended for use as the type of a function parameter that is not used
/// after the function in question returns.
///
/// This class does not own the callable, so it is not in general safe to store
/// a function_ref.
So who owns = {} here?

Choose a reason for hiding this comment

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

I'm seeing clang-tidy warnings/crashes in similar usage - #71363

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It can be changed. However, this class is only meant to be used as a temp to pass arguments with the caller being in charge of making sure the object remains alive and not really to store data.


private:
TypeID typeID;
};
Expand Down
8 changes: 5 additions & 3 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 Down
24 changes: 14 additions & 10 deletions mlir/lib/Dialect/GPU/IR/GPUDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1979,20 +1979,20 @@ gpu::SelectObjectAttr::verify(function_ref<InFlightDiagnostic()> emitError,
// GPU target options
//===----------------------------------------------------------------------===//

TargetOptions::TargetOptions(StringRef toolkitPath,
ArrayRef<std::string> linkFiles,
StringRef cmdOptions,
CompilationTarget compilationTarget)
TargetOptions::TargetOptions(
StringRef toolkitPath, ArrayRef<std::string> linkFiles,
StringRef cmdOptions, CompilationTarget compilationTarget,
function_ref<SymbolTable *()> getSymbolTableCallback)
: TargetOptions(TypeID::get<TargetOptions>(), toolkitPath, linkFiles,
cmdOptions, compilationTarget) {}
cmdOptions, compilationTarget, getSymbolTableCallback) {}

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

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

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

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

SymbolTable *TargetOptions::getSymbolTable() const {
return getSymbolTableCallback ? getSymbolTableCallback() : nullptr;
}

std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>>
TargetOptions::tokenizeCmdOptions() const {
std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>> options;
Expand Down
19 changes: 18 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,26 @@ void GpuModuleToBinaryPass::runOnOperation() {
.Default(-1);
if (targetFormat == -1)
getOperation()->emitError() << "Invalid format specified.";

// Lazy symbol table builder callback.
std::optional<SymbolTable> parentTable;
auto lazyTableBuilder = [&]() -> SymbolTable * {
// Build the table if it has not been built.
if (!parentTable) {
Operation *table = SymbolTable::getNearestSymbolTable(getOperation());
// It's up to the target attribute to determine if failing to find a
// symbol table is an error.
if (!table)
return nullptr;
parentTable = SymbolTable(table);
}
return &parentTable.value();
};

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