Repository: microsoft/onnxruntime
File: onnxruntime/core/providers/qnn/qnn_execution_provider.cc (C++)

Code before:
      ParseHtpPerformanceMode(value, htp_performance_mode);

      uint32_t htp_power_config_id = 0;
      if (GetHtpPowerConfigId(htp_power_config_id)) {
        ORT_RETURN_IF_ERROR(qnn_backend_manager_->SetHtpPowerConfig(htp_power_config_id,
                                                                    htp_performance_mode));
      }
    } else {
      LOGS_DEFAULT(ERROR) << "EP Dynamic Option \"" << key << "\" is not currently supported.";
      return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "Unsupported EP Dynamic Option");
    }

    key_it++;
    value_it++;
  }

  return Status::OK();
}

bool QNNExecutionProvider::GetHtpPowerConfigId(uint32_t& htp_power_config_id) {
  std::lock_guard<std::mutex> lock(config_id_mutex_);
  if (!htp_power_config_id_.has_value()) {
    return false;
  }

  htp_power_config_id = htp_power_config_id_.value();
  return true;
}

void QNNExecutionProvider::CreateHtpPowerConfigId() const {
  std::lock_guard<std::mutex> lock(config_id_mutex_);
  if (htp_power_config_id_.has_value()) {
    return;
  }

  constexpr uint32_t core_id = 0;
  uint32_t htp_power_config_id;

  Status rt = qnn_backend_manager_->CreateHtpPowerCfgId(device_id_, core_id, htp_power_config_id);

  if (rt == Status::OK()) {
    htp_power_config_id_ = htp_power_config_id;

    if (qnn::HtpPerformanceMode::kHtpDefault != default_htp_performance_mode_) {
      ORT_IGNORE_RETURN_VALUE(qnn_backend_manager_->SetHtpPowerConfig(htp_power_config_id,
                                                                      default_htp_performance_mode_));
    }
    if (default_rpc_control_latency_ > 0 || default_rpc_polling_time_ > 0) {
      ORT_IGNORE_RETURN_VALUE(qnn_backend_manager_->SetRpcPowerConfigs(htp_power_config_id,
                                                                       default_rpc_control_latency_,
                                                                       default_rpc_polling_time_));

Review comment (suggestion):
```suggestion
  htp_power_config_id = *htp_power_config_id_;
```

Code after:
      ParseHtpPerformanceMode(value, htp_performance_mode);

      uint32_t htp_power_config_id = 0;
      if (GetHtpPowerConfigId(htp_power_config_id)) {
        ORT_RETURN_IF_ERROR(qnn_backend_manager_->SetHtpPowerConfig(htp_power_config_id,
                                                                    htp_performance_mode));
      }
    } else {
      LOGS_DEFAULT(ERROR) << "EP Dynamic Option \"" << key << "\" is not currently supported.";
      return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "Unsupported EP Dynamic Option");
    }

    key_it++;
    value_it++;
  }

  return Status::OK();
}

bool QNNExecutionProvider::GetHtpPowerConfigId(uint32_t& htp_power_config_id) {
  std::lock_guard<std::mutex> lock(config_id_mutex_);
  if (!htp_power_config_id_.has_value()) {
    return false;
  }

  htp_power_config_id = *htp_power_config_id_;
  return true;
}

void QNNExecutionProvider::CreateHtpPowerConfigId() const {
  std::lock_guard<std::mutex> lock(config_id_mutex_);
  if (htp_power_config_id_.has_value()) {
    return;
  }

  constexpr uint32_t core_id = 0;
  uint32_t htp_power_config_id;

  Status rt = qnn_backend_manager_->CreateHtpPowerCfgId(device_id_, core_id, htp_power_config_id);

  if (rt == Status::OK()) {
    htp_power_config_id_ = htp_power_config_id;

    if (qnn::HtpPerformanceMode::kHtpDefault != default_htp_performance_mode_) {
      ORT_IGNORE_RETURN_VALUE(qnn_backend_manager_->SetHtpPowerConfig(htp_power_config_id,
                                                                      default_htp_performance_mode_));
    }
    if (default_rpc_control_latency_ > 0 || default_rpc_polling_time_ > 0) {
      ORT_IGNORE_RETURN_VALUE(qnn_backend_manager_->SetRpcPowerConfigs(htp_power_config_id,
                                                                       default_rpc_control_latency_,
                                                                       default_rpc_polling_time_));