1//===- TestLazyLoading.cpp - Pass to test operation lazy loading ---------===//
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#include "mlir/Bytecode/BytecodeReader.h"
10#include "mlir/Bytecode/BytecodeWriter.h"
11#include "mlir/IR/BuiltinOps.h"
12#include "mlir/IR/OperationSupport.h"
13#include "mlir/Pass/Pass.h"
14#include "llvm/Support/MemoryBufferRef.h"
15#include "llvm/Support/raw_ostream.h"
16#include <list>
17
18using namespace mlir;
19
20namespace {
21
22/// This is a test pass which LazyLoads the current operation recursively.
23struct LazyLoadingPass : public PassWrapper<LazyLoadingPass, OperationPass<>> {
24 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(LazyLoadingPass)
25
26 StringRef getArgument() const final { return "test-lazy-loading"; }
27 StringRef getDescription() const final { return "Test LazyLoading of op"; }
28 LazyLoadingPass() = default;
29 LazyLoadingPass(const LazyLoadingPass &) {}
30
31 void runOnOperation() override {
32 Operation *op = getOperation();
33 std::string bytecode;
34 {
35 BytecodeWriterConfig config;
36 if (version >= 0)
37 config.setDesiredBytecodeVersion(version);
38 llvm::raw_string_ostream os(bytecode);
39 if (failed(Result: writeBytecodeToFile(op, os, config))) {
40 op->emitError() << "failed to write bytecode at version "
41 << (int)version;
42 signalPassFailure();
43 return;
44 }
45 }
46 llvm::MemoryBufferRef buffer(bytecode, "test-lazy-loading");
47 Block block;
48 ParserConfig config(op->getContext(), /*verifyAfterParse=*/false);
49 BytecodeReader reader(buffer, config,
50 /*lazyLoad=*/true);
51 std::list<Operation *> toLoadOps;
52 if (failed(Result: reader.readTopLevel(block: &block, lazyOps: [&](Operation *op) {
53 toLoadOps.push_back(x: op);
54 return false;
55 }))) {
56 op->emitError() << "failed to read bytecode";
57 return;
58 }
59
60 llvm::outs() << "Has " << reader.getNumOpsToMaterialize()
61 << " ops to materialize\n";
62
63 // Recursively print the operations, before and after lazy loading.
64 while (!toLoadOps.empty()) {
65 Operation *toLoad = toLoadOps.front();
66 toLoadOps.pop_front();
67 llvm::outs() << "\n\nBefore Materializing...\n\n";
68 toLoad->print(os&: llvm::outs());
69 llvm::outs() << "\n\nMaterializing...\n\n";
70 if (failed(Result: reader.materialize(op: toLoad, lazyOpsCallback: [&](Operation *op) {
71 toLoadOps.push_back(x: op);
72 return false;
73 }))) {
74 toLoad->emitError() << "failed to materialize";
75 signalPassFailure();
76 return;
77 }
78 toLoad->print(os&: llvm::outs());
79 llvm::outs() << "\n";
80 llvm::outs() << "Has " << reader.getNumOpsToMaterialize()
81 << " ops to materialize\n";
82 }
83 }
84 Option<int> version{*this, "bytecode-version",
85 llvm::cl::desc("Specifies the bytecode version to use."),
86 llvm::cl::init(Val: -1)};
87};
88} // namespace
89
90namespace mlir {
91void registerLazyLoadingTestPasses() { PassRegistration<LazyLoadingPass>(); }
92} // namespace mlir
93