Skip to content

Commit 1991bd8

Browse files
committed
add bmv2.json
1 parent 1c14366 commit 1991bd8

File tree

14 files changed

+941
-0
lines changed

14 files changed

+941
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ install/
44
.vscode/
55
.cursor/
66
.idea/
7+
.DS_Store
78
compile_commands.json

tools/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
add_subdirectory(p4mlir-translate)
22
add_subdirectory(p4mlir-opt)
3+
add_subdirectory(p4mlir-export)

tools/p4mlir-export/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
2+
get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
3+
4+
set(LIBS
5+
${dialect_libs}
6+
${conversion_libs}
7+
8+
P4MLIR_P4HIR
9+
10+
MLIRFuncDialect
11+
MLIROptLib
12+
)
13+
14+
set(P4MLIR_EXPORT_SRCS
15+
p4mlir-export.cpp
16+
options.cpp
17+
json_emitter.cpp
18+
)
19+
20+
add_llvm_executable(p4mlir-export ${P4MLIR_EXPORT_SRCS})
21+
22+
target_link_libraries(p4mlir-export PRIVATE ${P4C_LIBRARIES} ${P4C_LIB_DEPS})
23+
24+
llvm_update_compile_flags(p4mlir-export)
25+
26+
target_link_libraries(p4mlir-export PRIVATE ${LIBS})
27+
28+
mlir_check_all_link_libraries(p4mlir-export)
29+
30+
add_custom_target(linkp4mlir_export_out
31+
COMMAND ${CMAKE_COMMAND} -E create_symlink
32+
${P4MLIR_BINARY_DIR}/bin/p4mlir-export
33+
${P4C_BINARY_DIR}/p4mlir-export
34+
COMMAND ${CMAKE_COMMAND} -E create_symlink
35+
${P4C_BINARY_DIR}/p4include
36+
${CMAKE_CURRENT_BINARY_DIR}/p4include
37+
)
38+
39+
add_dependencies(p4c_driver linkp4mlir_export_out)

tools/p4mlir-export/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CMD TO RUN P4HIRTOJSON:
2+
```
3+
{bin}/p4mlir-export --json-output {p4mlir-export}/bool_test.mlir > {p4mlir-export}/bool_test.json
4+
```
5+
6+
CMD TO GET BMV2 JSON FROM P4
7+
```
8+
{bin}/p4c --target bmv2 --arch v1model -o simplebmv2.json simplebmv2.p4
9+
```

tools/p4mlir-export/bool_test.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name":"",
3+
"operations":
4+
[
5+
{
6+
"attributes":
7+
{
8+
"value":
9+
{
10+
"type":"unknown","value":"#p4hir.bool<false> : !p4hir.bool"
11+
}
12+
},"name":"p4hir.const","regions":[],"result_types":
13+
[
14+
{
15+
"dialect":"p4hir",
16+
"value":"!p4hir.bool"
17+
}
18+
]
19+
}
20+
]
21+
,"type":"module"
22+
}

tools/p4mlir-export/bool_test.mlir

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module {
2+
%1 = p4hir.const #p4hir.bool<false> : !p4hir.bool
3+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
Licensed under the Apache License, Version 2.0 (the "License");
3+
you may not use this file except in compliance with the License.
4+
You may obtain a copy of the License at
5+
http://www.apache.org/licenses/LICENSE-2.0
6+
Unless required by applicable law or agreed to in writing, software
7+
distributed under the License is distributed on an "AS IS" BASIS,
8+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
See the License for the specific language governing permissions and
10+
limitations under the License.
11+
*/
12+
13+
#include "json_emitter.h"
14+
#include "p4mlir/Dialect/P4HIR/P4HIR_Dialect.h"
15+
#include "llvm/Support/JSON.h"
16+
#include "llvm/Support/raw_ostream.h"
17+
18+
using namespace llvm::json;
19+
20+
namespace P4::P4MLIR {
21+
22+
llvm::json::Value JsonEmitter::emitModule(mlir::ModuleOp module) {
23+
Object moduleObj;
24+
moduleObj["type"] = "module";
25+
moduleObj["name"] = module.getName() ? module.getName()->str() : "";
26+
27+
Array operations;
28+
for (auto &op : module.getOps()) {
29+
operations.push_back(emitOperation(&op));
30+
}
31+
moduleObj["operations"] = std::move(operations);
32+
33+
return moduleObj;
34+
}
35+
36+
llvm::json::Value JsonEmitter::emitOperation(mlir::Operation *op) {
37+
Object opObj;
38+
opObj["name"] = op->getName().getStringRef().str();
39+
40+
Object attrs;
41+
for (auto namedAttr : op->getAttrs()) {
42+
attrs[namedAttr.getName().str()] = emitAttribute(namedAttr.getValue());
43+
}
44+
opObj["attributes"] = std::move(attrs);
45+
46+
Array resultTypes;
47+
for (auto type : op->getResultTypes()) {
48+
resultTypes.push_back(emitType(type));
49+
}
50+
opObj["result_types"] = std::move(resultTypes);
51+
52+
Array regions;
53+
for (auto &region : op->getRegions()) {
54+
regions.push_back(emitRegion(region));
55+
}
56+
opObj["regions"] = std::move(regions);
57+
58+
return opObj;
59+
}
60+
61+
llvm::json::Value JsonEmitter::emitAttribute(mlir::Attribute attr) {
62+
Object attrObj;
63+
64+
if (auto strAttr = attr.dyn_cast<mlir::StringAttr>()) {
65+
attrObj["value"] = strAttr.getValue().str();
66+
attrObj["type"] = "string";
67+
} else if (auto intAttr = attr.dyn_cast<mlir::IntegerAttr>()) {
68+
attrObj["value"] = std::to_string(intAttr.getInt());
69+
attrObj["type"] = "integer";
70+
} else {
71+
std::string attrStr;
72+
llvm::raw_string_ostream os(attrStr);
73+
attr.print(os);
74+
attrObj["value"] = os.str();
75+
attrObj["type"] = "unknown";
76+
}
77+
78+
return attrObj;
79+
}
80+
81+
llvm::json::Value JsonEmitter::emitType(mlir::Type type) {
82+
Object typeObj;
83+
typeObj["dialect"] = type.getDialect().getNamespace().str();
84+
85+
std::string typeStr;
86+
llvm::raw_string_ostream os(typeStr);
87+
type.print(os);
88+
typeObj["value"] = os.str();
89+
90+
return typeObj;
91+
}
92+
93+
llvm::json::Value JsonEmitter::emitRegion(mlir::Region &region) {
94+
Object regionObj;
95+
96+
Array blocks;
97+
for (auto &block : region) {
98+
blocks.push_back(emitBlock(block));
99+
}
100+
regionObj["blocks"] = std::move(blocks);
101+
102+
return regionObj;
103+
}
104+
105+
llvm::json::Value JsonEmitter::emitBlock(mlir::Block &block) {
106+
Object blockObj;
107+
108+
Array arguments;
109+
for (auto &arg : block.getArguments()) {
110+
Object argObj;
111+
argObj["type"] = emitType(arg.getType());
112+
arguments.push_back(std::move(argObj));
113+
}
114+
blockObj["arguments"] = std::move(arguments);
115+
116+
Array operations;
117+
for (auto &op : block.getOperations()) {
118+
operations.push_back(emitOperation(&op));
119+
}
120+
blockObj["operations"] = std::move(operations);
121+
122+
return blockObj;
123+
}
124+
125+
} // namespace P4::P4MLIR
126+

tools/p4mlir-export/json_emitter.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Licensed under the Apache License, Version 2.0 (the "License");
3+
you may not use this file except in compliance with the License.
4+
You may obtain a copy of the License at
5+
http://www.apache.org/licenses/LICENSE-2.0
6+
Unless required by applicable law or agreed to in writing, software
7+
distributed under the License is distributed on an "AS IS" BASIS,
8+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
See the License for the specific language governing permissions and
10+
limitations under the License.
11+
*/
12+
13+
#ifndef P4MLIR_JSON_EMITTER_H
14+
#define P4MLIR_JSON_EMITTER_H
15+
16+
#include "mlir/IR/BuiltinOps.h"
17+
#include "mlir/IR/Operation.h"
18+
#include "llvm/Support/JSON.h"
19+
20+
namespace P4::P4MLIR {
21+
22+
class JsonEmitter {
23+
public:
24+
25+
// Emit a P4HIR-ISO json, given MLIR module.
26+
static llvm::json::Value emitModule(mlir::ModuleOp module);
27+
static llvm::json::Value emitOperation(mlir::Operation *op);
28+
static llvm::json::Value emitAttribute(mlir::Attribute attr);
29+
static llvm::json::Value emitType(mlir::Type type);
30+
static llvm::json::Value emitRegion(mlir::Region &region);
31+
static llvm::json::Value emitBlock(mlir::Block &block);
32+
33+
private:
34+
// Helper method to handle P4 HIR specific types
35+
llvm::json::Value emitP4HIRType(mlir::Type type);
36+
llvm::json::Value emitP4HIRAttribute(mlir::Attribute attr);
37+
};
38+
39+
} // namespace P4::P4MLIR
40+
41+
#endif // P4MLIR_JSON_EMITTER_H

tools/p4mlir-export/options.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Licensed under the Apache License, Version 2.0 (the "License");
3+
you may not use this file except in compliance with the License.
4+
You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
*/
14+
15+
#include "options.h"
16+
17+
using namespace P4::MLIR;
18+
19+
ExportOptions::ExportOptions() {
20+
registerOption(
21+
"--print-loc", nullptr,
22+
[this](const char *) {
23+
printLoc = true;
24+
return true;
25+
},
26+
"print location information in MLIR dump");
27+
registerOption(
28+
"--json-output", nullptr,
29+
[this](const char *) {
30+
jsonOutput = true;
31+
return true;
32+
},
33+
"output MLIR module in JSON format");
34+
}

tools/p4mlir-export/options.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Licensed under the Apache License, Version 2.0 (the "License");
3+
you may not use this file except in compliance with the License.
4+
You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
*/
14+
15+
#ifndef _P4MLIR_OPTIONS_H_
16+
#define _P4MLIR_OPTIONS_H_
17+
18+
#include "frontends/common/options.h"
19+
#include "frontends/common/parser_options.h"
20+
21+
namespace P4::MLIR {
22+
23+
class ExportOptions : public CompilerOptions {
24+
public:
25+
26+
bool printLoc = false;
27+
bool jsonOutput = false;
28+
29+
virtual ~ExportOptions() = default;
30+
31+
ExportOptions();
32+
ExportOptions(const ExportOptions &) = default;
33+
ExportOptions(ExportOptions &&) = delete;
34+
ExportOptions &operator=(const ExportOptions &) = default;
35+
ExportOptions &operator=(ExportOptions &&) = delete;
36+
};
37+
38+
using ExportContext = P4CContextWithOptions<ExportOptions>;
39+
40+
} // namespace P4::MLIR
41+
42+
#endif /* _P4MLIR_OPTIONS_H_ */

0 commit comments

Comments
 (0)