MLIR 22.0.0git
Target.cpp
Go to the documentation of this file.
1//===- Target.cpp - MLIR LLVM XeVM target compilation -----------*- C++ -*-===//
2//
3// Part of the LLVM Project, 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 defines XeVM target related functions including registration
10// calls for the `#xevm.target` compilation attribute.
11//
12//===----------------------------------------------------------------------===//
13
15
28#include "llvm/IR/LegacyPassManager.h"
29#include "llvm/Target/TargetMachine.h"
30
31#include "llvm/Bitcode/BitcodeWriter.h"
32#include "llvm/Config/Targets.h"
33#include "llvm/Support/FileSystem.h"
34#include "llvm/Support/FileUtilities.h"
35#include "llvm/Support/FormatVariadic.h"
36#include "llvm/Support/MemoryBuffer.h"
37#include "llvm/Support/Path.h"
38#include "llvm/Support/Process.h"
39#include "llvm/Support/Program.h"
40#include "llvm/Support/TargetSelect.h"
41#include "llvm/Support/raw_ostream.h"
42
43#include <cstdint>
44#include <cstdlib>
45
46using namespace mlir;
47using namespace mlir::xevm;
48
49namespace {
50// XeVM implementation of the gpu:TargetAttrInterface.
51class XeVMTargetAttrImpl
52 : public gpu::TargetAttrInterface::FallbackModel<XeVMTargetAttrImpl> {
53public:
54 std::optional<SmallVector<char, 0>>
55 serializeToObject(Attribute attribute, Operation *module,
56 const gpu::TargetOptions &options) const;
57
58 Attribute createObject(Attribute attribute, Operation *module,
59 const SmallVector<char, 0> &object,
60 const gpu::TargetOptions &options) const;
61};
62} // namespace
63
65 DialectRegistry &registry) {
66 registry.addExtension(+[](MLIRContext *ctx, XeVMDialect *dialect) {
67 XeVMTargetAttr::attachInterface<XeVMTargetAttrImpl>(*ctx);
68 });
69}
70
77
79 Operation &module, XeVMTargetAttr xeTarget,
81 : ModuleToObject(module, xeTarget.getTriple(), "", {}, xeTarget.getO()),
82 xeTarget(xeTarget), librariesToLink(targetOptions.getLibrariesToLink()),
83 targetOptions(targetOptions) {
84 if (xeTarget.getLinkFiles())
85 librariesToLink.append(xeTarget.getLinkFiles().begin(),
86 xeTarget.getLinkFiles().end());
87}
88
89XeVMTargetAttr SerializeGPUModuleBase::getTarget() const { return xeTarget; }
90
91std::optional<SmallVector<std::unique_ptr<llvm::Module>>>
93 if (librariesToLink.empty())
96 if (failed(loadBitcodeFilesFromList(module.getContext(), librariesToLink,
97 bcFiles)))
98 return std::nullopt;
99 return std::move(bcFiles);
100}
101
103 return dyn_cast<gpu::GPUModuleOp>(&SerializeGPUModuleBase::getOperation());
104}
105
106// There is 1 way to finalize IL to native code: IGC
107// There are 2 ways to access IGC: AOT (ocloc) and JIT (L0 runtime).
108// - L0 runtime consumes IL and is external to MLIR codebase (rt wrappers).
109// - `ocloc` tool can be "queried" from within MLIR.
110FailureOr<SmallVector<char, 0>>
112 StringRef inputFormat) {
113 using TmpFile = std::pair<llvm::SmallString<128>, llvm::FileRemover>;
114 // Find the `ocloc` tool.
115 std::optional<std::string> oclocCompiler = findTool("ocloc");
116 if (!oclocCompiler)
117 return failure();
118 Location loc = getGPUModuleOp().getLoc();
119 std::string basename = llvm::formatv(
120 "mlir-{0}-{1}-{2}", getGPUModuleOp().getNameAttr().getValue(),
121 getTarget().getTriple(), getTarget().getChip());
122
123 auto createTemp = [&](StringRef name,
124 StringRef suffix) -> FailureOr<TmpFile> {
125 llvm::SmallString<128> filePath;
126 if (auto ec = llvm::sys::fs::createTemporaryFile(name, suffix, filePath))
127 return getGPUModuleOp().emitError()
128 << "Couldn't create the temp file: `" << filePath
129 << "`, error message: " << ec.message();
130
131 return TmpFile(filePath, llvm::FileRemover(filePath.c_str()));
132 };
133 // Create temp file
134 FailureOr<TmpFile> asmFile = createTemp(basename, "asm");
135 FailureOr<TmpFile> binFile = createTemp(basename, "");
136 FailureOr<TmpFile> logFile = createTemp(basename, "log");
137 if (failed(logFile) || failed(asmFile) || failed(binFile))
138 return failure();
139 // Dump the assembly to a temp file
140 std::error_code ec;
141 {
142 llvm::raw_fd_ostream asmStream(asmFile->first, ec);
143 if (ec)
144 return emitError(loc) << "Couldn't open the file: `" << asmFile->first
145 << "`, error message: " << ec.message();
146
147 asmStream << asmStr;
148 if (asmStream.has_error())
149 return emitError(loc)
150 << "An error occurred while writing the assembly to: `"
151 << asmFile->first << "`.";
152
153 asmStream.flush();
154 }
155 // Set cmd options
156 std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>> cmdOpts =
157 targetOptions.tokenizeCmdOptions();
158 // Example: --gpu-module-to-binary="opts='opt1 opt2'"
159 const std::string cmdOptsStr = "\"" + llvm::join(cmdOpts.second, " ") + "\"";
161 {"ocloc", "compile", "-file", asmFile->first, inputFormat, "-device",
162 getTarget().getChip(), "-output", binFile->first, "-output_no_suffix",
163 "-options", cmdOptsStr});
164
165// Dump tool invocation commands.
166#define DEBUG_TYPE "serialize-to-binary"
167 LLVM_DEBUG({
168 llvm::dbgs() << "Tool invocation for module: "
169 << getGPUModuleOp().getNameAttr() << "\n";
170 llvm::interleave(oclocArgs, llvm::dbgs(), " ");
171 llvm::dbgs() << "\n";
172 });
173#undef DEBUG_TYPE
174 // Helper function for printing tool error logs.
175 std::string message;
176 auto emitLogError =
177 [&](StringRef toolName) -> FailureOr<SmallVector<char, 0>> {
178 if (message.empty()) {
179 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> toolStderr =
180 llvm::MemoryBuffer::getFile(logFile->first);
181 if (toolStderr)
182 return emitError(loc) << toolName << " invocation failed. Log:\n"
183 << toolStderr->get()->getBuffer();
184 else
185 return emitError(loc) << toolName << " invocation failed.";
186 }
187 return emitError(loc) << toolName
188 << " invocation failed, error message: " << message;
189 };
190 std::optional<StringRef> redirects[] = {
191 std::nullopt,
192 logFile->first,
193 logFile->first,
194 };
195 // Invoke ocloc.
196 if (llvm::sys::ExecuteAndWait(oclocCompiler.value(), oclocArgs, std::nullopt,
197 redirects, 0, 0, &message))
198 return emitLogError("`ocloc`");
199 binFile->first.append(".bin");
200 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> binaryBuffer =
201 llvm::MemoryBuffer::getFile(binFile->first);
202 if (!binaryBuffer)
203 return emitError(loc) << "Couldn't open the file: `" << binFile->first
204 << "`, error message: "
205 << binaryBuffer.getError().message();
206
207 StringRef bin = (*binaryBuffer)->getBuffer();
208 return SmallVector<char, 0>(bin.begin(), bin.end());
209}
210
211std::optional<std::string> SerializeGPUModuleBase::findTool(StringRef tool) {
212 // 1. Check the toolkit path given in the command line.
213 StringRef pathRef = targetOptions.getToolkitPath();
215 if (!pathRef.empty()) {
216 path.insert(path.begin(), pathRef.begin(), pathRef.end());
217 llvm::sys::path::append(path, "bin", tool);
218 if (llvm::sys::fs::can_execute(path))
219 return StringRef(path.data(), path.size()).str();
220 }
221 // 2. Check PATH.
222 if (std::optional<std::string> toolPath =
223 llvm::sys::Process::FindInEnvPath("PATH", tool))
224 return *toolPath;
225
226 getGPUModuleOp().emitError()
227 << "Couldn't find the `" << tool
228 << "` binary. Please specify the toolkit "
229 "path via GpuModuleToBinaryPass or add the compiler to $PATH`.";
230 return std::nullopt;
231}
232
233namespace {
234class SPIRVSerializer : public SerializeGPUModuleBase {
235public:
236 SPIRVSerializer(Operation &module, XeVMTargetAttr xeTarget,
237 const gpu::TargetOptions &targetOptions)
238 : SerializeGPUModuleBase(module, xeTarget, targetOptions) {}
239
240 static void init();
241
242 /// Serializes the LLVM module to an object format, depending on the
243 /// compilation target selected in target options.
244 FailureOr<SmallVector<char, 0>>
245 moduleToObject(llvm::Module &llvmModule) override;
246
247private:
248 /// Translates the LLVM module to SPIR-V binary using LLVM's
249 /// SPIR-V target.
250 std::optional<std::string>
251 translateToSPIRVBinary(llvm::Module &llvmModule,
252 llvm::TargetMachine &targetMachine);
253};
254} // namespace
255
256void SPIRVSerializer::init() {
257 static llvm::once_flag initializeBackendOnce;
258 llvm::call_once(initializeBackendOnce, []() {
259#if LLVM_HAS_SPIRV_TARGET
260 LLVMInitializeSPIRVTarget();
261 LLVMInitializeSPIRVTargetInfo();
262 LLVMInitializeSPIRVTargetMC();
263 LLVMInitializeSPIRVAsmPrinter();
264#endif
265 });
266}
267
268FailureOr<SmallVector<char, 0>>
269SPIRVSerializer::moduleToObject(llvm::Module &llvmModule) {
270#define DEBUG_TYPE "serialize-to-llvm"
271 LLVM_DEBUG({
272 llvm::dbgs() << "LLVM IR for module: " << getGPUModuleOp().getNameAttr()
273 << "\n";
274 llvm::dbgs() << llvmModule << "\n";
275 llvm::dbgs().flush();
276 });
277#undef DEBUG_TYPE
278
279 // Return LLVM IR if the compilation target is `offload`.
280 if (targetOptions.getCompilationTarget() == gpu::CompilationTarget::Offload)
282
283#if !LLVM_HAS_SPIRV_TARGET
284 return getGPUModuleOp()->emitError(
285 "The `SPIRV` target was not built. Please enable "
286 "it when building LLVM.");
287#endif // LLVM_HAS_SPIRV_TARGET
288
289 FailureOr<llvm::TargetMachine *> targetMachine = getOrCreateTargetMachine();
290 if (failed(targetMachine))
291 return getGPUModuleOp().emitError()
292 << "Target Machine unavailable for triple " << triple
293 << ", can't optimize with LLVM\n";
294
295 // Return SPIRV if the compilation target is `assembly`.
296 if (targetOptions.getCompilationTarget() ==
297 gpu::CompilationTarget::Assembly) {
298 FailureOr<SmallString<0>> serializedISA =
299 translateModuleToISA(llvmModule, **targetMachine,
300 [&]() { return getGPUModuleOp().emitError(); });
301 if (failed(serializedISA))
302 return getGPUModuleOp().emitError()
303 << "Failed translating the module to ISA." << triple
304 << ", can't compile with LLVM\n";
305
306#define DEBUG_TYPE "serialize-to-isa"
307 LLVM_DEBUG({
308 llvm::dbgs() << "SPIR-V for module: " << getGPUModuleOp().getNameAttr()
309 << "\n";
310 llvm::dbgs() << *serializedISA << "\n";
311 llvm::dbgs().flush();
312 });
313#undef DEBUG_TYPE
314
315 // Make sure to include the null terminator.
316 StringRef bin(serializedISA->c_str(), serializedISA->size() + 1);
317 return SmallVector<char, 0>(bin.begin(), bin.end());
318 }
319
320 // Level zero runtime is set up to accept SPIR-V binary
321 // translateToSPIRVBinary translates the LLVM module to SPIR-V binary
322 // using LLVM's SPIRV target.
323 // compileToBinary can be used in the future if level zero runtime
324 // implementation switches to native XeVM binary format.
325 std::optional<std::string> serializedSPIRVBinary =
326 translateToSPIRVBinary(llvmModule, **targetMachine);
327 if (!serializedSPIRVBinary)
328 return getGPUModuleOp().emitError()
329 << "Failed translating the module to Binary.";
330
331 if (serializedSPIRVBinary->size() % 4)
332 return getGPUModuleOp().emitError()
333 << "SPIRV code size must be a multiple of 4.";
334
335 StringRef bin(serializedSPIRVBinary->c_str(), serializedSPIRVBinary->size());
336 return SmallVector<char, 0>(bin.begin(), bin.end());
337}
338
339std::optional<std::string>
340SPIRVSerializer::translateToSPIRVBinary(llvm::Module &llvmModule,
341 llvm::TargetMachine &targetMachine) {
342 std::string targetISA;
343 llvm::raw_string_ostream stream(targetISA);
344
345 { // Drop pstream after this to prevent the ISA from being stuck buffering
346 llvm::buffer_ostream pstream(stream);
347 llvm::legacy::PassManager codegenPasses;
348 if (targetMachine.addPassesToEmitFile(codegenPasses, pstream, nullptr,
349 llvm::CodeGenFileType::ObjectFile))
350 return std::nullopt;
351
352 codegenPasses.run(llvmModule);
353 }
354 return targetISA;
355}
356
357std::optional<SmallVector<char, 0>>
358XeVMTargetAttrImpl::serializeToObject(Attribute attribute, Operation *module,
359 const gpu::TargetOptions &options) const {
360 if (!module)
361 return std::nullopt;
362 auto gpuMod = dyn_cast<gpu::GPUModuleOp>(module);
363 if (!gpuMod) {
364 module->emitError("expected to be a gpu.module op");
365 return std::nullopt;
366 }
367 auto xeTarget = cast<XeVMTargetAttr>(attribute);
368 if (xeTarget.getTriple().starts_with("spirv")) {
369 gpuMod.walk([&](LLVM::LLVMFuncOp funcOp) {
370 if (funcOp->hasAttr(gpu::GPUDialect::getKernelFuncAttrName())) {
371 funcOp.setIntelReqdSubGroupSize(16);
372 return WalkResult::interrupt();
373 }
374 return WalkResult::advance();
375 });
376
377 SPIRVSerializer serializer(*module, cast<XeVMTargetAttr>(attribute),
378 options);
379 serializer.init();
380
381#if !LLVM_HAS_SPIRV_TARGET
382 module->emitError("Cannot run `TargetRegistry::lookupTarget()` for SPIRV "
383 "without having the target built.");
384#endif
385
386 return serializer.run();
387 }
388 module->emitError("Unsupported XeVM target triple: ") << xeTarget.getTriple();
389 return std::nullopt;
390}
391
392Attribute
393XeVMTargetAttrImpl::createObject(Attribute attribute, Operation *module,
394 const SmallVector<char, 0> &object,
395 const gpu::TargetOptions &options) const {
396 Builder builder(attribute.getContext());
397 gpu::CompilationTarget format = options.getCompilationTarget();
398 auto xeTarget = cast<XeVMTargetAttr>(attribute);
399 SmallVector<NamedAttribute, 2> properties;
400 if (format == gpu::CompilationTarget::Assembly)
401 properties.push_back(
402 builder.getNamedAttr("O", builder.getI32IntegerAttr(xeTarget.getO())));
403
404 DictionaryAttr objectProps;
405 if (!properties.empty())
406 objectProps = builder.getDictionaryAttr(properties);
407
408 return builder.getAttr<gpu::ObjectAttr>(
409 attribute, format,
410 builder.getStringAttr(StringRef(object.data(), object.size())),
411 objectProps, /*kernels=*/nullptr);
412}
static llvm::ManagedStatic< PassManagerOptions > options
MLIRContext * getContext() const
Return the context this attribute belongs to.
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
LogicalResult loadBitcodeFilesFromList(llvm::LLVMContext &context, ArrayRef< Attribute > librariesToLink, SmallVector< std::unique_ptr< llvm::Module > > &llvmModules, bool failureOnError=true)
Loads multiple bitcode files.
virtual FailureOr< SmallVector< char, 0 > > moduleToObject(llvm::Module &llvmModule)
Serializes the LLVM IR bitcode to an object file, by default it serializes to LLVM bitcode.
Operation & getOperation()
Returns the operation being serialized.
ModuleToObject(Operation &module, StringRef triple, StringRef chip, StringRef features={}, int optLevel=3, function_ref< void(llvm::Module &)> initialLlvmIRCallback={}, function_ref< void(llvm::Module &)> linkedLlvmIRCallback={}, function_ref< void(llvm::Module &)> optimizedLlvmIRCallback={}, function_ref< void(StringRef)> isaCallback={})
Operation & module
Module to transform to a binary object.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
void appendDialectRegistry(const DialectRegistry &registry)
Append the contents of the given dialect registry to the registry associated with this context.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
static WalkResult advance()
Definition WalkResult.h:47
static WalkResult interrupt()
Definition WalkResult.h:46
This class serves as an opaque interface for passing options to the TargetAttrInterface methods.
Base class for all XeVM serializations from GPU modules into binary strings.
Definition Utils.h:27
gpu::TargetOptions targetOptions
GPU compilation target options.
Definition Utils.h:58
XeVMTargetAttr getTarget() const
Returns the target attribute.
Definition Target.cpp:89
FailureOr< SmallVector< char, 0 > > compileToBinary(StringRef asmStr, StringRef inputFormat)
Compiles to native code using ocloc.
Definition Target.cpp:111
std::optional< SmallVector< std::unique_ptr< llvm::Module > > > loadBitcodeFiles(llvm::Module &module) override
Loads the bitcode files in librariesToLink.
Definition Target.cpp:92
SmallVector< Attribute > librariesToLink
List of LLVM bitcode to link into after translation to LLVM IR.
Definition Utils.h:52
SerializeGPUModuleBase(Operation &module, XeVMTargetAttr target, const gpu::TargetOptions &targetOptions={})
Definition Target.cpp:78
XeVMTargetAttr xeTarget
XeVM Target attribute.
Definition Utils.h:48
std::optional< std::string > findTool(StringRef tool)
Returns the path to the tool used for serialization.
Definition Target.cpp:211
gpu::GPUModuleOp getGPUModuleOp()
Returns the gpu module being serialized.
Definition Target.cpp:102
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:573
void registerXeVMTargetInterfaceExternalModels(mlir::DialectRegistry &registry)
Registers the TargetAttrInterface for the #xevm.target attribute in the given registry.
Definition Target.cpp:64
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.