tool: validate: resolve ops and references; report any diagnostics

This commit is contained in:
2026-03-16 12:08:41 +00:00
parent 540c8e6d8b
commit 778835032d

View File

@@ -1,4 +1,5 @@
#include "../diag/diag.h"
#include "../resolve.h"
#include "cmd.h"
#include <assert.h>
@@ -14,6 +15,7 @@
#include <mie/dialect/dialect.h>
#include <mie/dialect/func.h>
#include <mie/dialect/index.h>
#include <mie/dialect/memref.h>
#include <mie/dialect/meta.h>
#include <mie/dialect/ptr.h>
#include <mie/dialect/scf.h>
@@ -45,6 +47,16 @@ enum {
ARG_FILEPATH,
};
static void report_diag(struct mie_ctx *ctx)
{
struct mie_diag *diag = mie_ctx_pop_diag(ctx);
while (diag) {
mie_diag_write_pretty(diag, b_stdtty_err);
/* TODO cleanup */
diag = mie_ctx_pop_diag(ctx);
}
}
static int validate_file(const char *path, const b_arglist *args)
{
b_file *file = NULL;
@@ -61,6 +73,7 @@ static int validate_file(const char *path, const b_arglist *args)
mie_builtin_dialect_create(ctx);
mie_meta_dialect_create(ctx);
mie_select_dialect_create(ctx);
mie_memref_dialect_create(ctx);
mie_ptr_dialect_create(ctx);
mie_arith_dialect_create(ctx);
mie_func_dialect_create(ctx);
@@ -81,50 +94,16 @@ static int validate_file(const char *path, const b_arglist *args)
struct mie_op *root = mie_op_create();
if (!mie_parser_parse_op(parse, NULL, root)) {
struct mie_diag *diag = mie_ctx_pop_diag(ctx);
while (diag) {
mie_diag_write_pretty(diag, b_stdtty_err);
/* TODO cleanup */
diag = mie_ctx_pop_diag(ctx);
}
report_diag(ctx);
printf("parse failed\n");
return -1;
}
enum mie_walker_flags walk_flags
= MIE_WALKER_F_INCLUDE_OPS | MIE_WALKER_F_PREORDER
| MIE_WALKER_F_FORWARD | MIE_WALKER_F_RECURSIVE;
struct mie_walker walker;
mie_walker_begin(&walker, root, walk_flags);
do {
const struct mie_walk_item *item = mie_walker_get(&walker);
for (size_t i = 0; i < item->i_depth; i++) {
fputs(" ", stdout);
if (!resolve_op(root, ctx)) {
report_diag(ctx);
return -1;
}
switch (item->i_type) {
case MIE_WALK_ITEM_REGION:
printf("looking at region %p...\n", item->i_region);
continue;
case MIE_WALK_ITEM_BLOCK:
printf("looking at block %p %s...\n", item->i_block,
item->i_block->b_name.n_str);
continue;
case MIE_WALK_ITEM_OP:
printf("resolving %p %s... ", item->i_op,
item->i_op->op_name);
bool ok = mie_ctx_resolve_op(ctx, item->i_op);
printf("%s\n", ok ? "OK" : "FAIL");
break;
default:
break;
}
} while (mie_walker_step(&walker) == MIE_SUCCESS);
mie_walker_end(&walker);
struct mie_pass *prefix_func_with_underscore = NULL;
enum mie_status status = mie_ctx_get_pass(
ctx, "prefix-func-with-underscore", NULL,
@@ -150,44 +129,52 @@ static int validate_file(const char *path, const b_arglist *args)
mie_pass_manager_run(pm, root);
struct mie_printer printer;
mie_printer_init(&printer, ctx, b_stdout, MIE_PRINT_F_ABBREVIATED);
mie_printer_init(
&printer, ctx, b_stdout,
MIE_PRINT_F_ABBREVIATED | MIE_PRINT_F_MARK_UNRESOLVED_ELEMENTS);
mie_printer_print_op(&printer, root);
printf("\n");
struct mie_op *func = mie_op_get_first_child_op(root);
walk_flags = MIE_WALKER_F_INCLUDE_OPS | MIE_WALKER_F_PREORDER
#if 0
struct mie_walker walker;
enum mie_walker_flags walk_flags = MIE_WALKER_F_INCLUDE_BLOCKS
| MIE_WALKER_F_PREORDER
| MIE_WALKER_F_FORWARD;
mie_walker_begin(&walker, func, walk_flags);
mie_walker_begin(&walker, root, walk_flags);
do {
const struct mie_walk_item *item = mie_walker_get(&walker);
for (size_t i = 0; i < item->i_depth; i++) {
fputs(" ", stdout);
}
switch (item->i_type) {
case MIE_WALK_ITEM_REGION:
printf("looking at region %p...\n", item->i_region);
break;
case MIE_WALK_ITEM_BLOCK:
printf("looking at block %p %s...\n", item->i_block,
item->i_block->b_name.n_str);
break;
case MIE_WALK_ITEM_OP:
if (item->i_op->op_flags & MIE_OP_F_OP_RESOLVED) {
printf("looking at %p %s.%s...\n", item->i_op,
item->i_op->op_dialect->d_name,
item->i_op->op_info->op_name);
} else {
printf("looking at %p %s...\n", item->i_op,
item->i_op->op_name);
}
break;
default:
break;
printf(" id=%u\n", item->i_block->b_id);
const char *idom = item->i_block->b_idom
? item->i_block->b_idom->b_name.n_str
: "Unk";
printf(" idom=%s\n", idom);
#if 0
const char *sdom = item->i_block->b_sdom
? item->i_block->b_sdom->b_name.n_str
: "Unk";
const char *dfs_parent
= item->i_block->b_dfs_parent
? item->i_block->b_dfs_parent->b_name.n_str
: "Unk";
printf(" sdom=%s\n", sdom);
printf(" dfs_parent=%s\n", dfs_parent);
#endif
printf(" preds=");
struct mie_block_predecessor *pred
= mie_block_get_first_predecessor(item->i_block);
while (pred) {
printf(" %s", pred->p_block->b_name.n_str);
pred = mie_block_get_next_predecessor(item->i_block, pred);
}
printf("\n");
} while (mie_walker_step(&walker) == MIE_SUCCESS);
mie_walker_end(&walker);
#endif
#if 0