Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Error Message No. 27 BUAA】rewrite error message #67109

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 82 additions & 20 deletions paddle/cinn/backends/codegen_c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ void CodeGenC::Compile(const ir::Module &module, const Outputs &outputs) {
auto source = Compile(module, OutputKind::CHeader);
str_ = "";
std::ofstream file(outputs.c_header_name);
CHECK(file.is_open()) << "failed to open file " << outputs.c_header_name;
PADDLE_ENFORCE_EQ(file.is_open(),
true,
phi::errors::InvalidArgument("failed to open file %s",
outputs.c_header_name));
file << source;
file.close();
LOG(WARNING) << "Output C header to file " << outputs.c_header_name;
Expand All @@ -54,7 +57,10 @@ void CodeGenC::Compile(const ir::Module &module, const Outputs &outputs) {
auto source = Compile(module, OutputKind::CImpl);
str_ = "";
std::ofstream file(outputs.c_source_name);
CHECK(file.is_open()) << "failed to open file " << outputs.c_source_name;
PADDLE_ENFORCE_EQ(file.is_open(),
true,
phi::errors::InvalidArgument("failed to open file %s",
outputs.c_source_name));
file << source;
file.close();
LOG(WARNING) << "Output C source to file " << outputs.c_source_name;
Expand Down Expand Up @@ -83,7 +89,10 @@ std::string CodeGenC::Compile(const ir::Module &module,

// TODO(LiuYang): Here the Ret type seems unuseful
void CodeGenC::Compile(const ir::LoweredFunc &function) {
CHECK(function.defined());
PADDLE_ENFORCE_EQ(
function.defined(),
true,
phi::errors::InvalidArgument("The function is not defined."));
IrPrinter::Visit(function);
str_ += "\n\n";
}
Expand Down Expand Up @@ -116,7 +125,10 @@ std::string CodeGenC::GetTypeName(Type type) {

// customized_type
if (type.is_customized_type()) {
CHECK(!type.customized_type().empty()) << "customized_type can't be empty.";
PADDLE_ENFORCE_EQ(type.customized_type().empty(),
false,
phi::errors::InvalidArgument(
"customized_type is empty. It can't be empty."));
auto customized_name = type.customized_type();
// get name of a cuda built-in vector type, it is started with a
// 'CudaVectorType::' prefix
Expand Down Expand Up @@ -343,7 +355,12 @@ void CodeGenC::Visit(const ir::Call *op) {
emitter.BindCodeGen(this);
emitter.Emit(op);
} else {
CHECK(!op->read_args.empty() || !op->write_args.empty());
if (op->read_args.empty())
PADDLE_ENFORCE_EQ(
op->read_args.empty(),
false,
phi::errors::InvalidArgument(
"Either read_args or write_args must not be empty."));
str_ += op->name;
str_ += "(";
PrintCallArgs(op);
Expand Down Expand Up @@ -403,10 +420,16 @@ void CodeGenC::PrintCall_get_address(const ir::Call *op) {
op->read_args.size(),
1UL,
::common::errors::InvalidArgument("The number of read_args should be 1"));
CHECK(op->write_args.empty());
PADDLE_ENFORCE_EQ(op->write_args.empty(),
true,
phi::errors::InvalidArgument(
"write_args is not empty. It must be empty."));
auto *read_var = op->read_args.front().as_var();
auto *read_buf = op->read_args.front().as_buffer();
CHECK(read_var || read_buf) << "Only Var or Buffer can get address";
if (!read_var)
PADDLE_ENFORCE_NOT_NULL(
read_buf,
phi::errors::InvalidArgument("Only Var or Buffer can get address"));

if (read_var) {
if (read_var->type().lanes() <= 1) str_ += "&";
Expand All @@ -420,18 +443,28 @@ void CodeGenC::PrintCall_get_address(const ir::Call *op) {
}

void CodeGenC::PrintCall_pod_values_to_array(const ir::Call *op) {
CHECK(!op->read_args.empty());
PADDLE_ENFORCE_EQ(op->read_args.empty(),
false,
phi::errors::InvalidArgument(
"The read_args is empty. It should not be empty."));
PADDLE_ENFORCE_EQ(op->write_args.size(),
1UL,
::common::errors::InvalidArgument(
"The number of write_args should be 1"));
auto output_var = op->write_args.front().as_var_ref();
CHECK(output_var.defined());
PADDLE_ENFORCE_EQ(
output_var.defined(),
true,
phi::errors::InvalidArgument(
"The variable 'output_var' is not be defined. It must be defined."));

std::vector<std::string> arg_names;
for (auto &arg : op->read_args) {
auto arg_var = arg.as_var();
CHECK(arg_var);
PADDLE_ENFORCE_NOT_NULL(
arg_var,
phi::errors::InvalidArgument(
"The 'arg_var' is an invalid argument. It must be true."));
arg_names.push_back(arg_var->name);
}

Expand All @@ -458,7 +491,11 @@ void CodeGenC::Visit(const ir::Load *op) {

Expr dense_strided_ramp = detail::StridedRampBase(offset, 1);
if (dense_strided_ramp.defined()) { // Loading a continuous Ramp address.
CHECK(op->type().is_vector());
PADDLE_ENFORCE_EQ(
op->type().is_vector(),
true,
phi::errors::InvalidArgument(
"The operation type is not a vector. It must be a vector."));
PrintStackVecType(op->type().ElementOf(), offset.type().lanes());
str_ += "::";
str_ += "Load(";
Expand All @@ -468,7 +505,11 @@ void CodeGenC::Visit(const ir::Load *op) {
str_ += ")";
} else if (offset.type().is_vector()) {
// gather
CHECK(op->type().is_vector());
PADDLE_ENFORCE_EQ(
op->type().is_vector(),
true,
phi::errors::InvalidArgument(
"The operation type is not a vector. It must be a vector."));
PrintStackVecType(op->type().ElementOf(), offset.type().lanes());
str_ += "::Load(";
str_ += op->tensor.As<ir::_Tensor_>()->name;
Expand All @@ -487,15 +528,21 @@ void CodeGenC::Visit(const ir::Load *op) {
}

void CodeGenC::Visit(const ir::Store *op) {
CHECK(op->is_addr_tensor());
PADDLE_ENFORCE_EQ(
op->is_addr_tensor(),
true,
phi::errors::InvalidArgument(
"The operation type is invalid. It must be an address tensor."));
ir::Expr offset = [&] {
if (store_to_offset_.count(op) == 0) {
store_to_offset_[op] = op->index();
}
return store_to_offset_.at(op);
}();
auto *tensor = op->tensor.As<ir::_Tensor_>();
CHECK(tensor);
PADDLE_ENFORCE_NOT_NULL(
tensor,
phi::errors::InvalidArgument("The tensor is null. It must not be null."));
str_ += tensor->name;
str_ += "[";
IrPrinter::Visit(offset);
Expand Down Expand Up @@ -527,7 +574,10 @@ void CodeGenC::Visit(const ir::_Buffer_ *op) { str_ += op->name; }
void CodeGenC::Visit(const ir::_Tensor_ *op) { str_ += op->buffer->name; }
void CodeGenC::Visit(const ir::Let *op) {
bool is_vec = false;
CHECK(op->type().valid());
PADDLE_ENFORCE_EQ(op->type().valid(),
true,
phi::errors::InvalidArgument(
"The operation type is invalid. It must be valid."));
if (op->body.defined() && op->body.As<ir::Broadcast>()) {
// broadcast's type is hard to print, so use c++11 auto instead.
str_ += "auto";
Expand Down Expand Up @@ -850,7 +900,10 @@ void CodeGenC::Visit(const ir::intrinsics::PodValueToX *op) {

void CodeGenC::Visit(const ir::intrinsics::BufferCreate *op) {
const ir::_Buffer_ *buffer_arg = op->buffer.as_buffer();
CHECK(buffer_arg);
PADDLE_ENFORCE_NOT_NULL(
buffer_arg,
phi::errors::InvalidArgument(
"The buffer argument is invalid. It must be true."));

str_ += runtime::intrinsic::buffer_create;
str_ += "(";
Expand Down Expand Up @@ -912,17 +965,26 @@ void CodeGenC::Visit(const ir::intrinsics::BuiltinIntrin *op) {
}

std::string ReadWholeFile(const std::string &path) {
CHECK(!path.empty());
PADDLE_ENFORCE_EQ(
path.empty(),
false,
phi::errors::InvalidArgument("The path is empty. It must not be empty."));
std::ifstream file(path);
CHECK(file.is_open()) << "Failed to open file: " << path;
PADDLE_ENFORCE_EQ(
file.is_open(),
true,
phi::errors::InvalidArgument("Failed to open file: %s", path.c_str()));
std::stringstream ss;
ss << file.rdbuf();
return ss.str();
}

void CodeGenC::PrintBuiltinCodes() {
CHECK(!FLAGS_cinn_x86_builtin_code_root.empty())
<< "The flag cinn_x86_builtin_code_root should be set first";
PADDLE_ENFORCE_EQ(
FLAGS_cinn_x86_builtin_code_root.empty(),
false,
phi::errors::InvalidArgument(
"The flag cinn_x86_builtin_code_root should be set first"));

const std::string x86_code_file = "_x86_builtin_source.cc";

Expand Down