|
| 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 ®ion : 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 ®ion) { |
| 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 | + |
0 commit comments