// Copyright (c) 2024 PaddlePaddle 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. #pragma once #include "paddle/ap/include/adt/adt.h" #include "paddle/ap/include/code_gen/dim_expr_kernel_arg_id.h" #include "paddle/ap/include/code_gen/in_tensor_data_ptr_kernel_arg_id.h" #include "paddle/ap/include/code_gen/out_tensor_data_ptr_kernel_arg_id.h" namespace ap::code_gen { template using KernelArgIdImpl = std::variant, InTensorDataPtrKernelArgId, OutTensorDataPtrKernelArgId>; template struct KernelArgId : public KernelArgIdImpl { using KernelArgIdImpl::KernelArgIdImpl; ADT_DEFINE_VARIANT_METHODS(KernelArgIdImpl); template ValueT CastTo() const { return Match([](const auto& impl) -> ValueT { return impl; }); } template static adt::Result CastFrom(const ValueT& val) { using RetT = adt::Result; return val.Match( [](const DimExprKernelArgId& impl) -> RetT { return impl; }, [](const InTensorDataPtrKernelArgId& impl) -> RetT { return impl; }, [](const OutTensorDataPtrKernelArgId& impl) -> RetT { return impl; }, [](const auto& impl) -> RetT { return adt::errors::TypeError{"KernelArgId::CastFrom() failed."}; }); } std::size_t GetHashValue() const { std::size_t hash_value = Match( [&](const auto& impl) -> std::size_t { return impl->GetHashValue(); }); return adt::hash_combine(this->index(), hash_value); } }; } // namespace ap::code_gen namespace std { template struct hash> { std::size_t operator()( const ap::code_gen::KernelArgId& arg_id) const { return arg_id.GetHashValue(); } }; } // namespace std