25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/StringSwitch.h"
32 #define GEN_PASS_DEF_GPUMODULETOBINARYPASS
33 #include "mlir/Dialect/GPU/Transforms/Passes.h.inc"
37 class GpuModuleToBinaryPass
38 :
public impl::GpuModuleToBinaryPassBase<GpuModuleToBinaryPass> {
41 void runOnOperation()
final;
45 void GpuModuleToBinaryPass::runOnOperation() {
49 .Cases(
"offloading",
"llvm", CompilationTarget::Offload)
50 .Cases(
"assembly",
"isa", CompilationTarget::Assembly)
51 .Cases(
"binary",
"bin", CompilationTarget::Binary)
52 .Cases(
"fatbinary",
"fatbin", CompilationTarget::Fatbin)
53 .Default(std::nullopt);
55 getOperation()->emitError() <<
"Invalid format specified.";
58 std::optional<SymbolTable> parentTable;
69 return &parentTable.value();
72 TargetOptions targetOptions(toolkitPath, linkFiles, cmdOptions, *targetFormat,
75 getOperation(), OffloadingLLVMTranslationAttrInterface(
nullptr),
77 return signalPassFailure();
81 LogicalResult moduleSerializer(GPUModuleOp op,
82 OffloadingLLVMTranslationAttrInterface handler,
87 if (!op.getTargetsAttr())
88 return op.
emitError(
"the module has no target attributes");
90 for (
auto targetAttr : op.getTargetsAttr()) {
91 assert(targetAttr &&
"Target attribute cannot be null.");
92 auto target = dyn_cast<gpu::TargetAttrInterface>(targetAttr);
94 "Target attribute doesn't implements `TargetAttrInterface`.");
95 std::optional<SmallVector<char, 0>> serializedModule =
96 target.serializeToObject(op, targetOptions);
97 if (!serializedModule) {
98 op.
emitError(
"An error happened while serializing the module.");
103 target.createObject(op, *serializedModule, targetOptions);
105 op.
emitError(
"An error happened while creating the object.");
108 objects.push_back(
object);
110 if (
auto moduleHandler =
111 dyn_cast_or_null<OffloadingLLVMTranslationAttrInterface>(
112 op.getOffloadingHandlerAttr());
113 !handler && moduleHandler)
114 handler = moduleHandler;
115 builder.setInsertionPointAfter(op);
116 builder.create<gpu::BinaryOp>(op.
getLoc(), op.
getName(), handler,
117 builder.getArrayAttr(objects));
124 Operation *op, OffloadingLLVMTranslationAttrInterface handler,
127 for (
Block &block : region.getBlocks())
129 llvm::make_early_inc_range(block.getOps<GPUModuleOp>()))
130 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.
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.