microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/compiler/qsc_codegen/src/qir.rs
121lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use qsc_data_structures::target::{Profile, TargetCapabilityFlags}; |
| 5 | use qsc_eval::val::Value; |
| 6 | use qsc_partial_eval::{ |
| 7 | PartialEvalConfig, Program, ProgramEntry, partially_evaluate, partially_evaluate_call, |
| 8 | }; |
| 9 | use qsc_rca::PackageStoreComputeProperties; |
| 10 | use qsc_rir::{passes::check_and_transform, rir}; |
| 11 | |
| 12 | pub mod v1; |
| 13 | pub mod v2; |
| 14 | |
| 15 | /// converts the given sources to RIR using the given language features. |
| 16 | pub fn fir_to_rir( |
| 17 | fir_store: &qsc_fir::fir::PackageStore, |
| 18 | capabilities: TargetCapabilityFlags, |
| 19 | compute_properties: &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. |
| 36 | pub fn fir_to_qir( |
| 37 | fir_store: &qsc_fir::fir::PackageStore, |
| 38 | capabilities: TargetCapabilityFlags, |
| 39 | compute_properties: &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. |
| 60 | pub fn fir_to_qir_from_callable( |
| 61 | fir_store: &qsc_fir::fir::PackageStore, |
| 62 | capabilities: TargetCapabilityFlags, |
| 63 | compute_properties: &PackageStoreComputeProperties, |
| 64 | callable: qsc_fir::fir::StoreItemId, |
| 65 | args: Value, |
| 66 | ) -> Result<String, qsc_partial_eval::Error> { |
| 67 | let mut program = partially_evaluate_call( |
| 68 | fir_store, |
| 69 | compute_properties, |
| 70 | callable, |
| 71 | args, |
| 72 | capabilities, |
| 73 | PartialEvalConfig { |
| 74 | generate_debug_metadata: false, |
| 75 | }, |
| 76 | )?; |
| 77 | check_and_transform(&mut program); |
| 78 | if capabilities <= Profile::AdaptiveRIF.into() { |
| 79 | Ok(v1::ToQir::<String>::to_qir(&program, &program)) |
| 80 | } else { |
| 81 | Ok(v2::ToQir::<String>::to_qir(&program, &program)) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /// converts the given callable to RIR using the given arguments and language features. |
| 86 | pub fn fir_to_rir_from_callable( |
| 87 | fir_store: &qsc_fir::fir::PackageStore, |
| 88 | capabilities: TargetCapabilityFlags, |
| 89 | compute_properties: &PackageStoreComputeProperties, |
| 90 | callable: qsc_fir::fir::StoreItemId, |
| 91 | args: Value, |
| 92 | partial_eval_config: PartialEvalConfig, |
| 93 | ) -> Result<(Program, Program), qsc_partial_eval::Error> { |
| 94 | let mut program = partially_evaluate_call( |
| 95 | fir_store, |
| 96 | compute_properties, |
| 97 | callable, |
| 98 | args, |
| 99 | capabilities, |
| 100 | partial_eval_config, |
| 101 | )?; |
| 102 | let orig = program.clone(); |
| 103 | check_and_transform(&mut program); |
| 104 | Ok((orig, program)) |
| 105 | } |
| 106 | |
| 107 | fn get_rir_from_compilation( |
| 108 | fir_store: &qsc_fir::fir::PackageStore, |
| 109 | compute_properties: &PackageStoreComputeProperties, |
| 110 | entry: &ProgramEntry, |
| 111 | capabilities: TargetCapabilityFlags, |
| 112 | partial_eval_config: PartialEvalConfig, |
| 113 | ) -> Result<rir::Program, qsc_partial_eval::Error> { |
| 114 | partially_evaluate( |
| 115 | fir_store, |
| 116 | compute_properties, |
| 117 | entry, |
| 118 | capabilities, |
| 119 | partial_eval_config, |
| 120 | ) |
| 121 | } |
| 122 | |