microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
joaoboechat/test-get-azdo-userid

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/compiler/qsc_codegen/src/qir.rs

136lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use qsc_data_structures::target::{Profile, TargetCapabilityFlags};
5use qsc_eval::val::Value;
6use qsc_partial_eval::{
7 PartialEvalConfig, Program, ProgramEntry, partially_evaluate, partially_evaluate_call,
8};
9use qsc_rca::PackageStoreComputeProperties;
10use qsc_rir::{passes::check_and_transform, rir};
11
12pub mod v1;
13pub mod v2;
14
15/// converts the given sources to RIR using the given language features.
16pub fn fir_to_rir(
17 fir_store: &qsc_fir::fir::PackageStore,
18 capabilities: TargetCapabilityFlags,
19 compute_properties: Option<PackageStoreComputeProperties>,
20 entry: &ProgramEntry,
21 partial_eval_config: PartialEvalConfig,
22) -> Result<(Program, Program), qsc_partial_eval::Error> {
23 let mut program = get_rir_from_compilation(
24 fir_store,
25 compute_properties,
26 entry,
27 capabilities,
28 partial_eval_config,
29 )?;
30 let orig = program.clone();
31 check_and_transform(&mut program);
32 Ok((orig, program))
33}
34
35/// converts the given sources to QIR using the given language features.
36pub fn fir_to_qir(
37 fir_store: &qsc_fir::fir::PackageStore,
38 capabilities: TargetCapabilityFlags,
39 compute_properties: Option<PackageStoreComputeProperties>,
40 entry: &ProgramEntry,
41) -> Result<String, qsc_partial_eval::Error> {
42 let mut program = get_rir_from_compilation(
43 fir_store,
44 compute_properties,
45 entry,
46 capabilities,
47 PartialEvalConfig {
48 generate_debug_metadata: false,
49 },
50 )?;
51 check_and_transform(&mut program);
52 if capabilities <= Profile::AdaptiveRIF.into() {
53 Ok(v1::ToQir::<String>::to_qir(&program, &program))
54 } else {
55 Ok(v2::ToQir::<String>::to_qir(&program, &program))
56 }
57}
58
59/// converts the given callable to QIR using the given arguments and language features.
60pub fn fir_to_qir_from_callable(
61 fir_store: &qsc_fir::fir::PackageStore,
62 capabilities: TargetCapabilityFlags,
63 compute_properties: Option<PackageStoreComputeProperties>,
64 callable: qsc_fir::fir::StoreItemId,
65 args: Value,
66) -> Result<String, qsc_partial_eval::Error> {
67 let compute_properties = compute_properties.unwrap_or_else(|| {
68 let analyzer = qsc_rca::Analyzer::init(fir_store, capabilities);
69 analyzer.analyze_all()
70 });
71
72 let mut program = partially_evaluate_call(
73 fir_store,
74 &compute_properties,
75 callable,
76 args,
77 capabilities,
78 PartialEvalConfig {
79 generate_debug_metadata: false,
80 },
81 )?;
82 check_and_transform(&mut program);
83 if capabilities <= Profile::AdaptiveRIF.into() {
84 Ok(v1::ToQir::<String>::to_qir(&program, &program))
85 } else {
86 Ok(v2::ToQir::<String>::to_qir(&program, &program))
87 }
88}
89
90/// converts the given callable to RIR using the given arguments and language features.
91pub fn fir_to_rir_from_callable(
92 fir_store: &qsc_fir::fir::PackageStore,
93 capabilities: TargetCapabilityFlags,
94 compute_properties: Option<PackageStoreComputeProperties>,
95 callable: qsc_fir::fir::StoreItemId,
96 args: Value,
97 partial_eval_config: PartialEvalConfig,
98) -> Result<(Program, Program), qsc_partial_eval::Error> {
99 let compute_properties = compute_properties.unwrap_or_else(|| {
100 let analyzer = qsc_rca::Analyzer::init(fir_store, capabilities);
101 analyzer.analyze_all()
102 });
103
104 let mut program = partially_evaluate_call(
105 fir_store,
106 &compute_properties,
107 callable,
108 args,
109 capabilities,
110 partial_eval_config,
111 )?;
112 let orig = program.clone();
113 check_and_transform(&mut program);
114 Ok((orig, program))
115}
116
117fn get_rir_from_compilation(
118 fir_store: &qsc_fir::fir::PackageStore,
119 compute_properties: Option<PackageStoreComputeProperties>,
120 entry: &ProgramEntry,
121 capabilities: TargetCapabilityFlags,
122 partial_eval_config: PartialEvalConfig,
123) -> Result<rir::Program, qsc_partial_eval::Error> {
124 let compute_properties = compute_properties.unwrap_or_else(|| {
125 let analyzer = qsc_rca::Analyzer::init(fir_store, capabilities);
126 analyzer.analyze_all()
127 });
128
129 partially_evaluate(
130 fir_store,
131 &compute_properties,
132 entry,
133 capabilities,
134 partial_eval_config,
135 )
136}
137