|
| 1 | +//===- TargetCompilationManager.h - Compilation Scheduler -----*- C++ -*-===// |
| 2 | +// |
| 3 | +// (C) Copyright IBM 2023. |
| 4 | +// |
| 5 | +// This code is part of Qiskit. |
| 6 | +// |
| 7 | +// This code is licensed under the Apache License, Version 2.0 with LLVM |
| 8 | +// Exceptions. You may obtain a copy of this license in the LICENSE.txt |
| 9 | +// file in the root directory of this source tree. |
| 10 | +// |
| 11 | +// Any modifications or derivative works of this code must retain this |
| 12 | +// copyright notice, and modified files need to carry a notice indicating |
| 13 | +// that they have been altered from the originals. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | +/// |
| 17 | +/// This file declares the classes for the top-level compilation scheduling |
| 18 | +/// interface. |
| 19 | +/// |
| 20 | +//===----------------------------------------------------------------------===// |
| 21 | +#ifndef TARGETCOMPILATIONMANAGER_H |
| 22 | +#define TARGETCOMPILATIONMANAGER_H |
| 23 | + |
| 24 | +#include "HAL/TargetSystem.h" |
| 25 | + |
| 26 | +#include "mlir/IR/BuiltinOps.h" |
| 27 | + |
| 28 | +#include "llvm/Support/Error.h" |
| 29 | + |
| 30 | +#include <string> |
| 31 | + |
| 32 | +using namespace qssc; |
| 33 | + |
| 34 | +namespace qssc::hal::compile { |
| 35 | + |
| 36 | +/// @brief Base class for the compiler's |
| 37 | +/// target compilation infrastructure. |
| 38 | +/// A target system is a tree of compilation targets. |
| 39 | +/// We aim to support compiling each disjoint |
| 40 | +/// target subtree independently. |
| 41 | +class TargetCompilationManager { |
| 42 | +protected: |
| 43 | + TargetCompilationManager(hal::TargetSystem &target, |
| 44 | + mlir::MLIRContext *context); |
| 45 | + |
| 46 | + using WalkTargetFunction = |
| 47 | + std::function<llvm::Error(hal::Target *, mlir::ModuleOp)>; |
| 48 | + // Depth first walker for a target system |
| 49 | + llvm::Error walkTarget(Target *target, mlir::ModuleOp targetModuleOp, |
| 50 | + const WalkTargetFunction &walkFunc, |
| 51 | + const WalkTargetFunction &postChildrenCallbackFunc); |
| 52 | + |
| 53 | +public: |
| 54 | + virtual ~TargetCompilationManager() = default; |
| 55 | + virtual const std::string getName() const = 0; |
| 56 | + |
| 57 | + /// Get the base target system to be compiled. |
| 58 | + virtual hal::Target &getTargetSystem() { return target; } |
| 59 | + |
| 60 | + /// Get the base MLIR context for this compilation scheduler. |
| 61 | + mlir::MLIRContext *getContext() { return context; }; |
| 62 | + |
| 63 | + /// @brief Compile only at the MLIR level for the full target |
| 64 | + /// system. |
| 65 | + /// @param moduleOp The root module operation to compile for. |
| 66 | + /// This must not be specialized to a system already. |
| 67 | + virtual llvm::Error compileMLIR(mlir::ModuleOp moduleOp) = 0; |
| 68 | + |
| 69 | + /// @brief Generate the full configured compilation pipeline |
| 70 | + /// for all targets of the base target system. This will also |
| 71 | + /// invoke compileMLIR. |
| 72 | + /// @param moduleOp The root module operation to compile for. |
| 73 | + /// This must not be specialized to a system already. |
| 74 | + /// @param payload The payload to populate. |
| 75 | + /// @param doCompileMLIR Whether to call compileMLIR prior to compiling the |
| 76 | + /// payload. Defaults to true. |
| 77 | + virtual llvm::Error compilePayload(mlir::ModuleOp moduleOp, |
| 78 | + qssc::payload::Payload &payload, |
| 79 | + bool doCompileMLIR = true) = 0; |
| 80 | + |
| 81 | + void enableIRPrinting(bool printBeforeAllTargetPasses, |
| 82 | + bool printAfterAllTargetPasses, |
| 83 | + bool printBeforeAllTargetPayload, |
| 84 | + bool printAfterTargetCompileFailure); |
| 85 | + |
| 86 | +protected: |
| 87 | + bool getPrintBeforeAllTargetPasses() { return printBeforeAllTargetPasses; } |
| 88 | + bool getPrintAfterAllTargetPasses() { return printAfterAllTargetPasses; } |
| 89 | + bool getPrintBeforeAllTargetPayload() { return printBeforeAllTargetPayload; } |
| 90 | + bool getPrintAfterTargetCompileFailure() { |
| 91 | + return printAfterTargetCompileFailure; |
| 92 | + } |
| 93 | + |
| 94 | + void printIR(llvm::StringRef msg, mlir::Operation *op, |
| 95 | + llvm::raw_ostream &out); |
| 96 | + |
| 97 | +private: |
| 98 | + hal::TargetSystem ⌖ |
| 99 | + mlir::MLIRContext *context; |
| 100 | + |
| 101 | + bool printBeforeAllTargetPasses = false; |
| 102 | + bool printAfterAllTargetPasses = false; |
| 103 | + bool printBeforeAllTargetPayload = false; |
| 104 | + bool printAfterTargetCompileFailure = false; |
| 105 | + |
| 106 | +}; // class TargetCompilationManager |
| 107 | + |
| 108 | +/// Register a set of useful command-line options that can be used to configure |
| 109 | +/// a target compilation scheduler. |
| 110 | +void registerTargetCompilationManagerCLOptions(); |
| 111 | + |
| 112 | +/// Apply any values provided to the target compilation scheduler options that |
| 113 | +/// were registered with 'registerTargetCompilationManagerCLOptions'. |
| 114 | +mlir::LogicalResult |
| 115 | +applyTargetCompilationManagerCLOptions(TargetCompilationManager &scheduler); |
| 116 | + |
| 117 | +} // namespace qssc::hal::compile |
| 118 | +#endif // TARGETCOMPILATIONMANAGER_H |
0 commit comments