Skip to content

Commit

Permalink
add unit rest.
Browse files Browse the repository at this point in the history
  • Loading branch information
winter-wang committed Mar 10, 2024
1 parent 1fd5d17 commit 2603c13
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 14 deletions.
24 changes: 20 additions & 4 deletions paddle/common/enforce.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,24 @@ class CommonNotMetException : public std::exception {
};

namespace enforce {

TEST_API void SkipPaddleFatal(bool skip = true);
TEST_API bool IsPaddleFatalSkip();

namespace details {

class PaddleFatalGuard {
public:
PaddleFatalGuard() : skip_paddle_fatal_(IsPaddleFatalSkip()) {
if (!skip_paddle_fatal_) SkipPaddleFatal(true);
}
~PaddleFatalGuard() {
if (!skip_paddle_fatal_) SkipPaddleFatal(false);
}

private:
bool skip_paddle_fatal_;
};
template <typename T>
struct CanToString {
private:
Expand Down Expand Up @@ -107,16 +124,13 @@ struct BinaryCompareMessageConverter<false> {
#define __THROW_ERROR_INTERNAL__(__ERROR_SUMMARY) \
do { \
HANDLE_THE_ERROR \
::common::enforce::SkipPaddleFatal(); \
throw ::common::enforce::EnforceNotMet( \
__ERROR_SUMMARY, __FILE__, __LINE__); \
END_HANDLE_THE_ERROR \
} while (0)

} // namespace details

TEST_API void SkipPaddleFatal(bool skip = true);
TEST_API bool IsPaddleFatalSkip();
TEST_API int GetCallStackLevel();
TEST_API std::string SimplifyErrorTypeFormat(const std::string& str);
TEST_API std::string GetCurrentTraceBackString(bool for_signal = false);
Expand Down Expand Up @@ -207,6 +221,8 @@ struct EnforceNotMet : public std::exception {
// Simple error message used when no C++ stack and python compile stack
// e.g. (InvalidArgument) ***
std::string simple_err_str_;

details::PaddleFatalGuard paddle_fatal_guard_;
};
/** HELPER MACROS AND FUNCTIONS **/
#ifndef PADDLE_MAY_THROW
Expand Down Expand Up @@ -368,6 +384,7 @@ class IrNotMetException : public std::exception {

private:
std::string err_str_;
::common::enforce::details::PaddleFatalGuard paddle_fatal_guard_;
};

#define IR_THROW(...) \
Expand All @@ -389,7 +406,6 @@ class IrNotMetException : public std::exception {
bool __cond__(COND); \
if (UNLIKELY(is_error(__cond__))) { \
try { \
::common::enforce::SkipPaddleFatal(); \
throw pir::IrNotMetException( \
paddle::string::Sprintf("Error occurred at: %s:%d :\n%s", \
__FILE__, \
Expand Down
1 change: 0 additions & 1 deletion paddle/phi/core/enforce.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ void ThrowWarnInternal(const std::string& message);
#define PADDLE_THROW(...) \
do { \
HANDLE_THE_ERROR \
::common::enforce::SkipPaddleFatal(); \
throw ::common::enforce::EnforceNotMet( \
::common::ErrorSummary(__VA_ARGS__), __FILE__, __LINE__); \
END_HANDLE_THE_ERROR \
Expand Down
2 changes: 1 addition & 1 deletion paddle/pir/include/core/op_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ typedef void (*VerifyPtr)(Operation *op);

class IR_API OpInfo {
public:
OpInfo() = default;
OpInfo(std::nullptr_t ptr = nullptr){}; // NOLINT

OpInfo(const OpInfo &other) = default;

Expand Down
2 changes: 1 addition & 1 deletion paddle/pir/include/core/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ValueImpl;
///
class IR_API Value {
public:
Value() = default;
Value(std::nullptr_t ptr = nullptr){}; // NOLINT

Value(detail::ValueImpl *impl) : impl_(impl) {} // NOLINT

Expand Down
4 changes: 2 additions & 2 deletions paddle/pir/src/core/op_result_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class OpResultImpl : public ValueImpl {
///
uint32_t index() const;

~OpResultImpl();
TEST_API ~OpResultImpl();

///
/// \brief attribute related public interfaces
Expand All @@ -60,7 +60,7 @@ class OpResultImpl : public ValueImpl {
///
class OpInlineResultImpl : public OpResultImpl {
public:
OpInlineResultImpl(Type type, uint32_t result_index);
TEST_API OpInlineResultImpl(Type type, uint32_t result_index);

static bool classof(const ValueImpl &value) {
return value.kind() < OUTLINE_RESULT_IDX;
Expand Down
19 changes: 19 additions & 0 deletions test/cpp/pir/core/block_argument_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,22 @@ TEST(block_argument_test, kwargs) {
EXPECT_EQ(block->kwargs_size(), 4u);
EXPECT_EQ(value.type(), builder.bool_type());
}

TEST(block_argument_test, fatal) {
auto block = new pir::Block();
auto arg = block->AddArg(nullptr);
auto op = pir::Operation::Create({arg}, {}, {}, nullptr);
EXPECT_DEATH(delete block,
"Destroyed a position block argument that is still in use.*");
auto kwarg = block->AddKwarg("a", nullptr);
arg.ReplaceAllUsesWith(kwarg);
block->ClearArgs();
EXPECT_DEATH(delete block,
"Destroyed a keyword block argument that is still in use.*");

op->Destroy();
op = pir::Operation::Create({}, {}, {}, nullptr, 0, {block});
EXPECT_DEATH(delete block, "Destroyed a block that is still in use.*");
op->Destroy();
delete block;
}
27 changes: 23 additions & 4 deletions test/cpp/pir/core/ir_value_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "paddle/pir/include/core/ir_context.h"
#include "paddle/pir/include/core/operation.h"
#include "paddle/pir/include/dialect/shape/utils/shape_analysis.h"
#include "paddle/pir/src/core/op_result_impl.h"

// This unittest is used to test the construction interfaces of value class and
// operation. The constructed test scenario is: a = OP1(); b = OP2(); c = OP3(a,
Expand Down Expand Up @@ -50,7 +51,7 @@ TEST(value_test, value_test) {
op1_inputs,
test::CreateAttributeMap({"op1_name"}, {"op1_attr"}),
op1_output_types,
pir::OpInfo());
nullptr);
op1->Print(std::cout);
pir::Value a = op1->result(0);
EXPECT_TRUE(a.use_empty());
Expand All @@ -61,7 +62,7 @@ TEST(value_test, value_test) {
op2_inputs,
test::CreateAttributeMap({"op2_name"}, {"op2_attr"}),
op2_output_types,
pir::OpInfo());
nullptr);
op2->Print(std::cout);
pir::Value b = op2->result(0);
EXPECT_TRUE(b.use_empty());
Expand All @@ -72,7 +73,7 @@ TEST(value_test, value_test) {
op3_inputs,
test::CreateAttributeMap({"op3_name"}, {"op3_attr"}),
op3_output_types,
pir::OpInfo());
nullptr);

EXPECT_TRUE(op1->result(0).HasOneUse());
EXPECT_TRUE(op2->result(0).HasOneUse());
Expand All @@ -88,7 +89,7 @@ TEST(value_test, value_test) {
op4_inputs,
test::CreateAttributeMap({"op4_name"}, {"op4_attr"}),
op4_output_types,
pir::OpInfo());
nullptr);
op4->Print(std::cout);

// Test 1:
Expand Down Expand Up @@ -135,3 +136,21 @@ TEST(value_test, value_test) {
VLOG(0) << op1->result(0).PrintUdChain() << std::endl;
op1->Destroy();
}

TEST(op_result_test, exception) {
EXPECT_THROW(
pir::detail::OpInlineResultImpl(nullptr, MAX_INLINE_RESULT_IDX + 1),
common::enforce::EnforceNotMet);
pir::IrContext *ctx = pir::IrContext::Instance();
auto op = pir::Operation::Create(
{}, {{"test", pir::Int32Attribute::get(ctx, 1)}}, {nullptr}, nullptr);
auto result = op->result(0);
auto op2 = pir::Operation::Create({result}, {}, {}, nullptr);
EXPECT_DEATH(op->Destroy(), "Destroyed a op_result that is still in use.*");
EXPECT_THROW(result.set_attribute("test", nullptr),
common::enforce::EnforceNotMet);
EXPECT_THROW(op->result(1), common::enforce::EnforceNotMet);
EXPECT_THROW(op->operand(1), common::enforce::EnforceNotMet);
op2->Destroy();
op->Destroy();
}
2 changes: 1 addition & 1 deletion test/cpp/pir/core/paddle_fatal_test.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
// 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.
Expand Down

0 comments on commit 2603c13

Please sign in to comment.