2525#include " qasm3/passes/ConstEvalPass.hpp"
2626#include " qasm3/passes/TypeCheckPass.hpp"
2727
28- #include < llvm/ADT/ArrayRef.h>
2928#include < llvm/ADT/DenseSet.h>
3029#include < llvm/ADT/STLExtras.h>
31- #include < llvm/ADT/SmallVector.h>
3230#include < llvm/ADT/StringMap.h>
3331#include < llvm/Support/raw_ostream.h>
3432#include < mlir/Dialect/Arith/IR/Arith.h>
3735#include < mlir/IR/MLIRContext.h>
3836#include < mlir/IR/OwningOpRef.h>
3937#include < mlir/IR/Value.h>
38+ #include < mlir/Support/LLVM.h>
4039
4140#include < cstddef>
4241#include < cstdint>
@@ -60,8 +59,8 @@ namespace {
6059// / Signature: (builder, gate-operands, evaluated-parameters).
6160// / For gates with implicit controls (cx, ccx, ...) all qubits including
6261// / the controls are in the qubits array, matching QASM3 operand order.
63- using GateFn = std::function< void (QCProgramBuilder&, llvm::ArrayRef<Value>,
64- llvm:: ArrayRef<double >)>;
62+ using GateFn =
63+ std::function< void (QCProgramBuilder&, ArrayRef<Value>, ArrayRef<double >)>;
6564
6665/* *
6766 * Build the static gate-name → GateFn dispatch table.
@@ -189,7 +188,7 @@ static const llvm::StringMap<GateFn> GATE_DISPATCH = buildGateDispatch();
189188
190189// / Local qubit scope used during compound gate body expansion.
191190// / Maps argument name → vector of MLIR qubit Values.
192- using QubitScope = llvm::StringMap<llvm:: SmallVector<Value>>;
191+ using QubitScope = llvm::StringMap<SmallVector<Value>>;
193192
194193/* * AST visitor that translates a QASM3 program directly into the QC dialect.
195194 *
@@ -232,7 +231,7 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
232231
233232 // / Measurement result tracking: register name → vector of i1 Values.
234233 // / Updated each time a measure is emitted. Used for if/else conditions.
235- llvm::StringMap<llvm:: SmallVector<Value>> bitValues;
234+ llvm::StringMap<SmallVector<Value>> bitValues;
236235
237236 // / Gate library: standard gates + user-defined compound gates
238237 std::map<std::string, std::shared_ptr<qasm3::Gate>> gates;
@@ -309,7 +308,7 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
309308 switch (sizedTy->type ) {
310309 case qasm3::Qubit: {
311310 auto reg = builder.allocQubitRegister (size);
312- llvm:: SmallVector<Value> qubits;
311+ SmallVector<Value> qubits;
313312 qubits.reserve (static_cast <size_t >(size));
314313 for (int64_t i = 0 ; i < size; ++i) {
315314 qubits.push_back (reg[static_cast <size_t >(i)]);
@@ -450,7 +449,7 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
450449
451450 void visitBarrierStatement (
452451 std::shared_ptr<qasm3::BarrierStatement> stmt) override {
453- llvm:: SmallVector<Value> qubits;
452+ SmallVector<Value> qubits;
454453 for (const auto & gate : stmt->gates ) {
455454 auto resolved = resolveGateOperand (gate, stmt->debugInfo );
456455 qubits.append (resolved.begin (), resolved.end ());
@@ -551,7 +550,7 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
551550 bool invert = false ;
552551 size_t nModifierControls = 0 ;
553552 // (count, isPositive) per ctrl modifier, in order
554- llvm:: SmallVector<std::pair<size_t , bool >> ctrlSpec;
553+ SmallVector<std::pair<size_t , bool >> ctrlSpec;
555554 for (const auto & mod : stmt->modifiers ) {
556555 if (std::dynamic_pointer_cast<qasm3::InvGateModifier>(mod)) {
557556 invert = !invert;
@@ -570,16 +569,16 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
570569 }
571570
572571 // Expand each operand to its qubit Values
573- std::vector<llvm:: SmallVector<Value>> expandedOperands;
572+ std::vector<SmallVector<Value>> expandedOperands;
574573 expandedOperands.reserve (stmt->operands .size ());
575574 for (const auto & operand : stmt->operands ) {
576575 expandedOperands.push_back (
577576 resolveGateOperandInScope (operand, scope, stmt->debugInfo ));
578577 }
579578
580579 // First nModifierControls slots are modifier-derived controls
581- llvm:: SmallVector<Value> posControls;
582- llvm:: SmallVector<Value> negControls;
580+ SmallVector<Value> posControls;
581+ SmallVector<Value> negControls;
583582 size_t ctrlIdx = 0 ;
584583 for (const auto & [n, positive] : ctrlSpec) {
585584 for (size_t i = 0 ; i < n; ++i, ++ctrlIdx) {
@@ -608,7 +607,7 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
608607 const size_t totalCtrlCount = nModifierControls + implicitCompatControls;
609608
610609 // Remaining slots are the gate's own operands (may broadcast)
611- std::vector<llvm:: SmallVector<Value>> gateOperands (
610+ std::vector<SmallVector<Value>> gateOperands (
612611 expandedOperands.begin () + static_cast <std::ptrdiff_t >(totalCtrlCount),
613612 expandedOperands.end ());
614613
@@ -654,7 +653,7 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
654653 }
655654
656655 for (size_t b = 0 ; b < broadcastWidth; ++b) {
657- llvm:: SmallVector<Value> iterQubits;
656+ SmallVector<Value> iterQubits;
658657 iterQubits.reserve (gateOperands.size ());
659658 for (const auto & ops : gateOperands) {
660659 iterQubits.push_back (ops.size () > 1 ? ops[b] : ops[0 ]);
@@ -677,15 +676,14 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
677676 }
678677
679678 // / Emit a single gate application, wrapping with ctrl/inv as needed.
680- void emitGate (const GateFn& gateFn, llvm::ArrayRef<Value> qubits,
681- llvm::ArrayRef<double > params,
682- llvm::ArrayRef<Value> posControls,
683- llvm::ArrayRef<Value> negControls, bool invert) {
679+ void emitGate (const GateFn& gateFn, ArrayRef<Value> qubits,
680+ ArrayRef<double > params, ArrayRef<Value> posControls,
681+ ArrayRef<Value> negControls, bool invert) {
684682 auto inner = [&] { gateFn (builder, qubits, params); };
685683
686684 auto withInv = [&] {
687685 if (invert) {
688- builder.inv (llvm:: function_ref<void ()>(inner));
686+ builder.inv (function_ref<void ()>(inner));
689687 } else {
690688 inner ();
691689 }
@@ -700,23 +698,21 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
700698 for (auto q : negControls) {
701699 builder.x (q);
702700 }
703- llvm::SmallVector<Value> allControls (posControls.begin (),
704- posControls.end ());
701+ SmallVector<Value> allControls (posControls.begin (), posControls.end ());
705702 allControls.append (negControls.begin (), negControls.end ());
706- builder.ctrl (allControls, llvm:: function_ref<void ()>(withInv));
703+ builder.ctrl (allControls, function_ref<void ()>(withInv));
707704 for (auto q : negControls) {
708705 builder.x (q);
709706 }
710707 }
711708
712709 // / Inline-expand a compound (user-defined) gate.
713- void
714- applyCompoundGate (const qasm3::CompoundGate& gate,
715- const std::vector<llvm::SmallVector<Value>>& gateOperands,
716- llvm::ArrayRef<Value> posControls,
717- llvm::ArrayRef<Value> negControls,
718- llvm::ArrayRef<double > params, bool invert,
719- const std::shared_ptr<qasm3::DebugInfo>& debugInfo) {
710+ void applyCompoundGate (const qasm3::CompoundGate& gate,
711+ const std::vector<SmallVector<Value>>& gateOperands,
712+ ArrayRef<Value> posControls,
713+ ArrayRef<Value> negControls, ArrayRef<double > params,
714+ bool invert,
715+ const std::shared_ptr<qasm3::DebugInfo>& debugInfo) {
720716 if (gate.targetNames .size () != gateOperands.size ()) {
721717 throw qasm3::CompilerError (" Compound gate operand count mismatch." ,
722718 debugInfo);
@@ -729,8 +725,8 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
729725 // Build local scope: argument name → Values
730726 QubitScope localScope;
731727 for (size_t i = 0 ; i < gate.targetNames .size (); ++i) {
732- localScope[gate.targetNames [i]] = llvm::SmallVector<Value>(
733- gateOperands[i].begin (), gateOperands[i].end ());
728+ localScope[gate.targetNames [i]] =
729+ SmallVector<Value>( gateOperands[i].begin (), gateOperands[i].end ());
734730 }
735731
736732 // Bind parameters as constants
@@ -748,7 +744,7 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
748744 } else if (const auto barrier =
749745 std::dynamic_pointer_cast<qasm3::BarrierStatement>(
750746 bodyStmt)) {
751- llvm:: SmallVector<Value> qubits;
747+ SmallVector<Value> qubits;
752748 for (const auto & g : barrier->gates ) {
753749 auto resolved =
754750 resolveGateOperandInScope (g, localScope, barrier->debugInfo );
@@ -768,7 +764,7 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
768764
769765 auto withInv = [&] {
770766 if (invert) {
771- builder.inv (llvm:: function_ref<void ()>(bodyFn));
767+ builder.inv (function_ref<void ()>(bodyFn));
772768 } else {
773769 bodyFn ();
774770 }
@@ -780,10 +776,9 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
780776 for (auto q : negControls) {
781777 builder.x (q);
782778 }
783- llvm::SmallVector<Value> allControls (posControls.begin (),
784- posControls.end ());
779+ SmallVector<Value> allControls (posControls.begin (), posControls.end ());
785780 allControls.append (negControls.begin (), negControls.end ());
786- builder.ctrl (allControls, llvm:: function_ref<void ()>(withInv));
781+ builder.ctrl (allControls, function_ref<void ()>(withInv));
787782 for (auto q : negControls) {
788783 builder.x (q);
789784 }
@@ -1011,7 +1006,7 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
10111006 // ===--- Operand resolution helpers ------------------------------------===//
10121007
10131008 // / Resolve a gate operand against the top-level qubit register map.
1014- llvm:: SmallVector<Value>
1009+ SmallVector<Value>
10151010 resolveGateOperand (const std::shared_ptr<qasm3::GateOperand>& operand,
10161011 const std::shared_ptr<qasm3::DebugInfo>& debugInfo) {
10171012 return resolveGateOperandInScope (operand, qubitRegisters, debugInfo);
@@ -1021,7 +1016,7 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
10211016 * compound-gate local argument scope). Returns the MLIR Values for the
10221017 * qubit(s) named by \p operand — a full register or a single indexed qubit.
10231018 */
1024- llvm:: SmallVector<Value> resolveGateOperandInScope (
1019+ SmallVector<Value> resolveGateOperandInScope (
10251020 const std::shared_ptr<qasm3::GateOperand>& operand,
10261021 const QubitScope& scope,
10271022 const std::shared_ptr<qasm3::DebugInfo>& debugInfo) {
0 commit comments