未验证 提交 1d2a91c6 编写于 作者: W winter-wang 提交者: GitHub

[IR] optimize the error log. (#55465)

上级 74206917
......@@ -18,18 +18,6 @@ OP_GET_INPUT_TEMPLATE = """ ir::Value {input_name}() {{ return operand({input_i
"""
OP_GET_OUTPUT_TEMPLATE = """ ir::OpResult {output_name}() {{ return result({output_index}); }}
"""
OP_GET_ATTRIBUTE_TEMPLATE = """ ir::Attribute attribute(const std::string &name) {
PADDLE_ENFORCE(attributes().count(name) > 0,
phi::errors::PreconditionNotMet("Attribute is not exist."));
return attributes().at(name);
}
template <typename T>
T attribute(const std::string &name) {
PADDLE_ENFORCE(attributes().count(name) > 0 && attributes().at(name).isa<T>(),
phi::errors::PreconditionNotMet("Attribute is not right."));
return attributes().at(name).dyn_cast<T>();
}
"""
def gen_op_get_inputs_outputs_str(
......@@ -51,5 +39,4 @@ def gen_op_get_inputs_outputs_str(
output_name=op_output_name_list[idx],
output_index=idx,
)
op_get_inputs_outputs_str += OP_GET_ATTRIBUTE_TEMPLATE
return op_get_inputs_outputs_str
......@@ -135,7 +135,10 @@ struct ArrayAttributeStorage : public AttributeStorage {
bool empty() const { return size_ == 0u; }
Attribute at(size_t index) const {
IR_ENFORCE(index < size_, "Invalid index");
IR_ENFORCE(index < size_,
"The index (%d) must be less than size (%d).",
index,
size_);
return data_[index];
}
......
......@@ -15,6 +15,7 @@
#pragma once
#include <type_traits>
#include "paddle/ir/core/enforce.h"
#include "paddle/ir/core/operation.h"
#include "paddle/ir/core/utils.h"
......@@ -68,25 +69,37 @@ class IR_API OpBase {
public:
explicit OpBase(Operation *operation = nullptr) : operation_(operation) {}
Operation *operation() const { return operation_; }
Operation *operation() const {
IR_ENFORCE(operation_, "Can't use operation() in a null op.");
return operation_;
}
explicit operator bool() const { return operation_ != nullptr; }
operator Operation *() const { return operation(); }
explicit operator bool() const { return operation() != nullptr; }
Operation *operator->() const { return operation(); }
operator Operation *() const { return operation_; }
IrContext *ir_context() const { return operation()->ir_context(); }
Operation *operator->() const { return operation_; }
uint32_t num_results() const { return operation()->num_results(); }
IrContext *ir_context() const { return operation_->ir_context(); }
uint32_t num_operands() const { return operation()->num_operands(); }
uint32_t num_results() const { return operation_->num_results(); }
const AttributeMap &attributes() const { return operation()->attributes(); }
uint32_t num_operands() const { return operation_->num_operands(); }
Value operand(uint32_t index) const { return operation()->operand(index); }
const AttributeMap &attributes() const { return operation_->attributes(); }
OpResult result(uint32_t index) const { return operation()->result(index); }
Value operand(uint32_t index) const { return operation_->operand(index); }
ir::Attribute attribute(const std::string &name) {
return operation()->attribute(name);
}
OpResult result(uint32_t index) const { return operation_->result(index); }
template <typename T>
T attribute(const std::string &name) {
return operation()->attribute<T>(name);
}
private:
Operation *operation_; // Not owned
......
......@@ -69,9 +69,9 @@ class IR_API alignas(8) Operation final {
template <typename T>
T attribute(const std::string &name) {
IR_ENFORCE(attributes().count(name) > 0 && attributes().at(name).isa<T>(),
"Attribute is not right.");
return attributes().at(name).dyn_cast<T>();
Attribute attr = attribute(name);
IR_ENFORCE(attr.isa<T>(), "Attribute (%s) type is not right.", name);
return attr.dyn_cast<T>();
}
void set_attribute(const std::string &key, Attribute value) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册