* Copyright (c) 2026 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
* @file ptr_ops.cpp
* \brief Pointer operations for ptoas IR scene (ptr.addptr, ptr.make_tensor)
*
* These ops emit PTO MLIR instructions (pto.addptr, pto.make_tensor_view)
*/
#include <any>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "core/dtype.h"
#include "core/logging.h"
#include "ir/expr.h"
#include "ir/kind_traits.h"
#include "ir/op_registry.h"
#include "ir/scalar_expr.h"
#include "ir/type.h"
#include "ir/type_inference.h"
#include "pypto_pro/error.h"
namespace pypto {
namespace ir {
using npu::tile_fwk::ExternalError;
TypePtr DeduceAddPtrType([[maybe_unused]] const std::vector<ExprPtr>& args,
[[maybe_unused]] const std::vector<std::pair<std::string, std::any>>& kwargs)
{
PRO_IR_CHECK(ExternalError::INVALID_ARGUMENT, args.size() == 0x2)
<< "ptr.addptr requires exactly 2 arguments (ptr, offset), but got " << args.size();
auto ptr_type = As<PtrType>(args[0]->GetType());
PRO_IR_CHECK(ExternalError::INVALID_TYPE, ptr_type)
<< "ptr.addptr requires first argument to be a PtrType, but got " << args[0]->GetType()->TypeName()
<< ". Use pl.Ptr[dtype] to annotate pointer parameters.";
auto offset_type = As<ScalarType>(args[1]->GetType());
PRO_IR_CHECK(ExternalError::INVALID_TYPE, offset_type)
<< "ptr.addptr requires second argument (offset) to be a ScalarType, but got "
<< args[1]->GetType()->TypeName();
PRO_IR_CHECK(ExternalError::INVALID_TYPE,
offset_type->dtype_.IsInt() || offset_type->dtype_ == DataType(DataType::INDEX))
<< "ptr.addptr offset must have integer or index dtype, but got " << offset_type->dtype_.ToString();
ExprPtr new_base_ptr;
ExprPtr new_offset;
if (ptr_type->base_ptr.has_value()) {
new_base_ptr = *ptr_type->base_ptr;
if (auto c1 = As<ConstInt>(*ptr_type->offset)) {
if (auto c2 = As<ConstInt>(args[1])) {
new_offset = std::make_shared<ConstInt>(c1->value_ + c2->value_, DataType::INT64, args[1]->span_);
} else {
new_offset = std::make_shared<Add>(*ptr_type->offset, args[1], DataType::INT64, args[1]->span_);
}
} else {
new_offset = std::make_shared<Add>(*ptr_type->offset, args[1], DataType::INT64, args[1]->span_);
}
} else {
new_base_ptr = args[0];
new_offset = args[1];
}
return std::make_shared<PtrType>(ptr_type->dtype_, new_base_ptr, new_offset);
}
REGISTER_OP("ptr.addptr")
.set_op_category("PtrOp")
.set_description("Advance a pointer by an integer offset (emits pto.addptr)")
.add_argument("ptr", "Input raw pointer (PtrType)")
.add_argument("offset", "Integer byte offset (ScalarType with integer/index dtype)")
.f_deduce_type([]([[maybe_unused]] const std::vector<ExprPtr>& args,
[[maybe_unused]] const std::vector<std::pair<std::string, std::any>>& kwargs) {
return DeduceAddPtrType(args, kwargs);
});
TypePtr DeduceMakePtrType([[maybe_unused]] const std::vector<ExprPtr>& args,
[[maybe_unused]] const std::vector<std::pair<std::string, std::any>>& kwargs)
{
PRO_IR_CHECK(ExternalError::INVALID_ARGUMENT, args.size() == 1)
<< "ptr.make_ptr requires exactly 1 argument (ptr), but got " << args.size();
auto ptr_type = As<PtrType>(args[0]->GetType());
auto tensor_type = As<TensorType>(args[0]->GetType());
PRO_IR_CHECK(ExternalError::INVALID_TYPE, ptr_type || tensor_type)
<< "ptr.make_ptr requires first argument to be a PtrType or TensorType, but got "
<< args[0]->GetType()->TypeName()
<< ". Use pl.Ptr[dtype] for pointer params or pl.Tensor[[shape], dtype] for tensor params.";
DataType source_dtype = ptr_type ? ptr_type->dtype_ : tensor_type->dtype_;
DataType result_dtype = GetOpKwarg<DataType>(kwargs, "dtype", std::optional<DataType>(source_dtype));
if (ptr_type && ptr_type->base_ptr.has_value() && ptr_type->offset.has_value()) {
return std::make_shared<PtrType>(result_dtype, *ptr_type->base_ptr, *ptr_type->offset);
}
return std::make_shared<PtrType>(result_dtype);
}
REGISTER_OP("ptr.make_ptr")
.set_op_category("PtrOp")
.set_description("Reinterpret a pointer as a different element dtype (emits a pointer cast)")
.add_argument("ptr", "Input raw pointer (PtrType)")
.set_attr<DataType>("dtype")
.f_deduce_type([]([[maybe_unused]] const std::vector<ExprPtr>& args,
[[maybe_unused]] const std::vector<std::pair<std::string, std::any>>& kwargs) {
return DeduceMakePtrType(args, kwargs);
});
TypePtr DeduceMakeTensorType([[maybe_unused]] const std::vector<ExprPtr>& args,
[[maybe_unused]] const std::vector<std::pair<std::string, std::any>>& kwargs)
{
PRO_IR_CHECK(ExternalError::INVALID_ARGUMENT, args.size() == 0x3)
<< "ptr.make_tensor requires exactly 3 arguments (ptr, shape, stride), but got " << args.size();
DataType source_dtype;
if (auto ptr_type = As<PtrType>(args[0]->GetType())) {
source_dtype = ptr_type->dtype_;
} else if (auto src_tensor_type = As<TensorType>(args[0]->GetType())) {
source_dtype = src_tensor_type->dtype_;
} else {
PRO_IR_CHECK(ExternalError::INVALID_TYPE, false)
<< "ptr.make_tensor requires first argument to be a PtrType or TensorType, but got "
<< args[0]->GetType()->TypeName()
<< ". Use pl.Ptr[dtype] to annotate pointer parameters, or pass an existing pl.Tensor.";
}
auto shape_tuple = As<MakeTuple>(args[1]);
PRO_IR_CHECK(ExternalError::INVALID_SHAPE, shape_tuple) << "ptr.make_tensor requires shape to be a MakeTuple";
auto stride_tuple = As<MakeTuple>(args[2]);
PRO_IR_CHECK(ExternalError::INVALID_TYPE, stride_tuple) << "ptr.make_tensor requires stride to be a MakeTuple";
const auto& stride = stride_tuple->elements_;
if (!stride.empty()) {
PRO_IR_CHECK(ExternalError::INVALID_SHAPE, shape_tuple->elements_.size() == stride.size())
<< "ptr.make_tensor shape rank (" << shape_tuple->elements_.size() << ") must match stride rank ("
<< stride.size() << ")";
}
DataType result_dtype = GetOpKwarg<DataType>(kwargs, "dtype", std::optional<DataType>(source_dtype));
constexpr size_t kBitsPerByte = 8;
if (!stride.empty() && result_dtype.GetBit() < kBitsPerByte) {
auto innermost = As<ConstInt>(stride.back());
PRO_IR_CHECK(ExternalError::INVALID_ARGUMENT, innermost != nullptr && innermost->value_ == 1)
<< "ptr.make_tensor: a " << result_dtype.ToString()
<< " view must be contiguous along its innermost axis (stride 1), because two elements "
<< "share a byte there and a half-byte cannot be addressed; got "
<< (innermost != nullptr ? std::to_string(innermost->value_) : std::string("a runtime stride"));
}
TensorView tv(stride, TensorLayout::ND, args[0]);
return std::make_shared<TensorType>(shape_tuple->elements_, result_dtype, std::nullopt, tv);
}
REGISTER_OP("ptr.make_tensor")
.set_op_category("PtrOp")
.set_description("Create a tensor view from a pointer or an existing tensor with shape and optional strides"
" (an empty stride means contiguous row-major layout; emits pto.make_tensor_view)")
.add_argument("ptr", "Input raw pointer (PtrType) or source tensor (TensorType)")
.add_argument("shape", "New shape dimensions (MakeTuple of ConstInt)")
.add_argument("stride", "Stride per dimension (MakeTuple of ConstInt or Var; empty means contiguous)")
.set_attr<DataType>("dtype")
.f_deduce_type([]([[maybe_unused]] const std::vector<ExprPtr>& args,
[[maybe_unused]] const std::vector<std::pair<std::string, std::any>>& kwargs) {
return DeduceMakeTensorType(args, kwargs);
});
}
}