Skip to content

[CIR] [CodeGen] Handling case not in switch scope directly #954

Closed as not planned
@ChuanqiXu9

Description

@ChuanqiXu9

Reproducer:

int func(int v) {
    switch(v) {
        case 1:
            return 43;

        default:
            if (v > 5) {
                case 2:
                    return 35;

                return 47;
            }

            return  99;
    }
}

Reduced from Spec2017. https://godbolt.org/z/zrqxsM9K4

Now it will crash at

assert(isSwitch() && "expected switch scope");
due to we're trying to create a switch region but not in swtich scope. The direct issue of layering issue is relatively easy to fix.

What confuses me is what is the expected CIR generated for the above code. A direct idea may be:

"cir.switch"(%value) (
case (equal, 1) {
    codes for case 1
},
case (default) {
   // codes for default case
   "cir.scope"() {
        // codes for if
        if (v > 5)
            cir.br ^bb2
        
        return 47
   }
},
case (equal, 2) {
    // codes fro case2
    ^bb2:
        return 35
})

I am hesitating since in

// have their own scopes but are distinct regions nonetheless.
I saw comments like the regions of cases are distinct regions, so I am afraid the above codes may break assumptions of isolations of regions (not sure if it is true)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions