chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 12:40:42 +08:00
commit e25996e7db
15472 changed files with 3536181 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
if(WIN32)
remove_definitions(-DPADDLE_DLL_EXPORT)
endif()
cc_library(
test_dialect
SRCS test_dialect.cc test_op.cc test_trait.cc test_interface.cc
test1_dialect.cc
DEPS gtest)
get_property(paddle_lib GLOBAL PROPERTY PADDLE_LIB_NAME)
target_link_libraries(test_dialect $<TARGET_LINKER_FILE:${paddle_lib}>)
+28
View File
@@ -0,0 +1,28 @@
// Copyright (c) 2023 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "paddle/pir/include/core/type_id.h"
#define IR_DECLARE_EXPLICIT_TEST_TYPE_ID(TYPE_CLASS) \
namespace pir { \
namespace detail { \
template <> \
class PADDLE_EXP_API TypeIdResolver<TYPE_CLASS> { \
public: \
static TypeId Resolve() { return id_; } \
static UniqueingId id_; \
}; \
} \
} // namespace pir
+38
View File
@@ -0,0 +1,38 @@
// Copyright (c) 2023 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "test/cpp/pir/tools/test1_dialect.h"
#include "paddle/pir/include/core/ir_printer.h"
#include "test/cpp/pir/tools/test_op.h"
namespace test1 {
Test1Dialect::Test1Dialect(pir::IrContext *context)
: pir::Dialect(name(), context, pir::TypeId::get<Test1Dialect>()) {
initialize();
}
void Test1Dialect::initialize() {
RegisterOps<Operation1, Operation2, Operation3, Operation4>();
}
pir::OpPrintFn Test1Dialect::PrintOperation(const pir::Operation &op) const {
return [](const pir::Operation &op, pir::IrPrinter &printer) {
printer.PrintOpResult(op);
printer.os << " =";
printer.os << " \"" << op.name() << "\"";
printer.PrintOpOperands(op);
};
}
} // namespace test1
IR_DEFINE_EXPLICIT_TYPE_ID(test1::Test1Dialect)
+32
View File
@@ -0,0 +1,32 @@
// Copyright (c) 2023 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "paddle/pir/include/core/dialect.h"
#include "test/cpp/pir/tools/macros_utils.h"
namespace test1 {
class Test1Dialect : public pir::Dialect {
public:
explicit Test1Dialect(pir::IrContext *context);
static const char *name() { return "test1"; }
pir::OpPrintFn PrintOperation(const pir::Operation &op) const override;
private:
void initialize();
};
} // namespace test1
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test1::Test1Dialect)
+54
View File
@@ -0,0 +1,54 @@
// Copyright (c) 2023 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "test/cpp/pir/tools/test_dialect.h"
#include "paddle/pir/include/core/ir_printer.h"
#include "test/cpp/pir/tools/test_op.h"
namespace test {
TestDialect::TestDialect(pir::IrContext *context)
: pir::Dialect(name(), context, pir::TypeId::get<TestDialect>()) {
initialize();
}
void TestDialect::initialize() {
RegisterOps<RegionOp,
BranchOp,
Operation1,
Operation2,
TraitExampleOp,
SameOperandsShapeTraitOp1,
SameOperandsShapeTraitOp2,
SameOperandsAndResultShapeTraitOp1,
SameOperandsAndResultShapeTraitOp2,
SameOperandsAndResultShapeTraitOp3,
SameOperandsElementTypeTraitOp1,
SameOperandsElementTypeTraitOp2,
SameOperandsAndResultElementTypeTraitOp1,
SameOperandsAndResultElementTypeTraitOp2,
SameOperandsAndResultElementTypeTraitOp3,
SameOperandsAndResultTypeTraitOp1,
SameOperandsAndResultTypeTraitOp2,
SameOperandsAndResultTypeTraitOp3>();
}
pir::OpPrintFn TestDialect::PrintOperation(const pir::Operation &op) const {
return [](const pir::Operation &op, pir::IrPrinter &printer) {
printer.PrintOpResult(op);
printer.os << " =";
printer.os << " \"" << op.name() << "\"";
printer.PrintOpOperands(op);
};
}
} // namespace test
IR_DEFINE_EXPLICIT_TYPE_ID(test::TestDialect)
+32
View File
@@ -0,0 +1,32 @@
// Copyright (c) 2023 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "paddle/pir/include/core/dialect.h"
#include "test/cpp/pir/tools/macros_utils.h"
namespace test {
class TestDialect : public pir::Dialect {
public:
explicit TestDialect(pir::IrContext *context);
static const char *name() { return "test"; }
pir::OpPrintFn PrintOperation(const pir::Operation &op) const override;
private:
void initialize();
};
} // namespace test
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::TestDialect)
+15
View File
@@ -0,0 +1,15 @@
// Copyright (c) 2023 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "test/cpp/pir/tools/test_interface.h"
IR_DEFINE_EXPLICIT_TYPE_ID(test::InferShapeInterface)
+66
View File
@@ -0,0 +1,66 @@
// Copyright (c) 2023 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <gtest/gtest.h>
#include <sstream>
#include "paddle/common/enforce.h"
#include "paddle/pir/include/core/block.h"
#include "paddle/pir/include/core/builder.h"
#include "paddle/pir/include/core/builtin_attribute.h"
#include "paddle/pir/include/core/builtin_op.h"
#include "paddle/pir/include/core/builtin_type.h"
#include "paddle/pir/include/core/dialect.h"
#include "paddle/pir/include/core/ir_context.h"
#include "paddle/pir/include/core/ir_printer.h"
#include "paddle/pir/include/core/op_base.h"
#include "paddle/pir/include/core/program.h"
#include "paddle/pir/include/core/region.h"
#include "test/cpp/pir/tools/macros_utils.h"
namespace test {
/// \brief Define built-in Interface, derived from OpInterfaceBase. Concepts and
/// Models need to be defined within the class. Concept defines abstract
/// interface functions, and Model is a template class that defines the specific
/// implementation of interface functions based on template parameters.
class InferShapeInterface : public pir::OpInterfaceBase<InferShapeInterface> {
public:
struct Concept {
explicit Concept(void (*infer_shape)(pir::Operation *))
: infer_shape(infer_shape) {}
void (*infer_shape)(pir::Operation *);
};
template <class ConcreteOp>
struct Model : public Concept {
static void InferShape(pir::Operation *op) {
ConcreteOp concrete_op = ConcreteOp(op);
if (concrete_op == nullptr) throw("concrete_op is nullptr");
concrete_op.InferShape();
}
Model() : Concept(InferShape) {}
};
InferShapeInterface(pir::Operation *op, Concept *impl)
: pir::OpInterfaceBase<InferShapeInterface>(op), impl_(impl) {}
void InferShape() { impl_->infer_shape(operation()); }
private:
Concept *impl_;
};
} // namespace test
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::InferShapeInterface)
+234
View File
@@ -0,0 +1,234 @@
// Copyright (c) 2023 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "test/cpp/pir/tools/test_op.h"
#include "paddle/common/enforce.h"
#include "paddle/common/errors.h"
#include "paddle/phi/core/enforce.h"
#include "paddle/pir/include/core/builtin_attribute.h"
namespace test {
void RegionOp::Build(pir::Builder &builder, pir::OperationArgument &argument) {
argument.AddRegion(nullptr);
}
void BranchOp::Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
const std::vector<pir::Value> &target_operands,
pir::Block *target) {
argument.AddInputs(target_operands.begin(), target_operands.end());
argument.AddSuccessor(target);
}
void BranchOp::VerifySig() const {
PADDLE_ENFORCE_EQ(
(*this)->num_successors(),
1u,
common::errors::InvalidArgument("successors number must equal to 1."));
PADDLE_ENFORCE_NOT_NULL(
(*this)->successor(0),
common::errors::InvalidArgument("successor[0] can't be nullptr"));
}
const char *Operation1::attributes_name[2] = {"op1_attr1", // NOLINT
"op1_attr2"}; // NOLINT
void Operation1::Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument) { // NOLINT
std::unordered_map<std::string, pir::Attribute> attributes{
{"op1_attr1", builder.str_attr("op1_attr2")},
{"op1_attr2", builder.str_attr("op1_attr2")}};
argument.AddOutput(builder.float32_type());
argument.AddAttributes(attributes);
}
void Operation1::VerifySig() const {
auto &attributes = this->attributes();
if (attributes.count("op1_attr1") == 0 ||
!attributes.at("op1_attr1").isa<pir::StrAttribute>()) {
PADDLE_THROW(common::errors::Fatal(
"Type of attribute: parameter_name is not right."));
}
if (attributes.count("op1_attr2") == 0 ||
!attributes.at("op1_attr2").isa<pir::StrAttribute>()) {
PADDLE_THROW(common::errors::Fatal(
"Type of attribute: parameter_name is not right."));
}
}
void TraitExampleOp::Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
pir::Value l_operand,
pir::Value r_operand,
pir::Type out_type) {
argument.AddInput(l_operand);
argument.AddInput(r_operand);
argument.AddOutput(out_type);
}
void SameOperandsShapeTraitOp2::Build(
pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
pir::Value l_operand,
pir::Value r_operand,
pir::Type out_type) {
argument.AddInput(l_operand);
argument.AddInput(r_operand);
argument.AddOutput(out_type);
}
void SameOperandsAndResultShapeTraitOp2::Build(
pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
pir::Value l_operand,
pir::Value r_operand) {
argument.AddInput(l_operand);
argument.AddInput(r_operand);
}
void SameOperandsAndResultShapeTraitOp3::Build(
pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
pir::Value l_operand,
pir::Value r_operand,
pir::Type out_type) {
argument.AddInput(l_operand);
argument.AddInput(r_operand);
argument.AddOutput(out_type);
}
void SameOperandsElementTypeTraitOp2::Build(
pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
pir::Value l_operand,
pir::Value r_operand,
pir::Type out_type) {
argument.AddInput(l_operand);
argument.AddInput(r_operand);
argument.AddOutput(out_type);
}
void SameOperandsAndResultElementTypeTraitOp2::Build(
pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
pir::Value l_operand,
pir::Value r_operand) {
argument.AddInput(l_operand);
argument.AddInput(r_operand);
}
void SameOperandsAndResultElementTypeTraitOp3::Build(
pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
pir::Value l_operand,
pir::Value r_operand,
pir::Type out_type1,
pir::Type out_type2) {
argument.AddInput(l_operand);
argument.AddInput(r_operand);
argument.AddOutput(out_type1);
argument.AddOutput(out_type2);
}
void SameOperandsAndResultTypeTraitOp2::Build(
pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
pir::Value l_operand,
pir::Value r_operand) {
argument.AddInput(l_operand);
argument.AddInput(r_operand);
}
void SameOperandsAndResultTypeTraitOp3::Build(
pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
pir::Value l_operand,
pir::Value r_operand,
pir::Type out_type1,
pir::Type out_type2) {
argument.AddInput(l_operand);
argument.AddInput(r_operand);
argument.AddOutput(out_type1);
argument.AddOutput(out_type2);
}
} // namespace test
IR_DEFINE_EXPLICIT_TYPE_ID(test::RegionOp)
IR_DEFINE_EXPLICIT_TYPE_ID(test::BranchOp)
IR_DEFINE_EXPLICIT_TYPE_ID(test::Operation1)
IR_DEFINE_EXPLICIT_TYPE_ID(test::Operation2)
IR_DEFINE_EXPLICIT_TYPE_ID(test::TraitExampleOp)
IR_DEFINE_EXPLICIT_TYPE_ID(test::SameOperandsShapeTraitOp1)
IR_DEFINE_EXPLICIT_TYPE_ID(test::SameOperandsShapeTraitOp2)
IR_DEFINE_EXPLICIT_TYPE_ID(test::SameOperandsAndResultShapeTraitOp1)
IR_DEFINE_EXPLICIT_TYPE_ID(test::SameOperandsAndResultShapeTraitOp2)
IR_DEFINE_EXPLICIT_TYPE_ID(test::SameOperandsAndResultShapeTraitOp3)
IR_DEFINE_EXPLICIT_TYPE_ID(test::SameOperandsElementTypeTraitOp1)
IR_DEFINE_EXPLICIT_TYPE_ID(test::SameOperandsElementTypeTraitOp2)
IR_DEFINE_EXPLICIT_TYPE_ID(test::SameOperandsAndResultElementTypeTraitOp1)
IR_DEFINE_EXPLICIT_TYPE_ID(test::SameOperandsAndResultElementTypeTraitOp2)
IR_DEFINE_EXPLICIT_TYPE_ID(test::SameOperandsAndResultElementTypeTraitOp3)
IR_DEFINE_EXPLICIT_TYPE_ID(test::SameOperandsAndResultTypeTraitOp1)
IR_DEFINE_EXPLICIT_TYPE_ID(test::SameOperandsAndResultTypeTraitOp2)
IR_DEFINE_EXPLICIT_TYPE_ID(test::SameOperandsAndResultTypeTraitOp3)
namespace test1 {
const char *Operation1::attributes_name[2] = {"op1_attr1", // NOLINT
"op1_attr3"}; // NOLINT
void Operation1::Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument) { // NOLINT
std::unordered_map<std::string, pir::Attribute> attributes{
{"op1_attr1", builder.str_attr("op1_attr1")},
{"op1_attr3", builder.str_attr("op1_attr3")}};
argument.AddOutput(builder.float32_type());
argument.AddAttributes(attributes);
}
void Operation1::VerifySig() const {
auto &attributes = this->attributes();
if (attributes.count("op1_attr1") == 0 ||
!attributes.at("op1_attr1").isa<pir::StrAttribute>()) {
PADDLE_THROW(common::errors::Fatal(
"Type of attribute: parameter_name is not right."));
}
if (attributes.count("op1_attr3") == 0 ||
!attributes.at("op1_attr3").isa<pir::StrAttribute>()) {
PADDLE_THROW(common::errors::Fatal(
"Type of attribute: parameter_name is not right."));
}
}
void Operation2::Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument) { // NOLINT
argument.AddOutput(builder.float32_type());
}
void Operation3::Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument,
pir::Value l_operand,
pir::Value r_operand) { // NOLINT
argument.AddInput(l_operand);
argument.AddInput(r_operand);
}
void Operation4::Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument) { // NOLINT
argument.AddOutput(builder.float32_type());
argument.AddOutput(builder.float32_type());
}
} // namespace test1
IR_DEFINE_EXPLICIT_TYPE_ID(test1::Operation1)
IR_DEFINE_EXPLICIT_TYPE_ID(test1::Operation2)
IR_DEFINE_EXPLICIT_TYPE_ID(test1::Operation3)
IR_DEFINE_EXPLICIT_TYPE_ID(test1::Operation4)
+401
View File
@@ -0,0 +1,401 @@
// Copyright (c) 2023 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <glog/logging.h>
#include "paddle/pir/include/core/builder.h"
#include "paddle/pir/include/core/builtin_type.h"
#include "paddle/pir/include/core/op_base.h"
#include "paddle/pir/include/core/op_trait.h"
#include "paddle/pir/include/core/operation_utils.h"
#include "test/cpp/pir/tools/macros_utils.h"
#include "test/cpp/pir/tools/test_interface.h"
#include "test/cpp/pir/tools/test_trait.h"
namespace test {
///
/// \brief TestRegionOp
///
class RegionOp : public pir::Op<RegionOp, OneRegionTrait> {
public:
using Op::Op;
static const char *name() { return "test.region"; }
static constexpr uint32_t attributes_num = 0;
static constexpr const char **attributes_name = nullptr;
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument); // NOLINT
void VerifySig() const {}
};
///
/// \brief TestBranchOp
///
class BranchOp : public pir::Op<BranchOp> {
public:
using Op::Op;
static const char *name() { return "test.branch"; }
static constexpr uint32_t attributes_num = 0;
static constexpr const char **attributes_name = nullptr;
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
const std::vector<pir::Value> &target_operands,
pir::Block *target);
void VerifySig() const;
};
// Define case op1.
class Operation1 : public pir::Op<Operation1> {
public:
using Op::Op;
static const char *name() { return "test.operation1"; }
static constexpr uint32_t attributes_num = 2;
static const char *attributes_name[attributes_num];
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument); // NOLINT
void VerifySig() const;
};
// Define op2.
class Operation2
: public pir::Op<Operation2, ReadOnlyTrait, InferShapeInterface> {
public:
using Op::Op;
static const char *name() { return "test.operation2"; }
static constexpr uint32_t attributes_num = 0;
static constexpr const char **attributes_name = nullptr;
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument) {} // NOLINT
void VerifySig() const {}
static void InferShape() { VLOG(2) << "This is op2's InferShape interface."; }
};
// Define TraitExampleOp.
class TraitExampleOp
: public pir::Op<TraitExampleOp,
pir::SameOperandsShapeTrait,
pir::SameOperandsAndResultShapeTrait,
pir::SameOperandsElementTypeTrait,
pir::SameOperandsAndResultElementTypeTrait,
pir::SameOperandsAndResultTypeTrait,
pir::SameTypeOperandsTrait> {
public:
using Op::Op;
static const char *name() { return "test.trait_example_op"; }
static constexpr uint32_t attributes_num = 0;
static constexpr const char **attributes_name = nullptr;
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
pir::Value l_operand,
pir::Value r_operand,
pir::Type out_type);
void VerifySig() const {}
};
// Define SameOperandsShapeTraitOp1.
class SameOperandsShapeTraitOp1
: public pir::Op<SameOperandsShapeTraitOp1, pir::SameOperandsShapeTrait> {
public:
using Op::Op;
static const char *name() { return "test.same_operands_shape_op1"; }
static constexpr uint32_t attributes_num = 0;
static constexpr const char **attributes_name = nullptr;
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument) {} // NOLINT
void VerifySig() const {}
};
// Define SameOperandsShapeTraitOp2.
class SameOperandsShapeTraitOp2
: public pir::Op<SameOperandsShapeTraitOp2, pir::SameOperandsShapeTrait> {
public:
using Op::Op;
static const char *name() { return "test.same_operands_shape_op2"; }
static constexpr uint32_t attributes_num = 0;
static constexpr const char **attributes_name = nullptr;
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
pir::Value l_operand,
pir::Value r_operand,
pir::Type out_type);
void VerifySig() const {}
};
// Define SameOperandsAndResultShapeTraitOp1.
class SameOperandsAndResultShapeTraitOp1
: public pir::Op<SameOperandsAndResultShapeTraitOp1,
pir::SameOperandsAndResultShapeTrait> {
public:
using Op::Op;
static const char *name() {
return "test.same_operands_and_result_shape_op1";
}
static constexpr uint32_t attributes_num = 0;
static constexpr const char **attributes_name = nullptr;
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument) {} // NOLINT
void VerifySig() const {}
};
// Define SameOperandsAndResultShapeTraitOp2.
class SameOperandsAndResultShapeTraitOp2
: public pir::Op<SameOperandsAndResultShapeTraitOp2,
pir::SameOperandsAndResultShapeTrait> {
public:
using Op::Op;
static const char *name() {
return "test.same_operands_and_result_shape_op2";
}
static constexpr uint32_t attributes_num = 0;
static constexpr const char **attributes_name = nullptr;
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
pir::Value l_operand,
pir::Value r_operand);
void VerifySig() const {}
};
// Define SameOperandsAndResultShapeTraitOp3.
class SameOperandsAndResultShapeTraitOp3
: public pir::Op<SameOperandsAndResultShapeTraitOp3,
pir::SameOperandsAndResultShapeTrait> {
public:
using Op::Op;
static const char *name() {
return "test.same_operands_and_result_shape_op3";
}
static constexpr uint32_t attributes_num = 0;
static constexpr const char **attributes_name = nullptr;
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
pir::Value l_operand,
pir::Value r_operand,
pir::Type out_type);
void VerifySig() const {}
};
// Define SameOperandsElementTypeTraitOp1.
class SameOperandsElementTypeTraitOp1
: public pir::Op<SameOperandsElementTypeTraitOp1,
pir::SameOperandsElementTypeTrait> {
public:
using Op::Op;
static const char *name() { return "test.same_operands_element_type_op1"; }
static constexpr uint32_t attributes_num = 0;
static constexpr const char **attributes_name = nullptr;
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument) {} // NOLINT
void VerifySig() const {}
};
// Define SameOperandsElementTypeTraitOp2.
class SameOperandsElementTypeTraitOp2
: public pir::Op<SameOperandsElementTypeTraitOp2,
pir::SameOperandsElementTypeTrait> {
public:
using Op::Op;
static const char *name() { return "test.same_operands_element_type_op1"; }
static constexpr uint32_t attributes_num = 0;
static constexpr const char **attributes_name = nullptr;
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
pir::Value l_operand,
pir::Value r_operand,
pir::Type out_type);
void VerifySig() const {}
};
// Define SameOperandsAndResultElementTypeTraitOp1.
class SameOperandsAndResultElementTypeTraitOp1
: public pir::Op<SameOperandsAndResultElementTypeTraitOp1,
pir::SameOperandsAndResultElementTypeTrait> {
public:
using Op::Op;
static const char *name() {
return "test.same_operands_and_result_element_type_op1";
}
static constexpr uint32_t attributes_num = 0;
static constexpr const char **attributes_name = nullptr;
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument) {} // NOLINT
void VerifySig() const {}
};
// Define SameOperandsAndResultElementTypeTraitOp2.
class SameOperandsAndResultElementTypeTraitOp2
: public pir::Op<SameOperandsAndResultElementTypeTraitOp2,
pir::SameOperandsAndResultElementTypeTrait> {
public:
using Op::Op;
static const char *name() {
return "test.same_operands_and_result_element_type_op2";
}
static constexpr uint32_t attributes_num = 0;
static constexpr const char **attributes_name = nullptr;
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
pir::Value l_operand,
pir::Value r_operand);
void VerifySig() const {}
};
// Define SameOperandsAndResultElementTypeTraitOp3.
class SameOperandsAndResultElementTypeTraitOp3
: public pir::Op<SameOperandsAndResultElementTypeTraitOp3,
pir::SameOperandsAndResultElementTypeTrait> {
public:
using Op::Op;
static const char *name() {
return "test.same_operands_and_result_element_type_op3";
}
static constexpr uint32_t attributes_num = 0;
static constexpr const char **attributes_name = nullptr;
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
pir::Value l_operand,
pir::Value r_operand,
pir::Type out_type1,
pir::Type out_type2);
void VerifySig() const {}
};
// Define SameOperandsAndResultTypeTraitOp1.
class SameOperandsAndResultTypeTraitOp1
: public pir::Op<SameOperandsAndResultTypeTraitOp1,
pir::SameOperandsAndResultTypeTrait> {
public:
using Op::Op;
static const char *name() { return "test.same_operands_and_result_type_op1"; }
static constexpr uint32_t attributes_num = 0;
static constexpr const char **attributes_name = nullptr;
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument) {} // NOLINT
void VerifySig() const {}
};
// Define SameOperandsAndResultTypeTraitOp2.
class SameOperandsAndResultTypeTraitOp2
: public pir::Op<SameOperandsAndResultTypeTraitOp2,
pir::SameOperandsAndResultTypeTrait> {
public:
using Op::Op;
static const char *name() { return "test.same_operands_and_result_type_op2"; }
static constexpr uint32_t attributes_num = 0;
static constexpr const char **attributes_name = nullptr;
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
pir::Value l_operand,
pir::Value r_operand);
void VerifySig() const {}
};
// Define SameOperandsAndResultTypeTraitOp3.
class SameOperandsAndResultTypeTraitOp3
: public pir::Op<SameOperandsAndResultTypeTraitOp3,
pir::SameOperandsAndResultTypeTrait> {
public:
using Op::Op;
static const char *name() { return "test.same_operands_and_result_type_op3"; }
static constexpr uint32_t attributes_num = 0;
static constexpr const char **attributes_name = nullptr;
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
pir::Value l_operand,
pir::Value r_operand,
pir::Type out_type1,
pir::Type out_type2);
void VerifySig() const {}
};
} // namespace test
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::RegionOp)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::BranchOp)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::Operation1)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::Operation2)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::TraitExampleOp)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::SameOperandsShapeTraitOp1)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::SameOperandsShapeTraitOp2)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::SameOperandsAndResultShapeTraitOp1)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::SameOperandsAndResultShapeTraitOp2)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::SameOperandsAndResultShapeTraitOp3)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::SameOperandsElementTypeTraitOp1)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::SameOperandsElementTypeTraitOp2)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::SameOperandsAndResultElementTypeTraitOp1)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::SameOperandsAndResultElementTypeTraitOp2)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::SameOperandsAndResultElementTypeTraitOp3)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::SameOperandsAndResultTypeTraitOp1)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::SameOperandsAndResultTypeTraitOp2)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::SameOperandsAndResultTypeTraitOp3)
namespace test1 {
// Define case op1.
class Operation1 : public pir::Op<Operation1> {
public:
using Op::Op;
static const char *name() { return "test1.operation1"; }
static constexpr uint32_t attributes_num = 2;
static const char *attributes_name[attributes_num];
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument); // NOLINT
void VerifySig() const;
};
// Define op2.
class Operation2 : public pir::Op<Operation2> {
public:
using Op::Op;
static const char *name() { return "test1.operation2"; }
static constexpr uint32_t attributes_num = 0;
static constexpr const char **attributes_name = nullptr;
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument); // NOLINT
void VerifySig() const {}
static void InferShape() { VLOG(2) << "This is op2's InferShape interface."; }
};
// Define op3.
class Operation3 : public pir::Op<Operation3> {
public:
using Op::Op;
static const char *name() { return "test1.operation3"; }
static constexpr uint32_t attributes_num = 0;
static constexpr const char **attributes_name = nullptr;
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument, // NOLINT
pir::Value l_operand,
pir::Value r_operand);
void VerifySig() const {}
static void InferShape() { VLOG(2) << "This is op3's InferShape interface."; }
};
// Define op4.
class Operation4 : public pir::Op<Operation4> {
public:
using Op::Op;
static const char *name() { return "test1.operation4"; }
static constexpr uint32_t attributes_num = 0;
static constexpr const char **attributes_name = nullptr;
static void Build(pir::Builder &builder, // NOLINT
pir::OperationArgument &argument); // NOLINT
void VerifySig() const {}
};
} // namespace test1
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test1::Operation1)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test1::Operation2)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test1::Operation3)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test1::Operation4)
+56
View File
@@ -0,0 +1,56 @@
// Copyright (c) 2023 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "paddle/fluid/pir/dialect/operator/ir/op_type.h"
#include "paddle/pir/include/core/builtin_type.h"
#include "paddle/pir/include/dialect/shape/utils/shape_analysis.h"
namespace test {
pir::AttributeMap CreateAttributeMap(
const std::vector<std::string> &attribute_names,
const std::vector<std::string> &attributes) {
pir::IrContext *ctx = pir::IrContext::Instance();
pir::AttributeMap attr_map;
for (size_t i = 0; i < attribute_names.size(); i++) {
pir::Attribute attr_value = pir::StrAttribute::get(ctx, attributes[i]);
attr_map.insert(
std::pair<std::string, pir::Attribute>(attribute_names[i], attr_value));
}
return attr_map;
}
pir::Operation *CreateDenseTensorOp(
pir::IrContext *ctx,
const phi::DDim &dims,
const std::vector<std::string> &attribute_names,
const std::vector<std::string> &attributes,
const pir::Type &dtype =
pir::Float32Type::get(pir::IrContext::Instance())) {
std::vector<pir::Value> op_inputs = {};
phi::DataLayout data_layout = phi::DataLayout::NCHW;
phi::LegacyLoD lod = {{0, 1, 2}};
size_t offset = 0;
std::vector<pir::Type> op_output_types = {
paddle::dialect::DenseTensorType::get(
ctx, dtype, dims, data_layout, lod, offset)};
return pir::Operation::Create(op_inputs,
CreateAttributeMap(attribute_names, attributes),
op_output_types,
pir::OpInfo());
}
} // namespace test
+32
View File
@@ -0,0 +1,32 @@
// Copyright (c) 2023 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "test/cpp/pir/tools/test_trait.h"
#include "glog/logging.h"
#include "paddle/common/enforce.h"
namespace test {
void OneRegionTrait::Verify(pir::Operation *op) {
VLOG(1) << "here";
PADDLE_ENFORCE_EQ(op->num_regions(),
1u,
common::errors::InvalidArgument(
"%s op has one region trait, but its region size is %u",
op->name(),
op->num_regions()));
}
} // namespace test
IR_DEFINE_EXPLICIT_TYPE_ID(test::ReadOnlyTrait)
IR_DEFINE_EXPLICIT_TYPE_ID(test::OneRegionTrait)
+38
View File
@@ -0,0 +1,38 @@
// Copyright (c) 2023 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <gtest/gtest.h>
#include <sstream>
#include "paddle/pir/include/core/op_base.h"
#include "test/cpp/pir/tools/macros_utils.h"
namespace test {
class ReadOnlyTrait : public pir::OpTraitBase<ReadOnlyTrait> {
public:
explicit ReadOnlyTrait(const pir::Operation *op)
: pir::OpTraitBase<ReadOnlyTrait>(op) {}
};
class OneRegionTrait : public pir::OpTraitBase<OneRegionTrait> {
public:
explicit OneRegionTrait(const pir::Operation *op)
: pir::OpTraitBase<OneRegionTrait>(op) {}
static void Verify(pir::Operation *op);
};
} // namespace test
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::ReadOnlyTrait)
IR_DECLARE_EXPLICIT_TEST_TYPE_ID(test::OneRegionTrait)