Skip to content

Commit 54586a6

Browse files
idaviscesarzc
andauthored
Add prereqs check and docs for building on mac (microsoft#1649)
Created to help with microsoft#1642 --------- Co-authored-by: César Zaragoza Cortés <cesarzc@microsoft.com>
1 parent e6c204a commit 54586a6

9 files changed

Lines changed: 57 additions & 41 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ To build this repository there are dependencies that need to be installed. These
2727

2828
- Python (<https://python.org>)
2929
- Rust (<https://www.rust-lang.org/tools/install>)
30+
- On all platforms, the `wasm32-unknown-unknown` must be installed to build the WASM based components
31+
```shell
32+
rustup target add wasm32-unknown-unknown
33+
```
34+
- On MacOS, ensure that both `aarch64` and `x86_64` targets are installed or you will encounter linking errors.
35+
```shell
36+
rustup target add x86_64-apple-darwin
37+
rustup target add aarch64-apple-darwin
38+
```
3039
- Node.js (<https://nodejs.org/>)
3140
- wasm-pack (<https://rustwasm.github.io/wasm-pack/installer/>)
3241
- A [C compiler](https://docs.rs/cc/latest/cc/#compile-time-requirements)

allocator/mimalloc-sys/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub static MI_ALIGNMENT_MAX: usize = 1024 * 1024; // 1 MiB
88
extern "C" {
99
/// Allocate size bytes aligned by alignment.
1010
/// size: the number of bytes to allocate
11-
/// alignment: the minimal alignment of the allocated memory. Must be less than MI_ALIGNMENT_MAX
11+
/// alignment: the minimal alignment of the allocated memory. Must be less than `MI_ALIGNMENT_MAX`
1212
/// returns: a pointer to the allocated memory, or null if out of memory. The returned pointer is aligned by alignment
1313
pub fn mi_malloc_aligned(size: usize, alignment: usize) -> *mut c_void;
1414
pub fn mi_zalloc_aligned(size: usize, alignment: usize) -> *mut c_void;

compiler/qsc_partial_eval/src/evaluation_context.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub struct Scope {
101101
/// Map that holds the values of local variables.
102102
hybrid_vars: FxHashMap<LocalVarId, Value>,
103103
/// Maps variable IDs to mutable variables, which contain their current kind.
104-
mutable_vars: FxHashMap<VariableId, MutableVar>,
104+
mutable_vars: FxHashMap<VariableId, MutableKind>,
105105
/// Number of currently active blocks (starting from where this scope was created).
106106
active_block_count: usize,
107107
}
@@ -166,12 +166,12 @@ impl Scope {
166166
}
167167

168168
/// Gets a mutable variable.
169-
pub fn find_mutable_var(&self, var_id: VariableId) -> Option<&MutableVar> {
169+
pub fn find_mutable_kind(&self, var_id: VariableId) -> Option<&MutableKind> {
170170
self.mutable_vars.get(&var_id)
171171
}
172172

173173
/// Gets a mutable mutable variable.
174-
pub fn find_mutable_var_mut(&mut self, var_id: VariableId) -> Option<&mut MutableVar> {
174+
pub fn find_mutable_var_mut(&mut self, var_id: VariableId) -> Option<&mut MutableKind> {
175175
self.mutable_vars.get_mut(&var_id)
176176
}
177177

@@ -197,11 +197,11 @@ impl Scope {
197197
}
198198

199199
// Insert a variable into the mutable variables map.
200-
pub fn insert_mutable_var(&mut self, var_id: VariableId, mutable_var: MutableVar) {
200+
pub fn insert_mutable_var(&mut self, var_id: VariableId, mutable_kind: MutableKind) {
201201
let Entry::Vacant(vacant) = self.mutable_vars.entry(var_id) else {
202202
panic!("mutable variable should not already exist");
203203
};
204-
vacant.insert(mutable_var);
204+
vacant.insert(mutable_kind);
205205
}
206206

207207
/// Determines whether we are currently evaluating a branch within the scope.
@@ -318,11 +318,7 @@ fn map_eval_value_to_value_kind(value: &Value) -> ValueKind {
318318
}
319319
}
320320

321-
pub struct MutableVar {
322-
pub id: LocalVarId,
323-
pub kind: MutableKind,
324-
}
325-
321+
#[derive(Clone, Copy, Debug)]
326322
pub enum MutableKind {
327323
Static(Literal),
328324
Dynamic,

compiler/qsc_partial_eval/src/lib.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ mod management;
1010

1111
use core::panic;
1212
use evaluation_context::{
13-
Arg, BlockNode, BranchControlFlow, EvalControlFlow, EvaluationContext, MutableKind, MutableVar,
14-
Scope,
13+
Arg, BlockNode, BranchControlFlow, EvalControlFlow, EvaluationContext, MutableKind, Scope,
1514
};
1615
use management::{QuantumIntrinsicsChecker, ResourceManager};
1716
use miette::Diagnostic;
@@ -223,11 +222,11 @@ impl<'a> PartialEvaluator<'a> {
223222
}
224223

225224
// Always bind the value to the hybrid map but do it differently depending of the value type.
226-
if let Some((var_id, mutable_var)) = self.try_create_mutable_variable(ident.id, &value) {
225+
if let Some((var_id, mutable_kind)) = self.try_create_mutable_variable(ident.id, &value) {
227226
// Keep track of whether the mutable variable is static or dynamic.
228227
self.eval_context
229228
.get_current_scope_mut()
230-
.insert_mutable_var(var_id, mutable_var);
229+
.insert_mutable_var(var_id, mutable_kind);
231230
} else {
232231
self.bind_value_in_hybrid_map(ident, value);
233232
}
@@ -1743,10 +1742,8 @@ impl<'a> PartialEvaluator<'a> {
17431742
// the variable if it is static at this moment.
17441743
if let Value::Var(var) = bound_value {
17451744
let current_scope = self.eval_context.get_current_scope();
1746-
if let Some(MutableVar {
1747-
kind: MutableKind::Static(literal),
1748-
..
1749-
}) = current_scope.find_mutable_var(var.id.into())
1745+
if let Some(MutableKind::Static(literal)) =
1746+
current_scope.find_mutable_kind(var.id.into())
17501747
{
17511748
map_rir_literal_to_eval_value(*literal)
17521749
} else {
@@ -2164,7 +2161,7 @@ impl<'a> PartialEvaluator<'a> {
21642161
&mut self,
21652162
local_var_id: LocalVarId,
21662163
value: &Value,
2167-
) -> Option<(rir::VariableId, MutableVar)> {
2164+
) -> Option<(rir::VariableId, MutableKind)> {
21682165
// Check if we can create a mutable variable for this value.
21692166
let var_ty = try_get_eval_var_type(value)?;
21702167

@@ -2189,11 +2186,8 @@ impl<'a> PartialEvaluator<'a> {
21892186
Operand::Literal(literal) => MutableKind::Static(literal),
21902187
Operand::Variable(_) => MutableKind::Dynamic,
21912188
};
2192-
let mutable_var = MutableVar {
2193-
id: local_var_id,
2194-
kind: mutable_kind,
2195-
};
2196-
Some((var_id, mutable_var))
2189+
2190+
Some((var_id, mutable_kind))
21972191
}
21982192

21992193
fn get_or_insert_callable(&mut self, callable: Callable) -> CallableId {
@@ -2485,8 +2479,9 @@ impl<'a> PartialEvaluator<'a> {
24852479
if matches!(rhs_operand, Operand::Variable(_))
24862480
|| current_scope.is_currently_evaluating_branch()
24872481
{
2488-
if let Some(mutable_var) = current_scope.find_mutable_var_mut(rir_var.variable_id) {
2489-
mutable_var.kind = MutableKind::Dynamic;
2482+
if let Some(mutable_kind) = current_scope.find_mutable_var_mut(rir_var.variable_id)
2483+
{
2484+
*mutable_kind = MutableKind::Dynamic;
24902485
}
24912486
}
24922487
} else {

compiler/qsc_rca/src/common.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ use std::{
1818
fmt::{Debug, Formatter},
1919
};
2020

21-
/// A represenation of a local symbol.
21+
/// A representation of a local symbol.
2222
#[derive(Clone, Debug)]
2323
pub struct Local {
2424
pub var: LocalVarId,
25-
pub pat: PatId,
2625
pub ty: Ty,
2726
pub kind: LocalKind,
2827
}
@@ -58,7 +57,6 @@ pub fn initialize_locals_map(input_params: &Vec<InputParam>) -> FxHashMap<LocalV
5857
id,
5958
Local {
6059
var: id,
61-
pat: param.pat,
6260
ty: param.ty.clone(),
6361
kind: LocalKind::InputParam(param.index),
6462
},

compiler/qsc_rca/src/core.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,6 @@ impl<'a> Analyzer<'a> {
13131313
let application_instance = self.get_current_application_instance_mut();
13141314
let local = Local {
13151315
var: ident.id,
1316-
pat: pat.id,
13171316
ty: pat.ty.clone(),
13181317
kind: local_kind,
13191318
};
@@ -2479,7 +2478,6 @@ fn derive_specialization_controls(
24792478
match &pat.kind {
24802479
PatKind::Bind(ident) => Some(Local {
24812480
var: ident.id,
2482-
pat: pat_id,
24832481
ty: pat.ty.clone(),
24842482
kind: LocalKind::SpecInput,
24852483
}),

compiler/qsc_rca/src/cycle_detection.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ impl<'a> CycleDetector<'a> {
5656
locals_map.insert(
5757
ident.id,
5858
Local {
59-
pat: pat_id,
6059
var: ident.id,
6160
ty: pat.ty.clone(),
6261
kind,

prereqs.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@
2626
print = functools.partial(print, flush=True)
2727

2828

29+
def get_installed_rust_targets() -> str:
30+
try:
31+
args = ["rustup", "target", "list", "--installed"]
32+
return subprocess.check_output(args, universal_newlines=True)
33+
except subprocess.CalledProcessError as e:
34+
message = f"Unable to determine installed rust targets: {str(e)}"
35+
raise Exception(message)
36+
37+
2938
def check_prereqs(install=False):
3039
### Check the Python version ###
3140
if (
@@ -110,6 +119,24 @@ def check_prereqs(install=False):
110119
else:
111120
raise Exception("Unable to determine the clippy version")
112121

122+
installed_rust_targets = get_installed_rust_targets()
123+
124+
# Ensure the required wasm target is installed
125+
target = "wasm32-unknown-unknown"
126+
if target not in installed_rust_targets:
127+
print("WASM rust target is not installed.")
128+
print("Please install the missing target by running:")
129+
print("rustup target add wasm32-unknown-unknown")
130+
131+
# On MacOS, ensure the required targets are installed
132+
if platform.system() == "Darwin":
133+
targets = ["aarch64-apple-darwin", "x86_64-apple-darwin"]
134+
if not all(target in installed_rust_targets for target in targets):
135+
print("One or both rust targets are not installed.")
136+
print("Please install the missing targets by running:")
137+
print("rustup target add aarch64-apple-darwin")
138+
print("rustup target add x86_64-apple-darwin")
139+
113140
### Check the Node.js version ###
114141
try:
115142
node_version = subprocess.check_output(["node", "-v"])

resource_estimator/src/counts.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ use num_complex::Complex;
99
use qsc::{interpret::Value, Backend};
1010
use rand::{rngs::StdRng, Rng, SeedableRng};
1111
use rustc_hash::FxHashMap;
12-
use std::{
13-
array,
14-
cell::RefCell,
15-
f64::{consts::PI, EPSILON},
16-
fmt::Debug,
17-
iter::Sum,
18-
};
12+
use std::{array, cell::RefCell, f64::consts::PI, fmt::Debug, iter::Sum};
1913

2014
use crate::system::LogicalResourceCounts;
2115

@@ -456,7 +450,7 @@ impl Backend for LogicalCounter {
456450

457451
fn rz(&mut self, theta: f64, q: usize) {
458452
let multiple = (theta / (PI / 4.0)).round();
459-
if ((multiple * (PI / 4.0)) - theta).abs() <= EPSILON {
453+
if ((multiple * (PI / 4.0)) - theta).abs() <= f64::EPSILON {
460454
let multiple = (multiple as i64).rem_euclid(8) as u64;
461455
if multiple & 1 == 1 {
462456
self.t(q);

0 commit comments

Comments
 (0)