Skip to content

Commit 4444207

Browse files
committed
split transforms into post-parsing and pre-compilation
Any program rewriting and optimization now happens in compiler-hook.
1 parent e5950b7 commit 4444207

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/cl-quil.lisp

+9-8
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
(in-package #:cl-quil.frontend)
77

8-
(defvar *standard-post-process-transforms*
9-
'(validate-defgate-loops expand-circuits type-check simplify-individual-instructions)
10-
"The standard transforms that are applied by PARSE-QUIL.")
8+
(defvar *standard-post-parsing-transforms*
9+
'(validate-defgate-loops expand-circuits type-check)
10+
"The standard transforms that are applied by PARSE-QUIL after parsing. (See also: *STANDARD-PRE-COMPILATION-TRANSFORMS*)")
1111

1212
(define-condition ambiguous-definition-condition ()
1313
((instruction :initarg :instruction
@@ -118,8 +118,9 @@ This also signals ambiguous definitions, which may be handled as needed."
118118
2. PARSER-EXTENSIONS is a list of parser functions which PARSE-PROGRAM-LINES may dispatch to.
119119
3. LEXER-EXTENSIONS is a list of lexer functions which LINE-LEXER may dispatch to."
120120
(handler-bind
121-
;; We disallow multiple declarations of the same memory region (even if equivalent).
122-
;; Otherwise, for gate or circuit definitions, the default choice is to "accept the mystery."
121+
;; We disallow multiple declarations of the same memory region
122+
;; (even if equivalent). Otherwise, for gate or circuit
123+
;; definitions, the default choice is to "accept the mystery."
123124
((ambiguous-definition-condition (a:disjoin
124125
#'error-on-ambiguous-memory-declaration
125126
ambiguous-definition-handler)))
@@ -163,7 +164,7 @@ This also signals ambiguous definitions, which may be handled as needed."
163164
(return nil)))))))
164165

165166
(defun parse (string &key originating-file
166-
(transforms *standard-post-process-transforms*)
167+
(transforms *standard-post-parsing-transforms*)
167168
(ambiguous-definition-handler #'continue))
168169
"Parse the input STRING which can be either Quil or OpenQASM code."
169170
(if (%check-for-qasm-header string)
@@ -174,7 +175,7 @@ This also signals ambiguous definitions, which may be handled as needed."
174175
:ambiguous-definition-handler ambiguous-definition-handler)))
175176

176177
(defun parse-quil (string &key originating-file
177-
(transforms *standard-post-process-transforms*)
178+
(transforms *standard-post-parsing-transforms*)
178179
(ambiguous-definition-handler #'continue))
179180
"Parse and process the Quil string STRING, which originated from the file ORIGINATING-FILE. Transforms in TRANSFORMS are applied in-order to the processed Quil string.
180181
@@ -263,7 +264,7 @@ In the presence of multiple definitions with a common signature, a signal is rai
263264
(read-it filename)))))
264265

265266
(defun safely-parse-quil (string &key originating-file
266-
(transforms *standard-post-process-transforms*)
267+
(transforms *standard-post-parsing-transforms*)
267268
(ambiguous-definition-handler #'continue))
268269
"Safely parse a Quil string STRING."
269270
(flet ((parse-it (string)

src/compiler-hook.lisp

+13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
;;;; For historical reasons, this entry point has been called
1212
;;;; "COMPILER-HOOK".
1313

14+
15+
(defvar *standard-pre-compilation-transforms*
16+
'(simplify-individual-instructions)
17+
"The standard transforms that are applied by COMPILER-HOOK before compilation. (See also: *STANDARD-POST-PARSING-TRANSFORMS*)")
18+
1419
;; Forward declaration from compressor.lisp
1520
(declaim (special *compressor-passes*))
1621

@@ -22,6 +27,7 @@
2227
&key
2328
(protoquil nil)
2429
(rewiring-type (prog-initial-rewiring-heuristic parsed-program chip-specification))
30+
(transforms *standard-pre-compilation-transforms*)
2531
(destructive nil))
2632
"Runs a full compiler pass on a parsed-program object.
2733
@@ -55,8 +61,15 @@ Returns a value list: (processed-program, of type parsed-program
5561
(> (qubits-needed parsed-program)
5662
(length (chip-spec-live-qubits chip-specification)))
5763
(not (parsed-program-has-preserve-blocks-p parsed-program)))
64+
(format-noise "COMPILER-HOOK: Compressing qubits.")
5865
(setf parsed-program (compress-qubits parsed-program)))
5966

67+
;; Apply transforms
68+
(dolist (xform transforms)
69+
(format-noise "COMPILER-HOOK: Applying transform ~A." xform)
70+
(setf parsed-program (transform xform parsed-program)))
71+
72+
;; Warm the lookup cache
6073
(warm-chip-spec-lookup-cache chip-specification)
6174

6275
;; we disallow compilation of programs that use memory aliasing

0 commit comments

Comments
 (0)