From 2603c133ed0556b01a0501874dbad6048bc2841b Mon Sep 17 00:00:00 2001 From: winter-wang <1030748926@qq.com> Date: Sun, 10 Mar 2024 04:23:12 +0000 Subject: [PATCH] add unit rest. --- paddle/common/enforce.h | 24 +++++++++++++++++---- paddle/phi/core/enforce.h | 1 - paddle/pir/include/core/op_info.h | 2 +- paddle/pir/include/core/value.h | 2 +- paddle/pir/src/core/op_result_impl.h | 4 ++-- test/cpp/pir/core/block_argument_test.cc | 19 +++++++++++++++++ test/cpp/pir/core/ir_value_test.cc | 27 ++++++++++++++++++++---- test/cpp/pir/core/paddle_fatal_test.cc | 2 +- 8 files changed, 67 insertions(+), 14 deletions(-) diff --git a/paddle/common/enforce.h b/paddle/common/enforce.h index 414cacd0034c7c..c02ec50aa0ba0e 100644 --- a/paddle/common/enforce.h +++ b/paddle/common/enforce.h @@ -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 struct CanToString { private: @@ -107,7 +124,6 @@ struct BinaryCompareMessageConverter { #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 \ @@ -115,8 +131,6 @@ struct BinaryCompareMessageConverter { } // 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); @@ -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 @@ -368,6 +384,7 @@ class IrNotMetException : public std::exception { private: std::string err_str_; + ::common::enforce::details::PaddleFatalGuard paddle_fatal_guard_; }; #define IR_THROW(...) \ @@ -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__, \ diff --git a/paddle/phi/core/enforce.h b/paddle/phi/core/enforce.h index 5ab15e6b0d6c01..c74e0ea52cfd33 100644 --- a/paddle/phi/core/enforce.h +++ b/paddle/phi/core/enforce.h @@ -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 \ diff --git a/paddle/pir/include/core/op_info.h b/paddle/pir/include/core/op_info.h index 124ed660db0f4c..994aed189fc6f9 100644 --- a/paddle/pir/include/core/op_info.h +++ b/paddle/pir/include/core/op_info.h @@ -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; diff --git a/paddle/pir/include/core/value.h b/paddle/pir/include/core/value.h index 0e1a2989e8f378..3a42cd539dfd21 100644 --- a/paddle/pir/include/core/value.h +++ b/paddle/pir/include/core/value.h @@ -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 diff --git a/paddle/pir/src/core/op_result_impl.h b/paddle/pir/src/core/op_result_impl.h index f5c0f53bdba54d..3671feef03fa9b 100644 --- a/paddle/pir/src/core/op_result_impl.h +++ b/paddle/pir/src/core/op_result_impl.h @@ -42,7 +42,7 @@ class OpResultImpl : public ValueImpl { /// uint32_t index() const; - ~OpResultImpl(); + TEST_API ~OpResultImpl(); /// /// \brief attribute related public interfaces @@ -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; diff --git a/test/cpp/pir/core/block_argument_test.cc b/test/cpp/pir/core/block_argument_test.cc index c9fb0ca9e8cc4a..32f57e8f5fd1b9 100644 --- a/test/cpp/pir/core/block_argument_test.cc +++ b/test/cpp/pir/core/block_argument_test.cc @@ -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; +} diff --git a/test/cpp/pir/core/ir_value_test.cc b/test/cpp/pir/core/ir_value_test.cc index d377d9c701fece..e8e1f3a26c8519 100644 --- a/test/cpp/pir/core/ir_value_test.cc +++ b/test/cpp/pir/core/ir_value_test.cc @@ -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, @@ -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()); @@ -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()); @@ -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()); @@ -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: @@ -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(); +} diff --git a/test/cpp/pir/core/paddle_fatal_test.cc b/test/cpp/pir/core/paddle_fatal_test.cc index 12e1ce9f0829d8..bdc60d11a32d93 100644 --- a/test/cpp/pir/core/paddle_fatal_test.cc +++ b/test/cpp/pir/core/paddle_fatal_test.cc @@ -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.