MLIR 23.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<mlir::gpu::SerializedObject>
55 serializeToObject(Attribute attribute, Operation *module,
56 const gpu::TargetOptions &options) const;
57
58 Attribute createObject(Attribute attribute, Operation *module,
59 const mlir::gpu::SerializedObject &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
247 /// Runs the serialization pipeline, returning `std::nullopt` on error.
248 std::optional<SmallVector<char, 0>> run() override;
249
250private:
251 /// Translates the LLVM module to SPIR-V binary using LLVM's
252 /// SPIR-V target.
253 std::optional<std::string>
254 translateToSPIRVBinary(llvm::Module &llvmModule,
255 llvm::TargetMachine &targetMachine);
256};
257} // namespace
258
259void SPIRVSerializer::init() {
260 static llvm::once_flag initializeBackendOnce;
261 llvm::call_once(initializeBackendOnce, []() {
262#if LLVM_HAS_SPIRV_TARGET
263 LLVMInitializeSPIRVTarget();
264 LLVMInitializeSPIRVTargetInfo();
265 LLVMInitializeSPIRVTargetMC();
266 LLVMInitializeSPIRVAsmPrinter();
267#endif
268 });
269}
270
271#if LLVM_HAS_SPIRV_TARGET
272static const std::vector<std::string> getDefaultSPIRVExtensions() {
273 return {
274 "SPV_EXT_relaxed_printf_string_address_space",
275 "SPV_INTEL_cache_controls",
276 "SPV_INTEL_variable_length_array",
277 };
278}
279
280namespace llvm {
281class Module;
282
283extern "C" bool
284SPIRVTranslateModule(Module *M, std::string &SpirvObj, std::string &ErrMsg,
285 const std::vector<std::string> &AllowExtNames,
286 const std::vector<std::string> &Opts);
287} // namespace llvm
288#endif
289
290FailureOr<SmallVector<char, 0>>
291SPIRVSerializer::moduleToObject(llvm::Module &llvmModule) {
292#define DEBUG_TYPE "serialize-to-llvm"
293 LLVM_DEBUG({
294 llvm::dbgs() << "LLVM IR for module: " << getGPUModuleOp().getNameAttr()
295 << "\n";
296 llvm::dbgs() << llvmModule << "\n";
297 llvm::dbgs().flush();
298 });
299#undef DEBUG_TYPE
300
301 // Return LLVM IR if the compilation target is `offload`.
302 if (targetOptions.getCompilationTarget() == gpu::CompilationTarget::Offload)
304
305#if !LLVM_HAS_SPIRV_TARGET
306 return getGPUModuleOp()->emitError(
307 "The `SPIRV` target was not built. Please enable "
308 "it when building LLVM.");
309#else
310
311 // Return SPIRV text if the compilation target is `assembly`.
312 // Note: Optimization passes are skipped and SPIRV extensions are
313 // not supported in this mode.
314 if (targetOptions.getCompilationTarget() ==
315 gpu::CompilationTarget::Assembly) {
316 FailureOr<llvm::TargetMachine *> targetMachine = getOrCreateTargetMachine();
317 if (failed(targetMachine))
318 return getGPUModuleOp().emitError()
319 << "Target Machine unavailable for triple " << triple
320 << ", can't optimize with LLVM\n";
321
322 FailureOr<SmallString<0>> serializedISA =
323 translateModuleToISA(llvmModule, **targetMachine,
324 [&]() { return getGPUModuleOp().emitError(); });
325 if (failed(serializedISA))
326 return getGPUModuleOp().emitError()
327 << "Failed translating the module to ISA." << triple
328 << ", can't compile with LLVM\n";
329
330#define DEBUG_TYPE "serialize-to-isa"
331 LLVM_DEBUG({
332 llvm::dbgs() << "SPIR-V for module: " << getGPUModuleOp().getNameAttr()
333 << "\n";
334 llvm::dbgs() << *serializedISA << "\n";
335 llvm::dbgs().flush();
336 });
337#undef DEBUG_TYPE
338
339 // Make sure to include the null terminator.
340 StringRef bin(serializedISA->c_str(), serializedISA->size() + 1);
341 return SmallVector<char, 0>(bin.begin(), bin.end());
342 }
343
344 // Level zero runtime is set up to accept SPIR-V binary
345 std::string serializedSPIRVBinary;
346 std::string ErrMsg;
347 std::vector<std::string> Opts;
348 Opts.push_back(triple.str());
349 Opts.push_back(std::to_string(optLevel));
350
351 bool success =
352 SPIRVTranslateModule(&llvmModule, serializedSPIRVBinary, ErrMsg,
353 getDefaultSPIRVExtensions(), Opts);
354
355 if (!success)
356 return getGPUModuleOp().emitError()
357 << "Failed translating the module to Binary."
358 << "Error message: " << ErrMsg;
359
360 if (serializedSPIRVBinary.size() % 4)
361 return getGPUModuleOp().emitError()
362 << "SPIRV code size must be a multiple of 4.";
363
364 StringRef bin(serializedSPIRVBinary.c_str(), serializedSPIRVBinary.size());
365 return SmallVector<char, 0>(bin.begin(), bin.end());
366#endif // LLVM_HAS_SPIRV_TARGET
367}
368
369std::optional<SmallVector<char, 0>> SPIRVSerializer::run() {
370 // Translate the module to LLVM IR.
371 llvm::LLVMContext llvmContext;
372 std::unique_ptr<llvm::Module> llvmModule = translateToLLVMIR(llvmContext);
373 if (!llvmModule) {
374 getOperation().emitError() << "Failed creating the llvm::Module.";
375 return std::nullopt;
376 }
377 setDataLayoutAndTriple(*llvmModule);
378
379 if (initialLlvmIRCallback)
380 initialLlvmIRCallback(*llvmModule);
381
382 // Link bitcode files.
383 handleModulePreLink(*llvmModule);
384 {
385 auto libs = loadBitcodeFiles(*llvmModule);
386 if (!libs)
387 return std::nullopt;
388 if (!libs->empty())
389 if (failed(linkFiles(*llvmModule, std::move(*libs))))
390 return std::nullopt;
391 handleModulePostLink(*llvmModule);
392 }
393
394 if (linkedLlvmIRCallback)
395 linkedLlvmIRCallback(*llvmModule);
396
397 // Return the serialized object.
398 return moduleToObject(*llvmModule);
399}
400
401std::optional<std::string>
402SPIRVSerializer::translateToSPIRVBinary(llvm::Module &llvmModule,
403 llvm::TargetMachine &targetMachine) {
404 std::string targetISA;
405 llvm::raw_string_ostream stream(targetISA);
406
407 { // Drop pstream after this to prevent the ISA from being stuck buffering
408 llvm::buffer_ostream pstream(stream);
409 llvm::legacy::PassManager codegenPasses;
410 if (targetMachine.addPassesToEmitFile(codegenPasses, pstream, nullptr,
411 llvm::CodeGenFileType::ObjectFile))
412 return std::nullopt;
413
414 codegenPasses.run(llvmModule);
415 }
416 return targetISA;
417}
418
419std::optional<mlir::gpu::SerializedObject>
420XeVMTargetAttrImpl::serializeToObject(Attribute attribute, Operation *module,
421 const gpu::TargetOptions &options) const {
422 if (!module)
423 return std::nullopt;
424 auto gpuMod = dyn_cast<gpu::GPUModuleOp>(module);
425 if (!gpuMod) {
426 module->emitError("expected to be a gpu.module op");
427 return std::nullopt;
428 }
429 auto xeTarget = cast<XeVMTargetAttr>(attribute);
430 if (xeTarget.getTriple().starts_with("spirv")) {
431 gpuMod.walk([&](LLVM::LLVMFuncOp funcOp) {
432 if (funcOp->hasAttr(gpu::GPUDialect::getKernelFuncAttrName())) {
433 funcOp.setIntelReqdSubGroupSize(16);
434 return WalkResult::interrupt();
435 }
436 return WalkResult::advance();
437 });
438
439 SPIRVSerializer serializer(*module, cast<XeVMTargetAttr>(attribute),
440 options);
441 serializer.init();
442
443#if !LLVM_HAS_SPIRV_TARGET
444 module->emitError("Cannot run `TargetRegistry::lookupTarget()` for SPIRV "
445 "without having the target built.");
446#endif
447
448 std::optional<SmallVector<char, 0>> binary = serializer.run();
449 if (!binary)
450 return std::nullopt;
451 return gpu::SerializedObject{std::move(*binary)};
452 }
453 module->emitError("Unsupported XeVM target triple: ") << xeTarget.getTriple();
454 return std::nullopt;
455}
456
457Attribute
458XeVMTargetAttrImpl::createObject(Attribute attribute, Operation *module,
459 const mlir::gpu::SerializedObject &object,
460 const gpu::TargetOptions &options) const {
461 Builder builder(attribute.getContext());
462 gpu::CompilationTarget format = options.getCompilationTarget();
463 auto xeTarget = cast<XeVMTargetAttr>(attribute);
464 SmallVector<NamedAttribute, 2> properties;
465 if (format == gpu::CompilationTarget::Assembly)
466 properties.push_back(
467 builder.getNamedAttr("O", builder.getI32IntegerAttr(xeTarget.getO())));
468
469 DictionaryAttr objectProps;
470 if (!properties.empty())
471 objectProps = builder.getDictionaryAttr(properties);
472
473 return builder.getAttr<gpu::ObjectAttr>(
474 attribute, format,
475 builder.getStringAttr(
476 StringRef(object.getObject().data(), object.getObject().size())),
477 objectProps, /*kernels=*/nullptr);
478}
return success()
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:717
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.