1//===-- Utils.h - MLIR XeVM target utils ------------------------*- C++ -*-===//
2//
3// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This files declares XeVM target related utility classes and functions.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef MLIR_TARGET_LLVM_XEVM_UTILS_H
14#define MLIR_TARGET_LLVM_XEVM_UTILS_H
15
16#include "mlir/Dialect/GPU/IR/CompilationInterfaces.h"
17#include "mlir/Dialect/GPU/IR/GPUDialect.h"
18#include "mlir/Dialect/LLVMIR/XeVMDialect.h"
19#include "mlir/IR/Attributes.h"
20#include "mlir/Target/LLVM/ModuleToObject.h"
21
22namespace mlir {
23namespace xevm {
24
25/// Base class for all XeVM serializations from GPU modules into binary strings.
26/// By default this class serializes into LLVM bitcode.
27class SerializeGPUModuleBase : public LLVM::ModuleToObject {
28public:
29 SerializeGPUModuleBase(Operation &module, XeVMTargetAttr target,
30 const gpu::TargetOptions &targetOptions = {});
31
32 /// Returns the target attribute.
33 XeVMTargetAttr getTarget() const;
34
35 /// Loads the bitcode files in `librariesToLink`.
36 std::optional<SmallVector<std::unique_ptr<llvm::Module>>>
37 loadBitcodeFiles(llvm::Module &module) override;
38
39 /// Returns the gpu module being serialized.
40 gpu::GPUModuleOp getGPUModuleOp();
41
42 /// Compiles to native code using `ocloc` (API or tool).
43 FailureOr<SmallVector<char, 0>> compileToBinary(StringRef asmStr,
44 StringRef inputFormat);
45
46protected:
47 /// XeVM Target attribute.
48 XeVMTargetAttr xeTarget;
49 /// List of LLVM bitcode to link into after translation to LLVM IR.
50 /// The attributes can be StringAttr pointing to a file path, or
51 /// a Resource blob pointing to the LLVM bitcode in-memory.
52 SmallVector<Attribute> librariesToLink;
53
54 /// Returns the path to the tool used for serialization.
55 std::optional<std::string> findTool(StringRef tool);
56
57 /// Compiles to native code using the `ocloc` command-line tool, communicating
58 /// through temporary files.
59 FailureOr<SmallVector<char, 0>>
60 compileToBinaryViaOclocTool(StringRef asmStr, StringRef inputFormat);
61
62 /// Compiles to native code using the `ocloc` shared library API, in-process,
63 /// without temporary files. Only available when the library is linked in.
64#if MLIR_XEVM_OCLOC_LIB_AVAILABLE
65 FailureOr<SmallVector<char, 0>>
66 compileToBinaryViaLibocloc(StringRef asmStr, StringRef inputFormat);
67#endif // MLIR_XEVM_OCLOC_LIB_AVAILABLE
68
69 /// GPU compilation target options.
70 gpu::TargetOptions targetOptions;
71};
72} // namespace xevm
73} // namespace mlir
74
75#endif // MLIR_TARGET_LLVM_XEVM_UTILS_H
76