Add debug options

This commit is contained in:
Dmitry Stogov 2022-04-27 14:47:52 +03:00
parent c89246f35a
commit 2b9e793b4e
7 changed files with 113 additions and 26 deletions

11
ir.h
View File

@ -483,6 +483,13 @@ typedef struct _ir_use_list {
#define IR_AVX (1<<24)
#define IR_HAS_CALLS (1<<25)
/* debug relted */
#ifdef IR_DEBUG
# define IR_DEBUG_SCCP (1<<28)
# define IR_DEBUG_GCM (1<<29)
# define IR_DEBUG_RA (1<<30)
#endif
typedef enum _ir_fold_action {
IR_FOLD_DO_RESTART,
IR_FOLD_DO_CSE,
@ -693,6 +700,10 @@ int ir_schedule(ir_ctx *ctx);
/* Liveness & Register Allocation (implementation in ir_ra.c) */
typedef int (*emit_copy_t)(ir_ctx *ctx, uint8_t type, int from, int to);
#ifdef IR_DEBUG
extern uint32_t debug_regset;
#endif
int ir_assign_virtual_registers(ir_ctx *ctx);
int ir_compute_live_ranges(ir_ctx *ctx);
int ir_coalesce(ir_ctx *ctx);

View File

@ -261,7 +261,7 @@ void ir_dump_live_ranges(ir_ctx *ctx, FILE *f)
}
fprintf(f, "{ # LIVE-RANGES (vregs_count=%d)\n", ctx->vregs_count);
for (i = 1; i <= ctx->vregs_count; i++) {
ir_live_interval *ival = ctx->live_intervals[i];
ir_live_interval *ival = ctx->live_intervals[i]->top;
if (ival) {
ir_live_range *p;

View File

@ -151,10 +151,12 @@ int ir_gcm(ir_ctx *ctx)
}
}
#if 0
fprintf(stderr, "GCM Schedule Early\n");
for (i = 1; i < ctx->insns_count; i++) {
fprintf(stderr, "%d -> %d\n", i, _blocks[i]);
#ifdef IR_DEBUG
if (ctx->flags & IR_DEBUG_GCM) {
fprintf(stderr, "GCM Schedule Early\n");
for (i = 1; i < ctx->insns_count; i++) {
fprintf(stderr, "%d -> %d\n", i, _blocks[i]);
}
}
#endif
@ -195,10 +197,12 @@ int ir_gcm(ir_ctx *ctx)
ir_mem_free(visited);
ir_list_free(&queue);
#if 0
fprintf(stderr, "GCM Schedule Late\n");
for (i = 1; i < ctx->insns_count; i++) {
fprintf(stderr, "%d -> %d\n", i, _blocks[i]);
#ifdef IR_DEBUG
if (ctx->flags & IR_DEBUG_GCM) {
fprintf(stderr, "GCM Schedule Late\n");
for (i = 1; i < ctx->insns_count; i++) {
fprintf(stderr, "%d -> %d\n", i, _blocks[i]);
}
}
#endif

View File

@ -1,4 +1,5 @@
#include "ir.h"
#include <stdlib.h>
static void help(const char *cmd)
{
@ -22,6 +23,12 @@ static void help(const char *cmd)
" --dump-use-lists - dump def->use lists\n"
" --dump-cfg - dump CFG (Control Flow Graph)\n"
" --dump-gcm - dump GCM schedule (Global Code Motion)\n"
#ifdef IR_DEBUG
" --debug-sccp - debug SCCP optimization pass\n"
" --debug-gcm - debug GCM optimization pass\n"
" --debug-ra - debug register allocator\n"
" --debug-regset <bit-mask> - restrict available register set\n"
#endif
" --version\n"
" --help\n",
cmd);
@ -301,6 +308,21 @@ int main(int argc, char **argv)
run = 1;
} else if (strcmp(argv[i], "-mavx") == 0) {
mflags |= IR_AVX;
#ifdef IR_DEBUG
} else if (strcmp(argv[i], "--debug-sccp") == 0) {
mflags |= IR_DEBUG_SCCP;
} else if (strcmp(argv[i], "--debug-gcm") == 0) {
mflags |= IR_DEBUG_GCM;
} else if (strcmp(argv[i], "--debug-ra") == 0) {
mflags |= IR_DEBUG_RA;
} else if (strcmp(argv[i], "--debug-regset") == 0) {
if (i + 1 == argc || argv[i + 1][0] == '-') {
fprintf(stderr, "ERROR: Invalid usage' (use --help)\n");
return 1;
}
debug_regset = strtoul(argv[i + 1], NULL, 0);
i++;
#endif
} else if (argv[i][0] == '-') {
fprintf(stderr, "ERROR: Unknown option '%s' (use --help)\n", argv[i]);
return 1;

32
ir_ra.c
View File

@ -6,6 +6,10 @@
#include "ir_x86.h"
#ifdef IR_DEBUG
uint32_t debug_regset = 0xffffffff; /* all 32 regisers */
#endif
/* RA - Register Allocation, Liveness, Coalescing and SSA Resolution */
int ir_assign_virtual_registers(ir_ctx *ctx)
@ -1175,6 +1179,10 @@ static ir_reg ir_try_allocate_free_reg(ir_ctx *ctx, int current, uint32_t len, i
}
}
#ifdef IR_DEBUG
available &= debug_regset;
#endif
/* for each interval it in active */
IR_BITSET_FOREACH(active, len, i) {
/* freeUntilPos[it.reg] = 0 */
@ -1242,6 +1250,13 @@ static ir_reg ir_try_allocate_free_reg(ir_ctx *ctx, int current, uint32_t len, i
ir_add_to_unhandled(ctx, unhandled, current);
//ir_allocate_spill_slot(ctx, current, ctx->data);
#ifdef IR_DEBUG
if (ctx->flags & IR_DEBUG_RA) {
ir_dump_live_ranges(ctx, stderr);
}
#endif
return reg;
} else {
return IR_REG_NONE;
@ -1288,6 +1303,10 @@ static ir_reg ir_allocate_blocked_reg(ir_ctx *ctx, int current, uint32_t len, ir
}
}
#ifdef IR_DEBUG
available &= debug_regset;
#endif
/* for each interval it in active */
IR_BITSET_FOREACH(active, len, i) {
/* nextUsePos[it.reg] = next use of it after start of current */
@ -1362,6 +1381,13 @@ static ir_reg ir_allocate_blocked_reg(ir_ctx *ctx, int current, uint32_t len, ir
ir_live_interval *child = ir_split_interval_at(ival, split_pos);
ctx->live_intervals[current] = child;
ir_add_to_unhandled(ctx, unhandled, current);
#ifdef IR_DEBUG
if (ctx->flags & IR_DEBUG_RA) {
ir_dump_live_ranges(ctx, stderr);
}
#endif
return IR_REG_NONE;
} else {
/* spill intervals that currently block reg */
@ -1404,6 +1430,12 @@ static ir_reg ir_allocate_blocked_reg(ir_ctx *ctx, int current, uint32_t len, ir
ir_add_to_unhandled(ctx, unhandled, current);
}
#ifdef IR_DEBUG
if (ctx->flags & IR_DEBUG_RA) {
ir_dump_live_ranges(ctx, stderr);
}
#endif
return reg;
}

View File

@ -587,26 +587,28 @@ int ir_sccp(ir_ctx *ctx)
}
}
#if 0
for (i = 1; i < ctx->insns_count; i++) {
if (IR_IS_CONST(_values[i].op)) {
fprintf(stderr, "%d. CONST(", i);
ir_print_const(ctx, &_values[i], stderr);
fprintf(stderr, ")\n");
#ifdef IR_DEBUG
if (ctx->flags & IR_DEBUG_SCCP) {
for (i = 1; i < ctx->insns_count; i++) {
if (IR_IS_CONST(_values[i].op)) {
fprintf(stderr, "%d. CONST(", i);
ir_print_const(ctx, &_values[i], stderr);
fprintf(stderr, ")\n");
#if IR_COMBO_COPY_PROPAGATION
} else if (_values[i].op == IR_COPY) {
fprintf(stderr, "%d. COPY(%d)\n", i, _values[i].op1);
} else if (_values[i].op == IR_COPY) {
fprintf(stderr, "%d. COPY(%d)\n", i, _values[i].op1);
#endif
} else if (IR_IS_TOP(i)) {
fprintf(stderr, "%d. TOP\n", i);
} else if (_values[i].op == IR_IF) {
fprintf(stderr, "%d. IF(%d)\n", i, _values[i].op1);
} else if (_values[i].op == IR_MERGE || _values[i].op == IR_LOOP_BEGIN) {
fprintf(stderr, "%d. MERGE(%d)\n", i, _values[i].op1);
} else if (!IR_IS_BOTTOM(i)) {
fprintf(stderr, "%d. %d\n", i, _values[i].op);
} else if (IR_IS_TOP(i)) {
fprintf(stderr, "%d. TOP\n", i);
} else if (_values[i].op == IR_IF) {
fprintf(stderr, "%d. IF(%d)\n", i, _values[i].op1);
} else if (_values[i].op == IR_MERGE || _values[i].op == IR_LOOP_BEGIN) {
fprintf(stderr, "%d. MERGE(%d)\n", i, _values[i].op1);
} else if (!IR_IS_BOTTOM(i)) {
fprintf(stderr, "%d. %d\n", i, _values[i].op);
}
}
}
}
#endif
for (i = 1; i < ctx->insns_count; i++) {

View File

@ -1,5 +1,6 @@
#include "ir.h"
#include <sys/time.h>
#include <stdlib.h>
#define BAILOUT 16
#define MAX_ITERATIONS 1000
@ -136,6 +137,21 @@ int main(int argc, char **argv)
}
} else if (strcmp(argv[i], "-mavx") == 0) {
mflags |= IR_AVX;
#ifdef IR_DEBUG
} else if (strcmp(argv[i], "--debug-sccp") == 0) {
mflags |= IR_DEBUG_SCCP;
} else if (strcmp(argv[i], "--debug-gcm") == 0) {
mflags |= IR_DEBUG_GCM;
} else if (strcmp(argv[i], "--debug-ra") == 0) {
mflags |= IR_DEBUG_RA;
} else if (strcmp(argv[i], "--debug-regset") == 0) {
if (i + 1 == argc || argv[i + 1][0] == '-') {
fprintf(stderr, "ERROR: Invalid usage' (use --help)\n");
return 1;
}
debug_regset = strtoul(argv[i + 1], NULL, 0);
i++;
#endif
} else {
/* pass*/
}