/* Copyright 2025 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.
==============================================================================*/

#include "xprof/utils/hlo_module_utils.h"

#include <memory>

#include "<gtest/gtest.h>"
#include "absl/status/statusor.h"
#include "xla/hlo/ir/hlo_computation.h"
#include "xla/hlo/ir/hlo_instruction.h"
#include "xla/hlo/ir/hlo_module.h"
#include "xla/hlo/ir/hlo_module_metadata.h"
#include "xla/hlo/testlib/hlo_hardware_independent_test_base.h"
#include "xla/tsl/platform/statusor.h"

namespace tensorflow {
namespace profiler {
namespace {

class HloModuleUtilsTest : public xla::HloHardwareIndependentTestBase {
 protected:
  absl::StatusOr<std::unique_ptr<xla::HloModule>> GetModuleWithStackFrames() {
    const char file_name[] = "main.py";
    const char function_name1[] = "func1";
    const int line_number1 = 10;
    const int column_number1 = 5;
    const char function_name2[] = "func2";
    const int line_number2 = 20;
    const int column_number2 = 1;
    const char text[] = R"(
    HloModule a_module

    ENTRY main {
      %c = s32[] constant(1)
      ROOT %result = s32[] parameter(0)
    }
    )";
    TF_ASSIGN_OR_RETURN(auto module, ParseAndReturnVerifiedModule(text));

    auto module_proto = module->ToProto();
    auto index = module_proto.mutable_stack_frame_index();
    index->add_file_names(file_name);        // id 1
    index->add_function_names(function_name1);  // id 1
    index->add_function_names(function_name2);  // id 2

    auto location1 = index->add_file_locations();  // id 1
    location1->set_file_name_id(1);
    location1->set_function_name_id(1);
    location1->set_line(line_number1);
    location1->set_column(column_number1);

    auto location2 = index->add_file_locations();  // id 2
    location2->set_file_name_id(1);
    location2->set_function_name_id(2);
    location2->set_line(line_number2);
    location2->set_column(column_number2);

    auto frame1 = index->add_stack_frames();  // id 1
    frame1->set_file_location_id(1);
    frame1->set_parent_frame_id(0);

    auto frame2 = index->add_stack_frames();  // id 2
    frame2->set_file_location_id(2);
    frame2->set_parent_frame_id(1);

    // Set the stack frame id of the root instruction to frame 2.
    const int stack_frame_id = 2;
    for (auto& computation : *module_proto.mutable_computations()) {
      if (computation.id() == module_proto.entry_computation_id()) {
        for (auto& instruction : *computation.mutable_instructions()) {
          if (instruction.id() == computation.root_id()) {
            instruction.mutable_metadata()->set_stack_frame_id(stack_frame_id);
            instruction.mutable_metadata()->set_source_file(file_name);
            instruction.mutable_metadata()->set_source_line(line_number2);
          }
        }
      }
    }

    return xla::HloModule::CreateFromProto(module_proto, module->config());
  }

  absl::StatusOr<std::unique_ptr<xla::HloModule>>
  GetModuleWithSourceLocation() {
    const char file_name[] = "main.py";
    const int line_number = 20;
    const char text[] = R"(
    HloModule a_module

    ENTRY main {
      %c = s32[] constant(1)
      ROOT %result = s32[] parameter(0)
    }
    )";
    TF_ASSIGN_OR_RETURN(auto module, ParseAndReturnVerifiedModule(text));

    auto module_proto = module->ToProto();

    for (auto& computation : *module_proto.mutable_computations()) {
      if (computation.id() == module_proto.entry_computation_id()) {
        for (auto& instruction : *computation.mutable_instructions()) {
          if (instruction.id() == computation.root_id()) {
            instruction.mutable_metadata()->set_source_file(file_name);
            instruction.mutable_metadata()->set_source_line(line_number);
          }
        }
      }
    }

    return xla::HloModule::CreateFromProto(module_proto, module->config());
  }
};

TEST_F(HloModuleUtilsTest, TestGetLocationStack) {
  TF_ASSERT_OK_AND_ASSIGN(
      std::unique_ptr<xla::HloModule> module_with_stack_frames,
      GetModuleWithStackFrames());
  const auto* root_instruction =
      module_with_stack_frames->entry_computation()->root_instruction();
  EXPECT_EQ(GetOpLocationStack(xla::StackFrameId{2}, *root_instruction),
            "main.py:20:1\nmain.py:10:5\n");
}

TEST_F(HloModuleUtilsTest, TestGetSourceInfo) {
  TF_ASSERT_OK_AND_ASSIGN(
      std::unique_ptr<xla::HloModule> module_with_stack_frames,
      GetModuleWithStackFrames());
  const auto* root_instruction =
      module_with_stack_frames->entry_computation()->root_instruction();
  auto source_info = GetSourceInfo(*root_instruction);
  EXPECT_EQ(source_info.source_file, "main.py");
  EXPECT_EQ(source_info.source_line, 20);
  EXPECT_EQ(source_info.stack_frame, "main.py:20:1\nmain.py:10:5\n");
}

TEST_F(HloModuleUtilsTest, TestGetSourceInfoFallback) {
  TF_ASSERT_OK_AND_ASSIGN(
      std::unique_ptr<xla::HloModule> module_with_source_location,
      GetModuleWithSourceLocation());
  const auto* root_instruction =
      module_with_source_location->entry_computation()->root_instruction();
  auto source_info = GetSourceInfo(*root_instruction);
  EXPECT_EQ(source_info.source_file, "main.py");
  EXPECT_EQ(source_info.source_line, 20);
  EXPECT_EQ(source_info.stack_frame, "");
}

}  // namespace
}  // namespace profiler
}  // namespace tensorflow
