Skip to content

Commit

Permalink
Set top-level location and allow locations in dump
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Korobeynikov <[email protected]>
  • Loading branch information
asl committed Jan 23, 2025
1 parent 429a8e6 commit 5faa064
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion tools/p4mlir-translate/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ limitations under the License.

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include "mlir/IR/OperationSupport.h"
#include "mlir/Pass/PassManager.h"
#include "p4mlir/Dialect/P4HIR/P4HIR_Dialect.h"
#pragma GCC diagnostic pop
Expand Down Expand Up @@ -142,7 +143,8 @@ int main(int argc, char *const argv[]) {
auto mod = P4::P4MLIR::toMLIR(context, program, &typeMap);
if (!mod) return EXIT_FAILURE;

mod->print(llvm::outs());
mlir::OpPrintingFlags flags;
mod->print(llvm::outs(), flags.enableDebugInfo(options.printLoc));

if (P4::Log::verbose()) std::cerr << "Done." << std::endl;
return P4::errorCount() > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
Expand Down
7 changes: 7 additions & 0 deletions tools/p4mlir-translate/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,11 @@ TranslateOptions::TranslateOptions() {
return true;
},
"parse the P4 input and run minimal set of frontend passes for type inference");
registerOption(
"--print-loc", nullptr,
[this](const char *) {
printLoc = true;
return true;
},
"print location information in MLIR dump");
}
1 change: 1 addition & 0 deletions tools/p4mlir-translate/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class TranslateOptions : public CompilerOptions {
public:
bool parseOnly = false;
bool typeinferenceOnly = false;
bool printLoc = false;

virtual ~TranslateOptions() = default;

Expand Down
5 changes: 5 additions & 0 deletions tools/p4mlir-translate/translate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,14 @@ mlir::OwningOpRef<mlir::ModuleOp> toMLIR(mlir::MLIRContext &context,
const P4::IR::P4Program *program,
const P4::TypeMap *typeMap) {
mlir::OpBuilder builder(&context);

auto moduleOp = mlir::ModuleOp::create(builder.getUnknownLoc());
builder.setInsertionPointToEnd(moduleOp.getBody());

if (auto sourceInfo = program->getSourceInfo(); sourceInfo.isValid()) {
moduleOp.setSymName(sourceInfo.getSourceFile().string_view());
moduleOp->setLoc(getLoc(builder, program));
}
P4HIRConverter conv(builder, typeMap);
program->apply(conv);

Expand Down

0 comments on commit 5faa064

Please sign in to comment.