16 #include "mlir/Config/mlir-config.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/ADT/StringSwitch.h"
33 #define GEN_PASS_DEF_GPUMODULETOBINARYPASS
34 #include "mlir/Dialect/GPU/Transforms/Passes.h.inc"
38 class GpuModuleToBinaryPass
39 :
public impl::GpuModuleToBinaryPassBase<GpuModuleToBinaryPass> {
43 void runOnOperation()
final;
47 void GpuModuleToBinaryPass::getDependentDialects(
50 registry.
insert<gpu::GPUDialect>();
51 registry.
insert<LLVM::LLVMDialect>();
52 #if MLIR_ENABLE_CUDA_CONVERSIONS
53 registry.
insert<NVVM::NVVMDialect>();
55 #if MLIR_ENABLE_ROCM_CONVERSIONS
56 registry.
insert<ROCDL::ROCDLDialect>();
58 registry.
insert<spirv::SPIRVDialect>();
61 void GpuModuleToBinaryPass::runOnOperation() {
65 .Cases(
"offloading",
"llvm", CompilationTarget::Offload)
66 .Cases(
"assembly",
"isa", CompilationTarget::Assembly)
67 .Cases(
"binary",
"bin", CompilationTarget::Binary)
68 .Cases(
"fatbinary",
"fatbin", CompilationTarget::Fatbin)
69 .Default(std::nullopt);
71 getOperation()->emitError() <<
"Invalid format specified.";
74 std::optional<SymbolTable> parentTable;
85 return &parentTable.value();
88 TargetOptions targetOptions(toolkitPath, linkFiles, cmdOptions, *targetFormat,
92 offloadingHandler ? dyn_cast<OffloadingLLVMTranslationAttrInterface>(
93 offloadingHandler.getValue())
94 : OffloadingLLVMTranslationAttrInterface(
nullptr),
96 return signalPassFailure();
101 OffloadingLLVMTranslationAttrInterface handler,
106 if (!op.getTargetsAttr())
107 return op.
emitError(
"the module has no target attributes");
109 for (
auto targetAttr : op.getTargetsAttr()) {
110 assert(targetAttr &&
"Target attribute cannot be null.");
111 auto target = dyn_cast<gpu::TargetAttrInterface>(targetAttr);
113 "Target attribute doesn't implements `TargetAttrInterface`.");
114 std::optional<SmallVector<char, 0>> serializedModule =
115 target.serializeToObject(op, targetOptions);
116 if (!serializedModule) {
117 op.
emitError(
"An error happened while serializing the module.");
121 Attribute object = target.createObject(*serializedModule, targetOptions);
123 op.
emitError(
"An error happened while creating the object.");
126 objects.push_back(
object);
128 if (
auto moduleHandler =
129 dyn_cast_or_null<OffloadingLLVMTranslationAttrInterface>(
130 op.getOffloadingHandlerAttr());
131 !handler && moduleHandler)
132 handler = moduleHandler;
133 builder.setInsertionPointAfter(op);
134 builder.create<gpu::BinaryOp>(op.
getLoc(), op.
getName(), handler,
135 builder.getArrayAttr(objects));
142 Operation *op, OffloadingLLVMTranslationAttrInterface handler,
145 for (
Block &block : region.getBlocks())
147 llvm::make_early_inc_range(block.getOps<GPUModuleOp>()))
148 if (
failed(moduleSerializer(module, handler, targetOptions)))
static MLIRContext * getContext(OpFoldResult val)
Attributes are known-constant values of operations.
Block represents an ordered list of Operations.
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
This class helps build Operations.
Operation is the basic unit of execution within MLIR.
MLIRContext * getContext()
Return the context this operation is associated with.
Location getLoc()
The source location the operation was defined or derived from.
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
OperationName getName()
The name of an operation is the key identifier for it.
void erase()
Remove this operation from its parent block and delete it.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
This class allows for representing and managing the symbol table used by operations with the 'SymbolT...
static Operation * getNearestSymbolTable(Operation *from)
Returns the nearest symbol table from a given operation from.
This class serves as an opaque interface for passing options to the TargetAttrInterface methods.
LogicalResult transformGpuModulesToBinaries(Operation *op, OffloadingLLVMTranslationAttrInterface handler=nullptr, const gpu::TargetOptions &options={})
Searches for all GPU modules in op and transforms them into GPU binary operations.
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
This class represents an efficient way to signal success or failure.