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 for (
const std::string &path : linkFiles)
74 TargetOptions targetOptions(toolkitPath, librariesToLink, cmdOptions,
75 elfSection, *targetFormat, lazyTableBuilder);
77 getOperation(), OffloadingLLVMTranslationAttrInterface(
nullptr),
79 return signalPassFailure();
83 LogicalResult moduleSerializer(GPUModuleOp op,
84 OffloadingLLVMTranslationAttrInterface handler,
89 if (!op.getTargetsAttr())
90 return op.emitError(
"the module has no target attributes");
92 for (
auto targetAttr : op.getTargetsAttr()) {
93 assert(targetAttr &&
"Target attribute cannot be null.");
94 auto target = dyn_cast<gpu::TargetAttrInterface>(targetAttr);
96 "Target attribute doesn't implements `TargetAttrInterface`.");
97 std::optional<SmallVector<char, 0>> serializedModule =
98 target.serializeToObject(op, targetOptions);
99 if (!serializedModule) {
100 op.emitError(
"An error happened while serializing the module.");
105 target.createObject(op, *serializedModule, targetOptions);
107 op.emitError(
"An error happened while creating the object.");
110 objects.push_back(
object);
112 if (
auto moduleHandler =
113 dyn_cast_or_null<OffloadingLLVMTranslationAttrInterface>(
114 op.getOffloadingHandlerAttr());
115 !handler && moduleHandler)
116 handler = moduleHandler;
117 builder.setInsertionPointAfter(op);
118 builder.create<gpu::BinaryOp>(op.getLoc(), op.getName(), handler,
119 builder.getArrayAttr(objects));
126 Operation *op, OffloadingLLVMTranslationAttrInterface handler,
129 for (
Block &block : region.getBlocks())
131 llvm::make_early_inc_range(block.getOps<GPUModuleOp>()))
132 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.
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
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.
const FrozenRewritePatternSet & patterns
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...