// 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/axpr/pointer_type_util.h" #include "paddle/ap/include/code_gen/kernel_arg_id.h" #include "paddle/ap/include/code_module/arg_type.h" namespace ap::code_gen { template struct KernelArgIdHelper { using BirNativeIrValue = typename BirNode::native_value_type; template adt::Result> CastToKernelArgId(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{ std::string() + "only DimExprKernelArgId, InTensorDataPtrKernelArgId and " "OutTensorDataPtrKernelArgId (not including '" + axpr::GetTypeName(val) + "') can be cast to KernelArgId"}; }); } adt::Result GetArgType( const KernelArgId& arg_id) { using RetT = adt::Result; return arg_id.Match( [](const DimExprKernelArgId&) -> RetT { return axpr::CppDataType(); }, [&](const InTensorDataPtrKernelArgId& in_data_ptr) -> RetT { ADT_LET_CONST_REF(ir_value, GetBirNativeIrValue(in_data_ptr->ir_value)); ADT_LET_CONST_REF(data_type, ir_value.GetDataType()); return axpr::GetConstPointerTypeFromDataType(data_type); }, [&](const OutTensorDataPtrKernelArgId& out_data_ptr) -> RetT { ADT_LET_CONST_REF(ir_value, GetBirNativeIrValue(out_data_ptr->ir_value)); ADT_LET_CONST_REF(data_type, ir_value.GetDataType()); return axpr::GetMutablePointerTypeFromDataType(data_type); }); } adt::Result GetBirNativeIrValue( const BirNode& bir_node) const { using RetT = adt::Result; return bir_node.Match( [&](const BirNativeIrValue& impl) -> RetT { return impl; }, [&](const typename BirNode::ref_value_type& impl) -> RetT { return impl.GetOwnerNativeIrValue(); }, [&](const auto& impl) -> RetT { using T = std::decay_t; return adt::errors::NotImplementedError{ std::string() + "GetBirNativeIrValue() failed. only 'NativeIrValue' and " "'RefIrValue' argument expected, but '" + typeid(T).name() + "' found."}; }); } }; } // namespace ap::code_gen