代码拉取完成,页面将自动刷新
From 959277b19b4a1a8991d05e2db4bdd5374e6c308e Mon Sep 17 00:00:00 2001
From: d00573793 <dingguangya1@huawei.com>
Date: Mon, 6 Mar 2023 23:01:18 +0800
Subject: [PATCH 23/23] [Pin-server] Add StructReorderPASS demo
diff --git a/include/PluginAPI/BasicPluginOpsAPI.h b/include/PluginAPI/BasicPluginOpsAPI.h
index d659662..d9f348d 100644
--- a/include/PluginAPI/BasicPluginOpsAPI.h
+++ b/include/PluginAPI/BasicPluginOpsAPI.h
@@ -72,6 +72,7 @@ public:
virtual void SetTypeFields(uint64_t declId, uint64_t fieldId) = 0;
virtual void LayoutType(uint64_t declId) = 0;
virtual void LayoutDecl(uint64_t declId) = 0;
+ virtual PluginIR::PluginTypeBase GetDeclType(uint64_t declId) = 0;
virtual vector<LocalDeclOp> GetDecls(uint64_t) = 0;
virtual LoopOp AllocateNewLoop(uint64_t) = 0;
diff --git a/include/PluginAPI/PluginServerAPI.h b/include/PluginAPI/PluginServerAPI.h
index 239dd23..c18bba9 100644
--- a/include/PluginAPI/PluginServerAPI.h
+++ b/include/PluginAPI/PluginServerAPI.h
@@ -65,6 +65,7 @@ public:
void SetTypeFields(uint64_t declId, uint64_t fieldId) override;
void LayoutType(uint64_t declId) override;
void LayoutDecl(uint64_t declId) override;
+ PluginIR::PluginTypeBase GetDeclType(uint64_t declId) override;
vector<LocalDeclOp> GetDecls(uint64_t) override;
PhiOp GetPhiOp(uint64_t) override;
diff --git a/include/PluginServer/PluginCom.h b/include/PluginServer/PluginCom.h
index af819ad..7deb615 100755
--- a/include/PluginServer/PluginCom.h
+++ b/include/PluginServer/PluginCom.h
@@ -60,6 +60,7 @@ public:
vector<mlir::Plugin::DeclBaseOp> GetFuncDeclsResult(void);
llvm::SmallVector<mlir::Plugin::FieldDeclOp> GetFieldsResult(void);
mlir::Plugin::DeclBaseOp GetBuildDeclResult(void);
+ PluginIR::PluginTypeBase GetDeclTypeResult(void);
mlir::Plugin::FieldDeclOp GetMakeNodeResult(void);
@@ -93,6 +94,7 @@ private:
uint64_t idResult;
vector<uint64_t> idsResult;
mlir::Value valueResult;
+ PluginIR::PluginTypeBase pTypeResult;
mlir::Plugin::DeclBaseOp declOp;
mlir::Plugin::FieldDeclOp fielddeclOp;
llvm::SmallVector<mlir::Plugin::FieldDeclOp> fieldsOps;
diff --git a/include/PluginServer/PluginServer.h b/include/PluginServer/PluginServer.h
index 29a790b..81f92e9 100644
--- a/include/PluginServer/PluginServer.h
+++ b/include/PluginServer/PluginServer.h
@@ -183,6 +183,12 @@ public:
return pluginCom.GetBuildDeclResult();
}
+ PluginIR::PluginTypeBase GetDeclTypeResult(const string& funName, const string& params)
+ {
+ RemoteCallClientWithAPI(funName, params);
+ return pluginCom.GetDeclTypeResult();
+ }
+
vector<mlir::Plugin::LocalDeclOp> GetLocalDeclResult(const string& funName, const string& params)
{
RemoteCallClientWithAPI(funName, params);
diff --git a/lib/PluginAPI/PluginServerAPI.cpp b/lib/PluginAPI/PluginServerAPI.cpp
index 537d389..e8a1f4c 100644
--- a/lib/PluginAPI/PluginServerAPI.cpp
+++ b/lib/PluginAPI/PluginServerAPI.cpp
@@ -613,6 +613,15 @@ void PluginServerAPI::LayoutType(uint64_t declId)
PluginServer::GetInstance()->RemoteCallClientWithAPI(funName, params);
}
+PluginIR::PluginTypeBase PluginServerAPI::GetDeclType(uint64_t declId)
+{
+ Json::Value root;
+ string funName = __func__;
+ root["declId"] = std::to_string(declId);
+ string params = root.toStyledString();
+ return PluginServer::GetInstance()->GetDeclTypeResult(funName, params);
+}
+
void PluginServerAPI::LayoutDecl(uint64_t declId)
{
Json::Value root;
diff --git a/lib/PluginServer/PluginCom.cpp b/lib/PluginServer/PluginCom.cpp
index 573de13..aaf4ba8 100755
--- a/lib/PluginServer/PluginCom.cpp
+++ b/lib/PluginServer/PluginCom.cpp
@@ -91,6 +91,12 @@ mlir::Plugin::DeclBaseOp PluginCom::GetBuildDeclResult(void)
return retOp;
}
+PluginIR::PluginTypeBase PluginCom::GetDeclTypeResult(void)
+{
+ PluginIR::PluginTypeBase retType = pTypeResult;
+ return retType;
+}
+
vector<mlir::Plugin::LoopOp> PluginCom::LoopOpsResult(void)
{
vector<mlir::Plugin::LoopOp> retLoops = loops;
@@ -161,6 +167,14 @@ void PluginCom::JsonDeSerialize(const string& key, const string& data)
this->cgnode = json.CGnodeOpJsonDeSerialize(data);
} else if (key == "LocalDeclOpResult") {
json.LocalDeclOpJsonDeSerialize(data, this->decls);
+ } else if (key == "FuncDeclsOpResult") {
+ json.FuncDeclsOpJsonDeSerialize(data, this->declOps);
+ } else if (key == "MakeNodeResult") {
+ Json::Value node;
+ Json::Reader reader;
+ reader.parse(data, node);
+ mlir::Value v = json.ValueJsonDeSerialize(node);
+ this->fielddeclOp = llvm::dyn_cast<mlir::Plugin::FieldDeclOp>(v.getDefiningOp());
} else if (key == "LoopOpResult") {
this->loop = json.LoopOpJsonDeSerialize (data);
} else if (key == "LoopOpsResult") {
@@ -180,9 +194,6 @@ void PluginCom::JsonDeSerialize(const string& key, const string& data)
} else if (key == "DeclOpResult") {
mlir::Value decl = json.DeclBaseOpJsonDeSerialize(data);
this->declOp = llvm::dyn_cast<mlir::Plugin::DeclBaseOp>(decl.getDefiningOp());
- printf("server 164 declop ----------------\n");
- printf("server 164 declop code %d\n", this->declOp.defCodeAttr().getInt());
- printf("server 165 declop ----------------\n");
} else if (key == "GetFieldsOpResult") {
json.FieldOpsJsonDeSerialize(data, this->fieldsOps);
} else if (key == "OpsResult") {
@@ -192,6 +203,8 @@ void PluginCom::JsonDeSerialize(const string& key, const string& data)
Json::Reader reader;
reader.parse(data, node);
this->valueResult = json.ValueJsonDeSerialize(node);
+ } else if (key == "PluginTypeResult") {
+ this->pTypeResult = json.TypeJsonDeSerialize(data);
} else if (key == "GetPhiOps") {
json.GetPhiOpsJsonDeSerialize(data, this->opData);
} else if (key == "IntegerResult") {
diff --git a/lib/PluginServer/PluginJson.cpp b/lib/PluginServer/PluginJson.cpp
index 7c2abbe..c7ce788 100755
--- a/lib/PluginServer/PluginJson.cpp
+++ b/lib/PluginServer/PluginJson.cpp
@@ -333,7 +333,6 @@ CGnodeOp PluginJson::CGnodeOpJsonDeSerialize(const string& data)
Json::Value root;
Json::Reader reader;
reader.parse(data, root);
- fprintf(stderr, "dgy server cgnode json %s\n", root.toStyledString().c_str());
mlir::OpBuilder builder(PluginServer::GetInstance()->GetContext());
uint64_t id = GetID(root["id"]);
diff --git a/user/InlineFunctionPass.cpp b/user/InlineFunctionPass.cpp
index d982d44..a51e6fe 100755
--- a/user/InlineFunctionPass.cpp
+++ b/user/InlineFunctionPass.cpp
@@ -33,7 +33,7 @@ static void UserOptimizeFunc(void)
if (allFunction[i].declaredInlineAttr().getValue())
count++;
}
- printf("declaredInline have %d functions were declared.\n", count);
+ fprintf(stderr, "declaredInline have %d functions were declared.\n", count);
}
int InlineFunctionPass::DoOptimize()
diff --git a/user/LocalVarSummeryPass.cpp b/user/LocalVarSummeryPass.cpp
index ccee9f7..c22bad4 100755
--- a/user/LocalVarSummeryPass.cpp
+++ b/user/LocalVarSummeryPass.cpp
@@ -51,10 +51,6 @@ static void LocalVarSummery(void)
std::string tyName = stTy.getName();
printf(" struct name is : %s\n", tyName.c_str());
- llvm::ArrayRef<mlir::Type> paramsType = stTy.getBody();
- for (auto tty :paramsType) {
- printf("\n struct arg id : %d\n", tty.dyn_cast<PluginIR::PluginTypeBase>().getPluginTypeID());
- }
llvm::ArrayRef<std::string> paramsNames = stTy.getElementNames();
for (auto name :paramsNames) {
std::string pName = name;
diff --git a/user/StructReorder.cpp b/user/StructReorder.cpp
index 612a093..f4e824e 100644
--- a/user/StructReorder.cpp
+++ b/user/StructReorder.cpp
@@ -44,6 +44,138 @@ mlir::OpBuilder* opBuilder = nullptr;
std::map<Block*, Value> defs_map;
std::map<uint64_t, std::string> opNameMap;
+static void dump_structtype(PluginIR::PluginTypeBase type)
+{
+ if (auto stTy = type.dyn_cast<PluginIR::PluginStructType>()) {
+ std::string tyName = stTy.getName();
+ fprintf(stderr, " struct name is : %s\n", tyName.c_str());
+
+ llvm::ArrayRef<std::string> paramsNames = stTy.getElementNames();
+ for (auto name :paramsNames) {
+ std::string pName = name;
+ fprintf(stderr, "\n struct argname is : %s\n", pName.c_str());
+ }
+ }
+}
+
+
+static void reorder_fields(FieldDeclOp& field, FieldDeclOp& newfield)
+{
+ if (!field) {
+ field = newfield;
+ unsigned size = newfield.GetTypeSize();
+ } else {
+ FieldDeclOp tmp = field;
+ // unsigned size = tmp.getResultType().dyn_cast<PluginIR::PluginTypeBase>().getPluginTypeID();
+ unsigned size = newfield.GetTypeSize();
+ if (newfield.GetTypeSize() > tmp.GetTypeSize()) {
+ newfield.SetDeclChain(tmp);
+ field = newfield;
+ }
+ }
+}
+
+static void create_new_fields(mlir::Plugin::DeclBaseOp& decl, llvm::SmallVector<mlir::Plugin::FieldDeclOp> recordfields)
+{
+ PluginAPI::PluginServerAPI pluginAPI;
+ FieldDeclOp fd;
+ for (auto &fielddecl : recordfields) {
+ FieldDeclOp field = pluginAPI.MakeNode(IDefineCode::FieldDecl);
+ field.SetName(fielddecl);
+ field.SetType(fielddecl);
+ field.SetDeclAlign(fielddecl);
+
+ field.SetSourceLocation(fielddecl);
+ field.SetUserAlign(fielddecl);
+ field.SetAddressable(fielddecl);
+ field.SetNonAddressablep(fielddecl);
+ field.SetVolatile(fielddecl);
+ field.SetDeclContext(decl.idAttr().getInt());
+
+ reorder_fields(fd, field);
+
+ }
+ pluginAPI.SetTypeFields(decl.idAttr().getInt(), fd.idAttr().getInt());
+ pluginAPI.LayoutType(decl.idAttr().getInt());
+ pluginAPI.LayoutDecl(decl.idAttr().getInt());
+ fprintf(stderr, "reorder struct type after :>>>\n");
+ dump_structtype(pluginAPI.GetDeclType(decl.idAttr().getInt()));
+}
+
+static void create_new_type(mlir::Plugin::DeclBaseOp& decl, llvm::SmallVector<mlir::Plugin::FieldDeclOp> recordfields)
+{
+ create_new_fields(decl, recordfields);
+}
+
+static void create_new_types(llvm::SmallVector<PluginIR::PluginTypeBase> recordTypes,
+ llvm::SmallVector<DeclBaseOp> recordDecls, llvm::SmallVector<mlir::Plugin::FieldDeclOp> recordFields)
+{
+ for (int i = 0; i < recordTypes.size(); i++) {
+
+ auto type = recordTypes[i].dyn_cast<PluginIR::PluginStructType>();
+ mlir::MLIRContext m_context;
+ m_context.getOrLoadDialect<PluginDialect>();
+ PluginIR::PluginTypeBase rPluginType = PluginIR::PluginUndefType::get(&m_context);
+ StringRef name = type.getName();
+ StringRef newName = name.str() + ".reorg." + to_string(i);
+ PluginAPI::PluginServerAPI pluginAPI;
+ DeclBaseOp decl = pluginAPI.BuildDecl(IDefineCode::TYPEDECL, newName, rPluginType);
+
+ create_new_type(decl, recordFields);
+
+ }
+
+}
+
+static void record_decl(mlir::Plugin::DeclBaseOp decl, llvm::SmallVector<mlir::Plugin::DeclBaseOp>& recordDecls)
+{
+ if (llvm::find(recordDecls, decl) == recordDecls.end())
+ {
+ recordDecls.push_back(decl);
+ }
+}
+
+static void record_fields(DeclBaseOp decl, llvm::SmallVector<mlir::Plugin::FieldDeclOp>& recordFields)
+{
+ PluginAPI::PluginServerAPI pluginAPI;
+ llvm::SmallVector<mlir::Plugin::FieldDeclOp> fields = pluginAPI.GetFields(decl.idAttr().getInt());
+ recordFields.insert(recordFields.end(), fields.begin(), fields.end());
+}
+
+static PluginIR::PluginTypeBase record_type(PluginIR::PluginTypeBase type, llvm::SmallVector<PluginIR::PluginTypeBase>& recordTypes,
+ DeclBaseOp decl, llvm::SmallVector<mlir::Plugin::FieldDeclOp>& recordFields)
+{
+ if (llvm::find(recordTypes, type) == recordTypes.end())
+ {
+ recordTypes.push_back(type);
+ }
+ record_fields(decl, recordFields);
+ return type;
+}
+
+static PluginIR::PluginTypeBase inner_type(PluginIR::PluginTypeBase type)
+{
+ while(type.isa<PluginIR::PluginPointerType>() || type.isa<PluginIR::PluginArrayType>()) {
+ if (auto t = type.dyn_cast<PluginIR::PluginPointerType>()) {
+ type = t.getElementType().dyn_cast<PluginIR::PluginTypeBase>();
+ } else if (auto t = type.dyn_cast<PluginIR::PluginArrayType>()) {
+ type = t.getElementType().dyn_cast<PluginIR::PluginTypeBase>();
+ }
+ }
+ return type;
+}
+
+
+static bool handle_type(PluginIR::PluginTypeBase type)
+{
+ type = inner_type(type);
+ if (type.isa<PluginIR::PluginStructType>()) {
+ fprintf(stderr, "handle struct type :>>>\n");
+ dump_structtype(type);
+ return true;
+ }
+ return false;
+}
static void ProcessStructReorder(uint64_t *fun)
{
@@ -57,14 +189,15 @@ static void ProcessStructReorder(uint64_t *fun)
mlir::OpBuilder opBuilder_temp = mlir::OpBuilder(context);
opBuilder = &opBuilder_temp;
string name = nodeOp.symbolNameAttr().getValue().str();
- fprintf(stderr, "Now process symbol : %s \n", name.c_str());
uint32_t order = nodeOp.orderAttr().getInt();
- fprintf(stderr, "Now process order : %d \n", order);
if (nodeOp.IsRealSymbol())
- fprintf(stderr, "Now process IsRealSymbol \n");
+ fprintf(stderr, "process RealSymbol : %s/%d \n", name.c_str(), order);
}
vector<FunctionOp> allFunction = pluginAPI.GetAllFunc();
+ llvm::SmallVector<PluginIR::PluginTypeBase> recordTypes;
+ llvm::SmallVector<mlir::Plugin::DeclBaseOp> recordDecls;
+ llvm::SmallVector<mlir::Plugin::FieldDeclOp> recordFields;
fprintf(stderr, "allfun size is %d\n", allFunction.size());
for (auto &funcOp : allFunction) {
context = funcOp.getOperation()->getContext();
@@ -72,6 +205,19 @@ static void ProcessStructReorder(uint64_t *fun)
opBuilder = &opBuilder_temp;
string name = funcOp.funcNameAttr().getValue().str();
fprintf(stderr, "Now process func : %s \n", name.c_str());
+ uint64_t funcID = funcOp.idAttr().getValue().getZExtValue();
+
+ vector<mlir::Plugin::DeclBaseOp> decls = pluginAPI.GetFuncDecls(funcID);
+
+ for (auto &decl : decls) {
+ auto type = decl.getResultType().dyn_cast<PluginIR::PluginTypeBase>();
+ if (!handle_type(type)) continue;
+ type = record_type(inner_type(type), recordTypes, decl, recordFields);
+
+ record_decl(decl, recordDecls);
+ }
+
+ create_new_types(recordTypes, recordDecls, recordFields);
}
}
diff --git a/user/user.cpp b/user/user.cpp
index ee2031a..6bfa524 100644
--- a/user/user.cpp
+++ b/user/user.cpp
@@ -27,10 +27,10 @@
void RegisterCallbacks(void)
{
PinServer::PluginServer *pluginServer = PinServer::PluginServer::GetInstance();
- // pluginServer->RegisterOpt(std::make_shared<PluginOpt::InlineFunctionPass>(PluginOpt::HANDLE_BEFORE_IPA));
- // pluginServer->RegisterOpt(std::make_shared<PluginOpt::LocalVarSummeryPass>(PluginOpt::HANDLE_BEFORE_IPA));
+ pluginServer->RegisterOpt(std::make_shared<PluginOpt::InlineFunctionPass>(PluginOpt::HANDLE_BEFORE_IPA));
+ pluginServer->RegisterOpt(std::make_shared<PluginOpt::LocalVarSummeryPass>(PluginOpt::HANDLE_BEFORE_IPA));
// PluginOpt::ManagerSetup setupData(PluginOpt::PASS_PHIOPT, 1, PluginOpt::PASS_INSERT_AFTER);
// pluginServer->RegisterPassManagerOpt(setupData, std::make_shared<PluginOpt::ArrayWidenPass>());
- PluginOpt::ManagerSetup setupData(PluginOpt::PASS_MAC, 1, PluginOpt::PASS_INSERT_AFTER);
- pluginServer->RegisterPassManagerOpt(setupData, std::make_shared<PluginOpt::StructReorderPass>());
+ // PluginOpt::ManagerSetup setupData(PluginOpt::PASS_MAC, 1, PluginOpt::PASS_INSERT_AFTER);
+ // pluginServer->RegisterPassManagerOpt(setupData, std::make_shared<PluginOpt::StructReorderPass>());
}
--
2.33.0
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。