/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef XPROF_UTILS_HLO_PROTO_MAP_H_
#define XPROF_UTILS_HLO_PROTO_MAP_H_

#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "xla/service/hlo.pb.h"
#include "xla/tsl/profiler/utils/xplane_visitor.h"
#include "tsl/profiler/protobuf/xplane.pb.h"

namespace tensorflow {
namespace profiler {

using ::tsl::profiler::XPlaneVisitor;

absl::flat_hash_map<uint64_t /*program_id*/, std::unique_ptr<xla::HloProto>>
ParseHloProtosFromXSpace(const tensorflow::profiler::XSpace& space);

class HloProtoMap {
 public:
  HloProtoMap() = default;
  virtual ~HloProtoMap() = default;

  // HloProtoMap manages unique_ptrs, so it cannot be implicitly copied.
  // Explicitly delete copy constructor and assignment, and provide default move
  // counterparts.
  HloProtoMap(const HloProtoMap&) = delete;
  HloProtoMap& operator=(const HloProtoMap&) = delete;
  HloProtoMap(HloProtoMap&&) = default;
  HloProtoMap& operator=(HloProtoMap&&) = default;

  void AddHloProtosFromXSpace(const tensorflow::profiler::XSpace& space);

  void AddHloProto(uint64_t program_id,
                   std::unique_ptr<const xla::HloProto> hlo_proto);

  size_t size() const { return hlo_protos_by_program_id_.size(); }
  bool empty() const { return hlo_protos_by_program_id_.empty(); }

  auto begin() const { return hlo_protos_by_program_id_.begin(); }
  auto end() const { return hlo_protos_by_program_id_.end(); }

  bool ContainsOptimizedModule(absl::string_view name) const {
    return hlo_protos_by_name_.contains(name);
  }

  bool ContainsOriginalModule(absl::string_view name) const {
    return original_hlo_protos_by_name_.contains(name);
  }

  bool contains(uint64_t program_id) const {
    return hlo_protos_by_program_id_.contains(program_id);
  }

  // Returns a list of HLO module names. If `is_original` is true, returns the
  // names of the original/unoptimized modules; otherwise, returns the names of
  // the optimized modules. The order of the modules is not guaranteed.
  std::vector<absl::string_view> GetModuleList(bool is_original = false) const;

  // Returns a list of module names sorted alphabetically.
  std::vector<absl::string_view> GetSortedModuleList() const;

  // Returns a list of hlo module names sorted first by heap trace size and then
  // by hlo module name alphabetically.
  std::vector<absl::string_view> GetSortedModuleListByHeapTraceSize() const;

  absl::StatusOr<const xla::HloProto*> GetHloProtoByModuleName(
      absl::string_view module_name) const;

  absl::StatusOr<const xla::HloProto*> GetHloProtoByProgramId(
      uint64_t program_id) const;

  // Original/Unoptimized HLO protos.
  void AddOriginalHloProto(uint64_t program_id,
                           std::unique_ptr<const xla::HloProto> hlo_proto);

  absl::StatusOr<const xla::HloProto*> GetOriginalHloProtoByProgramId(
      uint64_t program_id) const;

  absl::StatusOr<const xla::HloProto*> GetOriginalHloProtoByModuleName(
      absl::string_view module_name) const;

 private:
  absl::flat_hash_map<uint64_t, const xla::HloProto*> hlo_protos_by_program_id_;
  absl::flat_hash_map<std::string, const xla::HloProto*> hlo_protos_by_name_;
  std::vector<std::unique_ptr<const xla::HloProto>> owned_hlo_protos_;

  // Try to add proto to the map and returns true if the addition is successful
  // (i.e., the proto is new to the map).
  bool AddHloProto(uint64_t program_id, const xla::HloProto* hlo_proto);

  // Original/Unoptimized HLO protos.
  absl::flat_hash_map<uint64_t, const xla::HloProto*>
      original_hlo_protos_by_program_id_;
  absl::flat_hash_map<std::string, const xla::HloProto*>
      original_hlo_protos_by_name_;
  std::vector<std::unique_ptr<const xla::HloProto>> owned_original_hlo_protos_;

  bool AddOriginalHloProto(uint64_t program_id, const xla::HloProto* hlo_proto);
};

}  // namespace profiler
}  // namespace tensorflow

#endif  // XPROF_UTILS_HLO_PROTO_MAP_H_
