Skip to content

stage2: implement a pass on AIR which does dead store elimination #7718

@FireFox317

Description

@FireFox317

The following function generates invalid TZIR (changing const val to var val fixes it):

fn foo(ok: bool) void {
    const val: i32 = blk: {
        if (!ok) break :blk 10;
        break :blk 20;
    };
    var other: i32 = val;
}

TZIR output (I implemented TZIR printing for blocks etc, PR incoming):

Module.Function(name=foo):
  @1: i32 = 10;
  @2: void = {};
  @4: i32 = 20;
  %0: bool = arg(ok)
  %1: void = dbg_stmt()
  %2: *i32 = alloc()
  %3: i32 = block(
    %4: void = dbg_stmt()
    %5: void = block(
      %6: bool = not(%0)
      %7: noreturn = condbr(%6,
      then:
        %8: void = store(%2, @1)
        %9: noreturn = br(%3, @1)
      else:
        %10: noreturn = br(%5, @2)
      )
    )
    %11: void = dbg_stmt()
    %12: void = store(%2, @4)
    %13: noreturn = br(%3, @4)
  )
  %14: void = dbg_stmt()
  %15: *i32 = alloc()
  %16: void = store(%15, %3)
  %17: noreturn = retvoid()

The problem happens at instruction %16. Here we try to store %3 in %15, however %3 is the block instruction. But as you can see in instruction %8, we store the value in %2. So I think the problem is that instruction %16 should change to:

%16: void = store(%15, %2)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementSolving this issue will likely involve adding new logic or components to the codebase.frontendTokenization, parsing, AstGen, Sema, and Liveness.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions