Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IR build failed with "-O0" but succeeded with "-O2" option #32

Closed
stkeke opened this issue May 17, 2023 · 5 comments
Closed

IR build failed with "-O0" but succeeded with "-O2" option #32

stkeke opened this issue May 17, 2023 · 5 comments

Comments

@stkeke
Copy link
Contributor

stkeke commented May 17, 2023

The IR function below failed to compile with -O0 option, but okay with -O2 option.

Failed IR program

tony@Win11Z790I:~/ir$ cat t.ir
{
        uintptr_t c_1 = 0;
        bool c_2 = 0;
        bool c_3 = 1;
        int32_t c_4 = 1;
        int32_t c_5 = 2;
        l_1 = START(l_20);
        bool d_2 = PARAM(l_1, "cond", 0);
        bool d_3 = COPY(c_4);
        l_4 = END(l_1);
        l_5 = LOOP_BEGIN(l_4, l_18);
        int32_t d_6 = PHI(l_5, d_3, d_15);
        bool d_7 = NE(d_6, c_4);
        l_8 = IF(l_5, d_7);
        l_9 = IF_TRUE(l_8);
        int32_t d_10 = COPY(c_5);
        l_11 = END(l_9);
        l_12 = IF_FALSE(l_8);
        l_13 = END(l_12);
        l_14 = MERGE(l_11, l_13);
        int32_t d_15 = PHI(l_14, d_10, d_6);
        l_16 = IF(l_14, d_2);
        l_17 = IF_TRUE(l_16);
        l_18 = LOOP_END(l_17);
        l_19 = IF_FALSE(l_16);
        l_20 = RETURN(l_19, d_15);
}

tony@Win11Z790I:~/ir$ ~/ir/ir t.ir  -O0 --save 
ir_base[6].ops[2] (3) type is incompatible with result type (1 != 10)
ir: ir_check.c:347: ir_check: Assertion `ok' failed.
Aborted

tony@Win11Z790I:~/ir$ ~/ir/ir t.ir  -O2 --save 
{
        uintptr_t c_1 = 0;
        bool c_2 = 0;
        bool c_3 = 1;
        int32_t c_4 = 1;
        l_1 = START(l_9);
        bool d_2 = PARAM(l_1, "cond", 0);
        l_3 = END(l_1);
        l_4 = LOOP_BEGIN(l_3, l_7);
        l_5 = IF(l_4, d_2);
        l_6 = IF_TRUE(l_5);
        l_7 = LOOP_END(l_6);
        l_8 = IF_FALSE(l_5);
        l_9 = RETURN(l_8, c_4);
}

DO NOT USE for Reproduce

This is the good -O2 dump --- sorry for paste wrong program in first report.

tony@Win11Z790I:~/ir$ cat t.ir
{
        uintptr_t c_1 = 0;
        bool c_2 = 0;
        bool c_3 = 1;
        int32_t c_4 = 1;
        l_1 = START(l_9);
        bool d_2 = PARAM(l_1, "cond", 0);
        l_3 = END(l_1);
        l_4 = LOOP_BEGIN(l_3, l_7);
        l_5 = IF(l_4, d_2);
        l_6 = IF_TRUE(l_5);
        l_7 = LOOP_END(l_6);
        l_8 = IF_FALSE(l_5);
        l_9 = RETURN(l_8, c_4);
}
tony@Win11Z790I:~/ir$ ./ir -O0 t.ir --save
ir_base[6].ops[2] (3) type is incompatible with result type (1 != 10)
ir: ir_check.c:347: ir_check: Assertion `ok' failed.
Aborted

tony@Win11Z790I:~/ir$ ./ir -O2 t.ir --save
{
        uintptr_t c_1 = 0;
        bool c_2 = 0;
        bool c_3 = 1;
        int32_t c_4 = 1;
        l_1 = START(l_9);
        bool d_2 = PARAM(l_1, "cond", 0);
        l_3 = END(l_1);
        l_4 = LOOP_BEGIN(l_3, l_7);
        l_5 = IF(l_4, d_2);
        l_6 = IF_TRUE(l_5);
        l_7 = LOOP_END(l_6);
        l_8 = IF_FALSE(l_5);
        l_9 = RETURN(l_8, c_4);
}
@dstogov
Copy link
Owner

dstogov commented May 17, 2023

ir_base[6].ops[2] (3) type is incompatible with result type (1 != 10)

I can't reproduce this. I also don't see how this error might be emitted at all.
It indicates type check failure for instruction number 6 - l_6 = IF_TRUE(l_5);, but we don't not check result type of IF_TRUE.

@dstogov
Copy link
Owner

dstogov commented May 17, 2023

There is a error in the original program at bool d_3 = COPY(c_4);.
bool should be replaced by int32_t.
ir -O0 just detected that error.
It seems ir -O2 optimized out the problematic COPY before the check.

@stkeke
Copy link
Contributor Author

stkeke commented May 17, 2023

I tried to place ir_check(ctx); (suppose this function do the check) to the front of ir_compile_func function.
But still has the issue... did not catch the error with -O2.

tony@Win11Z790I:~/ir$ ./ir -O2 t.ir --save
{
        uintptr_t c_1 = 0;
        bool c_2 = 0;
        bool c_3 = 1;
        int32_t c_4 = 1;
        l_1 = START(l_9);
        bool d_2 = PARAM(l_1, "cond", 0);
        l_3 = END(l_1);
        l_4 = LOOP_BEGIN(l_3, l_7);
        l_5 = IF(l_4, d_2);
        l_6 = IF_TRUE(l_5);
        l_7 = LOOP_END(l_6);
        l_8 = IF_FALSE(l_5);
        l_9 = RETURN(l_8, c_4);
}

https://github.com/dstogov/ir/blob/master/ir_main.c#L151

// ir_main.c https://github.com/dstogov/ir/blob/master/ir_main.c#L151
int ir_compile_func(ir_ctx *ctx, int opt_level, uint32_t dump, const char *dump_file)
{
	ir_check(ctx); // Check before optimization - hope to catch error even with -O2

	if ((dump & (IR_DUMP_AFTER_LOAD|IR_DUMP_AFTER_ALL))
	 && !_save(ctx, dump, IR_DUMP_AFTER_LOAD, dump_file)) {
		return 0;
	}

	if (opt_level > 0 || (ctx->flags & (IR_GEN_NATIVE|IR_GEN_C))) {
		ir_build_def_use_lists(ctx);
	}

	/* Global Optimization */
	if (opt_level > 1) {
...
	ir_check(ctx); // After optimization check

	return 1;
}

@dstogov
Copy link
Owner

dstogov commented May 22, 2023

The COPY is eliminated by folding engine that works on the fly during IR construction or loading.
See https://github.com/dstogov/ir/blob/master/ir_fold.h#L1153
We may catch this error at the body of the rule adding IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type);

See 75edc8f

I think, this issue may be closed

@stkeke
Copy link
Contributor Author

stkeke commented May 22, 2023

@dstogov Thanks. Let's close it as "Fixed".

@stkeke stkeke closed this as completed May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants