-
-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathCompilerPipeline.h
More file actions
151 lines (133 loc) · 4.3 KB
/
Copy pathCompilerPipeline.h
File metadata and controls
151 lines (133 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright (c) 2023 - 2026 Chair for Design Automation, TUM
* Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH
* All rights reserved.
*
* SPDX-License-Identifier: MIT
*
* Licensed under the MIT License
*/
#pragma once
#include <mlir/Pass/PassManager.h>
#include <mlir/Support/LogicalResult.h>
#include <string>
namespace mlir {
class ModuleOp;
/**
* @brief Configuration for the quantum compiler pipeline
*
* @details
* Controls which stages of the compilation pipeline are executed and
* diagnostic options for profiling and debugging.
*/
struct QuantumCompilerConfig {
/// Convert to QIR at the end of the pipeline
bool convertToQIR = false;
/// Record intermediate IR at each stage for debugging/testing
bool recordIntermediates = false;
/// Enable pass timing statistics (MLIR builtin)
bool enableTiming = false;
/// Enable pass statistics (MLIR builtin)
bool enableStatistics = false;
/// Print IR after each stage
bool printIRAfterAllStages = false;
};
/**
* @brief Records the state of IR at various compilation stages
*
* @details
* Stores string representations of the MLIR module at different
* points in the compilation pipeline. Useful for testing and debugging.
* All stages are recorded when recordIntermediates is enabled.
*/
struct CompilationRecord {
std::string afterQCImport;
std::string afterInitialCanon;
std::string afterQCOConversion;
std::string afterQCOCanon;
std::string afterOptimization;
std::string afterOptimizationCanon;
std::string afterQCConversion;
std::string afterQCCanon;
std::string afterQIRConversion;
std::string afterQIRCanon;
};
/**
* @brief Main quantum compiler pipeline
*
* @details
* Provides a high-level interface for compiling quantum programs through
* the MQT compiler infrastructure. The pipeline stages are:
*
* 1. QC dialect (reference semantics) - imported from
* qc::QuantumComputation
* 2. Canonicalization + cleanup
* 3. QCO dialect (value semantics) - enables SSA-based optimizations
* 4. Canonicalization + cleanup
* 5. Quantum optimization passes
* 6. Canonicalization + cleanup
* 7. QC dialect - converted back for backend lowering
* 8. Canonicalization + cleanup
* 9. QIR (Quantum Intermediate Representation) - optional final lowering
* 10. Canonicalization + cleanup
*
* Following MLIR best practices, canonicalization and dead value removal
* are always run after each major transformation stage.
*/
class QuantumCompilerPipeline {
public:
explicit QuantumCompilerPipeline(const QuantumCompilerConfig& config = {})
: config_(config) {}
/**
* @brief Run the complete compilation pipeline on a module
*
* @details
* Executes all enabled compilation stages on the provided MLIR module.
* If recordIntermediates is enabled in the config, captures IR snapshots
* at every stage (10 snapshots total for full pipeline).
*
* Automatically configures the PassManager with:
* - Timing statistics if enableTiming is true
* - Pass statistics if enableStatistics is true
* - IR printing after each stage if printIRAfterAllStages is true
*
* @param module The MLIR module to compile
* @param record Optional pointer to record intermediate states
* @return success() if compilation succeeded, failure() otherwise
*/
LogicalResult runPipeline(ModuleOp module,
CompilationRecord* record = nullptr) const;
private:
/**
* @brief Add canonicalization and cleanup passes
*
* @details
* Always adds the standard MLIR canonicalization pass followed by dead
* value removal.
*/
static void addCleanupPasses(PassManager& pm);
/**
* @brief Add all available optimization passes
*/
static void addOptimizationPasses(PassManager& pm);
/**
* @brief Configure PassManager with diagnostic options
*
* @details
* Enables timing, statistics, and IR printing based on config flags.
* Uses MLIR's builtin PassManager configuration methods.
*/
void configurePassManager(PassManager& pm) const;
QuantumCompilerConfig config_;
};
/**
* @brief Utility to capture IR as string
*
* @details
* Prints the MLIR module to a string for recording or comparison.
*
* @param module The module to convert to string
* @return String representation of the IR
*/
std::string captureIR(ModuleOp module);
} // namespace mlir