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

Code before:
    if (requiredSize == size) {
      continue;
    }
    buffer.resize(requiredSize);
    return {std::move(buffer)};
  }
#else
  std::ignore = address;
#endif
  return {};
}

// OrtEpApi infrastructure to be able to use the QNN EP as an OrtEpFactory for auto EP selection.
struct QnnEpFactory : OrtEpFactory {
  QnnEpFactory(const OrtApi& ort_api_in,
               const OrtLogger& default_logger_in,
               const char* ep_name,
               std::unordered_map<OrtHardwareDeviceType, std::string> supported_backends)
      : ort_api{ort_api_in},
        default_logger{default_logger_in},
        ep_name{ep_name},
        supported_backends{std::move(supported_backends)} {
    ort_version_supported = ORT_API_VERSION;
    GetName = GetNameImpl;
    GetVendor = GetVendorImpl;
    GetVendorId = GetVendorIdImpl;
    GetVersion = GetVersionImpl;
    GetSupportedDevices = GetSupportedDevicesImpl;
    CreateEp = CreateEpImpl;
    ReleaseEp = ReleaseEpImpl;

    CreateAllocator = CreateAllocatorImpl;
    ReleaseAllocator = ReleaseAllocatorImpl;
    CreateDataTransfer = CreateDataTransferImpl;
    IsStreamAware = IsStreamAwareImpl;
    CreateSyncStreamForDevice = CreateSyncStreamForDeviceImpl;
  }

  // Returns the name for the EP. Each unique factory configuration must have a unique name.
  // Ex: a factory that supports NPU should have a different than a factory that supports GPU.
  static const char* GetNameImpl(const OrtEpFactory* this_ptr) noexcept {
    const auto* factory = static_cast<const QnnEpFactory*>(this_ptr);
    return factory->ep_name.c_str();
  }

  static const char* GetVendorImpl(const OrtEpFactory* this_ptr) noexcept {
    const auto* factory = static_cast<const QnnEpFactory*>(this_ptr);
    return factory->ep_vendor.c_str();
  }

  static uint32_t GetVendorIdImpl(const OrtEpFactory* this_ptr) noexcept {

Review comment (bug):
nit: I don't believe the condition `device_to_use == nullptr` will ever be true, given the current logic above. perhaps this selection logic can be simplified.

Code after:
      const OrtHardwareDevice* device_to_use = nullptr;
      if (num_devices == 0) {
        return Status(common::ONNXRUNTIME, ORT_EP_FAIL, "No devices were provided to QNN EP.");
      } else if (num_devices == 1) {
        device_to_use = devices[0];
      } else {
        const auto is_npu = [](const OrtHardwareDevice* device) { return device->type == OrtHardwareDeviceType_NPU; };
        const auto is_gpu = [](const OrtHardwareDevice* device) { return device->type == OrtHardwareDeviceType_GPU; };

        auto device_it = std::find_if(devices, devices + num_devices, is_npu);
        if (device_it != devices + num_devices) {
          LOGS_DEFAULT(WARNING) << "QNN EP only supports one device. Only the NPU device will be used.";
          device_to_use = *device_it;
        } else {
          device_it = std::find_if(devices, devices + num_devices, is_gpu);
          if (device_it != devices + num_devices) {
            LOGS_DEFAULT(WARNING)
                << "QNN EP only supports one device. An NPU device was not provided, so only the GPU device will be used.";
            device_to_use = *device_it;
          } else {
            return Status(common::ONNXRUNTIME, ORT_EP_FAIL,
                          "Multiple devices were provided to QNN EP, but neither an NPU nor a GPU was included.");
          }
        }
      }
      ORT_RETURN_IF(device_to_use == nullptr, "Failed to select device for QNN EP!");

      auto default_backends_it = kDefaultBackends.find(device_to_use->type);
      ORT_RETURN_IF(default_backends_it == kDefaultBackends.end(),
                    "Could not determine default backend path for device of type: ", device_to_use->type);

      // Identify the path of the current dynamic library, and expect that the backend library is in the same directory.
      onnxruntime::PathString current_path = GetDynamicLibraryLocationByAddress(
          reinterpret_cast<const void*>(&kDefaultBackends));

      std::filesystem::path parent_path;
      if (!current_path.empty()) {
        parent_path = std::filesystem::path{std::move(current_path)}.parent_path();
      }

      provider_options["backend_path"] = (parent_path / default_backends_it->second).string();
    }

    const ConfigOptions* config_options = &session_options.GetConfigOptions();

    std::array<const void*, 2> configs_array = {&provider_options, config_options};
    const void* arg = reinterpret_cast<const void*>(&configs_array);
    auto ep_factory = CreateExecutionProviderFactory(arg);
    ep = ep_factory->CreateProvider(session_options, logger);

    return Status::OK();