|
| 1 | +use std::{ |
| 2 | + collections::{BTreeMap, HashMap}, |
| 3 | + future::{self, Future}, |
| 4 | +}; |
| 5 | + |
| 6 | +use acvm::{acir::circuit::Opcode, Language}; |
| 7 | +use async_lsp::{ErrorCode, ResponseError}; |
| 8 | +use nargo::artifacts::debug::DebugArtifact; |
| 9 | +use nargo_toml::{find_package_manifest, resolve_workspace_from_toml, PackageSelection}; |
| 10 | +use noirc_driver::{CompileOptions, DebugFile, NOIR_ARTIFACT_VERSION_STRING}; |
| 11 | +use noirc_errors::{debug_info::OpCodesCount, Location}; |
| 12 | + |
| 13 | +use crate::{ |
| 14 | + types::{NargoProfileRunParams, NargoProfileRunResult}, |
| 15 | + LspState, |
| 16 | +}; |
| 17 | +use fm::FileId; |
| 18 | + |
| 19 | +pub(crate) fn on_profile_run_request( |
| 20 | + state: &mut LspState, |
| 21 | + params: NargoProfileRunParams, |
| 22 | +) -> impl Future<Output = Result<NargoProfileRunResult, ResponseError>> { |
| 23 | + future::ready(on_profile_run_request_inner(state, params)) |
| 24 | +} |
| 25 | + |
| 26 | +fn on_profile_run_request_inner( |
| 27 | + state: &LspState, |
| 28 | + params: NargoProfileRunParams, |
| 29 | +) -> Result<NargoProfileRunResult, ResponseError> { |
| 30 | + let root_path = state.root_path.as_deref().ok_or_else(|| { |
| 31 | + ResponseError::new(ErrorCode::REQUEST_FAILED, "Could not find project root") |
| 32 | + })?; |
| 33 | + |
| 34 | + let toml_path = find_package_manifest(root_path, root_path).map_err(|err| { |
| 35 | + // If we cannot find a manifest, we can't run the test |
| 36 | + ResponseError::new(ErrorCode::REQUEST_FAILED, err) |
| 37 | + })?; |
| 38 | + |
| 39 | + let crate_name = params.package; |
| 40 | + |
| 41 | + let workspace = resolve_workspace_from_toml( |
| 42 | + &toml_path, |
| 43 | + PackageSelection::DefaultOrAll, |
| 44 | + Some(NOIR_ARTIFACT_VERSION_STRING.to_string()), |
| 45 | + ) |
| 46 | + .map_err(|err| { |
| 47 | + // If we found a manifest, but the workspace is invalid, we raise an error about it |
| 48 | + ResponseError::new(ErrorCode::REQUEST_FAILED, err) |
| 49 | + })?; |
| 50 | + |
| 51 | + // Since we filtered on crate name, this should be the only item in the iterator |
| 52 | + match workspace.into_iter().next() { |
| 53 | + Some(_package) => { |
| 54 | + let (binary_packages, contract_packages): (Vec<_>, Vec<_>) = workspace |
| 55 | + .into_iter() |
| 56 | + .filter(|package| !package.is_library()) |
| 57 | + .cloned() |
| 58 | + .partition(|package| package.is_binary()); |
| 59 | + |
| 60 | + // # TODO(#3504): Consider how to incorporate Backend relevant information in wider context. |
| 61 | + let is_opcode_supported = |_opcode: &Opcode| true; |
| 62 | + let np_language = Language::PLONKCSat { width: 3 }; |
| 63 | + |
| 64 | + let (compiled_programs, compiled_contracts) = nargo::ops::compile_workspace( |
| 65 | + &workspace, |
| 66 | + &binary_packages, |
| 67 | + &contract_packages, |
| 68 | + np_language, |
| 69 | + is_opcode_supported, |
| 70 | + &CompileOptions::default(), |
| 71 | + ) |
| 72 | + .map_err(|err| ResponseError::new(ErrorCode::REQUEST_FAILED, err))?; |
| 73 | + |
| 74 | + let mut opcodes_counts: HashMap<Location, OpCodesCount> = HashMap::new(); |
| 75 | + let mut file_map: BTreeMap<FileId, DebugFile> = BTreeMap::new(); |
| 76 | + for compiled_program in &compiled_programs { |
| 77 | + let span_opcodes = compiled_program.debug.count_span_opcodes(); |
| 78 | + let debug_artifact: DebugArtifact = compiled_program.clone().into(); |
| 79 | + opcodes_counts.extend(span_opcodes); |
| 80 | + file_map.extend(debug_artifact.file_map); |
| 81 | + } |
| 82 | + |
| 83 | + for compiled_contract in &compiled_contracts { |
| 84 | + let functions = &compiled_contract.functions; |
| 85 | + let debug_artifact: DebugArtifact = compiled_contract.clone().into(); |
| 86 | + file_map.extend(debug_artifact.file_map); |
| 87 | + for contract_function in functions { |
| 88 | + let span_opcodes = contract_function.debug.count_span_opcodes(); |
| 89 | + opcodes_counts.extend(span_opcodes); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + let result = NargoProfileRunResult { file_map, opcodes_counts }; |
| 94 | + |
| 95 | + Ok(result) |
| 96 | + } |
| 97 | + None => Err(ResponseError::new( |
| 98 | + ErrorCode::REQUEST_FAILED, |
| 99 | + format!("Could not locate package named: {crate_name}"), |
| 100 | + )), |
| 101 | + } |
| 102 | +} |
0 commit comments